2

I am working on a project that needs to enable/disable specific USB storage devices.

My app gets all storage USB DeviceIDs, and depending on saved settings then needs to allow the device or not. I have used the code from this previous question for the enable/disable, and call it like so:

DisableHardware.DisableDevice(n => n.ToUpperInvariant().Contains(VidPid), true);

with string VidPid = "VID_8564&PID_1000";. I have stepped through the code, it all works perfectly (as far as I can tell) until the SetupDIChangeState call, which then returns -536870389 (E000020B as hex) as error code (from Marshall.GetLastWin32Error()).

Apparently this refers to either 1) the device not being present (which as far as I understand is not the case here, as all other calls in this class work fine, and I get `VidPid' from

private static List<WMUBClasses.USBDeviceInfo> GetUSBDevices()
{
    try
    {
        List<WMUBClasses.USBDeviceInfo> tList = new List<WMUBClasses.USBDeviceInfo>();
        ManagementObjectCollection collection;
        using (var searcher = new ManagementObjectSearcher(@"SELECT * FROM Win32_USBHub"))
        {
            collection = searcher.Get();
        }
        foreach (var device in collection)
        {
            if (!device.GetPropertyValue("Description").ToString().Contains("Storage"))
            {
                continue;
            }
            tList.Add(new WMUBClasses.USBDeviceInfo(
            (string)device.GetPropertyValue("DeviceID"),
            (string)device.GetPropertyValue("PNPDeviceID"),
            (string)device.GetPropertyValue("Description")
            ));
        }
        return tList;
    }
    catch (Exception ex)
    {
        return null;
    }
}

or 2) an incorrect build platform, but I have tried all the different combinations (Any CPU, Mixed Platforms, x86 and x64), they all return the same result.

I have also looked at this which is another approach to my problem (by creating and using a kernel mode filter driver), it just seems like killing a fly with a wrecking ball. To be honest I have no clue of how to go about using this (for someone that hasn't done any driver development it looks super intimidating, especially after having read some of the available documentation.)

Should I (A) keep using the SetupDi API calls to achieve my goal and if so, can anyone see what is wrong with the code or how I am using it? If not (A), should I (B) use the filter driver approach instead and if so, any pointers?

As stated in the header, I want to disable specific USB storage devices, so as far as I understand, this precludes using the Registry to disable ALL USB storage devices. So if neither of the above, does anyone have any other direction I should be looking at instead?

Community
  • 1
  • 1
Theon P
  • 47
  • 1
  • 8

0 Answers0