Example #1
0
        public static string GetDeviceDiagnostics()
        {
            var  stringBuilder = new StringBuilder();
            uint devices       = 0u;
            int  listSize      = Marshal.SizeOf(typeof(RawInputDeviceList));

            if (GetRawInputDeviceList(IntPtr.Zero, ref devices, (uint)listSize) != 0u)
            {
                throw new Win32Exception(Marshal.GetLastWin32Error());
            }
            var deviceListPtr = Marshal.AllocHGlobal((int)(listSize * devices));

            try
            {
                GetRawInputDeviceList(deviceListPtr, ref devices, (uint)listSize);
                int index = 0;
                while (index < devices)
                {
                    uint pcbSize            = 0u;
                    var  rawInputDeviceList = (RawInputDeviceList)Marshal.PtrToStructure(new IntPtr(deviceListPtr.ToInt64() + listSize * index), typeof(RawInputDeviceList));
                    GetRawInputDeviceInfo(rawInputDeviceList.hDevice, RawInputDeviceInfo.RIDI_DEVICENAME, IntPtr.Zero, ref pcbSize);
                    if (pcbSize <= 0u)
                    {
                        stringBuilder.AppendLine("pcbSize: " + pcbSize);
                        stringBuilder.AppendLine(Marshal.GetLastWin32Error().ToString());
                        string result = stringBuilder.ToString();
                        return(result);
                    }
                    var deviceInfoSize = (uint)Marshal.SizeOf(typeof(DeviceInfo));
                    var deviceInfo     = new DeviceInfo
                    {
                        Size = Marshal.SizeOf(typeof(DeviceInfo))
                    };
                    if (GetRawInputDeviceInfo(rawInputDeviceList.hDevice, 536870923u, ref deviceInfo, ref deviceInfoSize) <= 0u)
                    {
                        stringBuilder.AppendLine(Marshal.GetLastWin32Error().ToString());
                        string result = stringBuilder.ToString();
                        return(result);
                    }
                    var deviceInfoPtr = Marshal.AllocHGlobal((int)pcbSize);
                    try
                    {
                        GetRawInputDeviceInfo(rawInputDeviceList.hDevice, RawInputDeviceInfo.RIDI_DEVICENAME, deviceInfoPtr,
                                              ref pcbSize);
                        string device = Marshal.PtrToStringAnsi(deviceInfoPtr);
                        if (rawInputDeviceList.dwType == DeviceType.RimTypekeyboard ||
                            rawInputDeviceList.dwType == DeviceType.RimTypeHid)
                        {
                            string deviceDescription = GetDeviceDescription(device);
                            var    rawKeyboardDevice = new RawKeyboardDevice(Marshal.PtrToStringAnsi(deviceInfoPtr),
                                                                             (RawDeviceType)rawInputDeviceList.dwType, rawInputDeviceList.hDevice, deviceDescription);
                            stringBuilder.AppendLine(rawKeyboardDevice.ToString());
                            stringBuilder.AppendLine(deviceInfo.ToString());
                            stringBuilder.AppendLine(deviceInfo.KeyboardInfo.ToString());
                            stringBuilder.AppendLine(deviceInfo.HIDInfo.ToString());
                        }
                    }
                    finally
                    {
                        Marshal.FreeHGlobal(deviceInfoPtr);
                    }
                    index++;
                }
            }
            finally
            {
                Marshal.FreeHGlobal(deviceListPtr);
            }
            return(stringBuilder.ToString());
        }
Example #2
0
 public static extern uint GetRawInputDeviceInfo(IntPtr hDevice, uint command, ref DeviceInfo data,
                                                 ref uint dataSize);