Exemple #1
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);
        }