Hi.
I am writing a C# interface to a C++ / C driver for a hardware card and I am
having the following issue when trying to set up a callback function. When
the card generates an event that causes the driver to invoke the callback,
Visual Studio (or the application if running the straight executable)
crashes. Can anyone verify if I am setting the callback function correctly?
Has anyone run into a similar problem when passing delegates to C++ code that
expects a function pointer as input? Thanks.
C# Signature:
[DllImport(libVpbLocation)]
private static extern int vpb_set_event_callback(int handle,
CardCallbackEventHandler CardCallBack, object context);
C++ / C Function signature
int WINAPI vpb_set_event_callback(int handle, void (WINAPI
*event_callback)(VPB_EVENT *e, void *context), void *context)
C# Delegate Definition:
public delegate void CardCallbackEventHandler(ref VpbEvent eventData, object
context);
C# Callback function signature
private void Callback_Event(ref VpbEvent eventData, object context)
C# code that calls the function to set the callback
...
CardCallbackEventHandler callback = new CardCallbackEventHandler
(Callback_Event);
int RetValue = vpb_set_event_callback(Handle, callback, this);
...
Finally, the VpbEvent object is a struct with a few integer members.

Signature
Cheers,
John Eyles
Mattias Sjögren - 25 Aug 2005 16:35 GMT
John,
>void (WINAPI *event_callback)(VPB_EVENT *e, void *context)
[...]
>public delegate void CardCallbackEventHandler(ref VpbEvent eventData, object
>context);
void* translates to an IntPtr in C#, not an object.
Also keep in mind the fact that the VPB_EVENT* can be NULL in C++ (in
theory at least, I don't know if it ever is in this context), but ref
parameters can't be in C#.
And finally, if the callback is asynchronous, make sure you keep a
reference to the delegate to prevent it from being garbage collected.
Mattias

Signature
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Stu Mackellar - 01 Sep 2005 17:14 GMT
John,
I had a similar problem a couple of years ago with a frame grabber card. The
problem turned out to be that the callback function was expected to have the
__cdecl calling convention. This turned out to be a big problem because,
although C# supports changing the calling convention for outgoing P/Invoke
calls, it's not possible to change the calling convention for incoming
delegates, at least directly. However, the framework itself does support
this in the underlying IL. My solution, admittedly not very neat, was to
write a script that disassembled the assembly to IL, parsed it until it
found the declaration of the callback and added the
modopt([mscorlib]System.Runtime.CompilerServices.CallConvCdecl) modifier,
and then reassembled the IL.
This process is well described at the following URL (which is where I found
out about it in the first place):
http://www.dotnet247.com/247reference/msgs/17/87210.aspx
HTH,
Stu.
From: "Mattias Sjögren" <mattias.dont.want.spam@mvps.org>
Subject: Re: Passing C# delegates to C++ functions expecting a function
pointer
Date: 25 August 2005 16:35
John,
>void (WINAPI *event_callback)(VPB_EVENT *e, void *context)
[...]
>public delegate void CardCallbackEventHandler(ref VpbEvent eventData,
>object
>context);
void* translates to an IntPtr in C#, not an object.
Also keep in mind the fact that the VPB_EVENT* can be NULL in C++ (in
theory at least, I don't know if it ever is in this context), but ref
parameters can't be in C#.
And finally, if the callback is asynchronous, make sure you keep a
reference to the delegate to prevent it from being garbage collected.
Mattias

Signature
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.