Skip to main content
gamedev.net
🔒 Locked

Direct Input Problem

Started by methinks 2 replies 800+ views
Original Post
methinks
methinks
Let me try this again... (My connection killed my last posting attemp) I've implemented DirectInput in one of my projects, in order to read the keyboard. The program compiles just fine, but when I run it, I get the following error: Unhandled exception at 0x0049b34d in Valkyrie.exe: 0xC0000005: Access violation reading location 0xcccccccc. I've traced it back to where I call GetDeviceState(): keyboard->GetDeviceState(sizeof(keys),(LPVOID)&keys); but I have no idea what might be causing it, or how to start looking for the problem. Below is the code I used (the void * func(char * FUNC, param * PARAM) function is my message handler. In the case of init, it carries two parameters: hInstance and hWnd. Base Class:

//cNode.h

#ifndef CNODE_H
#define CNODE_H

//#include "defines.h"
#include "cError.h"

struct param
{
	void * data;
	param * next;
};

class cNode
{
public:
	virtual void * func(char * FUNC, param * PARAM)  = 0;
};

#endif	//CNODE_H


Header:

//cUserInput.h
//Reads from input devices (Mouse, keyboard)
//In this case, using directInput

#ifndef CINPUT_H
#define CINPUT_H

#include "cNode.h"
#include <dinput.h>

class cUserInput : public cNode
{
public:
	void * func(char * FUNC, param * PARAM);
	cUserInput();

private:
	LPDIRECTINPUT8			input;
	LPDIRECTINPUTDEVICE8	keyboard;
	char keys[256];
	int	bReturn;

	bool init(HINSTANCE hInst, HWND hWnd);
	void cleanup();
	bool keydown(int KEY);

	
};

#endif	//CINPUT_H


Main Code:

#include "cUserInput.h"

void * cUserInput::func(char * FUNC, param * PARAM)
{
	void * pReturn = NULL;
	if(FUNC == "INIT")
	{
		init((HINSTANCE) PARAM->data, (HWND) PARAM->next->data);
	}

	else if(FUNC == "GETESC")
	{
		bReturn = false;
		if(keydown(DIK_ESCAPE))
			bReturn = true;
		pReturn = &bReturn
	}

	return pReturn;
}

cUserInput::cUserInput()
{
	for(int i = 0; i<256; i++)
	{
		keys = NULL;
	}
}

bool cUserInput::init(HINSTANCE hInst, HWND hWnd)
{
    if(FAILED(DirectInput8Create(hInst,
								DIRECTINPUT_VERSION,
								IID_IDirectInput8,
								(void **)&input,
								NULL)))
		return false;

	if(FAILED(input->CreateDevice(GUID_SysKeyboard,&keyboard,NULL)))
		return false;

	if(FAILED(keyboard->SetDataFormat(&c_dfDIKeyboard)))
		return false;

	if(FAILED(keyboard->SetCooperativeLevel(hWnd,DISCL_EXCLUSIVE|DISCL_NOWINKEY)))
		return false;

	if(FAILED(keyboard->Acquire()))
		return false;

	return true;

}

void cUserInput::cleanup()
{
	if(keyboard != NULL)
	{
		keyboard->Unacquire();
		keyboard->Release();
		keyboard = NULL;
	}

	if(input != NULL)
	{
		input->Release();
		input = NULL;
	}
}

bool cUserInput::keydown(int KEY)
{
	keyboard->GetDeviceState(sizeof(keys),(LPVOID)&keys);
	if(keys[KEY] & 0x80)
	{
		return true;
	}
	return false;
}


Supernat02
Supernat02
What does your code do if the function "init" returns false? It looks as if "keyboard" is an invalid pointer. Breakpoint your software and test the creation of "keyboard" and then at the GetDeviceState function and inspect the values of keyboard and keys.
Chris ByersMicrosoft DirectX MVP - 2005
methinks
methinks
Thanks, That helped, I found the cause of the problem... Now to find a way to fix it...

The problem is that DirectInput8Create fails, returning before it has a chance to initiallize the keyboard. I need to check my pointers goind into the init, I have a feeling that's where the problem is.

Again, thanks for the help
methinks
methinks
It works now.
Thank you for your help

Topic Locked

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

Sign in to reply to this topic.