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

Direct Input acces violation

Started by cpp boy Dec 18, 2003 at 6:47 PM 8 replies 700+ views
Original Post
cpp boy
cpp boy
Ok. I got the book Windows Game Programming for dummies and I''m at the direct input part. The current stade of my directx sdks are the one that are included whit Microsoft Visual C++ 6.0. I''m not sure what version they are because there is no version on .h files but I''m assuming thats its the version 6 or 5. Here is the code of direct Input I''m using. #define INITGUID #include <windows.h> #include <dinput.h> #include <objbase.h> HINSTANCE hInstance; HWND hwnd; LPDIRECTINPUT lpdi; LPDIRECTINPUTDEVICE lpdikey; // Here is some codes about creating the window int Init() // this is where I''m implementing my direct Input code { if(DirectInputCreate(hInstance, DIRECTINPUT_VERSION, &lpdi, NULL) != DI_OK) { MessageBox(NULL, "Error", "Error", MB_OK | MB_ICONERROR); } lpdi->CreateDevice(GUID_SysKeyboard, &lpdikey, NULL); lpdikey->SetCooperativeLevel(hwnd, DISCL_BACKGROUND | DISCL_NONEXCLUSIVE); return 0; } Kevin
Kevin
JoeyBlow2
JoeyBlow2
Which line is giving you the access violation?
cpp boy
cpp boy
Normally the line : lpdikey->SetCooperativeLevel(hwnd, DISCL_BACKGROUND | DISCL_NONEXCLUSIVE);

is setting me a probleme but I have an empression that my direct Input is setting as I want to do.

Kevin
Kevin
cpp boy
cpp boy
So I guess no one of you is able to help whit my probleme.

Kevin
Kevin
Mezz
Mezz
Have you tried checking the return value from the CreateDevice() call?
It might be failing and leaving you with an invalid lpdikey pointer.

-Mezz
cpp boy
cpp boy
by the return value do you mean something like this :

if(lpdi)
lpdi->release();

cause if so when I try to do this it doesn''t work.

also I''ve tried this :

lpdi->release()

ans it didn''t work. But it had work for

if(lpdd) // Direct Draw
lpdd->release();

so thats about it.
Kevin
Dave Hunt
Dave Hunt
quote:

by the return value do you mean something like this :

if(lpdi)
lpdi->release();

cause if so when I try to do this it doesn''t work.

also I''ve tried this :

lpdi->release()

ans it didn''t work. But it had work for

if(lpdd) // Direct Draw
lpdd->release();

so thats about it.


You need to initialize lpdi and lpdikey to NULL when you declare them. Otherwise, they could have garbage in them and your code will think they got set by the create calls.

But what Mezz was refering to was the line:

lpdi->CreateDevice(GUID_SysKeyboard, &lpdikey, NULL);

You aren''t checking that call for failure like you did the DirectInputCreate call.
Pipo DeClown
Pipo DeClown
Have you tried using the newer versions of DI?

LPDIRECTINPUT8 g_lpDI;
LPDIRECTINPUTDEVICE8 g_lpDIDevice;


.. and put this:
g_lpDIDevice->SetDataFormat(&c_dfDIKeyboard);

.. Before the SetCoo..() function.

--
You''re Welcome,
Rick Wong

- sitting in his chair doing the most time-consuming thing..
VolkerG
VolkerG
First: Make sure you get the debug runtimes and watch the debug output.
Second: As mentioned before, test the return values (the HRESULT).
Third: Use FAILED() and co. to check for errors, not ... != *_OK.
johnnyBravo
johnnyBravo
did you check that you have initialised the program first, like the windows creation etc, ive done that many times...

Topic Locked

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

Sign in to reply to this topic.