Exemplo n.º 1
0
 public void EnumerateDevices()
 {
     lock (this._padLock)
     {
         try
         {
             this._deviceList.Clear();
             int  num  = 0;
             int  num2 = 0;
             int  num3 = 0;
             int  num4 = 0;
             uint num5 = 0u;
             int  num6 = Marshal.SizeOf(typeof(Rawinputdevicelist));
             if (Win32.GetRawInputDeviceList(IntPtr.Zero, ref num5, (uint)num6) == 0u)
             {
                 IntPtr intPtr = Marshal.AllocHGlobal((int)((long)num6 * (long)((ulong)num5)));
                 Win32.GetRawInputDeviceList(intPtr, ref num5, (uint)num6);
                 int num7 = 0;
                 while ((long)num7 < (long)((ulong)num5))
                 {
                     uint num8 = 0u;
                     Rawinputdevicelist rawinputdevicelist = (Rawinputdevicelist)Marshal.PtrToStructure(new IntPtr(intPtr.ToInt64() + (long)(num6 * num7)), typeof(Rawinputdevicelist));
                     Win32.GetRawInputDeviceInfo(rawinputdevicelist.hDevice, RawInputDeviceInfo.RIDI_DEVICENAME, IntPtr.Zero, ref num8);
                     if (num8 > 0u)
                     {
                         IntPtr intPtr2 = Marshal.AllocHGlobal((int)num8);
                         Win32.GetRawInputDeviceInfo(rawinputdevicelist.hDevice, RawInputDeviceInfo.RIDI_DEVICENAME, intPtr2, ref num8);
                         string device = Marshal.PtrToStringAnsi(intPtr2);
                         if (rawinputdevicelist.dwType == 1u)
                         {
                             string        deviceDescription = Win32.GetDeviceDescription(device);
                             RawInputEvent value             = new RawInputEvent
                             {
                                 DeviceName   = Marshal.PtrToStringAnsi(intPtr2),
                                 DeviceHandle = rawinputdevicelist.hDevice,
                                 DeviceType   = Win32.GetDeviceType(rawinputdevicelist.dwType),
                                 Name         = deviceDescription,
                                 Source       = num++.ToString(CultureInfo.InvariantCulture)
                             };
                             if (!this._deviceList.ContainsKey(rawinputdevicelist.hDevice))
                             {
                                 num4++;
                                 this._deviceList.Add(rawinputdevicelist.hDevice, value);
                             }
                         }
                         else
                         {
                             if (rawinputdevicelist.dwType == 0u)
                             {
                                 string        deviceDescription2 = Win32.GetDeviceDescription(device);
                                 RawInputEvent value2             = new RawInputEvent
                                 {
                                     DeviceName   = Marshal.PtrToStringAnsi(intPtr2),
                                     DeviceHandle = rawinputdevicelist.hDevice,
                                     DeviceType   = Win32.GetDeviceType(rawinputdevicelist.dwType),
                                     Name         = deviceDescription2,
                                     Source       = num2++.ToString(CultureInfo.InvariantCulture)
                                 };
                                 if (!this._deviceList.ContainsKey(rawinputdevicelist.hDevice))
                                 {
                                     num4++;
                                     this._deviceList.Add(rawinputdevicelist.hDevice, value2);
                                 }
                             }
                             else
                             {
                                 if (rawinputdevicelist.dwType == 2u)
                                 {
                                     string        deviceDescription3 = Win32.GetDeviceDescription(device);
                                     RawInputEvent value3             = new RawInputEvent
                                     {
                                         DeviceName   = Marshal.PtrToStringAnsi(intPtr2),
                                         DeviceHandle = rawinputdevicelist.hDevice,
                                         DeviceType   = Win32.GetDeviceType(rawinputdevicelist.dwType),
                                         Name         = deviceDescription3,
                                         Source       = num3++.ToString(CultureInfo.InvariantCulture)
                                     };
                                     if (!this._deviceList.ContainsKey(rawinputdevicelist.hDevice))
                                     {
                                         num4++;
                                         this._deviceList.Add(rawinputdevicelist.hDevice, value3);
                                     }
                                 }
                             }
                         }
                         Marshal.FreeHGlobal(intPtr2);
                     }
                     num7++;
                 }
                 Marshal.FreeHGlobal(intPtr);
                 this.NumberOfKeyboards = num4;
                 string msg = string.Format("EnumerateDevices() found {0}, Keyboard({1}), Mouse({2}), HID({3})", new object[]
                 {
                     this.NumberOfKeyboards.ToString(),
                     num.ToString(),
                     num2.ToString(),
                     num3.ToString()
                 });
                 DebugCenter.GetInstance().appendToFile(msg);
             }
         }
         catch (Exception ex)
         {
             DebugCenter.GetInstance().appendToFile(ex.Message + "\r\n" + ex.StackTrace);
         }
     }
 }
