Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / Interop / September 2005

Tip: Looking for answers? Try searching our database.

Passing C# delegates to C++ functions expecting a function pointer

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
John Eyles - 25 Aug 2005 13:51 GMT
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.


Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.