Пример #1
0
        /*
         * // TODO: Fix 1000=232 Bug
         * /// <summary>
         * /// Obtain the remaining number of Rearms for Microsoft Windows via DLL
         * /// </summary>
         * /// <returns>Remaining Rearm Count Number</returns>
         * public static int GetRearmCountDLL()
         * {
         *  // Microsoft Windows Application ID
         *  Guid windowsAppID = new Guid("55c92734-d682-4d71-983e-d6ec3f16059f");
         *
         *  // Handles
         *  IntPtr sppHandle = IntPtr.Zero;
         *
         *  // Open Handle to Microsoft Windows Software Licensing Service
         *  NativeMethods.SPPOpen(ref sppHandle);
         *
         *  // Get Microsoft Office Remaining Rearm Count from SPP
         *  uint descSize = 4;
         *  byte[] descBuffer = new byte[descSize];
         *  NativeMethods.SLDATATYPE slDataType = NativeMethods.SLDATATYPE.SL_DATA_DWORD;
         *  NativeMethods.SPPGetApplicationInformation(sppHandle, ref windowsAppID, "RemainingRearmCount", ref slDataType, ref descSize, ref descBuffer);
         *
         *  // Close Handle to Microsoft Windows Software Licensing Service
         *  NativeMethods.SPPClose(sppHandle);
         *
         *  // Return Rearm Count
         *  return descBuffer[0];
         * }
         */

        /// <summary>
        /// Rearm Microsoft Windows
        /// </summary>
        /// <returns>Output of Rearm Result and Any Errors</returns>
        public static string Rearm()
        {
            try
            {
                // Perform Rearm
                using (ManagementObject classInstance = new ManagementObject(@"root\CIMV2", "SoftwareLicensingService.Version='" + OSVersion.GetSPPSVCVersion() + "'", null))
                {
                    classInstance.InvokeMethod("ReArmWindows", null, null);
                }

                // Restart SPPSVC
                Services.StopSPPSVC();
                Services.StartSPPSVC();

                // On Windows 7 and Earlier, we don't need to reboot.
                if (OSVersion.GetWindowsNumber() < 6.2)
                {
                    return("<Microsoft Windows rearm successful.>");
                }
                return("<Microsoft Windows rearm successful.>" + Environment.NewLine + "You need to reboot for the rearm to take effect or Windows to work properly.");
            }
            catch (COMException ex)
            {
                return("<Microsoft Windows rearm failed>" + Environment.NewLine + LicenseErrorCode.GetErrorDescription("0x" + ex.ErrorCode.ToString("X8")));
            }
        }