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(); } } }
public static bool RemoveFromSystem( SetupApi.DeviceInfoSet devInfoSet, string hwID, bool strictSearch) // WARNING: Removes ONLY the particular device // instance referenced by 'devInfoSet' { SetupApi.SP_DEVINFO_DATA devInfoData; devInfoData = FindInSystem( hwID, devInfoSet, strictSearch ); if (devInfoData == null) { return(false); } SetupApi.SP_REMOVEDEVICE_PARAMS rparams = new SetupApi.SP_REMOVEDEVICE_PARAMS(); rparams.ClassInstallHeader.cbSize = (uint)Marshal.SizeOf(rparams.ClassInstallHeader); rparams.ClassInstallHeader.InstallFunction = SetupApi.DI_FUNCTION.DIF_REMOVE; rparams.HwProfile = 0; rparams.Scope = SetupApi.DI_REMOVE_DEVICE_GLOBAL; GCHandle handle1 = GCHandle.Alloc(rparams); if (!SetupApi.SetupDiSetClassInstallParams( devInfoSet.Get(), devInfoData, ref rparams, Marshal.SizeOf(rparams))) { Win32Error.Set("SetupDiSetClassInstallParams"); throw new Exception( Win32Error.GetFullErrMsg() ); } Trace.WriteLine( "Removing device \'" + hwID + "\' from system" ); if (!SetupApi.SetupDiCallClassInstaller( SetupApi.DI_FUNCTION.DIF_REMOVE, devInfoSet.Get(), devInfoData)) { Win32Error.Set("SetupDiCallClassInstaller"); if (Win32Error.GetErrorNo() != WinError.ERROR_KEY_DOES_NOT_EXIST) { throw new Exception( Win32Error.GetFullErrMsg() ); } } Trace.WriteLine("Remove should have worked"); return(true); }
private static void VifDisableEnable(bool enable) { string action = enable ? "enable" : "disable"; Trace.WriteLine("===> VifDisableEnable: \'" + action + "\'"); using (SetupApi.DeviceInfoSet devInfoSet = new SetupApi.DeviceInfoSet( IntPtr.Zero, "XENVIF", IntPtr.Zero, SetupApi.DiGetClassFlags.DIGCF_PRESENT | SetupApi.DiGetClassFlags.DIGCF_ALLCLASSES)) { SetupApi.SP_DEVINFO_DATA devInfoData = new SetupApi.SP_DEVINFO_DATA(); devInfoData.cbSize = (uint)Marshal.SizeOf(devInfoData); for (uint i = 0; SetupApi.SetupDiEnumDeviceInfo( devInfoSet.Get(), i, devInfoData); ++i) { SetupApi.PropertyChangeParameters pcParams = new SetupApi.PropertyChangeParameters(); pcParams.size = 8; pcParams.diFunction = SetupApi.DI_FUNCTION.DIF_PROPERTYCHANGE; pcParams.scope = SetupApi.Scopes.Global; if (enable) { pcParams.stateChange = SetupApi.StateChangeAction.Enable; } else { pcParams.stateChange = SetupApi.StateChangeAction.Disable; } pcParams.hwProfile = 0; var pinned = GCHandle.Alloc(pcParams, GCHandleType.Pinned); byte[] temp = new byte[Marshal.SizeOf(pcParams)]; Marshal.Copy( pinned.AddrOfPinnedObject(), temp, 0, Marshal.SizeOf(pcParams) ); var pdd = GCHandle.Alloc(devInfoData, GCHandleType.Pinned); if (!SetupApi.SetupDiSetClassInstallParams( devInfoSet.Get(), pdd.AddrOfPinnedObject(), pinned.AddrOfPinnedObject(), Marshal.SizeOf(pcParams))) { Win32Error.Set("SetupDiSetClassInstallParams"); Trace.WriteLine(Win32Error.GetFullErrMsg()); } if (!SetupApi.SetupDiCallClassInstaller( SetupApi.DI_FUNCTION.DIF_PROPERTYCHANGE, devInfoSet.Get(), pdd.AddrOfPinnedObject())) { Win32Error.Set("SetupDiCallClassInstaller"); Trace.WriteLine(Win32Error.GetFullErrMsg()); } pdd.Free(); pinned.Free(); } } Trace.WriteLine("<=== VifDisableEnable"); }