public int EnumerateDevices() { List<string> hardwareIds = new List<string>() { this.hardwareId }; int NumberOfDevices = 0; uint deviceCount = 0; int dwSize = (Marshal.SizeOf(typeof(RAWINPUTDEVICELIST))); if (GetRawInputDeviceList(IntPtr.Zero, ref deviceCount, (uint)dwSize) == 0) { IntPtr pRawInputDeviceList = Marshal.AllocHGlobal((int)(dwSize * deviceCount)); GetRawInputDeviceList(pRawInputDeviceList, ref deviceCount, (uint)dwSize); for (int i = 0; i < deviceCount; i++) { DeviceInfo dInfo; string deviceName; uint pcbSize = 0; RAWINPUTDEVICELIST rid = (RAWINPUTDEVICELIST)Marshal.PtrToStructure( new IntPtr((pRawInputDeviceList.ToInt32() + (dwSize * i))), 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 = (string)Marshal.PtrToStringAnsi(pData); if (rid.dwType == RIM_TYPEKEYBOARD || rid.dwType == RIM_TYPEHID) { foreach (var hardwareId in hardwareIds) { var deviceNameTemp = deviceName.ToUpper(); var hardwareIdTemp = hardwareId.ToUpper(); if (deviceNameTemp.Contains(hardwareIdTemp)) { dInfo = new DeviceInfo(); dInfo.deviceName = deviceName; dInfo.deviceHandle = rid.hDevice; dInfo.deviceType = GetDeviceType(rid.dwType); bool IsKeyboardDevice = false; string DeviceDesc = ReadReg(deviceName, ref IsKeyboardDevice); dInfo.Name = DeviceDesc; NumberOfDevices++; deviceList.Add(rid.hDevice, dInfo); } } } if (deviceName.ToUpper().Contains("ROOT")) { continue; } Marshal.FreeHGlobal(pData); } } Marshal.FreeHGlobal(pRawInputDeviceList); return NumberOfDevices; } else { throw new ApplicationException("An error occurred while retrieving the list of devices."); } }
public KeyControlEventArgs(DeviceInfo dInfo, DeviceType device) { m_deviceInfo = dInfo; m_device = device; }