Esempio n. 1
0
        //Name:     SetDeviceState
        //Inputs:   string[],bool
        //Outputs:  bool
        //Errors:   This method may throw the following exceptions.
        //          Failed to enumerate device tree!
        //Remarks:  This is nearly identical to the method above except it
        //          tries to match the hardware description against the criteria
        //          passed in.  If a match is found, that device will the be
        //          enabled or disabled based on bEnable.
        public bool SetDeviceState(string deviceToChangeState_ID, string deviceToChangeState_FriendlyName, bool bEnable)
        {
            Guid   myGUID   = System.Guid.Empty;
            IntPtr hDevInfo = Externs.SetupDiGetClassDevs(ref myGUID, 0, IntPtr.Zero, Externs.DIGCF_ALLCLASSES | Externs.DIGCF_PRESENT);

            if (hDevInfo.ToInt64() == Externs.INVALID_HANDLE_VALUE)
            {
                throw new Exception("Could retrieve handle for device");
            }

            Externs.SP_DEVINFO_DATA DeviceInfoData;
            DeviceInfoData = new Externs.SP_DEVINFO_DATA();

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

            //is devices exist for class
            DeviceInfoData.devInst   = 0;
            DeviceInfoData.classGuid = System.Guid.Empty;
            DeviceInfoData.reserved  = 0;
            UInt32        i;
            StringBuilder DeviceHardwareId   = new StringBuilder("");
            StringBuilder DeviceFriendlyName = new StringBuilder("");

            DeviceHardwareId.Capacity = DeviceFriendlyName.Capacity = Externs.MAX_DEV_LEN;
            for (i = 0; Externs.SetupDiEnumDeviceInfo(hDevInfo, i, DeviceInfoData); i++)
            {
                //Declare vars
                Externs.SetupDiGetDeviceRegistryProperty(hDevInfo, DeviceInfoData, Externs.SPDRP_HARDWAREID, 0, DeviceHardwareId, Externs.MAX_DEV_LEN, IntPtr.Zero);
                Externs.SetupDiGetDeviceRegistryProperty(hDevInfo, DeviceInfoData, Externs.SPDRP_FRIENDLYNAME, 0, DeviceFriendlyName, Externs.MAX_DEV_LEN, IntPtr.Zero);

                Console.WriteLine(DeviceHardwareId + " -- " + DeviceFriendlyName);
                //找到匹配
                if (DeviceHardwareId.ToString().ToLower().Contains(deviceToChangeState_ID.ToLower()) && DeviceFriendlyName.ToString().ToLower().Contains(deviceToChangeState_FriendlyName.ToLower()))
                {
                    Console.WriteLine("Found: " + DeviceFriendlyName);
                    bool couldChangeState = ChangeIt(hDevInfo, DeviceInfoData, bEnable);
                    if (!couldChangeState)
                    {
                        throw new Exception("Unable to change " + DeviceFriendlyName + " device state, make sure you have administrator privileges");
                    }
                    break;
                }
            }

            Externs.SetupDiDestroyDeviceInfoList(hDevInfo);

            return(true);
        }
Esempio n. 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;
            }
            Externs.SP_DEVINFO_DATA _DevinfoData;
            _DevinfoData = new Externs.SP_DEVINFO_DATA();
            _DevinfoData.classGuid = System.Guid.Empty;
            _DevinfoData.cbSize = 28;
            _DevinfoData.devInst = 0;
            _DevinfoData.reserved = 0;
            StringBuilder _DeviceName = new StringBuilder("");
            _DeviceName.Capacity = 1000;
            Int32 dwRequireSize = 0;
            uint i = 0;
            uint j = 0;
            while (Externs.SetupDiEnumDeviceInfo(_MainIntPtr, i, _DevinfoData))
            {
                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));
                //_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);

                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;
        }
Esempio n. 3
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);
        }
Esempio n. 4
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);
            }
        }