nnevod
nnevod

Reputation: 61

error calling managed code from unmanaged in C++/CLI

I'm writing a "driver" for a program, the driver communicates with some devices on network. I already have C# software working with the devices, so the plan is to reuse code.
So the driver dll is really an interop between program and and already availible assemblies, it's written in C++/CLI. The program calls methods described in interface, the interop dll calls C# code, that is how I see it.

I implement methods to be called by program using #pragma unmanaged

DeviceSearch::DeviceSearch(IDeviceSearchHandler* handler):m_handler(handler)
{
    ManagedWrapper mw;
    mw.Init();
}

ManagedWrapper is implemented in managed code, obviously

void ManagedWrapper::Init()
{

    //some code
}

However, the problem rises here. If the Init() is empty or calls methods/classes defined in C++, it's working ok. However, if I try to call the C# code (which is referenced using #using , where Facade.dll is the C# dll which performs some functions), I get access violation exception right when mw.Init() is called, not even within it.

Am I missing something really obvious I should do to make the interop work? Most information in the net just tells that it should "just work"

Upvotes: 1

Views: 2882

Answers (1)

wengseng
wengseng

Reputation: 1328

See if this helps:

According to How can i use a C# dll in a Win32 C++ project?

"Define an abstract interface class in your native C++ code, then create a concrete subclass inside the managed C++ DLL. Call into your C# objects in the method implementations.

Finally, export a factory function that will instantiate the implementation class and return a base-class pointer that your native code can use."

Upvotes: 1

Related Questions