all groups > visual c > june 2007 >
You're in the

visual c

group:

Handling callbacks from unmanaged code


Handling callbacks from unmanaged code kelvin.koogan NO[at]SPAM googlemail.com
6/25/2007 4:36:04 AM
visual c:
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
Re: Handling callbacks from unmanaged code Ben Voigt [C++ MVP]
6/25/2007 12:17:21 PM

[quoted text, click to view]

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.

Re: Handling callbacks from unmanaged code kelvin.koogan NO[at]SPAM googlemail.com
6/26/2007 12:33:35 AM
[quoted text, click to view]

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
Re: Handling callbacks from unmanaged code Ben Voigt [C++ MVP]
6/26/2007 9:41:04 AM

[quoted text, click to view]

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.

[quoted text, click to view]

AddThis Social Bookmark Button