directinput problem

Started by
14 comments, last by omegasyphon 22 years, 9 months ago
when i go to run my program i get an unhandled exception from this line how would i correct this? when i debug, it goes to the GetDeviceState line.
    

int keycontrol(GLfloat x)
{

	HRESULT hr = lpdikey->GetDeviceState(sizeof(buffer),&buffer);

		if (KEYDOWN(buffer,DIK_ESCAPE))
		PostQuitMessage(0);
		if (KEYDOWN(buffer,DIK_LEFT))
			x -= .5f;
		if (KEYDOWN(buffer,DIK_RIGHT))
			x += 0.5f;

	return x;
}

}

  
Edited by - omegasyphon on June 26, 2001 4:10:46 PM
Advertisement
How about:
HRESULT hr = lpdikey->GetDeviceState(sizeof(buffer),buffer);



- Goblineye Entertainment
The road to success is always under construction
Goblineye EntertainmentThe road to success is always under construction
that doesnt work
well Tornado is right, your previous code doesn''t need the & before buffer, otherwise the code looks good. it should work

------------------------------
BCB DX Library - RAD C++ Game development for BCB
taken out the the ampersand still gives me the same problem
u forst me to do this , mycode is pretty short tho

  #define INITGUID#include <windows.h>#include <fstream.h>#include <glut.h>#include <stdio.h>#include <math.h>#include <objbase.h>#include "dinput.h"#include "vars.h"#define glpi 3.1415fGLfloat	xrot,yrot;GLint bx=0,by=0;int mstate = 0;LPDIRECTINPUTDEVICE7 lpdikey	= NULL;LPDIRECTINPUT7		 lpdi		= NULL;UCHAR buffer[256];HWND main_window_handle = NULL; // save the window handleHINSTANCE main_instance      = NULL; // globally track hinstanceint initcontrols(){	if (DirectInputCreateEx(main_instance,DIRECTINPUT_VERSION,IID_IDirectInput7,(void**)&lpdi,NULL)!=DI_OK)		return 0;	if (lpdi->CreateDeviceEx(GUID_SysKeyboard,IID_IDirectInputDevice7,(void**)&lpdikey,NULL)!=DI_OK)		return 0;	if (lpdikey->SetCooperativeLevel(main_window_handle,DISCL_BACKGROUND | DISCL_NONEXCLUSIVE)!=DI_OK)		return 0;	if (lpdikey->SetDataFormat(&c_dfDIKeyboard)!=DI_OK)		return 0;	lpdikey->Acquire();	mstate=1;	return 1;}int keycontrol(GLfloat x){	if (lpdikey->GetDeviceState(256,buffer)!=DI_OK)		return 0;		if (buffer[DIK_ESCAPE] & 0x80)		PostQuitMessage(0);		if (buffer[DIK_LEFT] & 0x80)			x -= .5f;		if (buffer[DIK_RIGHT] & 0x80)			x += 0.5f;	return x;}int killkeyboard(){	// first unacquire the mouselpdikey->Unacquire();// now release the mouselpdikey->Release();// release directinputlpdi->Release();return 1;}void renderscene(){	GLfloat x;	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);	glMatrixMode(GL_MODELVIEW);	glPushMatrix();	glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);	glRotatef(x,1.0f,0.0f,0.0f);	glRotatef(30.0f,0.0f,0.0f,1.0f);	glEnableClientState(GL_VERTEX_ARRAY);	glVertexPointer(3,GL_FLOAT,0,cords);	glDrawElements(GL_TRIANGLE_STRIP,1600,GL_UNSIGNED_INT,corners);	glPopMatrix();	glutSwapBuffers();}void setuprc(){	glClearColor(0.0f,0.0f,0.0f,1.0f);	glColor3f(0.0f,1.0f,0.0f);}void ChangeSize(int w, int h)	{	GLfloat nRange = 100.0f;	// Prevent a divide by zero	if(h == 0)		h = 1;	// Set Viewport to window dimensions    glViewport(0, 0, w, h);	// Reset projection matrix stack	glMatrixMode(GL_PROJECTION);	glLoadIdentity();	// Establish clipping volume (left, right, bottom, top, near, far)    if (w <= h) 		glOrtho (-nRange, nRange, -nRange*h/w, nRange*h/w, -nRange, nRange);    else 		glOrtho (-nRange*w/h, nRange*w/h, -nRange, nRange, -nRange, nRange);	// Reset Model view matrix stack	glMatrixMode(GL_MODELVIEW);	glLoadIdentity();	}void main(){	GLfloat x=0.0;	if (mstate==0)	initcontrols();	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);	glutCreateWindow("game engine console");	glutReshapeFunc(ChangeSize);	glutDisplayFunc(renderscene);	setuprc();	keycontrol(x);	killkeyboard();	glutMainLoop();}  
Is buffer created:

char buffer[256];

of char* buffer = new char[256] ?

If it is the latter then sizeof( buffer ) is gong to evaluate to the size of a pointer and not the size of your buffer.


Seeya
Krippy

Edited by - krippy2k on June 29, 2001 11:55:25 AM
Are you positive that lpdikey is a valid DI pointer when you get to this code?

What does the exception say? Access violation?
it just says unhandled exception error 0xc0000005
and goes to the line in game controls

if (lpdikey->GetDeviceState(256,buffer)!=DI_OK)
Ok, 0xc0000005 is an access violation. It means that you are trying to access memory that you do not have permission to access.

Either lpdikey or buffer are not properly initialized before this call. I would be willing to bet that it is lpdikey. Post the code that you use to initialize lpdikey.

Seeya
Krippy

This topic is closed to new replies.

Advertisement