Skip to main content
GameDev.net gamedev.net
🔒 Locked

Direct Input initialization problem

Started by saravananps Jan 16, 2007 at 6:28 PM 0 replies 1.7k views
Original Post
saravananps
saravananps
Hi, I'm using directinput for getting input. Whenever i try to create the device the HRESULT shows a E_POINTER value and the device is not created
[sourcelang = "cpp"]
class CDirectInput
{
public:
	// Initialise the direct input system
	bool InitDirectInput (HINSTANCE hInstance);

	// This function creates the direct input keyboard device
	bool InitDIKeyboard (HWND g_WindowHandle);

	// This function creates the direct input mouse device
	bool InitDIMouse (HWND g_WindowHandle);

	// This function scans wheather a key is pressed
	bool KeyDown (char Key);

	// This function checks wheather a mouse button is pressed
	bool MouseButtonDown (int Button);

	// Scan for keypress and mouse movement
	void ScanInput();

	// This function terminates the existing direct input interface pointers
	void DI_Terminate();

private:

	LPDIRECTINPUT8       m_pDI;                 // the direct input object

	LPDIRECTINPUTDEVICE8 m_pDIKDevice;          // the direct input Keyboard device 
	char                 m_KeyboardState[256];
	
	LPDIRECTINPUTDEVICE8 m_pDIMDevice;          // the direct input Mouse device
	DIMOUSESTATE2        m_MouseState;

};

/////////////////////////////////////////////////////////////////////////////////////////////
/*** InitDirectInput ***/
// This function initialises the Direct Input system
////////////////////////////////////////////////////////////////////////////////////////////
bool CDirectInput::InitDirectInput (HINSTANCE hInstance)
{
	HRESULT hr = 0;

	// Create the Direct Input object
	hr = DirectInput8Create (hInstance,           // the instance handle to the application that is creating the object
							 DIRECTINPUT_VERSION, // specifies the version number of DI that this application requires
							 IID_IDirectInput8,   // identifier of the required interface
							 (void**)&m_pDI,      // address of the variable that will hold the DirectInput object
							 NULL);              // specifies the address of the controlling object's IUnknown interface 
	if (FAILED (hr))
	{
		return false;
	}

	return true;
}

/////////////////////////////////////////////////////////////////////////////////////////////
/*** InitDIKeyboard  ***/
// This function creates the Direct Input Keyboard device
// The device performs most of the keyboard-related tasks, using the methods of the interface
/////////////////////////////////////////////////////////////////////////////////////////////
bool CDirectInput::InitDIKeyboard (HWND g_WindowHandle)
{
	HRESULT hr = 0;

	// Create the Direct Input Keyboard device
	// Retrieve a pointer to IDirectInputDevice8 interface
	hr = m_pDI->CreateDevice (GUID_SysKeyboard,  // GUID for the device being created
							  &m_pDIKDevice,      // address of the variable to hold the DirectInput devicce upon its creation
							  NULL);             // specifies the address of the controlling object's IUnknown interface 
	
	if (FAILED(hr))
	{
		MessageBox (0, "Create DirectInput KeyBoard Device - Failed", "Error", 0);
		
		// Deallocate the existing DI interface pointers
		DI_Terminate();
		return false;
	}

	// Set the data format for the keyboard device
	hr = m_pDIKDevice->SetDataFormat (&c_dfDIKeyboard); // data format structure representing the keyboard
	
	if (FAILED(hr))
	{
		DI_Terminate();
		return false;
	}

	// Set the Keyboard device behaviour by using the Cooperative level
	hr = m_pDIKDevice->SetCooperativeLevel (g_WindowHandle,  // handle to the window that is requesting access to the device
										   DISCL_FOREGROUND | DISCL_NONEXCLUSIVE); // get input only when the window is cuurent active window in the desktop
															  // allow other running applications to use this device
	if (FAILED(hr))
	{
		DI_Terminate();
		return false;
	}

	// Acquire access to the keyboard device
	hr = m_pDIKDevice->Acquire();

	if (FAILED(hr))
	{
		DI_Terminate();
		return false;
	}

	return true;
}

/////////////////////////////////////////////////////////////////////////////////////////////
/*** InitDIMouse  ***/
// This function creates the Direct Input Mouse device
// The device performs most of the Mouse-related tasks, using the methods of the interface
/////////////////////////////////////////////////////////////////////////////////////////////
bool CDirectInput::InitDIMouse (HWND g_WindowHandle)
{
	HRESULT hr;

	// Create the Direct Input Mouse device
	// Retrieve a pointer to IDirectInputDevice8 interface
	hr = m_pDI->CreateDevice (GUID_SysMouse, // GUID for the device being created
							  &m_pDIMDevice, // address of the variable to hold the DirectInput devicce upon its creation
							  NULL);         // specifies the address of the controlling object's IUnknown interface 
	if (FAILED(hr))
	{
		MessageBox (0, "Create DirectInput Mouse Device - Failed", "Error", 0);
		
		// Deallocate the existing DI interface pointers
		DI_Terminate();
		return false;
	}

	// Set the daya format for the Mouse
	hr = m_pDIMDevice->SetDataFormat (&c_dfDIMouse);
	
	if (FAILED (hr))
	{
		DI_Terminate();
		return false;
	}

	// Set the Mouse behaviour by using the Cooperative level
	hr = m_pDIMDevice->SetCooperativeLevel (g_WindowHandle,  // handle to the window that is requesting access to the device
											DISCL_FOREGROUND | DISCL_NONEXCLUSIVE); // get input only when the window is cuurent active window in the desktop
															   // allow other running applications to use this device
	if (FAILED (hr))
	{
		DI_Terminate();
		return false;
	}

	// Acquire access to the mouse device
	hr = m_pDIMDevice->Acquire();
	
	if (FAILED (hr))
	{
		DI_Terminate();
		return false;
	}

	return true;

}


Could any one please help me to solve this problem.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.