示例#1
0
    public static List<DeviceDetails> EnumerateDevices()
    {
      uint deviceCount = 0;
      int dwSize = Marshal.SizeOf(typeof (RAWINPUTDEVICELIST));

      // Get the number of raw input devices in the list,
      // then allocate sufficient memory and get the entire list
      if (GetRawInputDeviceList(IntPtr.Zero, ref deviceCount, (uint) dwSize) == 0)
      {
        IntPtr pRawInputDeviceList = Marshal.AllocHGlobal((int) (dwSize*deviceCount));
        GetRawInputDeviceList(pRawInputDeviceList, ref deviceCount, (uint) dwSize);

        List<DeviceDetails> devices = new List<DeviceDetails>((int) deviceCount);

        // Iterate through the list, discarding undesired items
        // and retrieving further information on keyboard devices
        for (int i = 0; i < deviceCount; i++)
        {
          uint pcbSize = 0;

          IntPtr location;
          int offset = dwSize*i;

          if (IntPtr.Size == 4)
            location = new IntPtr(pRawInputDeviceList.ToInt32() + offset);
          else
            location = new IntPtr(pRawInputDeviceList.ToInt64() + offset);

          RAWINPUTDEVICELIST rid = (RAWINPUTDEVICELIST) Marshal.PtrToStructure(location, typeof (RAWINPUTDEVICELIST));

          GetRawInputDeviceInfo(rid.hDevice, RIDI_DEVICENAME, IntPtr.Zero, ref pcbSize);

          if (pcbSize > 0)
          {
            IntPtr pData = Marshal.AllocHGlobal((int) pcbSize);
            GetRawInputDeviceInfo(rid.hDevice, RIDI_DEVICENAME, pData, ref pcbSize);
            string deviceName = Marshal.PtrToStringAnsi(pData);

            // Drop the "root" keyboard and mouse devices used for Terminal Services and the Remote Desktop
            if (deviceName.ToUpperInvariant().Contains("ROOT"))
              continue;

            // If the device is identified in the list as a keyboard or 
            // HID device, create a DeviceInfo object to store information 
            // about it

            // Get Detailed Info ...
            uint size = (uint) Marshal.SizeOf(typeof (DeviceInfo));
            DeviceInfo di = new DeviceInfo();
            di.Size = Marshal.SizeOf(typeof (DeviceInfo));
            GetRawInputDeviceInfo(rid.hDevice, RIDI_DEVICEINFO, ref di, ref size);

            di = new DeviceInfo();
            di.Size = Marshal.SizeOf(typeof (DeviceInfo));
            GetRawInputDeviceInfo(rid.hDevice, RIDI_DEVICEINFO, ref di, ref size);

            DeviceDetails details = new DeviceDetails();
            //details.Name = deviceName;
            details.ID = deviceName;

            switch (di.Type)
            {
              case RawInputType.HID:
                {
                  string vidAndPid = String.Format("Vid_{0:x4}&Pid_{1:x4}", di.HIDInfo.VendorID, di.HIDInfo.ProductID);
                  details.Name = String.Format("HID: {0}", GetFriendlyName(vidAndPid));
                  //details.ID = GetDeviceDesc(deviceName);

                  details.UsagePage = di.HIDInfo.UsagePage;
                  details.Usage = di.HIDInfo.Usage;

                  devices.Add(details);
                  break;
                }

              case RawInputType.Keyboard:
                {
                  details.Name = "HID Keyboard";
                  //details.ID = String.Format("{0}-{1}", di.KeyboardInfo.Type, di.KeyboardInfo.SubType);

                  details.UsagePage = 0x01;
                  details.Usage = 0x06;

                  devices.Add(details);
                  break;
                }

              case RawInputType.Mouse:
                {
                  details.Name = "HID Mouse";

                  details.UsagePage = 0x01;
                  details.Usage = 0x02;

                  devices.Add(details);
                  break;
                }
            }

            Marshal.FreeHGlobal(pData);
          }
        }

        Marshal.FreeHGlobal(pRawInputDeviceList);

        return devices;
      }
      else
      {
        throw new InvalidOperationException("An error occurred while retrieving the list of devices");
      }
    }
