Ejemplo n.º 1
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);
        }
Ejemplo n.º 2
0
        //Name:     ChangeIt
        //Inputs:   pointer to hdev, SP_DEV_INFO, bool
        //Outputs:  bool
        //Errors:   This method may throw the following exceptions.
        //          Unable to change device state!
        //Remarks:  Attempts to enable or disable a device driver.
        //          IMPORTANT NOTE!!!   This code currently does not check the reboot flag.
        //          =================   Some devices require you reboot the OS for the change
        //                              to take affect.  If this describes your device, you
        //                              will need to look at the SDK call:
        //                              SetupDiGetDeviceInstallParams.  You can call it
        //                              directly after ChangeIt to see whether or not you need
        //                              to reboot the OS for you change to go into effect.
        private bool ChangeIt(IntPtr hDevInfo, Externs.SP_DEVINFO_DATA devInfoData, bool bEnable)
        {
            try
            {
                //Marshalling vars
                int    szOfPcp;
                IntPtr ptrToPcp;
                int    szDevInfoData;
                IntPtr ptrToDevInfoData;

                Externs.SP_PROPCHANGE_PARAMS pcp = new Externs.SP_PROPCHANGE_PARAMS();
                if (bEnable)
                {
                    pcp.ClassInstallHeader.cbSize          = Marshal.SizeOf(typeof(Externs.SP_CLASSINSTALL_HEADER));
                    pcp.ClassInstallHeader.InstallFunction = Externs.DIF_PROPERTYCHANGE;
                    pcp.StateChange = Externs.DICS_ENABLE;
                    pcp.Scope       = Externs.DICS_FLAG_GLOBAL;
                    pcp.HwProfile   = 0;

                    //Marshal the params
                    szOfPcp  = Marshal.SizeOf(pcp);
                    ptrToPcp = Marshal.AllocHGlobal(szOfPcp);
                    Marshal.StructureToPtr(pcp, ptrToPcp, true);
                    szDevInfoData    = Marshal.SizeOf(devInfoData);
                    ptrToDevInfoData = Marshal.AllocHGlobal(szDevInfoData);

                    if (Externs.SetupDiSetClassInstallParams(hDevInfo, ptrToDevInfoData, ptrToPcp, Marshal.SizeOf(typeof(Externs.SP_PROPCHANGE_PARAMS))))
                    {
                        Externs.SetupDiCallClassInstaller(Externs.DIF_PROPERTYCHANGE, hDevInfo, ptrToDevInfoData);
                    }
                    pcp.ClassInstallHeader.cbSize          = Marshal.SizeOf(typeof(Externs.SP_CLASSINSTALL_HEADER));
                    pcp.ClassInstallHeader.InstallFunction = Externs.DIF_PROPERTYCHANGE;
                    pcp.StateChange = Externs.DICS_ENABLE;
                    pcp.Scope       = Externs.DICS_FLAG_CONFIGSPECIFIC;
                    pcp.HwProfile   = 0;
                }
                else
                {
                    pcp.ClassInstallHeader.cbSize          = Marshal.SizeOf(typeof(Externs.SP_CLASSINSTALL_HEADER));
                    pcp.ClassInstallHeader.InstallFunction = Externs.DIF_PROPERTYCHANGE;
                    pcp.StateChange = Externs.DICS_DISABLE;
                    pcp.Scope       = Externs.DICS_FLAG_CONFIGSPECIFIC;
                    pcp.HwProfile   = 0;
                }
                //Marshal the params
                szOfPcp  = Marshal.SizeOf(pcp);
                ptrToPcp = Marshal.AllocHGlobal(szOfPcp);
                Marshal.StructureToPtr(pcp, ptrToPcp, true);
                szDevInfoData    = Marshal.SizeOf(devInfoData);
                ptrToDevInfoData = Marshal.AllocHGlobal(szDevInfoData);
                Marshal.StructureToPtr(devInfoData, ptrToDevInfoData, true);

                bool rslt1 = Externs.SetupDiSetClassInstallParams(hDevInfo, ptrToDevInfoData, ptrToPcp, Marshal.SizeOf(typeof(Externs.SP_PROPCHANGE_PARAMS)));
                bool rstl2 = Externs.SetupDiCallClassInstaller(Externs.DIF_PROPERTYCHANGE, hDevInfo, ptrToDevInfoData);
                if ((!rslt1) || (!rstl2))
                {
                    throw new Exception("Unable to change device state!");
                }
                else
                {
                    return(true);
                }
            }
            catch (Exception)
            {
                return(false);
            }
        }