Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Managed C++/CLI
  4. unmanaged class declaring managed member function as friend

unmanaged class declaring managed member function as friend

Scheduled Pinned Locked Moved Managed C++/CLI
question
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    godzooky
    wrote on last edited by
    #1

    i'm trying to accomplish something like this...

    class ManagedClass;

    class UnmanagedClass
    {
    protected:
    void SetValue( std::string val );
    friend void ManagedClass::SetValue( String* val );
    };

    namespace MyNamespace {
    class __gc ManagedClass
    {
    protected:
    void SetValue( String* val );
    };
    }

    i can't get it to compile. i have tried just about every combination of the namespace and class name in the forward declaration and friend function definition. anyone have any ideas?? thanks.

    A 1 Reply Last reply
    0
    • G godzooky

      i'm trying to accomplish something like this...

      class ManagedClass;

      class UnmanagedClass
      {
      protected:
      void SetValue( std::string val );
      friend void ManagedClass::SetValue( String* val );
      };

      namespace MyNamespace {
      class __gc ManagedClass
      {
      protected:
      void SetValue( String* val );
      };
      }

      i can't get it to compile. i have tried just about every combination of the namespace and class name in the forward declaration and friend function definition. anyone have any ideas?? thanks.

      A Offline
      A Offline
      Andy Wieberneit
      wrote on last edited by
      #2

      I don't think you really need that friend directive. Most probably, you could simply make your classes public and avoid inline methods. It seems that you want to call a managed method from an unmanaged class. However, this can't be done by a direct call, because managed objects are subject to garbage collection. To get a safe entry point into the managed heap, you have to use a GCHandle, which in turn is wrapped by the gcroot template. Try this:

      #include <vcclr.h>
      class ManagedClass;
      class UnmanagedClass
      {
      public:
      UnmanagedClass( ManagedClass* p );
      private:
      gcroot< ManagedClass* > m_pManaged;
      }

      You can then make your calls into the managed class with the m_pManaged pointer. There might be another problem: Since you have both managed and unmanaged code in one project, this project needs to be linked in mixed mode. This is because for the unmanaged code, you need the C runtime, which requires an entry point as well as statics. For an instruction of how to convert your project to mixed mode, see the article: "Converting Managed Extensions for C++ Projects from Pure Intermediate Language to Mixed Mode" in the MSDN.

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups