Original Post
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: Header: Main Code:
//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
//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
#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;
}