Ejemplo n.º 1
0
        /// <summary>
        /// 开启或者关闭指定的设备驱动
        /// 注意:该方法目前没有检查是否需要重新启动计算机。^.^
        /// </summary>
        /// <param name="hDevInfo"></param>
        /// <param name="devInfoData"></param>
        /// <param name="bEnable"></param>
        /// <returns></returns>
        private bool OpenClose(IntPtr hDevInfo, Externs.SP_DEVINFO_DATA devInfoData, bool bEnable, bool isRemove = false)
        {
            try
            {
                int    szOfPcp;
                IntPtr ptrToPcp;
                int    szDevInfoData;
                IntPtr ptrToDevInfoData;
                Externs.SP_PROPCHANGE_PARAMS SP_PROPCHANGE_PARAMS1   = new Externs.SP_PROPCHANGE_PARAMS();
                SP_REMOVEDEVICE_PARAMS       SP_REMOVEDEVICE_PARAMS1 = new SP_REMOVEDEVICE_PARAMS();
                if (bEnable)
                {
                    SP_PROPCHANGE_PARAMS1.ClassInstallHeader.cbSize          = Marshal.SizeOf(typeof(Externs.SP_CLASSINSTALL_HEADER));
                    SP_PROPCHANGE_PARAMS1.ClassInstallHeader.InstallFunction = Externs.DIF_PROPERTYCHANGE;
                    SP_PROPCHANGE_PARAMS1.StateChange = Externs.DICS_ENABLE;
                    SP_PROPCHANGE_PARAMS1.Scope       = Externs.DICS_FLAG_GLOBAL;
                    SP_PROPCHANGE_PARAMS1.HwProfile   = 0;

                    szOfPcp  = Marshal.SizeOf(SP_PROPCHANGE_PARAMS1);
                    ptrToPcp = Marshal.AllocHGlobal(szOfPcp);
                    Marshal.StructureToPtr(SP_PROPCHANGE_PARAMS1, 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);
                    }
                    SP_PROPCHANGE_PARAMS1.ClassInstallHeader.cbSize          = Marshal.SizeOf(typeof(Externs.SP_CLASSINSTALL_HEADER));
                    SP_PROPCHANGE_PARAMS1.ClassInstallHeader.InstallFunction = Externs.DIF_PROPERTYCHANGE;
                    SP_PROPCHANGE_PARAMS1.StateChange = Externs.DICS_ENABLE;
                    SP_PROPCHANGE_PARAMS1.Scope       = Externs.DICS_FLAG_CONFIGSPECIFIC;
                    SP_PROPCHANGE_PARAMS1.HwProfile   = 0;
                }
                else
                {
                    SP_PROPCHANGE_PARAMS1.ClassInstallHeader.cbSize = Marshal.SizeOf(typeof(Externs.SP_CLASSINSTALL_HEADER));
                    if (isRemove == true)
                    {
                        SP_REMOVEDEVICE_PARAMS1.ClassInstallHeader.cbSize          = Marshal.SizeOf(typeof(Externs.SP_CLASSINSTALL_HEADER));
                        SP_REMOVEDEVICE_PARAMS1.ClassInstallHeader.InstallFunction = Externs.DIF_REMOVE;
                        SP_REMOVEDEVICE_PARAMS1.Scope     = Externs.DI_REMOVEDEVICE_GLOBAL;
                        SP_REMOVEDEVICE_PARAMS1.HwProfile = 0;
                    }
                    else
                    {
                        SP_PROPCHANGE_PARAMS1.ClassInstallHeader.InstallFunction = Externs.DIF_PROPERTYCHANGE;
                        SP_PROPCHANGE_PARAMS1.StateChange = Externs.DICS_DISABLE;
                        SP_PROPCHANGE_PARAMS1.Scope       = Externs.DICS_FLAG_CONFIGSPECIFIC;
                        SP_PROPCHANGE_PARAMS1.HwProfile   = 0;
                    }
                }

                szDevInfoData    = Marshal.SizeOf(devInfoData);
                ptrToDevInfoData = Marshal.AllocHGlobal(szDevInfoData);
                Marshal.StructureToPtr(devInfoData, ptrToDevInfoData, true);
                bool rslt1 = false;
                bool rstl2 = false;
                if (isRemove == true && bEnable == false)
                {
                    szOfPcp  = Marshal.SizeOf(SP_REMOVEDEVICE_PARAMS1);
                    ptrToPcp = Marshal.AllocHGlobal(szOfPcp);
                    Marshal.StructureToPtr(SP_REMOVEDEVICE_PARAMS1, ptrToPcp, true);
                    rslt1 = Externs.SetupDiSetClassInstallParams(hDevInfo, ptrToDevInfoData, ptrToPcp, Marshal.SizeOf(typeof(Externs.SP_REMOVEDEVICE_PARAMS)));
                    rstl2 = Externs.SetupDiCallClassInstaller(Externs.DIF_REMOVE, hDevInfo, ptrToDevInfoData);
                }
                else
                {
                    szOfPcp  = Marshal.SizeOf(SP_PROPCHANGE_PARAMS1);
                    ptrToPcp = Marshal.AllocHGlobal(szOfPcp);
                    Marshal.StructureToPtr(SP_PROPCHANGE_PARAMS1, ptrToPcp, true);
                    rslt1 = Externs.SetupDiSetClassInstallParams(hDevInfo, ptrToDevInfoData, ptrToPcp, Marshal.SizeOf(typeof(Externs.SP_PROPCHANGE_PARAMS)));
                    rstl2 = Externs.SetupDiCallClassInstaller(Externs.DIF_PROPERTYCHANGE, hDevInfo, ptrToDevInfoData);
                }
                if ((!rslt1) || (!rstl2))
                {
                    throw new Exception("不能更改设备状态。");
                }
                else
                {
                    return(true);
                }
            }
            catch (Exception ex)
            {
                return(false);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 设置指定设备的状态
        /// </summary>
        /// <param name="match">设备名称</param>
        /// <param name="bEnable">是否启用</param>
        /// <returns></returns>
        public bool SetState(string[] match, bool bEnable)
        {
            try
            {
                UInt32 RequiredSize = 0;
                Guid   guid         = Guid.Empty;
                Guid[] guids        = new Guid[1];
                bool   res          = SetupDiClassGuidsFromNameA("ports", ref guids[0], RequiredSize, ref RequiredSize);
                if (res == false)
                {
                    guids = new Guid[RequiredSize];
                    res   = SetupDiClassGuidsFromNameA("ports", ref guids[0], RequiredSize, ref RequiredSize);

                    if (!res || RequiredSize == 0)
                    {
                        Debug.WriteLine("类型不正确");
                    }
                }
                if (res == true)
                {
                    List <DeviceEntity> list = new List <Hardware.DeviceEntity>();
                    Guid   myGUID            = System.Guid.Empty;
                    IntPtr hDevInfo          = SetupDiGetClassDevs(guids[0], 0, IntPtr.Zero, DIGCF_NOSET);
                    Debug.WriteLine("枚举设备 : " + Externs.GetLastError() + "---" + Marshal.GetLastWin32Error() + hDevInfo.ToInt64());
                    if (hDevInfo.ToInt64() == Externs.INVALID_HANDLE_VALUE)
                    {
                        throw new Exception("Invalid Handle");
                    }
                    else
                    {
                        Externs.SP_DEVINFO_DATA DeviceInfoData;
                        DeviceInfoData           = new Externs.SP_DEVINFO_DATA();
                        DeviceInfoData.cbSize    = Marshal.SizeOf(DeviceInfoData);
                        DeviceInfoData.devInst   = 0;
                        DeviceInfoData.classGuid = System.Guid.Empty;
                        DeviceInfoData.reserved  = 0;
                        UInt32        i;
                        StringBuilder DeviceID = new StringBuilder("", 256);
                        DeviceID.Capacity = Externs.MAX_DEV_LEN;
                        for (i = 0; Externs.SetupDiEnumDeviceInfo(hDevInfo, i, DeviceInfoData); i++)
                        {
                            //while (!Externs.SetupDiGetDeviceRegistryProperty(hDevInfo, DeviceInfoData, Externs.SPDRP_DEVICEDESC, 0, DeviceName, Externs.MAX_DEV_LEN, IntPtr.Zero))
                            //{
                            //}
                            if (CM_Get_Device_IDA(DeviceInfoData.devInst, DeviceID, 254, 0) == CR_SUCCESS)
                            {
                                bool bMatch = true;
                                foreach (string search in match)
                                {
                                    if (!DeviceID.ToString().ToLower().Contains(search.ToLower()))
                                    {
                                        bMatch = false;
                                        break;
                                    }
                                }
                                if (bMatch)
                                {
                                    OpenClose(hDevInfo, DeviceInfoData, bEnable, true);
                                }
                            }
                        }
                        Externs.SetupDiDestroyDeviceInfoList(hDevInfo);
                    }
                }
            }
            catch (Exception ex)
            {
                throw new Exception("枚举设备信息出错!", ex);
                return(false);
            }
            return(true);
        }