Beispiel #1
0
            public void getNumOfDevices()
            {
                if (GetRawInputDeviceList(IntPtr.Zero, ref deviceCount, (uint)dwSize) == 0)
                {
                    Console.WriteLine("The number of devices detected: {0}.\n\n", deviceCount);
                    IntPtr devList = Marshal.AllocHGlobal((int)(dwSize * deviceCount));

                GetRawInputDeviceList(devList, ref deviceCount, (uint)dwSize);
                for (int i = 0; i < deviceCount; i++)
                {
                    uint pcbSize = 0;
                    DeviceInfo dInfo;
                    deviceName = string.Empty;
                    RAWINPUTDEVICELIST rid = (RAWINPUTDEVICELIST)Marshal.PtrToStructure(new IntPtr((devList.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 || rid.dwType == RIM_TYPEMOUSE)
                        {
                            dInfo = new DeviceInfo();

                            dInfo.deviceName = (string)Marshal.PtrToStringAnsi(pData);
                            dInfo.deviceHandle = rid.hDevice;
                            dInfo.deviceType = GetDeviceType(rid.dwType);

                            string DeviceDesc = ReadReg(deviceName);

                            dInfo.deviceName = DeviceDesc;
                            Console.WriteLine("Device {0}:", i);
                            dInfo.outputDeviceInfo();
                        }
                        Marshal.FreeHGlobal(pData);
                    }

                }//end for loop
                Marshal.FreeHGlobal(devList);

                }
                else
                    throw new ApplicationException("Error! Unable to allocate device list buffer.");
            }