示例#1
0
        private static List <Device> EnumerateDevices(IntPtr devInfo)
        {
            List <Device> devices = new List <Device> ();
            Dictionary <string, Device> devicesByHwId = new Dictionary <string, Device> ();

            for (uint i = 0; ; i++)
            {
                WinApi.SP_DEVINFO_DATA devInfoData = new WinApi.SP_DEVINFO_DATA();
                devInfoData.cbSize = (uint)Marshal.SizeOf(devInfoData);

                if (!WinApi.SetupDiEnumDeviceInfo(devInfo, i, ref devInfoData))
                {
                    break;
                }

                Device device = Device.FromDevInfo(devInfo, devInfoData);
                if (device != null)
                {
                    if (!devicesByHwId.ContainsKey(device.HardwareId))
                    {
                        devicesByHwId[device.HardwareId] = device;

                        devices.Add(device);
                    }
                    else
                    {
                        // TODO: should solve this differently, the hardware
                        //       IDs aren't unique...
                    }
                }
            }

            return(devices);
        }
示例#2
0
 private Device(IntPtr devInfo, WinApi.SP_DEVINFO_DATA devInfoData, string name, string hardwareId, string physicalDeviceObjectName, UInt32 capabilities)
 {
     this.devInfo     = devInfo;
     this.devInfoData = devInfoData;
     this.name        = name;
     this.hardwareId  = hardwareId;
     this.physicalDeviceObjectName = physicalDeviceObjectName;
     this.capabilities             = capabilities;
 }
示例#3
0
        public static Device FromDevInfo(IntPtr devInfo, WinApi.SP_DEVINFO_DATA devInfoData)
        {
            Device device = null;

            string name       = null;
            string hardwareId = null;
            string physicalDeviceObjectName = null;
            UInt32 capabilities             = 0;

            byte[] buf = new byte[1024];
            uint   reqBufSize;

            if (WinApi.SetupDiGetDeviceRegistryProperty(devInfo, ref devInfoData, WinApi.SPDRP_FRIENDLYNAME, IntPtr.Zero, buf, (uint)buf.Length, out reqBufSize))
            {
                name = WinApi.ByteArrayToString(buf);
            }

            if (name == null)
            {
                if (WinApi.SetupDiGetDeviceRegistryProperty(devInfo, ref devInfoData, WinApi.SPDRP_DEVICEDESC, IntPtr.Zero, buf, (uint)buf.Length, out reqBufSize))
                {
                    name = WinApi.ByteArrayToString(buf);
                }
            }

            if (WinApi.SetupDiGetDeviceRegistryProperty(devInfo, ref devInfoData, WinApi.SPDRP_HARDWAREID, IntPtr.Zero, buf, (uint)buf.Length, out reqBufSize))
            {
                string[] tokens = WinApi.MarshalMultiStringToStringArray(buf);
                hardwareId = String.Join(";", tokens);
            }

            if (WinApi.SetupDiGetDeviceRegistryProperty(devInfo, ref devInfoData, WinApi.SPDRP_PHYSICAL_DEVICE_OBJECT_NAME, IntPtr.Zero, buf, (uint)buf.Length, out reqBufSize))
            {
                physicalDeviceObjectName = WinApi.ByteArrayToString(buf);
            }

            if (WinApi.SetupDiGetDeviceRegistryProperty(devInfo, ref devInfoData, WinApi.SPDRP_CAPABILITIES, IntPtr.Zero, buf, (uint)buf.Length, out reqBufSize))
            {
                capabilities = BitConverter.ToUInt32(buf, 0);
            }

            if (name != null && hardwareId != null)
            {
                device = new Device(devInfo, devInfoData, name, hardwareId, physicalDeviceObjectName, capabilities);
            }

            return(device);
        }
示例#4
0
文件: Devices.cs 项目: SayHalou/ospy
        private static List<Device> EnumerateDevices (IntPtr devInfo)
        {
            List<Device> devices = new List<Device> ();
            Dictionary<string, Device> devicesByHwId = new Dictionary<string, Device> ();

            for (uint i = 0; ; i++)
            {
                WinApi.SP_DEVINFO_DATA devInfoData = new WinApi.SP_DEVINFO_DATA ();
                devInfoData.cbSize = (uint)Marshal.SizeOf (devInfoData);

                if (!WinApi.SetupDiEnumDeviceInfo (devInfo, i, ref devInfoData))
                    break;

                Device device = Device.FromDevInfo (devInfo, devInfoData);
                if (device != null)
                {
                    if (!devicesByHwId.ContainsKey (device.HardwareId))
                    {
                        devicesByHwId[device.HardwareId] = device;

                        devices.Add (device);
                    }
                    else
                    {
                        // TODO: should solve this differently, the hardware
                        //       IDs aren't unique...
                    }
                }
            }

            return devices;
        }
示例#5
-1
文件: Devices.cs 项目: SayHalou/ospy
 private Device (IntPtr devInfo, WinApi.SP_DEVINFO_DATA devInfoData, string name, string hardwareId, string physicalDeviceObjectName, UInt32 capabilities)
 {
     this.devInfo = devInfo;
     this.devInfoData = devInfoData;
     this.name = name;
     this.hardwareId = hardwareId;
     this.physicalDeviceObjectName = physicalDeviceObjectName;
     this.capabilities = capabilities;
 }