E
Evan Burkitt
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 typedef'd to take void* parameters,
through which the DLL's function passes its void* parameter to the callback
function. This allows me to use class instances as callback handlers, as in
the example below.
//Executable and DLL know this:
typedef void (*callback)(void*);
//in DLL "Exporter.dll":
void ExportedFn(callback cb, void *opaque) { cb(opaque); }
//in executable:
class CallbackHandler
{
public:
static void Thunk(void *fromDll)
{
CallbackHandler *handler = static_cast<CallbackHandler*>(fromDll);
...
}
};
....
HANDLE dllInst = ::LoadLibrary("Exporter.dll");
typedef void (*DllExport)(callback, void*);
DllExport exportedFn = reinterpret_cast<DllFn>:GetProcAddress(dllInst,
"ExportedFn"));
CallbackHandler handlerInst;
exportedFn(CallbackHandler::Thunk, &handlerInst); /**/
....
What bothers me is that CallbackHandler::Thunk() must have a void* parameter
to match the callback signature, even though it's part of the
CallbackHandler class and its fromDll parameter is and must be a
CallbackHandler*.
My question is, is there a 'best practices' way to let Thunk()s parameter be
a CallbackHandler* and yet have the line marked with /**/ compile? By 'best
practice', I mean less of a blunt instrument than reinterpret_cast<> or a
C-style cast.
-Evan
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 typedef'd to take void* parameters,
through which the DLL's function passes its void* parameter to the callback
function. This allows me to use class instances as callback handlers, as in
the example below.
//Executable and DLL know this:
typedef void (*callback)(void*);
//in DLL "Exporter.dll":
void ExportedFn(callback cb, void *opaque) { cb(opaque); }
//in executable:
class CallbackHandler
{
public:
static void Thunk(void *fromDll)
{
CallbackHandler *handler = static_cast<CallbackHandler*>(fromDll);
...
}
};
....
HANDLE dllInst = ::LoadLibrary("Exporter.dll");
typedef void (*DllExport)(callback, void*);
DllExport exportedFn = reinterpret_cast<DllFn>:GetProcAddress(dllInst,
"ExportedFn"));
CallbackHandler handlerInst;
exportedFn(CallbackHandler::Thunk, &handlerInst); /**/
....
What bothers me is that CallbackHandler::Thunk() must have a void* parameter
to match the callback signature, even though it's part of the
CallbackHandler class and its fromDll parameter is and must be a
CallbackHandler*.
My question is, is there a 'best practices' way to let Thunk()s parameter be
a CallbackHandler* and yet have the line marked with /**/ compile? By 'best
practice', I mean less of a blunt instrument than reinterpret_cast<> or a
C-style cast.
-Evan