Direct Input Error

Started by
1 comment, last by Wilben 24 years ago
Hello, Ihave checked this through and I can''t see any errors, so here is my code. I get an access violation in GetInput Keyboard: WPInput.h: #define KEYDOWN(buf, key) (buf[key] & 0x80) char KeyboardBuff[256]; #include HINSTANCE DHInstance; LPDIRECTINPUT lpDI; HRESULT dival; LPDIRECTINPUTDEVICE keyboard; CreateInput(void) { DirectInputCreate(DHInstance, DIRECTINPUT_VERSION, &lpDI, NULL); if(dival==DI_OK) { MessageBox(hWnd, "Input create worked", "HOORAY!", MB_OK);} return 0; } CreateKeyboard(void) { dival=lpDI->CreateDevice(GUID_SysKeyboard, &keyboard, NULL); return 0; } SetKeyboardFormat(void) { dival=keyboard->SetDataFormat(&c_dfDIKeyboard); return 0; } SetCoopKeyboardNormal(void) { dival=keyboard->SetCooperativeLevel(hWnd, DISCL_FOREGROUND/DISCL_NONEXCLUSIVE); return 0; } GetInputKeyboard(void) { dival=keyboard->GetDeviceState(sizeof(KeyboardBuff), (LPVOID)&KeyboardBuff); return 0; } AcquireKeyboard(void) { keyboard->Acquire(); return 0; } simple.cpp: #include "WPWin.h" #include "WPInput.h" main(void) { CreateInput(); CreateKeyboard(); SetKeyboardFormat(); SetCoopKeyboardNormal(); AcquireKeyboard(); return 0; } /*----------------------------------------------------------------*\ Function: WindowProc(); Purpose: Handles window messages; \*----------------------------------------------------------------*/ LRESULT CALLBACK WindowProcedure(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) { switch(msg) { case WM_MOVE: MessageBox(hWnd, "WM_MOVE: The Window Moved", TITLE, MB_OK); return 0; case WM_DESTROY: MessageBox(hWnd, "WM_DESTROY: Exiting Application", "Goodbye!", MB_OK); PostQuitMessage(0); return 0; } GetInputKeyboard(); if (KEYDOWN(KeyboardBuff, DIK_LEFT)) { MessageBox(hWnd, "hum", "Goodbye!", MB_OK); } return DefWindowProc(hWnd, msg, wParam, lParam); } int PASCAL WinMain(HINSTANCE hInstance, HINSTANCE hPrev, LPSTR lpCmdLine, int Cmd) { StartRegWin(); wc.lpfnWndProc = WindowProcedure; EndRegWin(); MakeWin(); if(hWnd==NULL) return FALSE; else { ShowWindow (hWnd, Cmd); UpdateWindow(hWnd); } // Message loop; while(GetMessage(&msg,NULL,0,0)) { TranslateMessage(&msg); DispatchMessage (&msg); } return msg.wParam; } WPWin.h: #define WIN32_LEAN_AND_MEAN // Quicker build times; #include #define TITLE "WinCode" HWND hWnd; MSG msg; WNDCLASS wc; HINSTANCE hInstance; int Cmd; StartRegWin(void) { wc.style =CS_HREDRAW/CS_VREDRAW; wc.cbClsExtra =0; wc.cbWndExtra =0; wc.hInstance =hInstance; wc.hIcon =LoadIcon(hInstance,IDI_APPLICATION); wc.hCursor =LoadCursor(NULL,IDC_ARROW); wc.hbrBackground =(HBRUSH)(COLOR_WINDOW+3); wc.lpszMenuName =NULL; wc.lpszClassName =TITLE; return 0; } EndRegWin(void) { RegisterClass(&wc); return 0; } MakeWin(void) { hWnd = CreateWindow(TITLE, // class name; TITLE, // window name; WS_OVERLAPPEDWINDOW, // window style; CW_USEDEFAULT, CW_USEDEFAULT, // starting position (x,y); 320, 240, // width and height; NULL, // parent handle; NULL, // menu handle; hInstance, // instance handle; NULL); // other parameters; // Check if window creation failed; otherwise show and update; return 0; } I don''t belive the error is in WPWin.h because I''ve used it in code before and it worked fine. I''m really sorry to bother you all, but you are, eyt again, my last hope, Thanks, Wilben. Wilben wilben@nerdy.freeserve.co.uk
Advertisement
try leaving out the & in dival=keyboard->GetDeviceState(sizeof(KeyboardBuff), (LPVOID)&KeyboardBuff);
just an idea, it''s really hard to read code on the forum that is that long
Hi

Just one answer !

Check your Error Codes, on all Function Calls, not only the Creation call, because when the
dival=lpDI->CreateDevice(GUID_SysKeyboard, &keyboard, NULL);

Fails then keyboard points to NULL, and any operation done on keyboardfails with an access Violation !

Lars
--------> http://www.larswolter.de <---------

This topic is closed to new replies.

Advertisement