C2664

Hello

I have a fuction that one of the parameters is address of another function :

defenistion of callback function in my header file and a member of my class :

BOOL CALLBACK EnumJoysticksCallback(const DIDEVICEINSTANCE* pdidInstance,VOID* pContext );

where i want use it:

HRESULT CJoyDLL::InitDirectInput(HWND hDlg)

{

...

hr = g_pDI->EnumDevices( DI8DEVCLASS_GAMECTRL,

&CJoyDLL::EnumJoysticksCallback, /*here fuction needs address of EnumJoysticksCallBack, but this code generate C2664 ? */

NULL, DIEDFL_ATTACHEDONLY );

if ( FAILED(hr) )

return hr;

......

}

but when i define my function in my source file as a global function it works well :

hr = g_pDI->EnumDevices( DI8DEVCLASS_GAMECTRL, EnumJoysticksCallback , NULL, DIEDFL_ATTACHEDONLY );

[1239 byte] By [TPECI] at [2007-12-27]
# 1
A callback function can only be a global function or a static member of a class. The pointer you get when using a non-static member of a class is a C++ "pointer to member" style pointer that does not work as a callback. That's because calling back through that pointer would required the caller to also have a pointer to your object but that's not the case here.
MikeDanes at 2007-9-3 > top of Msdn Tech,Visual C++,Visual C++ General...
# 2

Callback function must be static or global. Non-static class function doesn't match EnumJoysticksCallback type. Use global or static callback function. To return back to class instance, use pContext parameter.

hr = g_pDI->EnumDevices( DI8DEVCLASS_GAMECTRL, EnumJoysticksCallback , this, DIEDFL_ATTACHEDONLY );


BOOL CALLBACK EnumJoysticksCallback( const DIDEVICEINSTANCE* pdidInstance,VOID* pContext )
{
CJoyDLL* pThis = (CJoyDLL*)pContext;

pThis->InstanceCallback(pdidInstance); // call instance function using class pointer passed as context
}

AlexFarber at 2007-9-3 > top of Msdn Tech,Visual C++,Visual C++ General...