Hello all,
I have been trying to call a function written in managed code, but it needs to be called from unmanaged code. Basically, I have a master thread within C++ code, that needs to update a .NET interface. The only way to do this is by calling a .NET function from within the master thread.
I have wrote a very basic program to try and get this working, but with no success. I have a std c++ class and a .net manged class. I create a .net delegate to that manged function, and pass the delegate to my standard class. It is then called from the main program. However, I keep getting an error which I have not been able to resolve.
If anyone could give me some suggestions as to where I am going wrong, that would be great. I have attached a zip file which demonstrates this, but with C# and a c++ dll. However, my interface needs to be in .net c++. You will have to rename the extension to .zip.
Code:
#include <iostream>
#include <tchar.h>
using namespace System;
class StdClass
{
public:
typedef int (__stdcall *Callback)(int x, int y);
StdClass(Callback ptr)
{
m_CallBack = ptr;
}
int Call(int x, int y)
{
return (*m_CallBack)(x, y);
}
private:
Callback m_CallBack;
};
__gc class GcClass
{
public:
int Call(int x, int y)
{
return x * y;
}
};
__delegate int CallbackDelegate(int x, int y);
int _tmain()
{
GcClass *gcClass = new GcClass();
CallbackDelegate *callBack = new CallbackDelegate(gcClass, &GcClass::Call);
StdClass *stdClass = new StdClass((StdClass::Callback)callBack);
int iResult = stdClass->Call(3, 3);
delete stdClass;
return 0;
}