public RAWINPUTDEVICELIST[] GetAllHIDDevices() { IntPtr pnumdevices = Marshal.AllocHGlobal(Marshal.SizeOf <uint>()); try { uint retval = Win32Interop.GetRawInputDeviceList(IntPtr.Zero, pnumdevices, (uint)Marshal.SizeOf <RAWINPUTDEVICELIST>()); if (retval == (uint)0xFFFFFFFF) { Win32Interop.GetLastErrorAndThrow(); } var devicesNum = Marshal.PtrToStructure <uint>(pnumdevices); var devices = new RAWINPUTDEVICELIST[devicesNum]; retval = Win32Interop.GetRawInputDeviceList( Marshal.UnsafeAddrOfPinnedArrayElement(devices, 0), pnumdevices, (uint)Marshal.SizeOf <RAWINPUTDEVICELIST>()); if (retval == (uint)0xFFFFFFFF) { Win32Interop.GetLastErrorAndThrow(); } return(devices); } finally { Marshal.FreeHGlobal(pnumdevices); } }
public string GetHIDDeviceName(RAWINPUTDEVICELIST device) { var strptr = Marshal.AllocHGlobal(255); var pSize = Marshal.AllocHGlobal(Marshal.SizeOf <uint>()); Marshal.StructureToPtr((uint)255, pSize, false); try { var retval = Win32Interop.GetRawInputDeviceInfoW(device.hDevice, 0x20000007, strptr, pSize); if (retval == 0xFFFFFFFF) { Win32Interop.GetLastErrorAndThrow(); } return(Marshal.PtrToStringUni(strptr)); } finally { Marshal.FreeHGlobal(pSize); Marshal.FreeHGlobal(strptr); } }
public RID_DEVICE_INFO GetHIDDeviceInfo(RAWINPUTDEVICELIST device) { var size = Marshal.SizeOf <RID_DEVICE_INFO>(); var infoStructPtr = Marshal.AllocHGlobal(size); var pSize = Marshal.AllocHGlobal(Marshal.SizeOf <uint>()); Marshal.StructureToPtr(size, pSize, false); try { var retval = Win32Interop.GetRawInputDeviceInfoW(device.hDevice, 0x2000000b, infoStructPtr, pSize); if (retval == 0xFFFFFFFF) { Win32Interop.GetLastErrorAndThrow(); } return(Marshal.PtrToStructure <RID_DEVICE_INFO>(infoStructPtr)); } finally { Marshal.FreeHGlobal(pSize); Marshal.FreeHGlobal(infoStructPtr); } }