Hi @Semenzato, Marco , Welcome to Microsoft Q&A,
You can disable the USB host controller by calling Windows APIs, such as SetupAPI or Device Manager COM Interface. These interfaces allow direct manipulation of device status, but require calling lower-level system libraries and encapsulating them using P/Invoke.
Below is a sample code that uses P/Invoke to call SetupAPI to disable the USB controller:
using System;
using System.Runtime.InteropServices;
public class USBDisabler
{
private const int DICS_DISABLE = 0x00000001;
private const int DICS_ENABLE = 0x00000002;
private const int DICS_FLAG_GLOBAL = 0x00000001;
private const int DIGCF_PRESENT = 0x00000002;
private const int DIGCF_ALLCLASSES = 0x00000004;
[DllImport("setupapi.dll", SetLastError = true)]
private static extern IntPtr SetupDiGetClassDevs(
ref Guid ClassGuid,
IntPtr Enumerator,
IntPtr hwndParent,
uint Flags);
[DllImport("setupapi.dll", SetLastError = true)]
private static extern bool SetupDiEnumDeviceInfo(
IntPtr DeviceInfoSet,
uint MemberIndex,
ref SP_DEVINFO_DATA DeviceInfoData);
[DllImport("setupapi.dll", SetLastError = true)]
private static extern bool SetupDiSetClassInstallParams(
IntPtr DeviceInfoSet,
ref SP_DEVINFO_DATA DeviceInfoData,
ref SP_CLASSINSTALL_HEADER ClassInstallParams,
uint ClassInstallParamsSize);
[DllImport("setupapi.dll", SetLastError = true)]
private static extern bool SetupDiCallClassInstaller(
uint InstallFunction,
IntPtr DeviceInfoSet,
ref SP_DEVINFO_DATA DeviceInfoData);
[StructLayout(LayoutKind.Sequential)]
private struct SP_DEVINFO_DATA
{
public uint cbSize;
public Guid ClassGuid;
public uint DevInst;
public IntPtr Reserved;
}
[StructLayout(LayoutKind.Sequential)]
private struct SP_CLASSINSTALL_HEADER
{
public uint cbSize;
public int InstallFunction;
}
public void DisableUSBControllers()
{
var usbClassGuid = new Guid("36fc9e60-c465-11cf-8056-444553540000"); // USB class GUID
IntPtr deviceInfoSet = SetupDiGetClassDevs(ref usbClassGuid, IntPtr.Zero, IntPtr.Zero, DIGCF_PRESENT | DIGCF_ALLCLASSES);
if (deviceInfoSet == IntPtr.Zero)
{
throw new Exception("Failed to get device info set for USB controllers.");
}
var deviceInfoData = new SP_DEVINFO_DATA();
deviceInfoData.cbSize = (uint)Marshal.SizeOf(deviceInfoData);
for (uint i = 0; SetupDiEnumDeviceInfo(deviceInfoSet, i, ref deviceInfoData); i++)
{
var header = new SP_CLASSINSTALL_HEADER
{
cbSize = (uint)Marshal.SizeOf(typeof(SP_CLASSINSTALL_HEADER)),
InstallFunction = DICS_DISABLE
};
if (!SetupDiSetClassInstallParams(deviceInfoSet, ref deviceInfoData, ref header, (uint)Marshal.SizeOf(header)) ||
!SetupDiCallClassInstaller(DICS_DISABLE, deviceInfoSet, ref deviceInfoData))
{
throw new Exception("Failed to disable USB controller.");
}
}
}
}
Best Regards,
Jiale
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.