473,387 Members | 1,465 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Callback Functions

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
Nov 16 '05 #1
6 2226
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

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 16 '05 #2

"db_from_mn" <db****@rtessentials.com> wrote in message
news:01****************************@phx.gbl...
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
Nov 16 '05 #3
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( )
{
....
}



-----Original Message-----
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

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ |

http://www.dotnetinterop.comPlease reply only to the newsgroup.
.

Nov 16 '05 #4
Hello Jock,
See my reply to Mattias.
Thanks for the attention.
Dennis
-----Original Message-----

"db_from_mn" <db****@rtessentials.com> wrote in message
news:01****************************@phx.gbl...
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 theerror
.

Nov 16 '05 #5
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
-----Original Message-----

"db_from_mn" <db****@rtessentials.com> wrote in message
news:01****************************@phx.gbl...
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 theerror
.

Nov 16 '05 #6
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
-----Original Message-----
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

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ |

http://www.dotnetinterop.comPlease reply only to the newsgroup.
.

Nov 16 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Eric Entressangle | last post by:
Hi all, is there any trouble setting an C++ static class method as callback function for a C program or library ? Thanks
5
by: Pratik | last post by:
what are callback functions? Where we require callback functions? In what scenario we require callback functions?
11
by: ajay.sonawane | last post by:
Hello ther I read somewhere that callback function should be global or static member function. 1: I could use static member functions but how could I access members of class which ar not static....
15
by: Felix Kater | last post by:
Hi, in a given library I register callback functions with this function: bool set_callback(int index, int (*callback_function)(long)); I need the callback function to also pass the index...
4
by: Jimmy | last post by:
I need to use Asynchronous Socket functions in a server application and am learning from sources such as the MSDN2 (http://msdn2.microsoft.com/en-us/library/bbx2eya8.aspx). What I observed is that...
4
by: Edwin Gomez | last post by:
I'm a C# developer and I'm new to Python. I would like to know if the concept of Asynchronous call-backs exists in Python. Basically what I mean is that I dispatch a thread and when the thread...
6
by: smmk25 | last post by:
Before I state the problem, I just want to let the readers know, I am knew to C++\CLI and interop so please forgive any newbie questions. I have a huge C library which I want to be able to use in...
2
by: Evan Burkitt | last post by:
Hi, all. I have a Windows DLL that exports a number of functions. These functions expect to receive a pointer to a callback function and an opaque void* parameter. The callback functions are...
1
by: kikivenkat | last post by:
Hi, I understand the concept of function pointers. I am a little confused with the call back functions. 1.If a function is invoked using a function pointer, then does it mean the function...
5
by: Jef Driesen | last post by:
I have a C DLL that I want to use from a C# project. The C header file contains these declarations: typedef void (*callback_t) (const unsigned char *data, unsigned int size, void *userdata);...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.