Archived

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

Direct Input fails on initialization

This topic is 5024 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, i checked around in the forum and couldnt find anything that helped fix my problem.. I have added all the correct library files and include files. I get neither errors nor warnings when i start the program, yet it fails when first initializing direct input. here is a bit of the code (i copied it from the Zen of game programming, and their example works FINE!!) Here is my class that i used:
LPDIRECTINPUT8 g_pDI = 0;
class CZenMouse
{
public:
	CZenMouse();
	~CZenMouse();

// Variables

public:
	
protected:
	LPDIRECTINPUTDEVICE8 m_pMouseDev;
	
	BOOL m_bInitialized;

	DIMOUSESTATE m_MouseData;
// Functions

public:
	HRESULT Initialize();
	HRESULT Poll();

	POINT GetMousePos();
	BOOL IsButtonDown( int Button );

protected:

};

CZenMouse::CZenMouse()
{
	m_pMouseDev = 0;

	m_bInitialized = FALSE;
}

CZenMouse::~CZenMouse()
{
	// Unacquire and release the mouse device

	if( m_pMouseDev )
	{
		m_pMouseDev->Unacquire();
		m_pMouseDev->Release();
	}
}

POINT CZenMouse::GetMousePos()
{
	// Holds mouse data

	POINT MousePos;

	// Get the data from the buffer

	MousePos.x = m_MouseData.lX;
	MousePos.y = m_MouseData.lY;
	
	// Return the position

	return MousePos;
}

BOOL CZenMouse::IsButtonDown( int Button )
{
	// Return the button status from the buffer	

	if( m_MouseData.rgbButtons[Button] & 0x80 )
		return TRUE;
	else
		return FALSE;
}

HRESULT CZenMouse::Poll()
{
	HRESULT r = 0;

	// Return if the object has not been initialized

	if( !m_bInitialized )
		return E_FAIL;

	// Get the state of the mouse

	r = m_pMouseDev->GetDeviceState( sizeof( DIMOUSESTATE ), &m_MouseData );
	if( FAILED( r ) )
	{
		// If the mouse has moved focus

		if( r == DIERR_INPUTLOST )
		{
			// Reacquire the mouse

			while( r == DIERR_INPUTLOST )
				r = m_pMouseDev->Acquire();

			// Try to test the state again

			if( SUCCEEDED( r ) )
				m_pMouseDev->GetDeviceState( sizeof( DIMOUSESTATE ), &m_MouseData );
			else
				return FALSE;
		}
		else
			return E_FAIL;
	}

	return S_OK;
}

HRESULT CZenMouse::Initialize()
{
	HRESULT r = 0;

	// Return if the DirectInput object does not exist

	if( !g_pDI )
		return E_FAIL;

	// Release the mouse device if it has already been created

	if( m_pMouseDev )
		m_pMouseDev->Release();

	// Create the mouse device

	r = g_pDI->CreateDevice( GUID_SysMouse, &m_pMouseDev, NULL );
	if( FAILED( r ) )
	{
		FatalError( "Unable to create mouse device" );
		return E_FAIL;
	}

	// Set the data format for the mouse

	r = m_pMouseDev->SetDataFormat( &c_dfDIMouse );
	if( FAILED( r ) )
	{
		FatalError( "Unable to set the mouse data format" );
		return E_FAIL;
	}

	// Set the cooperative level for the mouse

	r = m_pMouseDev->SetCooperativeLevel( p_hwnd, DISCL_EXCLUSIVE | DISCL_FOREGROUND );
	if( FAILED( r ) )
	{
		FatalError( "Unable to set the cooperative level for the mouse" );
		return E_FAIL;
	}

	// Acquire the physical mouse into the device

	r = m_pMouseDev->Acquire();
	if( FAILED( r ) )
	{
		FatalError( "Unable to acquire mouse" );
		return E_FAIL;
	}
	
	// Set the initialization flag to true

	m_bInitialized = TRUE;

	return S_OK;
}

// ------------------------- class CZenKeyboard


class CZenKeyboard
{
public:
	CZenKeyboard();
	~CZenKeyboard();

// Variables

public:

protected:
	LPDIRECTINPUTDEVICE8 m_pKeyDev;
	char m_KeyBuffer[256];

	BOOL m_bInitialized;

// Functions

public:
	HRESULT Initialize();

	BOOL IsKeyDown( int Key );

protected:

};

CZenKeyboard::CZenKeyboard()
{
	ZeroMemory( &m_KeyBuffer, sizeof( m_KeyBuffer ) );
	m_pKeyDev = 0;

	m_bInitialized = FALSE;
}

CZenKeyboard::~CZenKeyboard()
{
	if( m_pKeyDev )
	{
		m_pKeyDev->Unacquire();
		
		m_pKeyDev->Release();
	}
}

