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)(voi d*);
//in DLL "Exporter.d ll":
void ExportedFn(call back cb, void *opaque) { cb(opaque); }
//in executable:
class CallbackHandler
{
public:
static void Thunk(void *fromDll)
{
CallbackHandler *handler = static_cast<Cal lbackHandler*>( fromDll);
...
}
};
....
HANDLE dllInst = ::LoadLibrary(" Exporter.dll");
typedef void (*DllExport)(ca llback, void*);
DllExport exportedFn = reinterpret_cas t<DllFn>(::GetP rocAddress(dllI nst,
"ExportedFn "));
CallbackHandler handlerInst;
exportedFn(Call backHandler::Th unk, &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_cas t<or a
C-style cast.
-Evan