Delegates - what am I doing wrong?

P

Pepa Cougar

Hello, I'm writing a managed C++ wrapper for a legacy C++ code. I'm
wrapping a script-compiler class, which uses a callback to report
script errors back to the client.

The callback prototype is defined as:

typedef void (__stdcall *COMPILE_ERROR_CALLBACK)(int Line, char* Text,
void* Data);


The script compiler provides a method for setting a callback with the
following prototype:

void SetCompileErrorCallback(COMPILE_ERROR_CALLBACK Callback, void
*Data);



That was the unmanaged side. I have a managed C++ class wrapping the
native class, which defines a delegate:

__delegate void ScriptCompilerCallback(int Line, String* Text, IntPtr
Data);


I want to wrap the native SetCallback method like this:

bool ManagedClass::SetCompilerCallback(ScriptCompilerCallback*
Callback, IntPtr Data)
{
Native()->m_ScriptEngine->SetCompileErrorCallback(Callback,
Data.ToPointer());
return true;
}


But I'm getting the following compile error:

error C2664: 'NativeClass::SetCompileErrorCallback' : cannot convert
parameter 1 from 'Namespace::ManagedClass::ScriptCompilerCallback __gc
*' to 'COMPILE_ERROR_CALLBACK'
There is no context in which this conversion is possible


What am I doing wrong? What is the right way of passing a managed
delegate to the unmanaged code?
I tried to remove the third parameter from the callback (void* vs
IntPtr) also changing char* to const char* but I'm still getting the
same error.

I would be grateful for any advice. Thank you.
 
P

Pepa Cougar

Please...?

Or it isn't possible to pass a managed callback function to the native code
in a single mixed assembly? No other way than DllImport? I'd like to wrap
the unmanaged .lib into a .NET assembly. Please help.

-pc
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top