Archived

This topic is now archived and is closed to further replies.

Julio

GetDeviceState() problems

Recommended Posts

Julio    116
ok, I''m writing a DirectInput class so I don''t have to mess with it everytime I code something. I using code from the DirectX help file to make it easy. For some reason whenever I add the GetDeviceState() command, (to see a a key has been pressed), it gives me a runtime error. One of those, ''This Program Has Performed an Illegal Opperation''. Anyways, I was wondering what was wrong with this function. Here''s my header file so far.
    
#include <dinput.h>

class CDInput
{
public:
	int DI_Init(HINSTANCE h, HWND hwnd );
	int DI_Shutdown();
	int WINAPI  GetKey();	//key input

private:
	LPDIRECTINPUT7			DI;			
	LPDIRECTINPUTDEVICE7	DI_Key;						
	BYTE	buffer[256];
};						

int WINAPI CDInput::GetKey()
{
	HRESULT hr = DI_Key->GetDeviceState(sizeof(buffer), (LPVOID)&buffer);

	if(buffer[DIK_ESCAPE] & 0x80)
		return true;
	else
	return false;
}
CDInput::DI_Init(HINSTANCE h, HWND hwnd )
{
    // Create Direct Input

    if ( DirectInputCreateEx( h,			// Window Instance

        DIRECTINPUT_VERSION,						// Direct Input Version

        IID_IDirectInput7,							// Version 7

        (void**)&DI,								// Direct Input

        NULL ) )
    {
    return(false);									// Couldn''t Initialize Direct Input

    }

    // Create The Keyboard Device

    if ( DI->CreateDeviceEx( GUID_SysKeyboard,	// Define Which Device To Create (KeyBoard,Mouse Or Joystick)
        IID_IDirectInputDevice7,					// Version 7

        (void**)&DI_Key,							// KeyBoard Device

        NULL ) )
    {
		// Couldn''t Create The Keyboard Device

        return(false);								// Failed, Return False
    }

    if ( DI_Key->SetDataFormat(&c_dfDIKeyboard) )	// Set The Keyboard Data Format

    {
		// Couldn''t Set The Data Format

        return(false);								// Failed, Return False
    }

    // Set The Cooperative Level

    if ( DI_Key->SetCooperativeLevel(hwnd, DISCL_FOREGROUND | DISCL_EXCLUSIVE) )
    {
		// Couldn''t Set The Cooperative Level

        return(false);								// Failed, Return False
    }

    if (DI_Key)									// Did We Create The Keyboard Device?

        DI_Key->Acquire();						// If So, Acquire It
    else
        return(false);								// If Not, Return False

	return true;
}

CDInput::DI_Shutdown()
{
    if (DI)
    {
        if (DI_Key)
        {
            DI_Key->Unacquire();
            DI_Key->Release();
            DI_Key = NULL;
        }
        DI->Release();
        DI = NULL;
    }
	return true;
}    
thanks, Joe JoeMont001@aol.com www.polarisoft.n3.net

Share this post


Link to post
Share on other sites
BitBlt    386
Try making your buffer UCHAR buffer[256] instead of BYTE. Don''t know if it''ll help, but it''s worth a shot.



"We are the music makers, and we are the dreamers of the dreams."
- Willy Wonka

Share this post


Link to post
Share on other sites
MuteAngel    122
Well IMHO it''s because you don''t create a device for your keyboard. Look at your code. If the DirectInputCreateEx succeeds, you exit the method by returning FALSE. Therefore CreateDeviceEx is never called (btw, if CreateDeviceEx succeeds, you''ll exit the method too).

Basically, you''re calling GetDeviceState from a mad pointer.

Or for some reason, the message board didn''t display the ! or FAILED. In this case, ignore the above.

Share this post


Link to post
Share on other sites
msn12b    390
Alright, first off, you need to add a return type to the definition of DI_Init().

Secondly, all DirectX APIs return HRESULTs. If the call succeeds, it returns S_OK, which equals 0. So, you need to compare the return values to S_OK.