Exemplo n.º 2
0
        public static string DeviceAudit()
        {
            string       text         = "";
            FileStream   fileStream   = new FileStream("DeviceAudit.txt", FileMode.Create, FileAccess.Write);
            StreamWriter streamWriter = new StreamWriter(fileStream);
            int          num          = 0;
            uint         num2         = 0u;
            int          num3         = Marshal.SizeOf(typeof(Rawinputdevicelist));

            if (Win32.GetRawInputDeviceList(IntPtr.Zero, ref num2, (uint)num3) == 0u)
            {
                IntPtr intPtr = Marshal.AllocHGlobal((int)((long)num3 * (long)((ulong)num2)));
                Win32.GetRawInputDeviceList(intPtr, ref num2, (uint)num3);
                int num4 = 0;
                while ((long)num4 < (long)((ulong)num2))
                {
                    uint num5 = 0u;
                    Rawinputdevicelist rawinputdevicelist = (Rawinputdevicelist)Marshal.PtrToStructure(new IntPtr(intPtr.ToInt64() + (long)(num3 * num4)), typeof(Rawinputdevicelist));
                    Win32.GetRawInputDeviceInfo(rawinputdevicelist.hDevice, RawInputDeviceInfo.RIDI_DEVICENAME, IntPtr.Zero, ref num5);
                    if (num5 <= 0u)
                    {
                        streamWriter.WriteLine("pcbSize: " + num5);
                        streamWriter.WriteLine(Marshal.GetLastWin32Error().ToString());
                        streamWriter.Flush();
                        streamWriter.Close();
                        fileStream.Close();
                        return(text);
                    }
                    uint       num6       = (uint)Marshal.SizeOf(typeof(DeviceInfo));
                    DeviceInfo deviceInfo = new DeviceInfo
                    {
                        Size = Marshal.SizeOf(typeof(DeviceInfo))
                    };
                    if (Win32.GetRawInputDeviceInfo(rawinputdevicelist.hDevice, 536870923u, ref deviceInfo, ref num6) <= 0u)
                    {
                        streamWriter.WriteLine(Marshal.GetLastWin32Error());
                        streamWriter.Flush();
                        streamWriter.Close();
                        fileStream.Close();
                        return(text);
                    }
                    IntPtr intPtr2 = Marshal.AllocHGlobal((int)num5);
                    Win32.GetRawInputDeviceInfo(rawinputdevicelist.hDevice, RawInputDeviceInfo.RIDI_DEVICENAME, intPtr2, ref num5);
                    string device = Marshal.PtrToStringAnsi(intPtr2);
                    if (rawinputdevicelist.dwType == 1u || rawinputdevicelist.dwType == 2u)
                    {
                        string        deviceDescription = Win32.GetDeviceDescription(device);
                        RawInputEvent rawInputEvent     = new RawInputEvent
                        {
                            DeviceName   = Marshal.PtrToStringAnsi(intPtr2),
                            DeviceHandle = rawinputdevicelist.hDevice,
                            DeviceType   = Win32.GetDeviceType(rawinputdevicelist.dwType),
                            Name         = deviceDescription,
                            Source       = num++.ToString(CultureInfo.InvariantCulture)
                        };
                        streamWriter.WriteLine(rawInputEvent.ToString());
                        streamWriter.WriteLine(deviceInfo.ToString());
                        streamWriter.WriteLine(deviceInfo.KeyboardInfo.ToString());
                        streamWriter.WriteLine(deviceInfo.HIDInfo.ToString());
                        streamWriter.WriteLine("=========================================================================================================");
                        text += string.Format("{0}\r\n", rawInputEvent.ToString());
                        text += string.Format("{0}\r\n", deviceInfo.ToString());
                        text += string.Format("{0}\r\n", deviceInfo.KeyboardInfo.ToString());
                        text += string.Format("{0}\r\n", deviceInfo.HIDInfo.ToString());
                        text += "=========================================================================================================\r\n";
                    }
                    Marshal.FreeHGlobal(intPtr2);
                    num4++;
                }
                Marshal.FreeHGlobal(intPtr);
                streamWriter.Flush();
                streamWriter.Close();
                fileStream.Close();
                return(text);
            }
            throw new Win32Exception(Marshal.GetLastWin32Error());
        }