Author Topic: Request for Help: Joystick console program problem  (Read 4391 times)

yindengxie

  • Member
  • ***
  • Posts: 28
Request for Help: Joystick console program problem
« on: August 29, 2020, 03:13:24 pm »
Hello Everyone!

My joystick console program was initialized and everything was normal,
and the correct joystick product name could also be printed out, but
an error was always returned when hr = joystick->Poll(); I request you
to give me help and guidance. The complete program is attached below.
------------------------------------------------------------------------

#include "stdafx.h"
#include <InitGuid.h>

#define DIRECTINPUT_VERSION 0x0800

#ifndef D_INPUT
#define D_INPUT
#include <dinput.h>
#endif

char  ProductName[100];

LPDIRECTINPUT8 di;
LPDIRECTINPUTDEVICE8 joystick;
DIDEVICEINSTANCE pdidi;
DIDEVICEINSTANCE info;

DIJOYSTATE2 js; 
DIDEVCAPS capabilities;
HRESULT hr;

BOOL CALLBACK enumAxesCallback(const DIDEVICEOBJECTINSTANCE* instance, VOID* context)
{
 HWND hDlg = (HWND)context;

 DIPROPRANGE propRange;
 propRange.diph.dwSize = sizeof(DIPROPRANGE);
 propRange.diph.dwHeaderSize = sizeof(DIPROPHEADER);
 propRange.diph.dwHow = DIPH_BYID;
 propRange.diph.dwObj = instance->dwType;
 propRange.lMin = -50;
 propRange.lMax = +50;

 if (FAILED(joystick->SetProperty(DIPROP_RANGE, &propRange.diph))) return DIENUM_STOP;

 return DIENUM_CONTINUE;
}

BOOL CALLBACK enumCallback(const DIDEVICEINSTANCE* instance, VOID* context)
{
 HRESULT hr;

 hr = di->CreateDevice(instance->guidInstance, &joystick, NULL);

 if (FAILED(hr)) return DIENUM_CONTINUE;

 return DIENUM_STOP;
}

int main(int argc, char * argv[])
{
 while (1)
 {
  if (FAILED(hr = DirectInput8Create(GetModuleHandle(NULL), DIRECTINPUT_VERSION,IID_IDirectInput8, (VOID**)&di, NULL))) return hr;

  if (FAILED(hr = di->EnumDevices(DI8DEVCLASS_GAMECTRL, enumCallback, NULL, DIEDFL_ATTACHEDONLY))) return hr;
   
  if (joystick == NULL)  return E_FAIL;
 
  if (FAILED(hr = joystick->SetDataFormat(&c_dfDIJoystick2))) return hr;
 
  if (FAILED(hr = joystick->SetCooperativeLevel(GetConsoleWindow(), DISCL_NONEXCLUSIVE | DISCL_FOREGROUND))) return hr;
   
  capabilities.dwSize = sizeof(DIDEVCAPS);
  if (FAILED(hr = joystick->GetCapabilities(&capabilities))) return hr;
   
  if (FAILED(hr = joystick->EnumObjects(enumAxesCallback, NULL, DIDFT_AXIS))) return hr;
   
  info.dwSize = sizeof(DIDEVICEINSTANCE);
   
  if (FAILED(hr = joystick->GetDeviceInfo(&info))) return hr;
   
  int i = 0;
  while (i < MAX_PATH && info.tszProductName != 0) ProductName = info.tszProductName[i++];
 
  ProductName[MAX_PATH] = '\0';
  printf("ProductName = %s \n", ProductName);
 
  //--------------------- The above-mentioned all work OK!  Product Name is alao OK!
 
  while (1)
  { 
   if (joystick == NULL) return S_OK;
 
   hr = joystick->Poll();  //From this line begin, joystick->Poll() return Error !!!!!!!!!!!!!!!!!!!!!!!!

   if (FAILED(hr))
   {   
     hr = joystick->Acquire();
     
    while (hr == DIERR_INPUTLOST) hr = joystick->Acquire();
           
    if ((hr == DIERR_INVALIDPARAM) || (hr == DIERR_NOTINITIALIZED)) return E_FAIL;
   
    if (hr == DIERR_OTHERAPPHASPRIO)return S_OK;
   }

   if (FAILED(hr = joystick->GetDeviceState(sizeof(DIJOYSTATE), &js))) return hr;
         
   for (int i = 0; i < 11; i++) printf("Button = %d \n",js.rgbButtons);
       
   printf("X = %d \n", js.lX);
   printf("Y = %d \n", js.lY);
   printf("Z = %d \n", js.lZ);
  }

  if (joystick) joystick->Unacquire();
  return 0;
}
 return 0;
}