That should fix your problem.

MSN

Share this post


Link to post
Share on other sites
Julio    116
quote:

Why did you make it WINAPI?


like I said, I took this out of the dx documentation. that''s what they had so I tried to see if it would work. it didn''t make any difference.

quote:

Well IMHO it''s because you don''t create a device for your keyboard. Look at your code. If the DirectInputCreateEx succeeds, you exit the method by returning FALSE. Therefore CreateDeviceEx is never called (btw, if CreateDeviceEx succeeds, you''ll exit the method too).


I''ll take out the returns and see what happens.

quote:

Alright, first off, you need to add a return type to the definition of DI_Init().


exactly what type of return value do you mean?

thanks again,
Joe

JoeMont001@aol.com www.polarisoft.n3.net

Share this post


Link to post
Share on other sites
MuteAngel    122
Try like this :

        

int CDInput::DI_Init(HINSTANCE h, HWND hwnd )
{

HRESULT hResult;

// Create Direct Input

hResult = DirectInputCreateEx( h, // Window Instance

DIRECTINPUT_VERSION, // Direct Input Version

IID_IDirectInput7, // Version 7

(void**)&DI, // Direct Input

NULL );

if (FAILED(hResult))
{
return(false); // Couldn't Initialize Direct Input

}

// Create The Keyboard Device

hResult = DI->CreateDeviceEx( GUID_SysKeyboard, // Define Which Device To Create (KeyBoard,Mouse Or Joystick)
IID_IDirectInputDevice7, // Version 7

(void**)&DI_Key, // KeyBoard Device

NULL );
if (FAILED(hResult))
{
return(FALSE); // Couldn't Create The Keyboard Device

}

hResult = DI_Key->SetDataFormat(&c_dfDIKeyboard); // Set The Keyboard Data Format


if (FAILED(hResult))
{
return(FALSE); // Couldn't Set The Data Format

}

// Set The Cooperative Level

hResult = DI_Key->SetCooperativeLevel(hwnd, DISCL_FOREGROUND | DISCL_EXCLUSIVE);

if (FAILED(hResult))
{
return(FALSE); // Couldn't Set The Cooperative Level

}

if (DI_Key) // Did We Create The Keyboard Device?

DI_Key->Acquire(); // If So, Acquire It
else
return(FALSE); // If Not, Return False

return(TRUE);
}


Also, remember to set the return type of DI_Shutdown() to int in your implementation (and not only the declaration).

Hope this is readable.. The board doesn't like tabulations

Edited by - MuteAngel on October 29, 2000 9:58:32 PM

Share this post


Link to post
Share on other sites
BitBlt    386
Two more suggestions, don''t try ''em at the same time that way you know which one for sure is the culprit (if either):
1) Try calling SetDataFormat() before you call SetCooperativeLevel(), and
2) For your dwFlags parameter in SetCooperativeLevel(), try using DISCL_BACKGROUND | DISCL_NONEXCLUSIVE, of course, this also depends on if you''re in fullscreen or windowed mode.

These are some differences I saw when I took a look at my wrapper class. Hope it helps.



"We are the music makers, and we are the dreamers of the dreams."
- Willy Wonka

Share this post


Link to post
Share on other sites
Quantum    122
hmm... my direcinput code is exactly the same as yours apart from a couple of things:
im checking if FAILED() on the return values - doesn''t seem to make a difference if i leave it out

my functions to check the keyboard state aren''t WINAPI - doesn''t make a difference when they are

im creating mine in nonexclusive mode - ofcourse, this doesn''t make a difference except if you''re trying to use normal windows functions to access the keyboard

and my variables have different names

so the only thing i can think of is to reinstall the directx sdk, and directx itself - you''re code seems to be fine

hmm.. unless you''re not passing a valid HWND or HINSTANCE to the function.. but you are aren''t you?

Share this post


Link to post
Share on other sites