/// <summary> /// Changes the properties of a device by calling the <see cref="SetupDiSetClassInstallParams"/> and <see cref="SetupDiChangeState"/> functions /// </summary> /// <param name="installFunction">The function to call</param> /// <param name="stateChange">The new state</param> /// <param name="scope">Scope for the change</param> /// <param name="hardwareProfile">HardwareProfile to use</param> public void ChangeProperty(DIF installFunction, DICS stateChange, DICS_FLAGS scope, uint hardwareProfile) { var p = new SP_PROPCHANGE_PARAMS(); p.ClassInstallHeader.cbSize = (uint)Marshal.SizeOf(p.ClassInstallHeader); p.ClassInstallHeader.InstallFunction = installFunction; p.StateChange = stateChange; p.Scope = scope; p.HwProfile = hardwareProfile; var deviceInfo = this.deviceInfo; { //Set parameters if (!SetupDiSetClassInstallParams(Handle, ref deviceInfo, ref p, (uint)Marshal.SizeOf(p))) { throw new Win32ErrorException(); } KERNEL32.CheckLastError(); } { //Run change if (!SetupDiChangeState(Handle, ref deviceInfo)) { throw new Win32ErrorException(); } KERNEL32.CheckLastError(); } //if (!SetupDiCallClassInstaller(installFunction, m_Handle, ref deviceInfo)) }
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 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 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(); } } }