2

I am using the "joyGetPosEx" function to detect the joystick inputs:

JOYINFOEX joyInfoEx;
ZeroMemory(&joyInfoEx, sizeof(joyInfoEx));
joyInfoEx.dwSize = sizeof(joyInfoEx);
// poll for values
joyGetPosEx(JOYSTICKID1, &joyInfoEx);

When the joystick is connected, every thing is working fine and the function returns "JOYERR_NOERROR".

However, if during the program run, i disconnect the joystick and connect it to a different USB port, the "joyGetPosEx(JOYSTICKID1, &joyInfoEx);" keeps returning a "JOYERR_UNPLUGGED" value.

If i reconnect the device to the initial USB port, it srtarts working again.

Does any one know how to make the joystick work even if it gets connected to a new port?

Thanks in advance!

Costya
  • 21
  • 2
  • Unrelated to your question, but FYI, `JOYINFOEX joyInfoEx; ZeroMemory(&joyInfoEx, sizeof(joyInfoEx)); joyInfoEx.dwSize = sizeof(joyInfoEx);` can be expressed more succinctly as `JOYINFOEX joyInfoEx = { sizeof(JOYINFOEX) };`. You're writing C++, not C... ;-] – ildjarn Jun 24 '11 at 23:14
  • Thanks, good point, but still doesn't help me :) – Costya Jun 24 '11 at 23:20
  • Hard to see how this is a real problem. Contact the driver manufacturer for support. – Hans Passant Jun 25 '11 at 01:28

1 Answers1

0

I'd say you have a USB device with no serial number, so Windows identifies it by where it's plugged in. Does it start acting as JOYSTICKID2 by any chance?


We eventually discovered that the joyConfigChanged function is able to reconnect the joystick. [MSDN says that #include <dinput.h> is necessary to use this function, as it turns out, the order of includes is important: mmsystem.h first, then dinput.h.

Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
  • That was my first thought, so I polled for all of the possible devices (from JOYSTICKID1 to JOYSTICKID1+15). JOYSTICKID1 keeps returning JOYERR_UNPLUGGED, and the rest simply return JOYERR_PARMS. That also happens if i connect a different type of joystick, it still isn't recognized. Only when the original joystick is reconnected to the original port, it starts acting correctly again. – Costya Jun 25 '11 at 01:52
  • Any joy with [`joyConfigChanged`](http://msdn.microsoft.com/en-us/library/dd757104.aspx)? (yes, bad pun) – Ben Voigt Jun 25 '11 at 01:54
  • For some reason, i can't call the `joyConfigChanged` function. I am including the `MMSystem.h` and linking to `winmm.lib` but when i try to call the `joyConfigChanged` function i get: "error C3861: 'joyConfigChanged': identifier not found" When I look at the `MMSystem.h` file, indeed the function is not defined. Any thought? – Costya Jun 25 '11 at 01:58
  • I am including `dinput.h`. These are my includes: `#define DIRECTINPUT_VERSION 0x0800 #include #include ` However, if i add `#define _INC_MMSYSTEM` which is needed for the `joyConfigChanged` definition, then my code doesn't compile. – Costya Jun 25 '11 at 02:02
  • @Costya: Perhaps you must include `mmsystem.h` before `dinput.h`? – Ben Voigt Jun 25 '11 at 02:25
  • @ Ben Voigt Ok, this worked. As I suspected, and as you said, ineed a call to the `joyConfigChanged` function is needed. I guess it resets everything and enables the joystick to be polled from a new USB port. I was lacking one more include, `#include ` to be exact, and that makes everything compile and work as it should. This is what my final include looks like: `#define DIRECTINPUT_VERSION 0x0800` `#include ` `#include ` `#include ` Thanks, your comments were more then helpful!!! – Costya Jun 25 '11 at 02:58
  • You're welcome. I'll update the answer with this new information. – Ben Voigt Jun 25 '11 at 04:50