C++ Dot net: calling managed method from unmanaged code 
Author Message
 C++ Dot net: calling managed method from unmanaged code

Hi,

I am having trouble calling a managed method from an unmanaged object.
After two days I am at the point where I am able (finally) to pass a
pointer of the managed object to the unmanaged object but when I call
a method on the managed object via the object pointer I get a message
explaining how I can't call a managed method from unmanaged code. Here
is what I have so far...

//MANAGED CLASS
class ManagedClass
{
public:
    __event void ServerMessage(String* Message);
    ManagedClass::ManagedClass();

    void InvokeEvent()
    {
        ServerMessage("Test Server message");
    };

    bool InitListener();
private:
    UnManagedClass* mUnManagedClass;

Quote:
};

Managed::Managed()
{
    mUnManagedClass = new UnManagedClass(this);

Quote:
}

//UNMANAGED CLASS
class UnManagedClass
{
    public:
        UnManagedClass::UnManagedClass(gcroot<Managed*>inManaged);
        void onCmMsg(TibrvCmListener* listener, TibrvMsg& msg);
    private:
        gcroot<Managed*> ManagedPtr;

Quote:
};

UnManaged::UnManaged(gcroot<Managed*>inManaged)
{
    ManagedPtr = inManaged;
    //THIS DOES NOT WORK...
    inManaged->InvokeEvent();

Quote:
}

I have cut the code down to focus on the actual problem, any help
would be greatly appreciated


Tue, 31 May 2005 20:28:29 GMT  
 C++ Dot net: calling managed method from unmanaged code

Quote:

> [some code]

The following works:

<code>
#using <mscorlib.dll>
#include "vcclr.h"

using namespace System;

// forwards:
class UnManagedClass;

//MANAGED CLASS
__gc class ManagedClass
{
public:
  ManagedClass::ManagedClass();
  void InvokeEvent();

private:
    UnManagedClass* mUnManagedClass;

Quote:
};

//UNMANAGED CLASS
__nogc class UnManagedClass
{
  public:
    UnManagedClass(ManagedClass* inManaged);
    UnManagedClass();

    void SetAccordingObject(ManagedClass* inManaged);
   void onCmMsg();
  private:
    gcroot<ManagedClass*> ManagedPtr;

Quote:
};

void ManagedClass::InvokeEvent()
{
  System::Console::WriteLine(S"Test Server message");

Quote:
}

ManagedClass::ManagedClass()
{
  mUnManagedClass = new UnManagedClass(this);
  //mUnManagedClass->SetAccordingObject(this);

Quote:
}

void UnManagedClass::SetAccordingObject(ManagedClass* inManaged)
{
  ManagedPtr = inManaged;

Quote:
}

UnManagedClass::UnManagedClass(ManagedClass* inManaged)
{
  ManagedClass *pMan;
  //THIS DOES WORK...
  pMan->InvokeEvent();

Quote:
}

void main()
{
  ManagedClass *ms = new ManagedClass();

Quote:
}

</code>
--
Greetings
  Jochen

  Do you need a memory-leak finder ?
  http://www.codeproject.com/useritems/leakfinder.asp



Tue, 31 May 2005 22:54:36 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. C# client crashs when calling into Managed C++ which calls unmanaged c++ function

2. calling managed code from unmanaged code

3. Calling managed code from unmanaged Code

4. Inherit unmanaged c++ classes from .Net platform (managed c++ or c#)

5. How to call managed C++ DLL from unmanaged C++ EXE

6. Referencing data from unmanaged code to managed code in C++ Wrapper class

7. Fatal Error C1010 in Mixing Managed C++ and Unmanaged C++ Code

8. Creating a .NET managed library to talk to existing unmanaged code

9. Unmanaged coed calls managed code

10. Dot Net Should be called Dot Memory Hog!

11. Calling Managed Function from Unmanaged code

12. Calling managed code from unmanaged

 

 
Powered by phpBB® Forum Software