0

I am trying to programatically disable the built-in webcam on my laptop.

To do this, I have been refering to Win32 API function to programmatically enable/disable device (solution by justin grant) and Enable/Disable a device programmatically with VB.Net using the setup api.

I have made some changes to target the camera instead of the touchpad (changing the GUID and the device instance ID), however I am still getting an error.

I am new to using Win32 and am confused as to what the error might be.

The following code is the segment that throws the error. Apart from changing what was mentioned above, the code is identical to that provided in the first link, so I have only posted the following code:

    private static void EnableDevice(SafeDeviceInfoSetHandle handle, DeviceInfoData diData, bool enable)
    {
        PropertyChangeParameters @params = new PropertyChangeParameters();
        // The size is just the size of the header, but we've flattened the structure.
        // The header comprises the first two fields, both integer.
        @params.Size = 8;
        @params.DiFunction = DiFunction.PropertyChange;
        @params.Scope = Scopes.Global;
        if (enable)
        {
            @params.StateChange = StateChangeAction.Enable;
        }
        else
        {
            @params.StateChange = StateChangeAction.Disable;
        }

        bool result = NativeMethods.SetupDiSetClassInstallParams(handle, ref diData, ref @params, Marshal.SizeOf(@params));
        if (result == false) throw new Win32Exception();
        result = NativeMethods.SetupDiCallClassInstaller(DiFunction.PropertyChange, handle, ref diData);
        if (result == false)
        {
            int err = Marshal.GetLastWin32Error();
            if (err == (int)SetupApiError.NotDisableable)
                throw new ArgumentException("Device can't be disabled (programmatically or in Device Manager).");
            else if (err >= (int)SetupApiError.NoAssociatedClass && err <= (int)SetupApiError.OnlyValidateViaAuthenticode)
                throw new Win32Exception("SetupAPI error: " + ((SetupApiError)err).ToString());
            else
                throw new Win32Exception();
        }
    }

The error appears in the if statement at the bottom on the line:

 throw new Win32Exception("SetupAPI error: " + ((SetupApiError)err).ToString());

System.ComponentModel.Win32Exception: 'SetupAPI error: InWow64'

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
10aples
  • 103
  • 7
  • 1
    some setup functions can work only in native subsystem, not in wow64. say for example [`UpdateDriverForPlugAndPlayDevices`](https://learn.microsoft.com/en-us/windows/desktop/api/newdev/nf-newdev-updatedriverforplugandplaydevicesw) about `SetupDiCallClassInstaller` look for https://stackoverflow.com/questions/8684061/setupdicallclassinstaller-throws-error-in-wow64-when-compiled-for-32-bit-on-a-64 - so only solution built 64bit code here – RbMm May 01 '19 at 19:04
  • I am runnng a 64 bit system, does this mean using Win32 is no good to me? – 10aples May 01 '19 at 19:32
  • 1
    yes, you need use 64binary only for call some api, such `SetupDiCallClassInstaller` or say `UpdateDriverForPlugAndPlayDevices` – RbMm May 01 '19 at 20:09
  • Thanks for your help, I will look at using it! – 10aples May 01 '19 at 20:29

0 Answers0