Sign in to follow this  

IDirectInputDevice8::Acquire problem in my app

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

Hi guys, Hope you guys can give me assistance with this little 'DirectInput' problem I am having. Basically in my code when I try to Acquire() my keyboard device it is returning a 'FAILED' result and I am not sure why.
//------------- MAIN APPLICATION ENTRY POINT -------------//
... //Some include code omitted.

... //Some define code omitted.

IDirect3DDevice9 *D3DDevice = NULL;
IDirect3DVertexBuffer9 *VertexBuffer = NULL;
BaseWindow *baseWindow;

Equipment **equipment = new Equipment *[ EQUIPMENT_SIZE ];
int equipSize = Equipment::getCount();

Spacecraft **spaceship = new Spacecraft *[ AIRCRAFT_SIZE ];
PlayerControl *player;
Direct3DInput *direct3DInput;

struct CustomVertex
{
	FLOAT x, y, z;
	DWORD color;
};

void RandomNumGen::seedRandom();

//--- Function Prototypes ---//
void cleanUp();
void initStars();
void gameLoop();
void renderGame();
void setupGame();

int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd )
{
	baseWindow = new BaseWindow;
	baseWindow->registerWindowClass();
	baseWindow->createWindow();
	
	Direct3DUtility::init3D( hInstance, baseWindow->getHandle(), &D3DDevice );
	Direct3DUtility::initState( &D3DDevice );
	Direct3DUtility::initDirLight( &D3DDevice, Direct3DUtility::COLOR_WHITE );
	Direct3DUtility::initCamera( &D3DDevice );
		
	direct3DInput = new Direct3DInput;
	direct3DInput->initDirectInput( hInstance, baseWindow->getHandle() );

	setupGame();
	baseWindow->show();

	MSG msg;
	ZeroMemory( &msg, sizeof( MSG ));

	while( msg.message != WM_QUIT )
	{
		if( PeekMessage( &msg, 0, 0, 0, PM_REMOVE ))
		{
			TranslateMessage( &msg );
			DispatchMessage( &msg );
		}
		else
		{
			renderGame();
			Yield();
		}
	}

	UnregisterClass( baseWindow->getClassName(), hInstance );
	return 0;
}

void setupGame()
{
	... //code omitted.
}

void renderGame()
{
	... //code omitted.
}

void initStars()
{
	... //code omitted.
}

void cleanUp()
{
	// Releases resources (eg. memory, DirectX objects).
	... //code omitted
}


// ---------- Direct3DInput.h file ------------//
#ifndef DIRECT3DINPUT_H
#define DIRECT3DINPUT_H

#define DIRECTINPUT_VERSION 0x0800
#include <dinput.h>

#define KEY_SIZE 256

class Direct3DInput
{
public:
	Direct3DInput();
	void initDirectInput( HINSTANCE hInstance, HWND hWnd );
	bool keyDown( int key );

private:
	IDirectInput8 *inputSystem;
	IDirectInputDevice8 *keyboard;
	char keys[ KEY_SIZE ];
};

#endif

// ---------- Direct3DInput.cpp file -----------//
#include "Direct3DInput.h"

Direct3DInput::Direct3DInput()
{
	inputSystem = NULL;
	keyboard = NULL;
}

void Direct3DInput::initDirectInput( HINSTANCE hInstance, HWND hWnd )
{
	if( FAILED( DirectInput8Create( hInstance, DIRECTINPUT_VERSION, IID_IDirectInput8, ( void ** ) &inputSystem, NULL )))
	{
		MessageBox( 0, "Direct3DInput::initDirectInput() 1", 0, MB_OK );
	}

	if( FAILED( inputSystem->CreateDevice( GUID_SysKeyboard, &keyboard, NULL )))
	{
		MessageBox( 0, "Direct3DInput::initDirectInput() 2", 0, MB_OK );
	}

	if( FAILED( keyboard->SetDataFormat( &c_dfDIKeyboard )))
	{
		MessageBox( 0, "Direct3DInput::initDirectInput() 3", 0, MB_OK );
	}

	if( FAILED( keyboard->SetCooperativeLevel( hWnd, DISCL_FOREGROUND | DISCL_NONEXCLUSIVE )))
	{
		MessageBox( 0, "Direct3DInput::initDirectInput() 4", 0, MB_OK );
	}

	if( FAILED( keyboard->Acquire() ))
	{
		// When it gets to this part... it fails and hence goes into 'if' body.
		MessageBox( 0, "Direct3DInput::initDirectInput() 5", 0, MB_OK );
	}

	ZeroMemory(keys, KEY_SIZE * sizeof( char ));
}

... //other function omitted.


I even try catching the error values DIERR_INVALIDPARAM, DIERR_NOTINITIALIZED and DIERR_OTHERAPPHASPRIO but these are not the errors that is being returned. Let me show you what I mean...
if( FAILED( keyboard->Acquire() ))
{
	if( S_FALSE == DIERR_NOTINITIALIZED )
	{
		MessageBox( 0, "Direct3DInput::initDirectInput() 5", 0, MB_OK );
	}
}
But when I do the above... it doesn't go into the 2nd if at all, even if I change the error value to one of the other ones, the 2nd 'if' doesn't catch it. So I don't really know why I can't acquire my keyboard. Any help is really appreciated :-D P.S. Some will tell me not to use DirectInput anymore... and I've read some post about it. In the near future I will properly research and get a more in depth understanding of it all... but for now and my this case I've decided to stick with it.

Share this post


Link to post
Share on other sites
Quote:
Original post by tenpoundbear
Let me show you what I mean...

if( FAILED( keyboard->Acquire() ))
{
if( S_FALSE == DIERR_NOTINITIALIZED )
{
MessageBox( 0, "Direct3DInput::initDirectInput() 5", 0, MB_OK );
}
}


But when I do the above... it doesn't go into the 2nd if at all, even if I change the error value to one of the other ones, the 2nd 'if' doesn't catch it.

So I don't really know why I can't acquire my keyboard.

The if statement will never be true:


//
// Success codes
//
#define S_OK ((HRESULT)0x00000000L)
#define S_FALSE ((HRESULT)0x00000001L)



You instead want to store the HRESULT returned by Acquire, then check it for FAILURE, and then look at it for further info.

Share this post


Link to post
Share on other sites
Thanks Mattd,

I think I have the problem resolved now.
Took your advised and amended my coded like this.


void Direct3DInput::initDirectInput( HINSTANCE hInstance, HWND hWnd )
{
HRESULT hResult;
... //Old code omitted

// Call Acquire() to gain access to our input device.
hResult = keyboard->Acquire();
if( hResult == DIERR_OTHERAPPHASPRIO )
{
MessageBox( 0, "Direct3DInput::initDirectInput() 5", 0, MB_OK );
}

... //Old code omitted.
}

When I looked up what the error meant which was, "Another application has a higher priority level, preventing this call from succeeding. This value is equal to the E_ACCESSDENIED standard DirectInput return value. This error can be returned when an application has only foreground access to a device but is attempting to acquire the device while in the background."

Hence I just move my "baseWindow->show();" up a few lines in the main application entry point and that seems to have fixed it.

Thanks :)

Share this post


Link to post
Share on other sites

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