i have notice that using the joyGetPosEx() function, when the joystick isn't connected, that the memory, that the application uses, is increasing.
so is there another way just for see if 1 joystick is connected to pc?

Dani AI

Generated

Short summary and next step

Good call on using raw input — registering for device notifications is the right way to get arrival/removal events, and then enumerating/querying devices is the robust way to answer “is a joystick connected?”. was right to check connection type; ’s RegisterRawInputDevices + device-change handler is the correct starting point (you need the dev-notify flag to get arrival/removal messages). (learn.microsoft.com)

How to test with GetRawInputDeviceInfo

  1. Call GetRawInputDeviceList to get all raw-device handles.
  2. For each hDevice call GetRawInputDeviceInfo with the RIDI_DEVICEINFO command (set RID_DEVICE_INFO.cbSize first) to retrieve the RID_DEVICE_INFO for that device. Inspect the returned structure: if dwType == RIM_TYPEHID check the HID fields (usUsagePage/usUsage) to determine the top‑level collection type. Those HID fields are exactly what tell you “this device is a joystick/gamepad” (they’re part of RID_DEVICE_INFO_HID). (learn.microsoft.com)

Minimal detection example (conceptual C++)

Make a small helper that returns true when any raw device reports a HID top-level collection whose usage page/usage matches a game controller:

// concept: call GetRawInputDeviceList, then for each device:
//   RID_DEVICE_INFO rdi; rdi.cbSize = sizeof(rdi);
//   GetRawInputDeviceInfo(hDev, RIDI_DEVICEINFO, &rdi, &cb);
//   if (rdi.dwType == RIM_TYPEHID &&
//       rdi.hid.usUsagePage == 0x01 &&
//       (rdi.hid.usUsage == 0x04 || rdi.hid.usUsage == 0x05)) return true;

(see the docs for exact return/error handling and the RIDI_* commands). (learn.microsoft.com)

Caveats and fallbacks

Some controllers (modern Xbox controllers / XInput) may need XInput or the newer GameInput/Windows.Gaming.Input APIs for full detection/feature support — they aren’t always exposed the same way as generic HID top‑level collections. If raw‑input enumeration misses a device, fall back to checking the device interface name (RIDI_DEVICENAME) or enumerate HID class interfaces via HidD_GetHidGuid + SetupDi APIs and inspect VID/PID or the preparsed data. (learn.microsoft.com)

This approach avoids polling legacy joy APIs and gives a deterministic way to detect connected HID joysticks/gamepads.

Recommended Answers

All 4 Replies

How is the joystick connected to the computer? Analog game port, serial port, usb port? What?

USB

for use the WM_INPUT_DEVICE_CHANGE message, i must use the RegisterRawInputDevices() : https://msdn.microsoft.com/en-us/library/windows/desktop/ms645591%28v=vs.85%29.aspx

see the 'Community Additions'.
so i did:

case WM_CREATE:
                {
                    if(WindowMain == NULL || WindowMain ==GetDesktopWindow())
                        WindowMain = HandleWindow;

                    SetTimer(inst->hwnd,JoystickTimer,150,NULL);
                    SetTimer(inst->hwnd,KeyBoardTimer,150,NULL);

                    RAWINPUTDEVICE rid;

                    rid.usUsagePage = 1;
                    rid.usUsage     = 4; // Joystick
                    rid.dwFlags     = 0;
                    rid.hwndTarget  = inst->hwnd;

                    if(!RegisterRawInputDevices(&rid, 1, sizeof(RAWINPUTDEVICE)))
                        SetWindowText(inst->hwnd,"error");
                }
                break;


                case 0x00FE: //#define WM_INPUT_DEVICE_CHANGE          0x00FE
                {
                    SetWindowText(inst->hwnd,"enter device");

                    return 0;
                }
                break;

i'm using windows 7. but the message isn't activated when i connect or disconnect a device :(

finally works ;)

case 0x00FE:
                {

                    if(wParam==1)
                    {
                        SetWindowText(inst->hwnd,"enter device");
                    }
                    else if(wParam==2)
                    {
                        SetWindowText(inst->hwnd,"exit device");
                    }


                    return 0;
                }
                break;

                case WM_CREATE:
                {
                    if(WindowMain == NULL || WindowMain ==GetDesktopWindow())
                        WindowMain = HandleWindow;

                    SetTimer(inst->hwnd,JoystickTimer,150,NULL);
                    SetTimer(inst->hwnd,KeyBoardTimer,150,NULL);

                    RAWINPUTDEVICE rid;

                    rid.usUsagePage = 1 ;
                    rid.usUsage     = 4; // Joystick
                    rid.dwFlags     = 0x00002000;
                    rid.hwndTarget  = inst->hwnd;

                    if(!RegisterRawInputDevices(&rid, 1, sizeof(RAWINPUTDEVICE)))
                        SetWindowText(inst->hwnd,"error");
                }
                break;

how use the GetRawInputDeviceInfo() for test if a joystick is connected to pc?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.