I need to integrate a native C++ library into a C# project. Now in this C++ library there is class with virtual functions that I need to inherit from in C++/CLI. So in C++/CLI I wrote someting like
class C++CliClass : public C++Class
{
C++CliClass(Callback callback) { iCallback = callback; }
virtual VirualFunctionCallFromC++(int x, int y, int z, SomeClass *p)
{
// I need to call C++/CLI here
iCallback(x, y, z, p);
}
private:
Callback iCallback;
}
I defined the callback function as:
typedef int (__cdecl *Callback)(int x, int y, int z, SomeClass *p);
The idea is now that C++ library calls the virtual function of the C++Cli
class which on his turn calls the call back which gets me hopefully into C#.
// This delegate definition is needed to setup the callback for the C++ class
delegate int CallbackDelegate(int x, int y, int z, SomeClass *p);
So now I defined a managed C++/CLI class
public ref class GCClass
{
public:
delegate <Byte>^ GetDataDelegate();
GCClass(GetData^ getDataDelegate) { iGetDataDelegate = getDataDelegate };
private:
GetDataDelegate ^iGetDataDelegate;
int GetData(int x, int y, int z, SomeClass *p)
{
// call delegate into C#
<Byte>^ data = iGetDataDelegate->Invoke();
}
public:
void SomeFunctionWhichEventuallyCallsC++Libary
{
// create a delegate for the method that will call the C# delegate
CallbackDelegate ^d = gcnew CallbackDelegate(this, &GCClass::GetData);
IntPtr del = System::Runtime::InteropServices::Marshal::GetFunctionPointerForDelegate(d);
// Install in the C++ class
C++CliClass(del.ToPointer());
// Setup the C++ library and install my C++ class into the library
SomeObjectOfTheLibrary->Install(&C++CliClass);
SomeObjectOfTheLibrary->DoSometing() // will call the C++ virtual function and end up in C#
// The delegate is no longer needed anymore
}
Until here the code. So what I was hoping to achieve is that someone can call a method of my managed C++/CLI class which uses a native C++ library to do his stuff. The C++ library calls on his turn the C++/CLI callback and finally a C# delegate is called. Now finally the question: everything goes fine in debug mode. In release mode however sometimes an AccesException is thrown or sometimes the application just hangs. I suspect that it has something to do with different calling conventions for C++/CLI and C++. For example I observed that the second time the callback is called the value of iCallback is different from the first time it was called. However for all next calls the value of iCallback does not change anymore. I would expect that the iCallback value should be always the same but I'm not sure because I don't know how the framework internally works to be able to call a delegate from C++. I also tried to define the calling convention of the CallbackDelegate with [UnmanagedFunctionPointer(Cdecl)]. I tried all options but had no result: I always end up in an exception or the application hangs forever. Can someone give me a hint of what might be wrong?