Archived

This topic is now archived and is closed to further replies.

DirectInput8 newbie CALLBACK error question

This topic is 5574 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Newbie question.. I get this error when compiling this code. error C2664: 'IDirectInput8A::EnumDevices' : cannot convert parameter 2 from 'BOOL (DIDEVICEINSTANCE *,void *)' to 'LPDIENUMDEVICESCALLBACKA' Anyone know why? I have followed the msdn joystick tutorials for directx8. Typecast to (LPDIENUMDEVICESCALLBACK) doesn't work either.
  
bool csForceFeedback::init(){
	// Create a DirectInput device

	if (FAILED(hr = DirectInput8Create(GetModuleHandle(NULL), DIRECTINPUT_VERSION, 
		IID_IDirectInput8, (VOID**)&di, NULL))) {
			csPrintf("DirectInput interface creation failed!\n");
			return false;
		}
		csPrintf("DirectInput interface created!\n");


		if( FAILED( hr = di->EnumDevices( DI8DEVCLASS_GAMECTRL,EnumJoysticksCallback,	NULL, DIEDFL_ATTACHEDONLY ) ) ) {
			csPrintf("Joystick not found!\n");
			return false;
		}

		if( NULL == g_pJoystick ) {
			csPrintf("Joystick not found!\n");
			return false;
		}
		csPrintf("Joystick found!\n");

		return true;
}

BOOL CALLBACK csForceFeedback::EnumJoysticksCallback(DIDEVICEINSTANCE*pdidInstance, VOID* pContext)
{
    HRESULT hri;

    // Obtain an interface to the enumerated joystick.

    hri = di->CreateDevice(pdidInstance->guidInstance,  
                                &g_pJoystick, NULL);
    if(FAILED(hri)) 
        return DIENUM_CONTINUE;

    return DIENUM_STOP;
}
  
[edited by - Jim Adams on September 11, 2002 8:36:36 AM]

Share this post


Link to post
Share on other sites
public:
bool Initialize(iObjectRegistry* iobject_reg);
csForceFeedback(iBase* iParent);
virtual ~csForceFeedback();
virtual bool init();
BOOL CALLBACK EnumJoysticksCallback(DIDEVICEINSTANCE* pdidInstance, VOID* pContext);

};

declared like this.. in the same cpp file.

Share this post


Link to post
Share on other sites
It looks like there's two problems then. The first is that EnumJoysticksCallback() takes a pointer to const DIDEVICEINSTANCE (LPC DIDEVICEINSTANCE). I think I missed that the first time round myself...

So pop a const in before DIDEVICEINSTANCE *. Then you'll probably hit the second problem. A pointer to member function is not the same as a pointer to function. Effectively the member function has an implicit parameter which passes the address of the object the member function was called on (the "this" pointer). You'll need to declare EnumJoysticksCallback like this:


class csForceFeedback
{
public:
static BOOL CALLBACK EnumJoysticksCallback(const DIDEVICEINSTANCE* pdidInstance, VOID* pContext);
};


But then you won't be able to get at the data members of csForceFeedback from inside EnumJoysticksCallback. If you need to be able to access them, you make use of the third parameter to EnumDevices. This is a void pointer which is passed to the enumeration function on every call.


// Pass the address of this object to the enumproc
hr = di->EnumDevices( DI8DEVCLASS_GAMECTRL,EnumJoysticksCallback, (void *)this, DIEDFL_ATTACHEDONLY)));

// And here's how you use the address of the object
BOOL CALLBACK csForceFeedback::EnumJoysticksCallback(DIDEVICEINSTANCE*pdidInstance, VOID* pContext)
{
csForceFeedback *self = (csForceFeedback *)pContext;

// Assuming di is a member of csForceFeedback, here's how you use it
self->di->CreateDevice(pdidInstance->guidInstance, &g_pJoystick, NULL);
}


[edited by - Krunk on September 11, 2002 7:15:46 AM]

Share this post


Link to post
Share on other sites