Callback Functions

  • Thread starter Thread starter db_from_mn
  • Start date Start date
D

db_from_mn

I'm having a lot of difficulty getting even the simplest
callback function (void return, no arguments) to work,
from a dll to C#.
When the callback is invoked from the dll, I get a fatal
error, that shuts down the app.
I suspect that the function pointer is passed incorrectly.
I've studied the documentation, and I seem to have things
configured properly.
Are there any bugs that I need to know of, or any hints,
other than what I've found in the documentation?
Thank in advance,

Dennis
 
When the callback is invoked from the dll, I get a fatal
error, that shuts down the app.

So you're passing out the delegate to unmanaged code?

Can you post some code?



Mattias
 
db_from_mn said:
I'm having a lot of difficulty getting even the simplest
callback function (void return, no arguments) to work,
from a dll to C#.
When the callback is invoked from the dll, I get a fatal
error, that shuts down the app.
I suspect that the function pointer is passed incorrectly.
I've studied the documentation, and I seem to have things
configured properly.
Are there any bugs that I need to know of, or any hints,
other than what I've found in the documentation?
Thank in advance,

Dennis

It would help if we could see some code or some snippet that is causing the
error
 
Here is some code, to illustrate my implementation:

In the dll:
typedef void (CALLBACK * UCANEVENT_CALLBACK)( void );
A function to pass the callback function:
UCAN_API eUCANERROR_T eUCanOpenDriver( UCANEVENT_CALLBACK
lpEventCallbackFn );
Where...
UCAN_API is defined as: extern "C" __declspec(dllexport)
eUCANERROR_T is an enumeration.

In the C# code:
public delegate void UCanEventCallbackDelegate( );
[DllImport("UCanApi.dll")]
public static extern eUCANERROR eUCanOpenDriver(
UCanEventCallbackDelegate lpEventCallbackFn);
(This is defined as a member function of the class
CUCanApi)

UCanEventCallbackDelegate MyUCanEventCallback =
new UCanEventCallbackDelegate( UCanEventCallback );
eError = CUCanApi.eUCanOpenDriver( MyUCanEventCallback );

// Callback implementation
void UCanEventCallback( )
{
....
}
 
I solved this problem.
I had not created the delegate instance at the class
level, but in the constructor. It was therefore being
claimed by the garbage-collector.
Thanks for your attention.
Dennis
 
Hello Mattias,
I solved this problem.
I had not created the delegate instance at the class
level, but in the constructor. It was therefore being
claimed by the garbage-collector.
Thanks for your attention.
Dennis
 

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

Back
Top