Beispiel #1
0
        /// <summary>
        /// Enable a device.
        /// </summary>
        /// <param name="classGuid">The class guid of the device. Available in the device manager.</param>
        /// <param name="instanceId">The device instance id of the device. Available in the device manager.</param>
        public static void EnableDevice(Guid classGuid, string instanceId)
        {
            SafeDeviceInfoSetHandle diSetHandle = null;

            try
            {
                // Get the handle to a device information set for all devices matching classGuid that are present on the system.
                diSetHandle = NativeMethods.SetupDiGetClassDevs(ref classGuid, null, IntPtr.Zero, SetupDiGetClassDevsFlags.Present);

                // Get the device information data for each matching device.
                DeviceInfoData[] diData = GetDeviceInfoData(diSetHandle);

                // Find the index of our instance.
                int index = GetIndexOfInstance(diSetHandle, diData, instanceId);

                // The size is just the size of the header, but we've flattened the structure.
                // The header comprises the first two fields, both integer.
                PropertyChangeParameters @params = new PropertyChangeParameters {
                    Size = 8, DiFunction = DiFunction.PropertyChange, Scope = Scopes.Global, StateChange = StateChangeAction.Enable
                };

                // Set Parameters
                bool result = NativeMethods.SetupDiSetClassInstallParams(diSetHandle, ref diData[index], ref @params, Marshal.SizeOf(@params));
                if (result == false)
                {
                    throw new Win32Exception();
                }

                // Enable Device
                result = NativeMethods.SetupDiCallClassInstaller(DiFunction.PropertyChange, diSetHandle, ref diData[index]);
                if (result == false)
                {
                    throw new ApplicationException("Failed to enable device!");
                }
            }
            finally
            {
                if (diSetHandle != null)
                {
                    if (diSetHandle.IsClosed == false)
                    {
                        diSetHandle.Close();
                    }
                    diSetHandle.Dispose();
                }
            }
        }
Beispiel #2
0
 internal static extern bool SetupDiSetClassInstallParams(SafeDeviceInfoSetHandle deviceInfoSet, [In] ref DeviceInfoData deviceInfoData, [In] ref PropertyChangeParameters classInstallParams, int classInstallParamsSize);