示例#2
0
        public static List <DeviceDetails> EnumerateDevices()
        {
            uint deviceCount = 0;
            int  dwSize      = Marshal.SizeOf(typeof(RAWINPUTDEVICELIST));

            // Get the number of raw input devices in the list,
            // then allocate sufficient memory and get the entire list
            if (GetRawInputDeviceList(IntPtr.Zero, ref deviceCount, (uint)dwSize) == 0)
            {
                IntPtr pRawInputDeviceList = Marshal.AllocHGlobal((int)(dwSize * deviceCount));
                GetRawInputDeviceList(pRawInputDeviceList, ref deviceCount, (uint)dwSize);

                List <DeviceDetails> devices = new List <DeviceDetails>((int)deviceCount);

                // Iterate through the list, discarding undesired items
                // and retrieving further information on keyboard devices
                for (int i = 0; i < deviceCount; i++)
                {
                    string deviceName;
                    uint   pcbSize = 0;

                    RAWINPUTDEVICELIST rid;

                    IntPtr location;
                    int    offset = dwSize * i;

                    if (IntPtr.Size == 4)
                    {
                        location = new IntPtr(pRawInputDeviceList.ToInt32() + offset);
                    }
                    else
                    {
                        location = new IntPtr(pRawInputDeviceList.ToInt64() + offset);
                    }

                    rid = (RAWINPUTDEVICELIST)Marshal.PtrToStructure(location, typeof(RAWINPUTDEVICELIST));

                    GetRawInputDeviceInfo(rid.hDevice, RIDI_DEVICENAME, IntPtr.Zero, ref pcbSize);

                    if (pcbSize > 0)
                    {
                        IntPtr pData = Marshal.AllocHGlobal((int)pcbSize);
                        GetRawInputDeviceInfo(rid.hDevice, RIDI_DEVICENAME, pData, ref pcbSize);
                        deviceName = Marshal.PtrToStringAnsi(pData);

                        // Drop the "root" keyboard and mouse devices used for Terminal Services and the Remote Desktop
                        if (deviceName.ToUpperInvariant().Contains("ROOT"))
                        {
                            continue;
                        }

                        // If the device is identified in the list as a keyboard or
                        // HID device, create a DeviceInfo object to store information
                        // about it

                        // Get Detailed Info ...
                        uint       size = (uint)Marshal.SizeOf(typeof(DeviceInfo));
                        DeviceInfo di   = new DeviceInfo();
                        di.Size = Marshal.SizeOf(typeof(DeviceInfo));
                        GetRawInputDeviceInfo(rid.hDevice, RIDI_DEVICEINFO, ref di, ref size);

                        di      = new DeviceInfo();
                        di.Size = Marshal.SizeOf(typeof(DeviceInfo));
                        GetRawInputDeviceInfo(rid.hDevice, RIDI_DEVICEINFO, ref di, ref size);

                        DeviceDetails details = new DeviceDetails();
                        //details.Name = deviceName;
                        details.ID = deviceName;

                        switch (di.Type)
                        {
                        case RawInputType.HID:
                        {
                            string vidAndPid = String.Format("Vid_{0:x4}&Pid_{1:x4}", di.HIDInfo.VendorID, di.HIDInfo.ProductID);
                            details.Name = String.Format("HID: {0}", GetFriendlyName(vidAndPid));
                            //details.ID = GetDeviceDesc(deviceName);

                            details.UsagePage = di.HIDInfo.UsagePage;
                            details.Usage     = di.HIDInfo.Usage;

                            devices.Add(details);
                            break;
                        }

                        case RawInputType.Keyboard:
                        {
                            details.Name = "HID Keyboard";
                            //details.ID = String.Format("{0}-{1}", di.KeyboardInfo.Type, di.KeyboardInfo.SubType);

                            details.UsagePage = 0x01;
                            details.Usage     = 0x06;

                            devices.Add(details);
                            break;
                        }

                        case RawInputType.Mouse:
                        {
                            details.Name = "HID Mouse";

                            details.UsagePage = 0x01;
                            details.Usage     = 0x02;

                            devices.Add(details);
                            break;
                        }
                        }

                        Marshal.FreeHGlobal(pData);
                    }
                }

                Marshal.FreeHGlobal(pRawInputDeviceList);

                return(devices);
            }
            else
            {
                throw new InvalidOperationException("An error occurred while retrieving the list of devices");
            }
        }