Seth
Seth

Reputation: 8373

Create Managed Object From Unmanaged Class Function

I am trying to create a Managed C++/CLI object in unmanaged code.

  1. Is this possible?
  2. If so, am I doing it right? see code below

    #include <vcclr.h>
    #include <ManagedClass.h>
    
    // compiled with /clr
    namespace X 
    {
        class UnmanagedClass
        {
            UnmanagedClass(){}
            ~UnmanagedClass(){}
    
            gcroot<Y::ManagedClass^> m_guiControl;
    
            void functionA()
            {
                 m_guiControl = new gcroot<Y::ManagedClass^>;
            }
        }
    }
    
    // compiled into Managed dll with /clr
    // in file ManagedClass.h in a separate project
    using namespace System::ComponentModel;
    // more usings here ..etc
    
    namespace Y {
        public ref class ManagedClass : public System::Windows::Forms::UserControl
        {
            // implementation here
    
        }
    }
    

When I compile the UnmanagedClass source file, I keep getting a whole lot of errors with the first one being error C2039: 'ComponentModel' : is not a member of 'System'. How come it is not recognising ComponentModel?

I thought this was suppose to be IJW (it just works) ;-)

Upvotes: 1

Views: 5104

Answers (1)

Yochai Timmer
Yochai Timmer

Reputation: 49231

Here's an example for a wrapper:

class UnmanagedClass
{
    gcroot<ManagedClass^> m_managed;

public:
    UnmanagedClass() 
    {
       m_managed = gcnew ManagedClass();
    }
};

Look here:

C++/CLI - Managed class to C# events

wrapper to c++/cli

Edit:

When you get an error on a using statement, and you know it's supposed to exist, It's usually because that dll isn't referenced.

Go to the projects references, choose add reference.

You can add .Net assemblies in the .Net tab. Find the one you need and add it.

Upvotes: 1

Related Questions