Sign in to follow this  
das_echo

Access Violation using a Joystick

Recommended Posts

Hi, Today I found a cool codesnippet to access a joystick by using DirectInput. After some changes it seamed to work, but everytime I use the GetDeviceState-function I get an Access Violation Error. //Codesnippet: // Multitap Joypad Plugin // Supports many Joypads // Written by WolfCoder (2006) #pragma comment(lib,"dinput.lib") #pragma comment(lib,"dinput8.lib") #pragma comment(lib,"dxguid") #include <dinput.h> // DirectInput header LPDIRECTINPUT lpdii; HINSTANCE hmain; unsigned short num_joypads = 0; // Number of plugged in joypads char joynames[8][MAX_PATH]; // Strings of all the names GUID joyid[8]; // Joypad ID's LPDIRECTINPUTDEVICE lpdiid; LPDIRECTINPUTDEVICE2 joy[8]; DIJOYSTATE joys[8]; DIPROPRANGE joyrange; bool joy_in[8]; typedef struct { short joy_axis_x,joy_axis_y; bool button[36]; }joyplug_state; // Specialized joypad state joyplug_state joystate[8]; bool CALLBACK enum_joypads(LPCDIDEVICEINSTANCE lpddi,LPVOID guid_ptr) { if(num_joypads == 8) return DIENUM_STOP; // Record the GUID joyid[num_joypads] = lpddi->guidInstance; // Copy the joypad name for(int x = 0;x < MAX_PATH;x++) joynames[num_joypads][x] = lpddi->tszProductName[x]; joy_in[num_joypads] = true; num_joypads++; // A joypad was registered return DIENUM_CONTINUE; } // Creates the joypad objects bool init_joypads(HINSTANCE hmodule) { if(FAILED(DirectInput8Create(hmodule,DIRECTINPUT_VERSION,IID_IDirectInput8,reinterpret_cast<LPVOID*>(&lpdii),NULL))) // Create the main object return false; // Return failure code // Now guid all devices if(FAILED(lpdii->EnumDevices(DI8DEVCLASS_GAMECTRL,(LPDIENUMDEVICESCALLBACKW)enum_joypads,NULL,DIEDFL_ATTACHEDONLY))) return false; // Return failure code // Now create all devices for(int x = 0;x < num_joypads;x++) { lpdii->CreateDevice(joyid[x],&lpdiid,NULL); if(FAILED(lpdiid->QueryInterface(IID_IDirectInputDevice2,(void**)&joy[x]))) return false; lpdiid->Release(); lpdiid = NULL; // Set cooperative level if(FAILED(joy[x]->SetCooperativeLevel(GetDesktopWindow(),DISCL_BACKGROUND | DISCL_NONEXCLUSIVE))) return false; if(FAILED(joy[x]->SetDataFormat(&c_dfDIJoystick))) return false; // Set range and dead zones // X Range joyrange.lMin = -255; joyrange.lMax = 255; joyrange.diph.dwSize = sizeof(DIPROPRANGE); joyrange.diph.dwHeaderSize = sizeof(DIPROPHEADER); joyrange.diph.dwObj = DIJOFS_X; joyrange.diph.dwHow = DIPH_BYOFFSET; joy[x]->SetProperty(DIPROP_RANGE,&joyrange.diph); // Y Range joyrange.lMin = -255; joyrange.lMax = 255; joyrange.diph.dwSize = sizeof(DIPROPRANGE); joyrange.diph.dwHeaderSize = sizeof(DIPROPHEADER); joyrange.diph.dwObj = DIJOFS_Y; joyrange.diph.dwHow = DIPH_BYOFFSET; joy[x]->SetProperty(DIPROP_RANGE,&joyrange.diph); // Acquire the joypad if(FAILED(joy[x]->Acquire())) return false; // Poll the joypad if(FAILED(joy[x]->Poll())) return false; } return true; // Return success code } void free_joypads() { // Call this on game exit for(int x = 0;x <8;x++) { if(joy[x]) { joy[x]->Unacquire(); joy[x]->Release(); } } if(lpdii) lpdii->Release(); } // Returns number of joypads int joypads() { return num_joypads; } // Returns a joypad vendor name (returns the pointer to string) char *str_joy(int index) { return &joynames[index][0]; } // Gathers the joypad states void get_state() { // Call this every frame for(int x = 0;x < num_joypads;x++) { // Get states of joypads if(joy_in[x]) { if(FAILED(joy[x]->GetDeviceState(sizeof(DIJOYSTATE),(LPVOID)&joys[x]))) { joy_in[x] = false; } joystate[x].joy_axis_x = (short)joys[x].lX; joystate[x].joy_axis_y = (short)joys[x].lY; for(int y = 0;y < 32;y++) { if(joys[x].rgbButtons[y] > 32) joystate[x].button[y] = true; else joystate[x].button[y] = false; } switch(joys[x].rgdwPOV[0]) { case -1: joystate[x].button[32] = false; // UP joystate[x].button[33] = false; // RIGHT joystate[x].button[34] = false; // DOWN joystate[x].button[35] = false; // LEFT break; case 0: joystate[x].button[32] = true; // UP joystate[x].button[33] = false; // RIGHT joystate[x].button[34] = false; // DOWN joystate[x].button[35] = false; // LEFT break; case 4500: joystate[x].button[32] = true; // UP joystate[x].button[33] = true; // RIGHT joystate[x].button[34] = false; // DOWN joystate[x].button[35] = false; // LEFT break; case 9000: joystate[x].button[32] = false; // UP joystate[x].button[33] = true; // RIGHT joystate[x].button[34] = false; // DOWN joystate[x].button[35] = false; // LEFT break; case 9000+4500: joystate[x].button[32] = false; // UP joystate[x].button[33] = true; // RIGHT joystate[x].button[34] = true; // DOWN joystate[x].button[35] = false; // LEFT break; case 18000: joystate[x].button[32] = false; // UP joystate[x].button[33] = false; // RIGHT joystate[x].button[34] = true; // DOWN joystate[x].button[35] = false; // LEFT break; case 18000+4500: joystate[x].button[32] = false; // UP joystate[x].button[33] = false; // RIGHT joystate[x].button[34] = true; // DOWN joystate[x].button[35] = true; // LEFT break; case 18000+9000: joystate[x].button[32] = false; // UP joystate[x].button[33] = false; // RIGHT joystate[x].button[34] = false; // DOWN joystate[x].button[35] = true; // LEFT break; case 18000+9000+4500: joystate[x].button[32] = true; // UP joystate[x].button[33] = false; // RIGHT joystate[x].button[34] = false; // DOWN joystate[x].button[35] = true; // LEFT break; } } else { ZeroMemory(&joystate[x],sizeof(joyplug_state)); } } } // WinMain: #include "stdafx.h" #include "joystick2.h" #include <stdio.h> int WinMain(HINSTANCE hInstance, HINSTANCE prevInstance, LPSTR lpCmdLine, int nShowCmd){ init_joypads(hInstance); get_state(); return 1; } Please, can somebody help me? This thing is driving me nuts. Greetings, echo

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Well, it might help if you placed your code snippet in code tags. However, I suggest you use something that already wraps this from you. Maybe SDL, OIS, or something similar.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this