Handling callbacks from unmanaged code

K

kelvin.koogan

I have an unmanaged DLL which calls my managed C++ app using a
callback (standard C++ function pointer). I can receive the callbacks
OK using a static function with global scope, but I can't find a way
to get hold of a pointer to my Form to pass it the data.

Any ideas?

Application seems to have the only pointer to the form but no way for
me to retrieve it (in VC 2003 anyway). Any attempt to create a static
pointer to the form seems to fail.

Ideas gratefully received.

TIA,
KK
 
B

Ben Voigt [C++ MVP]

I have an unmanaged DLL which calls my managed C++ app using a
callback (standard C++ function pointer). I can receive the callbacks
OK using a static function with global scope, but I can't find a way
to get hold of a pointer to my Form to pass it the data.

In the unmanaged world, a callback (almost) always comes with a extra
parameter to pass application data.

..NET can also make a bound delegate into a function pointer.

If you show the declaration of the callback and the line used to register
it, we can probably help more.
 
K

kelvin.koogan

In the unmanaged world, a callback (almost) always comes with a extra
parameter to pass application data.

.NET can also make a bound delegate into a function pointer.

If you show the declaration of the callback and the line used to register
it, we can probably help more.

Definition of callback

typedef void (*CallbackFn)(BYTE*, int);

Function to register callback

Initialise(const char *pPort, CallbackFn callback);

Note this is C++ not C.

KK
 
B

Ben Voigt [C++ MVP]

Definition of callback

typedef void (*CallbackFn)(BYTE*, int);

Function to register callback

Initialise(const char *pPort, CallbackFn callback);

Note this is C++ not C.

Then Initialise is a member function of some class? Does pPort get passed
to the callback as its first parameter?

If not, you'll need a .NET bound delegate created in the usual way, and use
::System::Runtime::InteropServices::Marshal::GetFunctionPointerForDelegate
to get an unmanaged function pointer from it.
 

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