Sign in to follow this  
shadyvillian127

direct x input problems

Recommended Posts

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:

[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();
[/code]

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

Share this post


Link to post
Share on other sites
Hi,

1) DirectInput8Create() function creates an IDirectInput8* object. Why are you creating seperate objects for mouse and keyboard? It's irrelevant. Look:
[code]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.
[/code]

For some details, take a look at [url="http://www.toymaker.info/Games/html/directinput.html"]here[/url].

hth.
-R

Share this post


Link to post
Share on other sites
oh I get it. but it still dont get get why its failing. here the new code:

[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();
[/code]

Share this post


Link to post
Share on other sites
[quote name='thecakeisalie' timestamp='1305919946' post='4813589']
oh I get it. but it still dont get get why its failing.
[/quote]
According to the [url="http://msdn.microsoft.com/en-us/library/ee416756%28v=VS.85%29.aspx"]documentation for DirectInput8Create[/url], 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.

Share this post


Link to post
Share on other sites

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