Ejemplo n.º 1
0
        // enable/disable...
        private static void EnableDevice(SafeDeviceInfoSetHandle handle, DeviceInfoData diData, bool enable)
        {
            var @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;
            @params.StateChange = enable ? StateChangeAction.Enable : StateChangeAction.Disable;

            var 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)
            {
                var 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();
                }
            }
        }
Ejemplo n.º 2
0
 public static extern bool SetupDiSetClassInstallParams(SafeDeviceInfoSetHandle deviceInfoSet,
                                                        [In] ref DeviceInfoData deviceInfoData,
                                                        [In] ref PropertyChangeParameters classInstallParams,
                                                        int classInstallParamsSize);