Wrapping unmanaged C++ callbacks in C#

M

Marc Eaddy

Hi,

I have problem wrapping a callback declared in the
unmanged C++ and call a function that sets the callback from C#.

What I want to do is to first wrap an unmanaged C++ class using managed C++.
Then I access this managed C++ code from my C# code.

So here's the unmanaged C++ header: (I don't have access to the source code.
What I have is the .dll and the header file)

// a callback definition
typedef void (__cdecl * callBackFunc)(unsigned char * imageData);

class __declspec(dllexport) UnmanagedClass {
public:
.......
.......
void setCallBack(callBackFunc callback);
.......
}

Now, I want to somehow wrap this class in managed C++, so that I can pass a
delegate as shown below from C# that will be called whenever this
callBackFunc is called in the unmanaged C++ code.

[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate void CallBackFunc(IntPtr imageData);

How can I wrap the callback (callBackFunc) as well as the setCallBack(...)
function in the unmanaged C++ so that I can
pass this delegate from C# to the wrapped managed C++ code?

Also, is it possible to directly access the unmanaged C++ class from C#
instead of wrapping the class in managed C++ code first?

Any help would be appreciated.

Thanks
Ohan
 
B

Ben Voigt [C++ MVP]

Marc said:
Hi,

I have problem wrapping a callback declared in the
unmanged C++ and call a function that sets the callback from C#.

What I want to do is to first wrap an unmanaged C++ class using
managed C++. Then I access this managed C++ code from my C# code.

So here's the unmanaged C++ header: (I don't have access to the
source code. What I have is the .dll and the header file)

// a callback definition
typedef void (__cdecl * callBackFunc)(unsigned char * imageData);

class __declspec(dllexport) UnmanagedClass {
public:
.......
.......
void setCallBack(callBackFunc callback);
.......
}

Now, I want to somehow wrap this class in managed C++, so that I can
pass a delegate as shown below from C# that will be called whenever
this callBackFunc is called in the unmanaged C++ code.

[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate void CallBackFunc(IntPtr imageData);

How can I wrap the callback (callBackFunc) as well as the
setCallBack(...) function in the unmanaged C++ so that I can
pass this delegate from C# to the wrapped managed C++ code?

Declare a delegate type, create an event in the C++ ref class.

Then use Marshal::GetFunctionPointerForDelegate and cast to the right
unmanaged function pointer type, pass that pointer to the unmanaged code.

If the list of event handlers can change, you may want to use
GetFunctionPointerForDelegate on a delegate created from a member function
of the ref class that in turn calls the event handlers.
Also, is it possible to directly access the unmanaged C++ class from
C# instead of wrapping the class in managed C++ code first?

Nope. Unmanged C++ classes are not usable outside the module which defines
them. Of course you can export a C-compatible interface of loose functions
that accept a pointer to the object as the first parameter, like the Windows
API does.
 

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