I have a COM server, done in C#, where one of the methods takes a callback function.
[ComVisible(true)]
[ClassInterface(ClassInterfaceType.AutoDual)]
public class REDIServer
{
public bool Connect([MarshalAs(UnmanagedType.FunctionPtr)] ref CallBackFunction callback)
{
// send callback deeper into C# code
return _service.Connect(callback);
}
When generating tlb code out of that dll so I can use it in my C++ client, it looks like this:
virtual HRESULT __stdcall Connect (
/*[in,out]*/ long * callback,
/*[out,retval]*/ VARIANT_BOOL * pRetVal ) = 0;
And finally, when I try to use the Connect() call from C++, I am using it like this:
void Callback()
{
cout << "got a callback\n";
}
int main()
{
HRESULT hr = CoInitialize(NULL);
try
{
REDIXLServer::_REDIServerPtr ptr(__uuidof(REDIXLServer::REDIServer));
VARIANT_BOOL ret;
hr = ptr->Connect((long *) &Callback, &ret);
}
catch (_com_error & e)
{
cout << e.ErrorMessage() << endl;
}
The program crashes on the Conect() call. I am getting a message box "Managed Debugging Assistant 'InvalidFunctionPointerInDelegate has detected a problem.... Invalid function pointer was passed into the runtime to be converted to a delegate...."
So what am I doing wrong? Thank you