Sign in to follow this  

DirectInput question (sending application-defined data to enum function)

This topic is 3742 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

Hello! I asked a similar question in the general programming forum, but I think this is more of a DirectX-specific question... First of all, look at this: DIEnumDevicesCallback by Microsoft The part I'm interested is the "LPVOID pvRef" parameter... How do I send a pointer to a struct through this variable? For example, look at this example callback function...
BOOL CALLBACK EnumJoysticksCallback( const DIDEVICEINSTANCE* pdidInstance,
		VOID* pContext )
{
	// Write to a variable in the struct "DXStuff" through "pContext"...
	pContext->DXStuff->joyInUse = TRUE;
}
Here's where this callback function is being called from...
HRESULT Init_JoyInput(DirectStuff *DXStuff)
{
	// Send 'DXStuff' to EnumJoysticksCallback() using this pointer...
	// ??? (dunno how to do it)

	// Enumerate joysticks using EnumJoysticksCallback()...
	if( FAILED( DXStuff->g_pDI->EnumDevices( DI8DEVCLASS_GAMECTRL, 
		EnumJoysticksCallback,
		NULL, DIEDFL_ATTACHEDONLY ) ) )
	{
		// DirectInput couldn't enumerate anything.  Something wrong?
		return E_FAIL;
	}
}
I'd like to send "DXStuff" to 'EnumJoysticksCallback()' from 'Init_JoyInput()'... How do I do that? I've worn a bald spot from all my head-scratching on this one. Thanks in advance for the help!

Share this post


Link to post
Share on other sites

DXStuff->g_pDI->EnumDevices( DI8DEVCLASS_GAMECTRL, EnumJoysticksCallback, ( void* )DXStuff, DIEDFL_ATTACHEDONLY );

BOOL CALLBACK EnumJoysticksCallback( const DIDEVICEINSTANCE* pdidInstance, void* pContext )
{
// Write to a variable in the struct "DXStuff" through "pContext"...
DirectStuff *DXStuff = ( DirectStuff* )pContext;
DXStuff->joyInUse = TRUE;

}



EDIT: Added example.

Share this post


Link to post
Share on other sites
Awesome, thanks for the info Mike.Popoloski. That worked very well! But I'm not sure how to actually send 'DXStuff' to "EnumJoysticksCallback()" from either "Init_JoyInput()" or through "DXStuff->g_pDI->EnumDevices()"... Could you tell me how to do that? If I can get that part to work then it'll finally get my callback and "Init_JoyInput()" functions working together.

Share this post


Link to post
Share on other sites
The line at the top of my example shows how to do it. Just cast your pointer to a void* pointer and you will be fine. If you need a full example:


HRESULT Init_JoyInput(DirectStuff *DXStuff)
{
// Send 'DXStuff' to EnumJoysticksCallback() using this pointer...
// ??? (dunno how to do it)

// Enumerate joysticks using EnumJoysticksCallback()...
if( FAILED( DXStuff->g_pDI->EnumDevices( DI8DEVCLASS_GAMECTRL,
EnumJoysticksCallback,
( void* )DXStuff, DIEDFL_ATTACHEDONLY ) ) )
{
// DirectInput couldn't enumerate anything. Something wrong?
return E_FAIL;
}
}

Share this post


Link to post
Share on other sites
Sorry, I didn't notice that bit of code at the top of your first example. That's really cool! Thanks for the help Mike.Popoloski, I really appreciate it! [smile]

Share this post


Link to post
Share on other sites

This topic is 3742 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this