How to disable a device from .NET

Semenzato, Marco 20 Reputation points
2024-10-30T13:39:29.4833333+00:00

Hello,

I need a POC of "USB disabling" from a .NET application.

I made several experiments on windows in order to achieve what I need.

The first experiment I made was going into regedit and search for the key "Computer\HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\USBSTOR" . Then find the "Start" key and set to 4 --> USB disable. This approach is working fine but the issue is that at machine's reboot, if you plug an USB for the first time it is recognized from the system. From 2nd time on, the system do not recognize it, correctly. I'm not sure why, probably because the BIOS enables it by default and at the first attempt remains enabled. This approach do not solve my problem.

The second approach I tried is going into "Device Manager", I picked the peripheral (in my case USB host controller) and I disabled it by right click-->Disable device. This approach seems to work at first time, even after the reboot. (unlike the first approach)

Now I need to find a way to let my C# program do the job. Is there a way to access to device properties?

My approach may not be the most efficent, so feel free to propose a better approach. Important thing to me is to disable USB from the windows startup.

Thanks in advance,

Marco.

Developer technologies .NET Other
Developer technologies C#
0 comments No comments
{count} votes

Accepted answer
  1. Anonymous
    2024-10-30T14:31:57.58+00:00

    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.

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.