1

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) ;-)

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
Seth
  • 8,213
  • 14
  • 71
  • 103

1 Answers1

1

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.

Community
  • 1
  • 1
Yochai Timmer
  • 48,127
  • 24
  • 147
  • 185
  • Updated question - it still doesn't work, it doesnt seem to recognise all the using statements in the managed C++ object.. – Seth Apr 06 '11 at 08:32
  • 2
    it's not enough to add using directives. you need to add references to the appropriate .NET assembly for your VC++ project. – Marius Bancila Apr 06 '11 at 08:39
  • Yay! thanks Marius that is the answer I was looking for. Same as for a C# .NET except I didn't know how to do it for a C++ project. You should put that down as an answer. – Seth Apr 06 '11 at 08:46