direct x input problems

Started by
3 comments, last by shadyvillian127 12 years, 11 months ago
I'm learning direct x input from this book but I can't get my program to not crash. it builds ok but the DirectInput8Create always fails. I've searched on the internet and nothing helped. Here is my code:


HRESULT Result;

Result = DirectInput8Create(hInstance, DIRECTINPUT_VERSION, IID_IDirectInput8, (void**)&KeyboardInput, NULL);
if(FAILED(Result))
{
MessageBox(NULL, "Error", "Failed to create keyboard input device!", MB_ICONERROR|MB_OK);
return -1;
}
Result = DirectInput8Create(hInstance, DIRECTINPUT_VERSION, IID_IDirectInput8, (void**)&MouseInput, NULL);
if(FAILED(Result))
{
MessageBox(NULL, "Error", "Failed to create mouse input device!", MB_ICONERROR|MB_OK);
return -1;
}

KeyboardInput->CreateDevice(GUID_SysKeyboard, &KeyboardID, NULL);
MouseInput->CreateDevice(GUID_SysMouse, &MouseID, NULL);

KeyboardID->SetDataFormat(&c_dfDIKeyboard);
KeyboardID->SetCooperativeLevel(hWnd, DISCL_FOREGROUND | DISCL_NONEXCLUSIVE);
KeyboardID->Acquire();


See anything wrong? I doubled check the code alot. it crashes on DirectInput8Create.
Advertisement
Hi,

1) DirectInput8Create() function creates an IDirectInput8* object. Why are you creating seperate objects for mouse and keyboard? It's irrelevant. Look:
LPDIRECTINPUT8 yourDirectInputObject = NULL;
Result = DirectInput8Create (hInstance, DIRECTINPUT_VERSION, IID_IDirectInput8, (LPVOID*) &yourDirectInputObject, 0);
if (FAILED(Result))
//blah blah here.

LPDIRECTINPUTDEVICE8 pMouseDev = NULL, pKeyboardDev = NULL;
Result = yourDirectInputObject->CreateDevice (GUID_SysKeyboard, &pKeyboardDev, 0);
if (FAILED(Result))
//another blah blah here.

Result = yourDirectInputObject->CreateDevice (GUID_SysMouse, &pMouseDev, 0);
if (FAILED(Result))
//and another blah blah here.


For some details, take a look at here.

hth.
-R
There's no "hard", and "the impossible" takes just a little time.
oh I get it. but it still dont get get why its failing. here the new code:


HRESULT Result;

Result = DirectInput8Create(hInstance, DIRECTINPUT_VERSION, IID_IDirectInput8, (void**)&Input, NULL);
if(FAILED(Result))
{
MessageBox(NULL, "Failed to create input device!", "Error", MB_ICONERROR|MB_OK);
return -1;
}

Input->CreateDevice(GUID_SysKeyboard, &KeyboardID, NULL);
Input->CreateDevice(GUID_SysMouse, &MouseID, NULL);

KeyboardID->SetDataFormat(&c_dfDIKeyboard);
KeyboardID->SetCooperativeLevel(hWnd, DISCL_FOREGROUND | DISCL_NONEXCLUSIVE);
KeyboardID->Acquire();

oh I get it. but it still dont get get why its failing.

According to the documentation for DirectInput8Create, if the function fails, the return value can be one of four error codes. That should give you, and us, a better idea about what's going wrong.
It returned DIERR_INVALIDPARAM.

EDIT: I fixed it. I changed hInstance with GetModuleHandle(NULL).

This topic is closed to new replies.

Advertisement