yindengxie

  • Member
  • ***
  • Posts: 28
Re: Request for Help: Joystick console program problem
« Reply #1 on: August 29, 2020, 03:19:47 pm »
#include "stdafx.h"
#include <InitGuid.h>

#define DIRECTINPUT_VERSION 0x0800

#ifndef D_INPUT
#define D_INPUT
#include <dinput.h>
#endif

char  ProductName[100];

LPDIRECTINPUT8 di;
LPDIRECTINPUTDEVICE8 joystick;
DIDEVICEINSTANCE pdidi;
DIDEVICEINSTANCE info;

DIJOYSTATE2 js;
DIDEVCAPS capabilities;
HRESULT hr;

BOOL CALLBACK enumAxesCallback(const DIDEVICEOBJECTINSTANCE* instance, VOID* context)
{
 HWND hDlg = (HWND)context;

 DIPROPRANGE propRange;
 propRange.diph.dwSize = sizeof(DIPROPRANGE);
 propRange.diph.dwHeaderSize = sizeof(DIPROPHEADER);
 propRange.diph.dwHow = DIPH_BYID;
 propRange.diph.dwObj = instance->dwType;
 propRange.lMin = -50;
 propRange.lMax = +50;

 if (FAILED(joystick->SetProperty(DIPROP_RANGE, &propRange.diph))) return DIENUM_STOP;

 return DIENUM_CONTINUE;
}

BOOL CALLBACK enumCallback(const DIDEVICEINSTANCE* instance, VOID* context)
{
 HRESULT hr;

 hr = di->CreateDevice(instance->guidInstance, &joystick, NULL);

 if (FAILED(hr)) return DIENUM_CONTINUE;

 return DIENUM_STOP;
}

int main(int argc, char * argv[])
{
  if (FAILED(hr = DirectInput8Create(GetModuleHandle(NULL), DIRECTINPUT_VERSION,IID_IDirectInput8, (VOID**)&di, NULL))) return hr;

  if (FAILED(hr = di->EnumDevices(DI8DEVCLASS_GAMECTRL, enumCallback, NULL, DIEDFL_ATTACHEDONLY))) return hr;
   
  if (joystick == NULL)  return E_FAIL;
 
  if (FAILED(hr = joystick->SetDataFormat(&c_dfDIJoystick2))) return hr;
 
  if (FAILED(hr = joystick->SetCooperativeLevel(GetConsoleWindow(), DISCL_NONEXCLUSIVE | DISCL_FOREGROUND))) return hr;
   
  capabilities.dwSize = sizeof(DIDEVCAPS);
  if (FAILED(hr = joystick->GetCapabilities(&capabilities))) return hr;
   
  if (FAILED(hr = joystick->EnumObjects(enumAxesCallback, NULL, DIDFT_AXIS))) return hr;
   
  info.dwSize = sizeof(DIDEVICEINSTANCE);
   
  if (FAILED(hr = joystick->GetDeviceInfo(&info))) return hr;
   
  int i = 0;
  while (i < MAX_PATH && info.tszProductName != 0) ProductName = info.tszProductName[i++];
 
  ProductName[MAX_PATH] = '\0';
  printf("ProductName = %s \n", ProductName);
 
  //--------------------- The above-mentioned all work OK!  Product Name is alao OK!
 
  while (1)
  {
   if (joystick == NULL) return S_OK;
 
   hr = joystick->Poll();  //From this line begin, joystick->Poll() return Error !!!!!!!!!!!!!!!!!!!!!!!!

   if (FAILED(hr))
   {   
     hr = joystick->Acquire();
     
    while (hr == DIERR_INPUTLOST) hr = joystick->Acquire();
           
    if ((hr == DIERR_INVALIDPARAM) || (hr == DIERR_NOTINITIALIZED)) return E_FAIL;
   
    if (hr == DIERR_OTHERAPPHASPRIO)return S_OK;
   }

   if (FAILED(hr = joystick->GetDeviceState(sizeof(DIJOYSTATE), &js))) return hr;
         
   for (int i = 0; i < 11; i++) printf("Button = %d \n",js.rgbButtons);
       
   printf("X = %d \n", js.lX);
   printf("Y = %d \n", js.lY);
   printf("Z = %d \n", js.lZ);
  }

  if (joystick) joystick->Unacquire();
  return 0;
}

yindengxie

  • Member
  • ***
  • Posts: 28
Re: Request for Help: Joystick console program problem
« Reply #2 on: August 29, 2020, 05:08:23 pm »
The problem has been solved.
THANKS!