samrocketman / ekeyfinder

Archive of my sourceforge project Enchanted Keyfinder. Software key retrieval on Windows.
http://ekeyfinder.sourceforge.net/
GNU General Public License v3.0
61 stars 14 forks source link

Change Product Key in Windows Vista and Windows 7 #11

Open samrocketman opened 8 years ago

samrocketman commented 8 years ago

There is a VBScript built into Windows Vista and 7 which allows command line interaction with changing the product key. C:\WINDOWS\system32\slmgr.vbs

More information about it: http://technet.microsoft.com/en-us/library/ff793433.aspx

I think we should use portions of it to change the product key similar to how the product key is changed in Windows XP within keyfinder (also uses VBScript).

Though it might be wiser to just reference the original VBScript without keyfinder creating one at all.

Migrated from https://sourceforge.net/p/ekeyfinder/feature-requests/5/

samrocketman commented 8 years ago

InstallProductKey function from slmgr.vbs from WinVista and Win7.

' Method for Windows Vista
Private Sub InstallProductKey(strProductKey)
    Dim objService, objProduct
    Dim lRet, strDescription, strOutput

    For Each objService in g_objWMIService.InstancesOf(ServiceClass)
        On Error Resume Next
        objService.InstallProductKey(strProductKey)
        For Each objProduct in g_objWMIService.InstancesOf(ProductClass)
            If (objProduct.PartialProductKey <> "") Then
                strDescription = objProduct.Description
                If IsKmsServer(strDescription) Then
                    ' Set the KMS version in the registry
                    lRet = SetRegistryStr(HKEY_LOCAL_MACHINE, SLKeyPath, "KeyManagementServiceVersion", objService.Version)
                    If (lRet <> 0) Then
                        QuitWithError CStr(Hex(lRet))
                    End If
                Else
                    lRet = DeleteRegistryValue(HKEY_LOCAL_MACHINE, SLKeyPath, "KeyManagementServiceVersion")
                    If (lRet <> 0 And lRet <> 2 And lRet <> 5) Then
                        QuitWithError CStr(Hex(lRet))
                    End If
                End If
            End If
        Next
        QuitIfError()
    Next

    strOutput = Replace(GetResource("L_MsgInstalledPKey"), "%PKEY%", strProductKey)
    LineOut strOutput
End Sub

' Method for Windows 7
Private Sub InstallProductKey(strProductKey)
    Dim objService, objProduct
    Dim lRet, strDescription, strOutput, strVersion
    Dim iIsPrimaryWindowsSku, bIsKMS

    bIsKMS = False

    On Error Resume Next

    set objService = GetServiceObject("Version")
    strVersion = objService.Version
    objService.InstallProductKey(strProductKey)
    QuitIfError()

    ' Installing a product key could change Windows licensing state. 
    ' Since the service determines if it can shut down and when is the next start time
    ' based on the licensing state we should reconsume the licenses here.    
    objService.RefreshLicenseStatus()

    For Each objProduct in GetProductCollection(ProductIsPrimarySkuSelectClause, PartialProductKeyNonNullWhereClause)
        strDescription = objProduct.Description

        iIsPrimaryWindowsSku = GetIsPrimaryWindowsSKU(objProduct)
        If (iIsPrimaryWindowsSku = 2) Then
            OutputIndeterminateOperationWarning(objProduct)
        End If

        If IsKmsServer(strDescription) Then
            bIsKMS = True
            Exit For
        End If
    Next

    If (bIsKMS = True) Then
        ' Set the KMS version in the registry (64 and 32 bit versions)
        lRet = SetRegistryStr(HKEY_LOCAL_MACHINE, SLKeyPath, "KeyManagementServiceVersion", strVersion)
        If (lRet <> 0) Then
            QuitWithError Hex(lRet)
        End If

        If ExistsRegistryKey(HKEY_LOCAL_MACHINE, SLKeyPath32) Then
            lRet = SetRegistryStr(HKEY_LOCAL_MACHINE, SLKeyPath32, "KeyManagementServiceVersion", strVersion)
            If (lRet <> 0) Then
                QuitWithError Hex(lRet)
            End If
        End If
    Else
        ' Clear the KMS version in the registry (64 and 32 bit versions)
        lRet = DeleteRegistryValue(HKEY_LOCAL_MACHINE, SLKeyPath, "KeyManagementServiceVersion")
        If (lRet <> 0 And lRet <> 2 And lRet <> 5) Then
            QuitWithError Hex(lRet)
        End If

        lRet = DeleteRegistryValue(HKEY_LOCAL_MACHINE, SLKeyPath32, "KeyManagementServiceVersion")
        If (lRet <> 0 And lRet <> 2 And lRet <> 5) Then
            QuitWithError Hex(lRet)
        End If
    End If

    strOutput = Replace(GetResource("L_MsgInstalledPKey"), "%PKEY%", strProductKey)
    LineOut strOutput
End Sub