/// <summary> /// Part in charge to really change the device's state /// </summary> /// <param name="info"></param> /// <param name="devdata"></param> /// <param name="status"></param> private static void ChangeReaderStatus(IntPtr info, SP_DEVINFO_DATA devdata, DeviceState status) { string devicepathTemp = GetStringPropertyForDevice(info, devdata, 0); SP_CLASSINSTALL_HEADER header = new SP_CLASSINSTALL_HEADER(); header.cbSize = (UInt32)Marshal.SizeOf(header); header.InstallFunction = DIF_PROPERTYCHANGE; SP_PROPCHANGE_PARAMS propchangeparams = new SP_PROPCHANGE_PARAMS(); propchangeparams.ClassInstallHeader = header; if (status == DeviceState.On || status == DeviceState.Off) { propchangeparams.StateChange = (status == DeviceState.Off) ? DICS_DISABLE : DICS_ENABLE; propchangeparams.Scope = DICS_FLAG_GLOBAL; } else // reset { propchangeparams.StateChange = DICS_PROPCHANGE; propchangeparams.Scope = DICS_FLAG_CONFIGSPECIFIC; } propchangeparams.HwProfile = 0; SetupDiSetClassInstallParams(info, ref devdata, ref propchangeparams, (UInt32)Marshal.SizeOf(propchangeparams)); CheckError("SetupDiSetClassInstallParams"); SetupDiChangeState(info, ref devdata); CheckError("SetupDiChangeState"); }
public static void ChangeDeviceEnabledState( DeviceInfoListHandle deviceInfoList, ref SP_DEVINFO_DATA deviceInfoData, DiClassInstallState state, DiClassInstallScope scope, DiClassInstallFunction installFunction ) { var pcp = new SP_PROPCHANGE_PARAMS(); pcp.Header.cbSize = (uint)Marshal.SizeOf(pcp.Header.GetType()); pcp.Header.InstallFunction = installFunction; pcp.StateChange = state; pcp.Scope = scope; pcp.HwProfile = 0; SetupDiSetClassInstallParams(deviceInfoList, ref deviceInfoData, pcp); if (!SetupDiCallClassInstaller( installFunction, deviceInfoList.DangerousGetHandle(), ref deviceInfoData )) { var error = Marshal.GetLastWin32Error(); if (error != 0) { throw new Win32Exception(error); } else { throw new Exception("Failed to change device enabled state"); } } }
public void SetEnabled(Boolean enabled) { using (SafeDeviceInfoSetHandle dis = SetupApi.SetupDiGetClassDevsA(IntPtr.Zero, DeviceID, IntPtr.Zero, DICFG.DEVICEINTERFACE | DICFG.ALLCLASSES)) { if (dis.IsInvalid) { throw new Win32Exception(); } SP_DEVINFO_DATA dd = new SP_DEVINFO_DATA(true); if (!SetupApi.SetupDiEnumDeviceInfo(dis, 0, ref dd)) { throw new Win32Exception(); } SP_PROPCHANGE_PARAMS PropChangeParams = new SP_PROPCHANGE_PARAMS(); PropChangeParams.ClassInstallHeader.cbSize = Marshal.SizeOf(PropChangeParams.ClassInstallHeader); PropChangeParams.ClassInstallHeader.InstallFunction = UsbApi.DIF_PROPERTYCHANGE; PropChangeParams.Scope = UsbApi.DICS_FLAG_GLOBAL; // or use DICS_FLAG_CONFIGSPECIFIC to limit to current HW profile PropChangeParams.HwProfile = 0; //Current hardware profile PropChangeParams.StateChange = enabled ? UsbApi.DICS_ENABLE : UsbApi.DICS_DISABLE; if (!SetupApi.SetupDiSetClassInstallParams(dis, ref dd, ref PropChangeParams.ClassInstallHeader, Marshal.SizeOf(PropChangeParams))) { throw new Win32Exception(); } if (!SetupApi.SetupDiCallClassInstaller(UsbApi.DIF_PROPERTYCHANGE, dis, ref dd)) { throw new Win32Exception(); } } }
/// <summary> /// Attempts to enable or disable a device driver. /// </summary> /// <param name="deviceInfoSet">Pointer to device.</param> /// <param name="deviceInfoData"></param> /// <param name="bEnable"></param> /// <returns>State of success.</returns> /// <remarks> /// 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. /// Errors: This method may throw the following exceptions. /// Unable to change device state! /// </remarks> private static bool SetDeviceState(IntPtr deviceInfoSet, SP_DEVINFO_DATA deviceInfoData, bool bEnable) { try { SP_CLASSINSTALL_HEADER header = new SP_CLASSINSTALL_HEADER(); header.cbSize = (UInt32)Marshal.SizeOf(header); header.InstallFunction = DIF_PROPERTYCHANGE; SP_PROPCHANGE_PARAMS classInstallParams = new SP_PROPCHANGE_PARAMS(); classInstallParams.ClassInstallHeader = header; classInstallParams.StateChange = bEnable ? DICS_ENABLE : DICS_DISABLE; classInstallParams.Scope = DICS_FLAG_GLOBAL; classInstallParams.HwProfile = 0; var classInstallParamsSize = (UInt32)Marshal.SizeOf(classInstallParams); bool result = SetupDiSetClassInstallParams(deviceInfoSet, ref deviceInfoData, classInstallParams, classInstallParamsSize); if (result) { result = SetupDiChangeState(deviceInfoSet, ref deviceInfoData); } if (!result) { var ex = new Win32Exception(); SetupDiCallClassInstaller(DIF_PROPERTYCHANGE, deviceInfoSet, ref deviceInfoData); } return(result); } catch (Exception) { return(false); } }
public static void EnableDevice(Func <string, bool> hardwareIdFilter, bool enable) { Guid nullGuid = Guid.Empty; using (SafeDeviceInformationSetHandle infoSet = SetupDiGetClassDevs(ref nullGuid, null, IntPtr.Zero, DIGCF.ALLCLASSES)) { CheckWin32CallSuccess(!infoSet.IsInvalid); SP_DEVINFO_DATA devInfo = new SP_DEVINFO_DATA(); devInfo.cbSize = (UInt32)Marshal.SizeOf(devInfo); for (uint index = 0; ; ++index) { CheckWin32CallSuccess(SetupDiEnumDeviceInfo(infoSet, index, ref devInfo)); string hardwareId = GetStringPropertyForDevice(infoSet, ref devInfo, SPDRP.HARDWAREID); if ((!string.IsNullOrEmpty(hardwareId)) && (hardwareIdFilter(hardwareId))) { break; } } SP_CLASSINSTALL_HEADER classinstallHeader = new SP_CLASSINSTALL_HEADER(); classinstallHeader.cbSize = (UInt32)Marshal.SizeOf(classinstallHeader); classinstallHeader.InstallFunction = DIF.PROPERTYCHANGE; SP_PROPCHANGE_PARAMS propchangeParams = new SP_PROPCHANGE_PARAMS { ClassInstallHeader = classinstallHeader, StateChange = enable ? DICS.ENABLE : DICS.DISABLE, Scope = DICS_FLAG.GLOBAL, HwProfile = 0, }; CheckWin32CallSuccess(SetupDiSetClassInstallParams(infoSet, ref devInfo, ref propchangeParams, (UInt32)Marshal.SizeOf(propchangeParams))); CheckWin32CallSuccess(SetupDiChangeState(infoSet, ref devInfo)); } }
public static void DisableDeviceByID(string hardwareId, bool disable = true) { ListDevicesAdvanced( action: (IntPtr info, SP_DEVINFO_DATA devdata, Device device) => { SP_CLASSINSTALL_HEADER header = new SP_CLASSINSTALL_HEADER(); header.cbSize = (UInt32)Marshal.SizeOf(header); header.InstallFunction = DIF_PROPERTYCHANGE; SP_PROPCHANGE_PARAMS propchangeparams = new SP_PROPCHANGE_PARAMS(); propchangeparams.ClassInstallHeader = header; propchangeparams.StateChange = disable ? DICS_DISABLE : DICS_ENABLE; propchangeparams.Scope = DICS_FLAG_GLOBAL; propchangeparams.HwProfile = 0; SetupDiSetClassInstallParams(info, ref devdata, ref propchangeparams, (UInt32)Marshal.SizeOf(propchangeparams)); CheckError("SetupDiSetClassInstallParams"); SetupDiChangeState( info, ref devdata); CheckError("SetupDiChangeState"); }, filter: device => device.ID.Contains(hardwareId) ); }
public static void ChangeDeviceState(string filter, bool disable) { IntPtr info = IntPtr.Zero; Guid NullGuid = Guid.Empty; try { info = SetupDiGetClassDevsW(ref NullGuid, null, IntPtr.Zero, DIGCF_ALLCLASSES); CheckError("SetupDiGetClassDevs"); SP_DEVINFO_DATA devdata = new SP_DEVINFO_DATA(); devdata.cbSize = (UInt32)Marshal.SizeOf(devdata); // Get first device matching device criterion. for (uint i = 0;; i++) { SetupDiEnumDeviceInfo(info, i, out devdata); // if no items match filter, throw if (Marshal.GetLastWin32Error() == ERROR_NO_MORE_ITEMS) { CheckError("No device found matching filter.", 0xcffff); } CheckError("SetupDiEnumDeviceInfo"); string devicepath = GetStringPropertyForDevice(info, devdata, 1); // SPDRP_HARDWAREID if (devicepath != null && devicepath.Contains(filter)) { break; } } SP_CLASSINSTALL_HEADER header = new SP_CLASSINSTALL_HEADER(); header.cbSize = (UInt32)Marshal.SizeOf(header); header.InstallFunction = DIF_PROPERTYCHANGE; SP_PROPCHANGE_PARAMS propchangeparams = new SP_PROPCHANGE_PARAMS { ClassInstallHeader = header, StateChange = disable ? DICS_DISABLE : DICS_ENABLE, Scope = DICS_FLAG_GLOBAL, HwProfile = 0 }; SetupDiSetClassInstallParams(info, ref devdata, ref propchangeparams, (UInt32)Marshal.SizeOf(propchangeparams)); CheckError("SetupDiSetClassInstallParams"); SetupDiChangeState(info, ref devdata); //SetupDiCallClassInstaller(DIF_PROPERTYCHANGE, info, ref devdata); CheckError("SetupDiChangeState"); } finally { if (info != IntPtr.Zero) { SetupDiDestroyDeviceInfoList(info); } } }
public static bool ChangePropertyDevice(Guid guidClass, string deviceInstanceId, DiChangeState changeState) { IntPtr deviceInfoList = IntPtr.Zero; try { SP_DEVICE_INFO_DATA deviceInfoData = new SP_DEVICE_INFO_DATA(); deviceInfoData.cbSize = Marshal.SizeOf(deviceInfoData); //Get device information deviceInfoList = SetupDiGetClassDevs(guidClass, deviceInstanceId, IntPtr.Zero, DiGetClassFlag.DIGCF_DEVICEINTERFACE); if (!SetupDiEnumDeviceInfo(deviceInfoList, 0, ref deviceInfoData)) { Debug.WriteLine("SetupDi: Failed getting device info."); return(false); } //Set property change param SP_PROPCHANGE_PARAMS propertyParams = new SP_PROPCHANGE_PARAMS(); propertyParams.classInstallHeader.cbSize = Marshal.SizeOf(propertyParams.classInstallHeader); propertyParams.classInstallHeader.installFunction = DiFunction.DIF_PROPERTYCHANGE; propertyParams.changeStateFlag = DiChangeStateFlag.DICS_FLAG_GLOBAL; propertyParams.stateChange = changeState; //Prepare the device if (!SetupDiSetClassInstallParams(deviceInfoList, ref deviceInfoData, ref propertyParams, Marshal.SizeOf(propertyParams))) { Debug.WriteLine("SetupDi: Failed to set install params."); return(false); } //Change the property if (!SetupDiCallClassInstaller(DiFunction.DIF_PROPERTYCHANGE, deviceInfoList, ref deviceInfoData)) { Debug.WriteLine("SetupDi: Failed to change property."); return(false); } return(true); } catch (Exception ex) { Debug.WriteLine("Failed to change property: " + ex.Message); return(false); } finally { if (deviceInfoList != IntPtr.Zero) { SetupDiDestroyDeviceInfoList(deviceInfoList); } } }
public bool ChangeDeviceState(DICS newState) { SP_PROPCHANGE_PARAMS pcp = new SP_PROPCHANGE_PARAMS(); pcp.ClassInstallHeader.cbSize = Marshal.SizeOf(typeof(SP_CLASSINSTALL_HEADER)); pcp.ClassInstallHeader.InstallFunction = DI_FUNCTION.DIF_PROPERTYCHANGE; pcp.StateChange = (int)newState; pcp.Scope = (int)DICS_FLAG.DICS_FLAG_GLOBAL; pcp.HwProfile = 0; if (!SetupDiSetClassInstallParams(deviceEnumerator.hardwareDeviceInfo, ref devinfoData, ref pcp, Marshal.SizeOf(pcp))) return false; if (!SetupDiCallClassInstaller((uint)DI_FUNCTION.DIF_PROPERTYCHANGE, deviceEnumerator.hardwareDeviceInfo, ref devinfoData)) return false; return true; }
/// <summary> /// 启用鼠标类设备 /// </summary> public static void EnableMouse() { IntPtr info = IntPtr.Zero; Guid NullGuid = Guid.Empty; try { info = SetupDiGetClassDevsW(ref NullGuid, null, IntPtr.Zero, DIGCF.ALLCLASSES); SP_DEVINFO_DATA devdata = new SP_DEVINFO_DATA(); devdata.cbSize = (UInt32)Marshal.SizeOf(devdata); ///遍历设备 for (uint i = 0; SetupDiEnumDeviceInfo(info, i, out devdata); i++) { if (devdata.ClassGuid == new Guid(Mouse)) { SP_CLASSINSTALL_HEADER header = new SP_CLASSINSTALL_HEADER(); header.cbSize = (UInt32)Marshal.SizeOf(header); header.InstallFunction = DIF.PROPERTYCHANGE; SP_PROPCHANGE_PARAMS propchangeparams = new SP_PROPCHANGE_PARAMS { ClassInstallHeader = header, StateChange = DICS.ENABLE, Scope = DICS_FLAG.GLOBAL, HwProfile = 0 }; SetupDiSetClassInstallParams(info, ref devdata, ref propchangeparams, (UInt32)Marshal.SizeOf(propchangeparams)); SetupDiChangeState(info, ref devdata); } } } catch (Exception ex) { throw new Exception(string.Format("ChangeMouseState failed,the reason is {0}", ex.Message)); } finally { if (info != IntPtr.Zero) { SetupDiDestroyDeviceInfoList(info); } } }
protected virtual bool RestartDevice(string instanceId) { var deviceInfoSet = IntPtr.Zero; try { var deviceInterfaceData = new SP_DEVICE_INTERFACE_DATA(); deviceInterfaceData.cbSize = Marshal.SizeOf(deviceInterfaceData); deviceInfoSet = SetupDiGetClassDevs(ref _class, IntPtr.Zero, IntPtr.Zero, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE); if (SetupDiOpenDeviceInfo(deviceInfoSet, instanceId, IntPtr.Zero, 0, ref deviceInterfaceData)) { var props = new SP_PROPCHANGE_PARAMS(); props.ClassInstallHeader = new SP_CLASSINSTALL_HEADER(); props.ClassInstallHeader.cbSize = Marshal.SizeOf(props.ClassInstallHeader); props.ClassInstallHeader.InstallFunction = DIF_PROPERTYCHANGE; props.Scope = DICS_FLAG_GLOBAL; props.StateChange = DICS_PROPCHANGE; props.HwProfile = 0x00; if (SetupDiSetClassInstallParams(deviceInfoSet, ref deviceInterfaceData, ref props, Marshal.SizeOf(props))) { return(SetupDiChangeState(deviceInfoSet, ref deviceInterfaceData)); } } } catch (Exception ex) { Log.ErrorFormat("{0} {1}", ex.HelpLink, ex.Message); throw; } finally { if (deviceInfoSet != IntPtr.Zero) { SetupDiDestroyDeviceInfoList(deviceInfoSet); } } return(false); }
public bool ChangeDeviceState(DICS newState) { SP_PROPCHANGE_PARAMS pcp = new SP_PROPCHANGE_PARAMS(); pcp.ClassInstallHeader.cbSize = Marshal.SizeOf(typeof(SP_CLASSINSTALL_HEADER)); pcp.ClassInstallHeader.InstallFunction = DI_FUNCTION.DIF_PROPERTYCHANGE; pcp.StateChange = (int)newState; pcp.Scope = (int)DICS_FLAG.DICS_FLAG_GLOBAL; pcp.HwProfile = 0; if (!SetupDiSetClassInstallParams(deviceEnumerator._HardwareDeviceInfo, ref DevinfoData, ref pcp, Marshal.SizeOf(pcp))) { return(false); } if (!SetupDiCallClassInstaller((uint)DI_FUNCTION.DIF_PROPERTYCHANGE, deviceEnumerator._HardwareDeviceInfo, ref DevinfoData)) { return(false); } return(true); }
public static bool StateChange(bool Enable, int SelectedItem, IntPtr DevInfo) { bool result = false; SP_DEVINFO_DATA devInfoData = new SP_DEVINFO_DATA();; devInfoData.cbSize = Marshal.SizeOf(typeof(SP_DEVINFO_DATA)); if (true == SetupDiEnumDeviceInfo(DevInfo, SelectedItem, devInfoData)) { SP_PROPCHANGE_PARAMS pcp = new SP_PROPCHANGE_PARAMS();; pcp.ClassInstallHeader.cbSize = Marshal.SizeOf(typeof(SP_CLASSINSTALL_HEADER)); pcp.ClassInstallHeader.InstallFunction = DIF_PROPERTYCHANGE; pcp.Scope = DICS_FLAG_CONFIGSPECIFIC; pcp.StateChange = (Enable ? DICS_ENABLE : DICS_DISABLE); if (true == SetupDiSetClassInstallParams(DevInfo, devInfoData, pcp, Marshal.SizeOf(pcp))) { if (true == SetupDiCallClassInstaller(DIF_PROPERTYCHANGE, DevInfo, devInfoData)) { result = true; } else { uint ret = (uint)Win32.GetLastError(); } } } return(result); }
private void SetHardwareEnabled(IntPtr m_MainIntPtr, bool p_Enabled, SP_DEVINFO_DATA p_DevInfoData) { SP_PROPCHANGE_PARAMS _HardwareParams = new SP_PROPCHANGE_PARAMS(); _HardwareParams.ClassInstallHeader.cbSize = Marshal.SizeOf(typeof(SP_CLASSINSTALL_HEADER)); _HardwareParams.ClassInstallHeader.InstallFunction = (int)DIF.DIF_PROPERTYCHANGE; _HardwareParams.StateChange = (int)DICS.DICS_DISABLE; _HardwareParams.Scope = (int)DICS.DICS_FLAG_CONFIGSPECIFIC; if (p_Enabled) { _HardwareParams.StateChange = (int)DICS.DICS_ENABLE; _HardwareParams.Scope = (int)DICS.DICS_FLAG_GLOBAL; IntPtr _HardwareParamsIntPtr = Marshal.AllocHGlobal(Marshal.SizeOf(_HardwareParams)); Marshal.StructureToPtr(_HardwareParams, _HardwareParamsIntPtr, true); IntPtr _DevInfoDataIntPtr = Marshal.AllocHGlobal(Marshal.SizeOf(p_DevInfoData)); if (SetupDiSetClassInstallParams(m_MainIntPtr, _DevInfoDataIntPtr, _HardwareParamsIntPtr, Marshal.SizeOf(typeof(SP_PROPCHANGE_PARAMS)))) { SetupDiCallClassInstaller(DIF.DIF_PROPERTYCHANGE, m_MainIntPtr, _DevInfoDataIntPtr); } _HardwareParams.ClassInstallHeader.cbSize = Marshal.SizeOf(typeof(SP_CLASSINSTALL_HEADER)); _HardwareParams.ClassInstallHeader.InstallFunction = (int)DIF.DIF_PROPERTYCHANGE; _HardwareParams.StateChange = (int)DICS.DICS_ENABLE; _HardwareParams.Scope = (int)DICS.DICS_FLAG_CONFIGSPECIFIC; _HardwareParams.HwProfile = 0; } IntPtr _SetHardwareParamsIntPtr = Marshal.AllocHGlobal(Marshal.SizeOf(_HardwareParams)); Marshal.StructureToPtr(_HardwareParams, _SetHardwareParamsIntPtr, true); IntPtr _SetDevInfoDataIntPtr = Marshal.AllocHGlobal(Marshal.SizeOf(p_DevInfoData)); Marshal.StructureToPtr(p_DevInfoData, _SetDevInfoDataIntPtr, true); SetupDiSetClassInstallParams(m_MainIntPtr, _SetDevInfoDataIntPtr, _SetHardwareParamsIntPtr, Marshal.SizeOf(typeof(SP_PROPCHANGE_PARAMS))); SetupDiCallClassInstaller(DIF.DIF_PROPERTYCHANGE, m_MainIntPtr, _SetDevInfoDataIntPtr); }
protected static extern bool SetupDiSetClassInstallParams(IntPtr DeviceInfoSet, ref SP_DEVICE_INTERFACE_DATA DeviceInterfaceData, ref SP_PROPCHANGE_PARAMS ClassInstallParams, int ClassInstallParamsSize);
protected virtual bool RestartDevice(string instanceId) { var deviceInfoSet = IntPtr.Zero; try { var deviceInterfaceData = new SP_DEVICE_INTERFACE_DATA(); deviceInterfaceData.cbSize = Marshal.SizeOf(deviceInterfaceData); deviceInfoSet = SetupDiGetClassDevs(ref _class, IntPtr.Zero, IntPtr.Zero, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE); if (SetupDiOpenDeviceInfo(deviceInfoSet, instanceId, IntPtr.Zero, 0, ref deviceInterfaceData)) { var props = new SP_PROPCHANGE_PARAMS(); props.ClassInstallHeader = new SP_CLASSINSTALL_HEADER(); props.ClassInstallHeader.cbSize = Marshal.SizeOf(props.ClassInstallHeader); props.ClassInstallHeader.InstallFunction = DIF_PROPERTYCHANGE; props.Scope = DICS_FLAG_GLOBAL; props.StateChange = DICS_PROPCHANGE; props.HwProfile = 0x00; if (SetupDiSetClassInstallParams(deviceInfoSet, ref deviceInterfaceData, ref props, Marshal.SizeOf(props))) { return SetupDiChangeState(deviceInfoSet, ref deviceInterfaceData); } } } catch (Exception ex) { Log.ErrorFormat("{0} {1}", ex.HelpLink, ex.Message); throw; } finally { if (deviceInfoSet != IntPtr.Zero) { SetupDiDestroyDeviceInfoList(deviceInfoSet); } } return false; }
public void DisableDevice(Func <string, bool> filter, bool disable = true, bool toggle = false) { IntPtr info = IntPtr.Zero; Guid NullGuid = Guid.Empty; try { info = SetupDiGetClassDevs( ref NullGuid, null, IntPtr.Zero, DIGCF_ALLCLASSES); CheckError("SetupDiGetClassDevs"); // Get first device matching device criterion. SP_DEVINFO_DATA devdata; for (uint i = 0; ; i++) { devdata = new SP_DEVINFO_DATA(); devdata.cbSize = (UInt32)Marshal.SizeOf(devdata); SetupDiEnumDeviceInfo(info, i, out devdata); if (Marshal.GetLastWin32Error() == ERROR_NO_MORE_ITEMS) { break; } CheckError("SetupDiEnumDeviceInfo"); //var hardwareIds = GetStringPropertyForDevice(info, devdata, propId: SPDRP.SPDRP_HARDWAREID); var devId = GetDeviceId(devdata.devInst); if (devId == null || !filter(devId)) { continue; } // Skip devices that aren't working with no issues (e.g. disconnected devices) uint status; uint probNum; var succ = CM_Get_DevNode_Status(out status, out probNum, devdata.devInst, 0); if (succ != 0) { continue; } SP_CLASSINSTALL_HEADER header = new SP_CLASSINSTALL_HEADER(); header.cbSize = (UInt32)Marshal.SizeOf(header); header.InstallFunction = DIF_PROPERTYCHANGE; SP_PROPCHANGE_PARAMS propchangeparams = new SP_PROPCHANGE_PARAMS(); propchangeparams.ClassInstallHeader = header; propchangeparams.StateChange = disable ? DICS_DISABLE : DICS_ENABLE; propchangeparams.Scope = DICS_FLAG_GLOBAL; propchangeparams.HwProfile = 0; SetupDiSetClassInstallParams(info, ref devdata, ref propchangeparams, (UInt32)Marshal.SizeOf(propchangeparams)); CheckError("SetupDiSetClassInstallParams"); SetupDiChangeState( info, ref devdata); CheckError("SetupDiChangeState"); if (toggle) { propchangeparams.StateChange = !disable ? DICS_DISABLE : DICS_ENABLE; SetupDiSetClassInstallParams(info, ref devdata, ref propchangeparams, (UInt32)Marshal.SizeOf(propchangeparams)); CheckError("SetupDiSetClassInstallParams"); SetupDiChangeState( info, ref devdata); CheckError("SetupDiChangeState"); } } } finally { if (info != IntPtr.Zero) { SetupDiDestroyDeviceInfoList(info); } } }
public static void SetDeviceState(string instanceId, DICS state, DeviceClass?deviceClass = null) { SafeDeviceInfoSetHandle diSetHandle = null; try { GCHandle guid = new GCHandle(); if (deviceClass.HasValue) { var classGuid = Device.DeviceClassGuids[(int)deviceClass]; guid = GCHandle.Alloc(classGuid, GCHandleType.Pinned); diSetHandle = NativeMethods.SetupDiGetClassDevs(guid.AddrOfPinnedObject(), null, IntPtr.Zero, SetupDiGetClassDevsFlags.Null); } else { diSetHandle = NativeMethods.SetupDiGetClassDevs(IntPtr.Zero, null, IntPtr.Zero, SetupDiGetClassDevsFlags.AllClasses); } if (diSetHandle.IsInvalid) { throw new Win32Exception(Marshal.GetLastWin32Error(), "Error calling SetupDiGetClassDevs"); } //Get the device information data for each matching device. var diData = GetDeviceInfoData(diSetHandle); if (!string.IsNullOrEmpty(instanceId)) { // Find the index of of the instance int index = GetIndexOfInstance(diSetHandle, diData, instanceId); if (index == -1) { throw new IndexOutOfRangeException(string.Format("The device '{0}' could not be found", instanceId)); } diData = new SP_DEVINFO_DATA[] { diData[index] }; } for (int i = 0; i < diData.Length; i++) { SP_CLASSINSTALL_HEADER installHeader = default(SP_CLASSINSTALL_HEADER); installHeader.InstallFunction = DIF.DIF_PROPERTYCHANGE; installHeader.cbSize = (uint)Marshal.SizeOf(installHeader); SP_PROPCHANGE_PARAMS propertyChangeParameters = default(SP_PROPCHANGE_PARAMS); propertyChangeParameters.ClassInstallHeader = installHeader; propertyChangeParameters.HwProfile = 0; propertyChangeParameters.Scope = DICS.DICS_FLAG_CONFIGSPECIFIC; propertyChangeParameters.StateChange = state; if (NativeMethods.SetupDiSetClassInstallParams(diSetHandle, ref diData[i], ref propertyChangeParameters, (uint)Marshal.SizeOf(propertyChangeParameters))) { if (!NativeMethods.SetupDiCallClassInstaller(DIF.DIF_PROPERTYCHANGE, diSetHandle, ref diData[i])) { throw new Win32Exception(Marshal.GetLastWin32Error(), "Error calling SetupDiCallClassInstaller"); } } } } finally { if (diSetHandle != null) { if (diSetHandle.IsClosed == false) { diSetHandle.Close(); } diSetHandle.Dispose(); } } }
static extern bool SetupDiSetClassInstallParams(IntPtr DeviceInfoSet, ref SP_DEVINFO_DATA DeviceInfoData, SP_PROPCHANGE_PARAMS ClassInstallParams, UInt32 ClassInstallParamsSize);
public static void DisableDevice(Func<string, bool> filter, bool disable = true) { IntPtr info = IntPtr.Zero; Guid NullGuid = Guid.Empty; try { info = SetupDiGetClassDevsW( ref NullGuid, null, IntPtr.Zero, DIGCF_ALLCLASSES); CheckError("SetupDiGetClassDevs"); SP_DEVINFO_DATA devdata = new SP_DEVINFO_DATA(); devdata.cbSize = (UInt32)Marshal.SizeOf(devdata); // Get first device matching device criterion. for (uint i = 0; ; i++) { SetupDiEnumDeviceInfo(info, i, out devdata); // if no items match filter, throw if (Marshal.GetLastWin32Error() == ERROR_NO_MORE_ITEMS) CheckError("No device found matching filter.", 0xcffff); CheckError("SetupDiEnumDeviceInfo"); string devicepath = GetStringPropertyForDevice(info, devdata, 1); // SPDRP_HARDWAREID // Uncomment to print name/path //Console.WriteLine(GetStringPropertyForDevice(info, // devdata, DEVPKEY_Device_DeviceDesc)); //Console.WriteLine(" {0}", devicepath); if (devicepath != null && filter(devicepath)) break; } SP_CLASSINSTALL_HEADER header = new SP_CLASSINSTALL_HEADER(); header.cbSize = (UInt32)Marshal.SizeOf(header); header.InstallFunction = DIF_PROPERTYCHANGE; SP_PROPCHANGE_PARAMS propchangeparams = new SP_PROPCHANGE_PARAMS(); propchangeparams.ClassInstallHeader = header; propchangeparams.StateChange = disable ? DICS_DISABLE : DICS_ENABLE; propchangeparams.Scope = DICS_FLAG_GLOBAL; propchangeparams.HwProfile = 0; SetupDiSetClassInstallParams(info, ref devdata, ref propchangeparams, (UInt32)Marshal.SizeOf(propchangeparams)); CheckError("SetupDiSetClassInstallParams"); SetupDiChangeState( info, ref devdata); CheckError("SetupDiChangeState"); } finally { if (info != IntPtr.Zero) SetupDiDestroyDeviceInfoList(info); } }
private static int ChangeDeviceState(string hardwareId, DIC_STATE stateChange) { IntPtr deviceInfoSet = IntPtr.Zero; int error = 0; try { deviceInfoSet = SetupDiGetClassDevsEx( IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, DIGCF_ALLCLASSES, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero); SP_DEVINFO_DATA deviceInfoData = new SP_DEVINFO_DATA(); deviceInfoData.CbSize = (uint)Marshal.SizeOf(deviceInfoData); uint index = 0; while (SetupDiEnumDeviceInfo( deviceInfoSet, index, ref deviceInfoData)) { index++; uint requiredSize = 0; SetupDiGetDeviceRegistryProperty( deviceInfoSet, ref deviceInfoData, SPDRP_HARDWAREID, IntPtr.Zero, IntPtr.Zero, 0, ref requiredSize); if (requiredSize == 0) { continue; } IntPtr pPropertyBuffer = Marshal.AllocHGlobal((int)requiredSize); if (!SetupDiGetDeviceRegistryProperty( deviceInfoSet, ref deviceInfoData, SPDRP_HARDWAREID, IntPtr.Zero, pPropertyBuffer, requiredSize, IntPtr.Zero)) { continue; } string hwId = Marshal.PtrToStringAuto(pPropertyBuffer); Marshal.FreeHGlobal(pPropertyBuffer); if (hwId.Equals(hardwareId)) { SP_PROPCHANGE_PARAMS propChangeParams = new SP_PROPCHANGE_PARAMS(); propChangeParams.ClassInstallHeader.CbSize = (uint)Marshal.SizeOf(propChangeParams.ClassInstallHeader); propChangeParams.ClassInstallHeader.InstallFunction = DI_FUNCTION.DIF_PROPERTYCHANGE; propChangeParams.Scope = DI_REMOVEDEVICE_GLOBAL; propChangeParams.StateChange = stateChange; if (!SetupDiSetClassInstallParams( deviceInfoSet, ref deviceInfoData, ref propChangeParams, (uint)Marshal.SizeOf(propChangeParams))) { error = Marshal.GetLastWin32Error(); } if (!SetupDiChangeState( deviceInfoSet, ref deviceInfoData)) { error = Marshal.GetLastWin32Error(); } } } } finally { if (deviceInfoSet != IntPtr.Zero) { SetupDiDestroyDeviceInfoList(deviceInfoSet); } } return(error); }
/// <summary> /// 通过设备唯一标识PnpDeviceID,启用设备 /// </summary> /// <param name="pnpDeviceId"></param> /// <returns></returns> public static bool EnableDeviceByPnpDeviceId(string pnpDeviceId) { IntPtr info = IntPtr.Zero; Guid NullGuid = Guid.Empty; bool isSuccess = false; try { info = SetupDiGetClassDevsW(ref NullGuid, pnpDeviceId, IntPtr.Zero, DIGCF.DEVICEINTERFACE); SP_DEVINFO_DATA devdata = new SP_DEVINFO_DATA(); devdata.cbSize = (UInt32)Marshal.SizeOf(devdata); ///遍历设备 for (uint i = 0; SetupDiEnumDeviceInfo(info, i, out devdata); i++) { int size = 512; StringBuilder stringBuilder = new StringBuilder(size); uint result = Cfgmgr32.Cfgmgr32Base.CM_Get_Device_ID(devdata.DevInst, stringBuilder, size); if (result == 0) { string currentPnpDeviceID = stringBuilder.ToString(); if (currentPnpDeviceID == pnpDeviceId) { SP_CLASSINSTALL_HEADER header = new SP_CLASSINSTALL_HEADER(); header.cbSize = (UInt32)Marshal.SizeOf(header); header.InstallFunction = DIF.PROPERTYCHANGE; SP_PROPCHANGE_PARAMS propchangeparams = new SP_PROPCHANGE_PARAMS { ClassInstallHeader = header, StateChange = DICS.ENABLE, Scope = DICS_FLAG.GLOBAL, HwProfile = 0 }; SetupDiSetClassInstallParams(info, ref devdata, ref propchangeparams, (UInt32)Marshal.SizeOf(propchangeparams)); SetupDiChangeState(info, ref devdata); isSuccess = true; } } continue; } return(isSuccess); } catch (Exception ex) { Console.WriteLine(string.Format("EnableDeviceByPnpDeviceId failed,the reason is {0}", ex.Message)); return(isSuccess); } finally { if (info != IntPtr.Zero) { SetupDiDestroyDeviceInfoList(info); } } }
protected static extern Boolean SetupDiSetClassInstallParams(IntPtr DeviceInfoSet, ref SP_DEVICE_INTERFACE_DATA DeviceInterfaceData, ref SP_PROPCHANGE_PARAMS ClassInstallParams, Int32 ClassInstallParamsSize);
internal static extern bool SetupDiSetClassInstallParams(IntPtr deviceInfoSet, ref SP_DEVINFO_DATA deviceInfoData, ref SP_PROPCHANGE_PARAMS classInstallParams, int classInstallParamsSize);
public static void DisableDevice(Func <string, bool> filter, bool disable = true) { IntPtr info = IntPtr.Zero; Guid NullGuid = Guid.Empty; try { info = SetupDiGetClassDevsW( ref NullGuid, null, IntPtr.Zero, DIGCF_ALLCLASSES); CheckError("SetupDiGetClassDevs"); SP_DEVINFO_DATA devdata = new SP_DEVINFO_DATA(); devdata.cbSize = (UInt32)Marshal.SizeOf(devdata); // Get first device matching device criterion. for (uint i = 0; ; i++) { SetupDiEnumDeviceInfo(info, i, out devdata); // if no items match filter, throw if (Marshal.GetLastWin32Error() == ERROR_NO_MORE_ITEMS) { CheckError("No device found matching filter.", 0xcffff); } CheckError("SetupDiEnumDeviceInfo"); // By Hardware ID //string devicepath = GetStringPropertyForDevice(info, devdata, 1); // SPDRP_HARDWAREID // By Instance Path string devicepath = GetDeviceInstanceId(info, devdata); // Uncomment to print name/path //Console.WriteLine(GetStringPropertyForDevice(info, // devdata, DEVPKEY_Device_DeviceDesc)); //Console.WriteLine(" {0}", devicepath); if (devicepath != null && filter(devicepath)) { break; } } SP_CLASSINSTALL_HEADER header = new SP_CLASSINSTALL_HEADER(); header.cbSize = (UInt32)Marshal.SizeOf(header); header.InstallFunction = DIF_PROPERTYCHANGE; SP_PROPCHANGE_PARAMS propchangeparams = new SP_PROPCHANGE_PARAMS(); propchangeparams.ClassInstallHeader = header; propchangeparams.StateChange = disable ? DICS_DISABLE : DICS_ENABLE; propchangeparams.Scope = DICS_FLAG_GLOBAL; propchangeparams.HwProfile = 0; SetupDiSetClassInstallParams(info, ref devdata, ref propchangeparams, (UInt32)Marshal.SizeOf(propchangeparams)); CheckError("SetupDiSetClassInstallParams"); SetupDiChangeState( info, ref devdata); CheckError("SetupDiChangeState"); } finally { if (info != IntPtr.Zero) { SetupDiDestroyDeviceInfoList(info); } } }
/// <summary> /// Part in charge to really change the device's state /// </summary> /// <param name="info"></param> /// <param name="devdata"></param> /// <param name="status"></param> private static void ChangeReaderStatus(IntPtr info, SP_DEVINFO_DATA devdata, DeviceState status) { string devicepathTemp = GetStringPropertyForDevice(info, devdata, 0); SP_CLASSINSTALL_HEADER header = new SP_CLASSINSTALL_HEADER(); header.cbSize = (UInt32)Marshal.SizeOf(header); header.InstallFunction = DIF_PROPERTYCHANGE; SP_PROPCHANGE_PARAMS propchangeparams = new SP_PROPCHANGE_PARAMS(); propchangeparams.ClassInstallHeader = header; if(status == DeviceState.On || status == DeviceState.Off) { propchangeparams.StateChange = (status == DeviceState.Off) ? DICS_DISABLE : DICS_ENABLE; propchangeparams.Scope = DICS_FLAG_GLOBAL; } else // reset { propchangeparams.StateChange = DICS_PROPCHANGE; propchangeparams.Scope = DICS_FLAG_CONFIGSPECIFIC; } propchangeparams.HwProfile = 0; SetupDiSetClassInstallParams(info, ref devdata, ref propchangeparams, (UInt32)Marshal.SizeOf(propchangeparams)); CheckError("SetupDiSetClassInstallParams"); SetupDiChangeState(info, ref devdata); CheckError("SetupDiChangeState"); }
public static extern bool SetupDiSetClassInstallParams (IntPtr deviceInfoSet, ref SP_DEVINFO_DATA deviceInfoData, ref SP_PROPCHANGE_PARAMS classInstallParams, int classInstallParamsSize);
public static extern bool SetupDiSetClassInstallParams([In] IntPtr deviceInfoSet, [In] ref SP_DEVINFO_DATA deviceInfoData, [In] ref SP_PROPCHANGE_PARAMS classInstallParams, uint classInstallParamsSize);
private static extern bool SetupDiSetClassInstallParams( IntPtr deviceInfoSet, [In] ref SP_DEVINFO_DATA deviceInfoData, [In] ref SP_PROPCHANGE_PARAMS classInstallParams, UInt32 ClassInstallParamsSize);
public static extern Boolean SetupDiSetClassInstallParams(IntPtr DeviceInfoSet, SP_DEVINFO_DATA DeviceInfoData, SP_PROPCHANGE_PARAMS ClassInstallParams, int ClassInstallParamsSize);
public static void DisableDevice(Func <string, bool> filter, bool disable = true, bool toggle = false) { IntPtr info = IntPtr.Zero; Guid NullGuid = Guid.Empty; try { info = SetupDiGetClassDevs( ref NullGuid, null, IntPtr.Zero, DIGCF_ALLCLASSES); CheckError("SetupDiGetClassDevs"); // Get first device matching device criterion. SP_DEVINFO_DATA devdata; for (uint i = 0; ; i++) { devdata = new SP_DEVINFO_DATA(); devdata.cbSize = (UInt32)Marshal.SizeOf(devdata); SetupDiEnumDeviceInfo(info, i, out devdata); if (Marshal.GetLastWin32Error() == ERROR_NO_MORE_ITEMS) { throw new ApplicationException("No device matching that Hardware Id found."); } CheckError("SetupDiEnumDeviceInfo"); var hardwareIds = GetStringPropertyForDevice(info, devdata, propId: SPDRP.SPDRP_HARDWAREID); if (hardwareIds != null && filter(hardwareIds)) { try { SP_CLASSINSTALL_HEADER header = new SP_CLASSINSTALL_HEADER(); header.cbSize = (UInt32)Marshal.SizeOf(header); header.InstallFunction = DIF_PROPERTYCHANGE; SP_PROPCHANGE_PARAMS propchangeparams = new SP_PROPCHANGE_PARAMS(); propchangeparams.ClassInstallHeader = header; propchangeparams.StateChange = disable ? DICS_DISABLE : DICS_ENABLE; propchangeparams.Scope = DICS_FLAG_GLOBAL; propchangeparams.HwProfile = 0; SetupDiSetClassInstallParams(info, ref devdata, ref propchangeparams, (UInt32)Marshal.SizeOf(propchangeparams)); CheckError("SetupDiSetClassInstallParams"); SetupDiChangeState( info, ref devdata); CheckError("SetupDiChangeState"); if (toggle) { propchangeparams.StateChange = !disable ? DICS_DISABLE : DICS_ENABLE; SetupDiSetClassInstallParams(info, ref devdata, ref propchangeparams, (UInt32)Marshal.SizeOf(propchangeparams)); CheckError("SetupDiSetClassInstallParams"); SetupDiChangeState( info, ref devdata); CheckError("SetupDiChangeState"); } break; } catch { } } } } finally { if (info != IntPtr.Zero) { SetupDiDestroyDeviceInfoList(info); } } }