Ejemplo n.º 1
0
        public static void EnableDevice(IntPtr handle, SP_DEVINFO_DATA diData, bool enable)
        {
            var parameters = 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.
            parameters.Size        = 8;
            parameters.DiFunction  = DiFunction.PropertyChange;
            parameters.Scope       = Scopes.Global;
            parameters.StateChange = enable ? StateChangeAction.Enable : StateChangeAction.Disable;

            if (!NativeMethods.SetupDiSetClassInstallParams(handle, ref diData, ref parameters, Marshal.SizeOf(parameters)))
            {
                throw new Win32Exception(Marshal.GetLastWin32Error());
            }

            if (!NativeMethods.SetupDiCallClassInstaller(DiFunction.PropertyChange, handle, ref diData))
            {
                var err = Marshal.GetLastWin32Error();
                if (err == (int)SetupApiError.NotDisableable)
                {
                    throw new ArgumentException("Device can't be disabled (programmatically or in Device Manager).");
                }
                if (err >= (int)SetupApiError.NoAssociatedClass && err <= (int)SetupApiError.OnlyValidateViaAuthenticode)
                {
                    throw new Win32Exception("SetupAPI error: " + (SetupApiError)err);
                }
                throw new Win32Exception(err);
            }
        }
Ejemplo n.º 2
0
            // enable/disable...
            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)
                {
                    string errorMessage = new Win32Exception(Marshal.GetLastWin32Error()).Message;
                    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();
                    }
                }
            }
Ejemplo n.º 3
0
        private static void EnableDevice(SafeDeviceInfoSetHandle handle, DeviceInfoData diData, bool enable)
        {
            PropertyChangeParameters propertyChangeParameters = new PropertyChangeParameters
            {
                Size        = 8,
                DiFunction  = DiFunction.PropertyChange,
                Scope       = Scopes.Global,
                StateChange =
                    enable
                                                                            ? StateChangeAction.Enable
                                                                            : StateChangeAction.Disable
            };

            // Set the parameters
            bool result = Win32.SetupDiSetClassInstallParams(handle, ref diData, ref propertyChangeParameters,
                                                             Marshal.SizeOf(propertyChangeParameters));

            if (result == false)
            {
                throw new Win32Exception();
            }

            // Apply the parameters
            result = Win32.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).");
                }
                if (err <= ( int )SetupApiError.NoAssociatedClass &&
                    err >= ( int )SetupApiError.OnlyValidateViaAuthenticode)
                {
                    throw new Win32Exception("SetupAPI error: " + (( SetupApiError )err));
                }
                throw new Win32Exception();
            }
        }
 public static extern bool SetupDiSetClassInstallParams(SafeDeviceInfoSetHandle deviceInfoSet,
                                                        [In()] ref DeviceInfoData deviceInfoData, [In()] ref PropertyChangeParameters classInstallParams,
                                                        int classInstallParamsSize);
Ejemplo n.º 5
0
 internal static extern bool SetupDiSetClassInstallParams(IntPtr deviceInfoSet, [In] ref SP_DEVINFO_DATA deviceInfoData,
                                                          [In] ref PropertyChangeParameters classInstallParams, int classInstallParamsSize);