Sign in to follow this  
Harvester944

Problem with DirectInput8Create

Recommended Posts

Well, here is the problem, plain and simple. the code below, when compiled, comes up with "c:\Program Files\microsoft directx 9.0 sdk(febuary 2005)\include\dinput.h: DIRECTINPUT_VERSION undefined. defaulting to 0x0800" and no further problems. However, when it is run, it always gives me the message box "DirectInput8Create has failed". So far, I have been unable to figure out why it is doing this, other than the obvious fact that DirectInput8Create is not returning a positive hResult.
// this is the hResult variable
HRESULT hResult;

// create the DirectInput object
hResult = DirectInput8Create(hInstance, DIRECTINPUT_VERSION, IID_IDirectInput8,     
                             (void**)&DirectInputObject, NULL);

// check to make sure that creation of the direct input object was succesful
if (FAILED ( hResult ) )
{
   MessageBox(NULL, "DirectInput8Create has failed", "Error", MB_OK);
   return false;
}
All help is appreciated, thank you -Harvester944

Share this post


Link to post
Share on other sites
Thank you for your fast replies.

I compared the hResult variable to the four that JClayton gave, and it turned up with "DIERR_INVALIDPARAM". Wondering if my hInstance might be invalide after all, I replaced hInstance with GetModuleHandle(NULL). I now get the "program has encounterd a problem and needs to close. We are sorry for the inconvenience..." ect. This happens before any message boxes appear. I checked back in the earlier code and found I was passing the hInstance variable to my DirectInputInitialization function incorrectly. When changed so that it is passed correctly, I get the same problem as when I use GetModuleHandle(NULL) Any ideas?

-Harvester944

Share this post


Link to post
Share on other sites
I had a similar problem with initialising DirectInput - have you left out the following define?

#define DIRECTINPUT_VERSION 0x0800

...make sure its included or it won't work. ^_^

Share this post


Link to post
Share on other sites
Have you tried doing Rebuild All? Do you have a debugger you can use? Taht should give you more information than "This program has encounterd a problem and needs to close". What compiler are you using?

Share this post


Link to post
Share on other sites
I am afraid that #define DIRECTINPUT_VERSION 0x0800 didn't work, it is also defined in the header file (dinput.h). I tried rebuild all, same problem. Running the debugger causes a pop-up window to appear which says :
"Unhandled exception at 0x00413385 in program.exe: 0xC0000005: Access violation reading location 0x00000000." Selectiong break leaves me with a large yellow arrow pointing at this line of code :

DirectInputDevice->GetDeviceState( sizeof(buffer), (LPVOID)&buffer );

I am using Microsoft Visual Studio .NET 2003

Thanks for your replies.

-Harvester944

Share this post


Link to post
Share on other sites
So, is it getting past the DirectInput8Create() call now, and failing when you get the device state? Perhaps the device is unacquired/lost?

Do you have all the debugging levels turned up to the max in the DirectX control panel etc. If you turn those up it might give you more helpful error messages (assuming you run from within the debugger).

EDIT: Your buffer parameters look a little suspect - what device is that for & how are they declared?

-Mezz

Share this post


Link to post
Share on other sites
Quote:
Original post by Harvester944
"Unhandled exception at 0x00413385 in program.exe: 0xC0000005: Access violation reading location 0x00000000." Selectiong break leaves me with a large yellow arrow pointing at this line of code :

DirectInputDevice->GetDeviceState( sizeof(buffer), (LPVOID)&buffer );
reading location 0x00000000. - That's a NULL pointer. And the only thing on that line that can be NULL is DirectInputDevice. So either your device setup is failing, or you're releasing your device accidently, after allocating it.

EDIT: Actually, if buffer is a member variable, and is the first variable in your class, then your this pointer could be NULL. Although I very much doubt that you'd have the buffer as a member variable in your class.

Share this post


Link to post
Share on other sites
Quote:
Original post by Evil Steve
EDIT: Actually, if buffer is a member variable, and is the first variable in your class, then your this pointer could be NULL. Although I very much doubt that you'd have the buffer as a member variable in your class.


I do. Is it a bad idea?

-Mezz

Share this post


Link to post
Share on other sites
Quote:
Original post by Mezz
Quote:
Original post by Evil Steve
EDIT: Actually, if buffer is a member variable, and is the first variable in your class, then your this pointer could be NULL. Although I very much doubt that you'd have the buffer as a member variable in your class.


I do. Is it a bad idea?
Oh, I was getting confused, sorry. I was thinking of GetDeviceData, since there's no real reason you'd keep the buffer for that around. But keeping the value of GetDeviceState is perfectly reasonable.

Share this post


Link to post
Share on other sites
Is buffer declared as a pointer? If so, has it been initialized by the time you call GetDeviceState? I'm with Evil on this one. It looks like your DirectInputDevice is NULL.

When you are dropped into the debugger, there should be a window with all of the variables currently in scope listed. You should be able to tell from that which one(s) is/are NULL.

Share this post


Link to post
Share on other sites
Evil Steve and Dave Hunt were correct, the DirectInputDevice was NULL. The problem was not that I was accidently releasing it, nor was the device setup failing. The problem originated in the fact that my device setup function only created the device for that function(this was not intentional), meaning that while the error checking code inside the function thought I had a valid DirectInputDevice, once the program left that function I no longer did. It works now, I am glad you could help.

Thanks again,

-Harvester944

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