Exemplo n.º 1
0
        public static void ReEnableDevice(string deviceInstanceId)
        {
            Guid hidGuid = new Guid();

            NativeMethods.HidD_GetHidGuid(ref hidGuid);
            IntPtr deviceInfoSet = NativeMethods.SetupDiGetClassDevs(ref hidGuid, deviceInstanceId, 0, NativeMethods.DIGCF_PRESENT | NativeMethods.DIGCF_DEVICEINTERFACE);

            NativeMethods.SP_DEVINFO_DATA deviceInfoData = new NativeMethods.SP_DEVINFO_DATA();
            deviceInfoData.cbSize = Marshal.SizeOf(deviceInfoData);

            if (!NativeMethods.SetupDiEnumDeviceInfo(deviceInfoSet, 0, ref deviceInfoData))
            {
                throw new Exception("Error getting device info data, error code = " + Marshal.GetLastWin32Error());
            }

            // Check that we have a unique device
            if (NativeMethods.SetupDiEnumDeviceInfo(deviceInfoSet, 1, ref deviceInfoData))
            {
                throw new Exception("Can't find unique device");
            }

            NativeMethods.SP_PROPCHANGE_PARAMS propChangeParams = new NativeMethods.SP_PROPCHANGE_PARAMS();
            propChangeParams.classInstallHeader.cbSize          = Marshal.SizeOf(propChangeParams.classInstallHeader);
            propChangeParams.classInstallHeader.installFunction = NativeMethods.DIF_PROPERTYCHANGE;
            propChangeParams.stateChange = NativeMethods.DICS_DISABLE;
            propChangeParams.scope       = NativeMethods.DICS_FLAG_GLOBAL;
            propChangeParams.hwProfile   = 0;

            if (!NativeMethods.SetupDiSetClassInstallParams(deviceInfoSet, ref deviceInfoData, ref propChangeParams, Marshal.SizeOf(propChangeParams)))
            {
                throw new Exception("Error setting class install params, error code = " + Marshal.GetLastWin32Error());
            }

            NativeMethods.SetupDiCallClassInstaller(NativeMethods.DIF_PROPERTYCHANGE, deviceInfoSet, ref deviceInfoData);
            // TEST: If previous SetupDiCallClassInstaller fails, just continue
            // otherwise device will likely get permanently disabled.

            /*if (!success)
             * {
             *  throw new Exception("Error disabling device, error code = " + Marshal.GetLastWin32Error());
             * }
             */

            Stopwatch.Restart();
            while (Stopwatch.ElapsedMilliseconds < 50)
            {
                // Use SpinWait to keep control of current thread. Using Sleep could potentially
                // cause other events to get run out of order
                System.Threading.Thread.SpinWait(100);
            }
            Stopwatch.Stop();

            propChangeParams.stateChange = NativeMethods.DICS_ENABLE;

            if (!NativeMethods.SetupDiSetClassInstallParams(deviceInfoSet, ref deviceInfoData, ref propChangeParams, Marshal.SizeOf(propChangeParams)))
            {
                throw new Exception("Error setting class install params, error code = " + Marshal.GetLastWin32Error());
            }

            if (!NativeMethods.SetupDiCallClassInstaller(NativeMethods.DIF_PROPERTYCHANGE, deviceInfoSet, ref deviceInfoData))
            {
                throw new Exception("Error enabling device, error code = " + Marshal.GetLastWin32Error());
            }

            Stopwatch.Restart();
            while (Stopwatch.ElapsedMilliseconds < 50)
            {
                // Use SpinWait to keep control of current thread. Using Sleep could potentially
                // cause other events to get run out of order
                System.Threading.Thread.SpinWait(100);
            }
            Stopwatch.Stop();

            NativeMethods.SetupDiDestroyDeviceInfoList(deviceInfoSet);
        }