コード例 #1
0
 public HardwareInfo(string p_DeviceName, Guid p_ClassGuid, int p_Size, int p_DevInst, ulong p_Reserved, string p_HardwareId, string p_Statusstr, Externs.DeviceStatus p_Status)
 {
     m_ClassGuid  = p_ClassGuid;
     m_DeviceName = p_DeviceName;
     m_DevInst    = p_DevInst;
     m_Reserved   = p_Reserved;
     m_Size       = p_Size;
     m_HardwareId = p_HardwareId;
     m_Statusstr  = p_Statusstr;
     m_Status     = p_Status;
 }
コード例 #2
0
        public static IList <HardwareInfo> GetHardwareTable(out Dictionary <Guid, HardwareInfo> ParentDic)
        {
            IList <HardwareInfo> _ReturnList = new List <HardwareInfo>();

            ParentDic = new Dictionary <Guid, HardwareInfo>();

            Guid   _NewGuid    = Guid.Empty;
            IntPtr _MainIntPtr = Externs.SetupDiGetClassDevs(ref _NewGuid, 0, IntPtr.Zero, Externs.DIGCF_ALLCLASSES | Externs.DIGCF_PRESENT);

            //if (_MainIntPtr.ToInt32() == -1)
            //{
            //    return _ReturnList;
            //}
            //
            if (_MainIntPtr.ToInt64() == Externs.INVALID_HANDLE_VALUE)
            {
                throw new Exception("Invalid Handle");
            }
            //
            Externs.SP_DEVINFO_DATA _DevinfoData;
            _DevinfoData           = new Externs.SP_DEVINFO_DATA();
            _DevinfoData.classGuid = System.Guid.Empty;


            //for 32-bit, IntPtr.Size = 4
            //for 64-bit, IntPtr.Size = 8
            if (IntPtr.Size == 4)
            {
                _DevinfoData.cbSize = 28;
            }
            else if (IntPtr.Size == 8)
            {
                _DevinfoData.cbSize = 32;
            }


            //_DevinfoData.cbSize =28;
            _DevinfoData.devInst  = 0;
            _DevinfoData.reserved = 0;
            StringBuilder _DeviceName      = new StringBuilder("");
            StringBuilder DeviceHardwareId = new StringBuilder("");

            UInt32 status, problem;
            string dstatustr = "";

            Externs.DeviceStatus deviceStatus = Externs.DeviceStatus.Unknown;



            _DeviceName.Capacity      = 1000;
            DeviceHardwareId.Capacity = 1000;
            Int32 dwRequireSize = 0;
            uint  i             = 0;
            uint  j             = 0;

            while (Externs.SetupDiEnumDeviceInfo(_MainIntPtr, i, _DevinfoData))
            {
                if (Externs.SetupDiGetDeviceRegistryProperty(_MainIntPtr, _DevinfoData, Externs.SPDRP_FRIENDLYNAME, 0,
                                                             _DeviceName, (uint)_DeviceName.Capacity, IntPtr.Zero))
                {
                    //状态
                    if (Externs.CM_Get_DevNode_Status(out status, out problem, i, 0) == Externs.CR_SUCCESS)
                    {
                        deviceStatus = ((status & Externs.DN_DISABLEABLE) > 0) ? Externs.DeviceStatus.Enabled : Externs.DeviceStatus.Disabled;
                    }

                    //设备的DeviceHardwareId
                    Externs.SetupDiGetDeviceRegistryProperty(_MainIntPtr, _DevinfoData, Externs.SPDRP_HARDWAREID, 0,
                                                             DeviceHardwareId, Externs.MAX_DEV_LEN, IntPtr.Zero);
                    _ReturnList.Add(new HardwareInfo(_DeviceName.ToString(), _DevinfoData.classGuid, _DevinfoData.cbSize, _DevinfoData.devInst, _DevinfoData.reserved, DeviceHardwareId.ToString(), dstatustr, deviceStatus));
                }
                else
                {
                    Externs.SetupDiGetDeviceRegistryProperty(_MainIntPtr, _DevinfoData, Externs.SPDRP_DEVICEDESC,
                                                             0,
                                                             _DeviceName, (uint)_DeviceName.Capacity, IntPtr.Zero);
                    _ReturnList.Add(new HardwareInfo(_DeviceName.ToString(), _DevinfoData.classGuid, _DevinfoData.cbSize, _DevinfoData.devInst, _DevinfoData.reserved, DeviceHardwareId.ToString(), dstatustr, deviceStatus));
                }



                //_ReturnList.Add(_DeviceName.ToString());
                Console.WriteLine("子节点:" + _DeviceName.ToString());
                if (!Externs.SetupDiGetClassDescription(ref _DevinfoData.classGuid,
                                                        _DeviceName,
                                                        260,
                                                        ref dwRequireSize))
                {
                    i++;
                    continue;
                }
                ;

                //往字典里边添加父节点
                HardwareInfo hardwareInfo = new HardwareInfo(_DeviceName.ToString(),
                                                             _DevinfoData.classGuid, _DevinfoData.cbSize, _DevinfoData.devInst,
                                                             _DevinfoData.reserved, DeviceHardwareId.ToString(), dstatustr, deviceStatus);

                if (!ParentDic.ContainsKey(hardwareInfo.ClassGuid))
                {
                    ParentDic.Add(hardwareInfo.ClassGuid, hardwareInfo);
                    Console.WriteLine("设备Guid:{0}已加入字典\n ",
                                      ParentDic[hardwareInfo.ClassGuid].DeviceName);
                }

                //foreach (KeyValuePair<string, string> kvp in openWith)
                //{
                //    Console.WriteLine("Key = {0}, Value = {1}",
                //        kvp.Key, kvp.Value);
                //}



                //给父节点添加新节点
                Console.WriteLine("父节点:" + _DeviceName + (++j).ToString());
                Console.WriteLine("====================");
                i++;
            }
            Console.WriteLine("\n\n\n");
            foreach (KeyValuePair <Guid, HardwareInfo> kvp in ParentDic)
            {
                Console.WriteLine("Guid = {0}, 设备类型 = {1}",
                                  kvp.Key, kvp.Value.DeviceName);
            }
            return(_ReturnList);
        }