HRESULT CZenKeyboard::Initialize()
{
	HRESULT r;

	if( !g_pDI )
		return E_FAIL;

	// Release the device if it has already been created

	if( m_pKeyDev )
	{
		m_pKeyDev->Unacquire();
		m_pKeyDev->Release();
	}
	
	// Create the device for the keyboard

	r = g_pDI->CreateDevice( GUID_SysKeyboard, &m_pKeyDev, NULL );
	if( FAILED( r ) )
	{
		FatalError( "Failed to create key device" );
		return E_FAIL;
	}

	// Set the data format for the device

	r = m_pKeyDev->SetDataFormat( &c_dfDIKeyboard );
	if( FAILED( r ) )
	{
		FatalError( "Unable to set the keyboard data format" );
		return E_FAIL;
	}

	// Set the cooperative level

	r = m_pKeyDev->SetCooperativeLevel( p_hwnd, DISCL_FOREGROUND | DISCL_NONEXCLUSIVE );
	if( FAILED( r ) )
	{
		FatalError( "Unable to set keyboard cooperative level" );
		return E_FAIL;
	}
	
	// Acquire the device

	r = m_pKeyDev->Acquire();
	if( FAILED( r ) )
	{
		FatalError( "Unable to acquire the keyboard" );
		return E_FAIL;
	}

	// Set the initialization flag to true

	m_bInitialized = TRUE;

	return S_OK;
}

BOOL CZenKeyboard::IsKeyDown( int Key )
{
	HRESULT r = 0;

	// Make sure the keyboard has been initialized

	if( !m_bInitialized )
		return FALSE;

	// Get the state of the keyboard into the key buffer

	r = m_pKeyDev->GetDeviceState( sizeof( m_KeyBuffer ), &m_KeyBuffer );
	if( FAILED( r ) )
	{
		// If the device is not acquired...

		if( r == DIERR_INPUTLOST )
		{
			// ...then reacquire the device

			while( r == DIERR_INPUTLOST )
				r = m_pKeyDev->Acquire();

			if( SUCCEEDED( r ) )
				m_pKeyDev->GetDeviceState( sizeof( m_KeyBuffer ), &m_KeyBuffer );
			else
				return FALSE;
		}
		else
			// ...Otherwise it was some other error

			return FALSE;
	}

	// Check if the key was set

	if( m_KeyBuffer[Key] & 0x80 )
		return TRUE;
	else
		return FALSE;
}

CZenKeyboard g_Keyboard;
CZenMouse g_Mouse;

HRESULT InitializeInput()
{
	if( g_pDI )
		g_pDI->Release();

	HRESULT r = 0;

	// Create the IDirectInput8 object

	r = DirectInput8Create( p_instance, DIRECTINPUT_VERSION, IID_IDirectInput8, (void**)&g_pDI, NULL );
	if( FAILED( r ) )
	{
		FatalError( "Failed to create DirectInput" );
		return E_FAIL;
	}

	// Initialize the keyboard

	r = g_Keyboard.Initialize();
	if( FAILED( r ) )
	{	
		FatalError( "Failed to initialize keyboard" );
		return E_FAIL;
	}	

	// Initialize the mouse

	r = g_Mouse.Initialize();
	if( FAILED( r ) )
	{
		FatalError( "Unable to initialize mouse" );
		return E_FAIL;
	}

	return S_OK;
}

HRESULT ShutdownInput()
{
	// Get rid of the IDirectInput8 object

	if( g_pDI )
	{
		g_pDI->Release();
		g_pDI = 0;
	}
	
	return E_FAIL;
}
Any thoughts on how i could make direct input initialize correctly?

Share this post


Link to post
Share on other sites
- When does it fail exactly? On initializing the object, the mouse, or the keyboard?
Try stepping into the program with a debugger and see where it failed. Take the failed hresult, and use it with DirectX Error Lookup tool.

- Use the Debug runtime when developing DX Apps. Turn debug output on for DInput, and it''ll tell you what''s wrong in your debugger output window.

- It''s usually not encouraging to post large code chunks in a post. Usually, you should only provide the relevant parts. If you didn''t know the relevant parts, it''s OK
That should be obvious when you step into the code.

Muhammad Haggag,
Optimize
Bitwise account: MHaggag -

Share this post


Link to post
Share on other sites
i did step through during debug and found out it failed on initialization of direct input itself was what failed... now i dont know how to check what went wrong, and im not a very good debugger, but all i know is that the return value was set to:

-2147024809

can anyone help me to find out what that is?

Share this post


Link to post
Share on other sites
ok well i found the error lookup tool and typed in my error number, but it came out with this input:

HRESULT: 0x4702482a (1191331882)
Name: Unknown
Description: n/a
Severity code: Success
Facility Code: Unknown (1794)
Error Code: 0x482a (18474)

it didnt seem to find out what was wronge... any ideas?

Share this post


Link to post
Share on other sites
quote:
Original post by Pirosan
ok well i found the error lookup tool and typed in my error number, but it came out with this input:

HRESULT: 0x4702482a (1191331882)
Name: Unknown
Description: n/a
Severity code: Success
Facility Code: Unknown (1794)
Error Code: 0x482a (18474)

it didnt seem to find out what was wronge... any ideas?

The HRESULT you''ve pasted into the lookup tool is wrong. It''s saying 0x4702482a hexadecimal, (1191331882) decimal (while yours is -2147024809)

Anyway, I checked it out:
HRESULT: 0x80070057 (2147942487)
Name: E_INVALIDARG
Description: An invalid parameter was passed to the returning function
Severity code: Failed
Facility Code: FACILITY_WIN32 (7)
Error Code: 0x0057 (87)

As you can see, it''s saying there''s an invalid parameter. Most probably, it''s the HINSTANCE. How did you retrieve that?

Muhammad Haggag,
Optimize
Bitwise account: MHaggag -

Share this post


Link to post
Share on other sites
Thank you very much... as it turned out i had created an instance and didnt point it to the instance created when the window was created. i changed this and now it initializes fine.

Thanks again for the help!!!

[edited by - pirosan on April 16, 2004 5:45:59 PM]

Share this post


Link to post
Share on other sites