[WMI] Help

Discussion in 'Mixed Languages' started by flare4000, Nov 24, 2010.

  1. flare4000

    flare4000 MDL Senior Member

    Apr 23, 2010
    414
    111
    10
    #1 flare4000, Nov 24, 2010
    Last edited: Nov 24, 2010
    Hello I am trying to use wmi code for my app and would like to know if there is an activate command similar to the slmgr - ato command for windows?

    Thanks

    Flare4000
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  2. Calistoga

    Calistoga MDL Senior Member

    Jul 25, 2009
    420
    198
    10
    I haven't used any of these methods before, but it might be what you want:
    [​IMG]

    WMI Code Generator
     
  3. flare4000

    flare4000 MDL Senior Member

    Apr 23, 2010
    414
    111
    10
    I keep getting COM error
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  4. CODYQX4

    CODYQX4 MDL Developer

    Sep 4, 2009
    4,804
    45,138
    150
    #4 CODYQX4, Dec 7, 2010
    Last edited: Dec 7, 2010
    It requires an ID which shows in the bottom, but some of them are for key types windows can use but not necessarily using (As in one could be Retail but you are using OEM).

    I'm trying to do this in AutoKMS so I'm trying to find a way to figure out which ID is active.

    Let me see if I can get mine working as we want the same thing. It will be in C# though so you'll have to port the code if you need to.

    I'm making it like I WMI activate Office 2010.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  5. CODYQX4

    CODYQX4 MDL Developer

    Sep 4, 2009
    4,804
    45,138
    150
    #5 CODYQX4, Dec 7, 2010
    Last edited by a moderator: Apr 20, 2017
    OK you get to see my AutoKMS for Windows code

    Code:
    public void ActivateKMSWindows()
            {
                ManagementObject classInstance;
    
                // Get Windows Version To Pass It To Future proof code from version changes (Like Service Packs)
                string WinVersion = string.Empty;
                try
                {
                    ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM SoftwareLicensingService");
    
                    foreach (ManagementObject queryObj in searcher.Get())
                    {
                        WinVersion = Convert.ToString(queryObj["Version"]);
                    }
                }
                catch (Exception)
                {
                    logWrite = "Failed To WMI Query Windows Version! Maybe this version of Windows isn't supported?";
                    log.Write(logWrite);
                }            
    
                // Set Windows KMS Host
                try
                {
                    classInstance = new ManagementObject("root\\CIMV2", "SoftwareLicensingService.Version='" + WinVersion + "'", null);
    
                    // Obtain in-parameters for the method
                    ManagementBaseObject inParams = classInstance.GetMethodParameters("SetKeyManagementServiceMachine");
    
                    // Add the input parameters.
                    inParams["MachineName"] = "127.0.0.1";
    
                    // Execute the method and obtain the return values.
                    ManagementBaseObject outParams = classInstance.InvokeMethod("SetKeyManagementServiceMachine", inParams, null);
    
                    logWrite = "Set Windows KMS Host";
                    log.Write(logWrite);
                }
                catch (Exception)
                {
                    logWrite = "Failed To Set Windows KMS Host! Maybe this version of Windows isn't supported?";
                    log.Write(logWrite);
                }
    
                // Determine the SKUID of the active Windows license (We need it to call Activate via WMI)
                string SKUID = string.Empty;
                try
                {
                    ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM SoftwareLicensingProduct");
    
                    foreach (ManagementObject queryObj in searcher.Get())
                    {
                        SKUID = Convert.ToString(queryObj["ID"]);
                        int LicStatus = Convert.ToInt32(queryObj["LicenseStatus"]);
    
                        // Zero means the license is not active, 1 means it is. Only 1 Windows key should be active so stop the loop when we don't get a zero.
                        if (LicStatus != 0)
                        {
                            break;
                        }
                    }
                }
                catch (Exception)
                {
                    logWrite = "Failed To Query Windows License Status! Maybe this version of Windows isn't supported?";
                    log.Write(logWrite);
                }
    
                // Activate (Uses Loop to retry for KMS)
                int loop = 1;
                bool success = false;
    
                classInstance = new ManagementObject("root\\CIMV2", "SoftwareLicensingProduct.ID='" + SKUID + "'", null);
                while (loop < (Settings.AutoKMSSettings.ActAttempts + 1) && success == false)
                {
                    // Execute the method and obtain the return values.
                    try
                    {
                        ManagementBaseObject outParams = classInstance.InvokeMethod("Activate", null, null);
                        success = true;
                    }
                    catch (Exception)
                    {
                        loop++;
                    }
                }
                if (success == false)
                {
                    logWrite = logWrite + Environment.NewLine + "Failed To Activate " + Settings.AutoKMSSettings.ActAttempts + " Times.";                
                }
                else
                {
                    logWrite = logWrite + Environment.NewLine + "Successfully activated after " + loop + " attempts.";                
                }
    
                if (Settings.AutoKMSSettings.AutoRemoveKMSHost == true)
                {
                    try
                    {
                        classInstance.InvokeMethod("ClearKeyManagementServiceMachine", null, null);
                        logWrite = "Removed Windows KMS Host";
                        log.Write(logWrite);
                    }
                    catch (Exception)
                    {
                        logWrite = "Failed To Remove Windows KMS Host! Maybe this version of Windows isn't supported?";
                        log.Write(logWrite);
                    }
                }
            }
    The log stuff won't work out of the box as it's something AutoKMS handles. This code in C# compiled fine for me but I did not test it.

    Let me know if it works as you'll be helping me test it as I'm using that function inside the upcoming AutoKMS to activate Windows. It's geared toward KMS but should attempt activation on anything because it should figure out which license Windows is using. I'd edit it to eliminate the loop if it works as attempting online retail activation with a blocked key 10 times would be time consuming and fail. As I said it should work but it's built to handle KMS best (it adds and removes localhost as KMS for Windows).

    EDIT: I confirm it works, but you will have to edit it as it is dependent on AutoKMS and that loop is only good for KMS (you don't want non-KMS activation in a loop)
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  6. flare4000

    flare4000 MDL Senior Member

    Apr 23, 2010
    414
    111
    10
    #6 flare4000, Dec 7, 2010
    Last edited by a moderator: Apr 20, 2017
    (OP)
    I manage to covert most of the code to vb.net although I can't get the loop to work. Here is vb.net
    Code:
        Dim classInstance As ManagementObject
            Dim outParams As ManagementBaseObject
            Dim inParams As ManagementBaseObject
            ' Get Windows Version To Pass It To Future proof code from version changes (Like Service Packs)
            Dim WinVersion As String = String.Empty
            Try
                Dim searcher As New ManagementObjectSearcher("root\CIMV2", "SELECT * FROM SoftwareLicensingService")
    
                For Each queryObj As ManagementObject In searcher.[Get]()
                    WinVersion = Convert.ToString(queryObj("Version"))
                Next
            Catch generatedExceptionName As Exception
            End Try
            ' Set Windows KMS Host
            Try
                classInstance = New ManagementObject("root\CIMV2", "SoftwareLicensingService.Version='" + WinVersion + "'", Nothing)
    
                ' Obtain in-parameters for the method
                inParams = classInstance.GetMethodParameters("SetKeyManagementServiceMachine")
    
                ' Add the input parameters.
                inParams("MachineName") = "127.0.0.1"
            Catch Err As Exception
    
            End Try
            ' Execute the method and obtain the return values.
            Try
                outParams = classInstance.InvokeMethod("SetKeyManagementServiceMachine", inParams, Nothing)
            Catch generatedExceptionName As Exception
            End Try
            ' Determine the SKUID of the active Windows license (We need it to call Activate via WMI)
            Dim SKUID As String = String.Empty
            Try
                Dim searcher As New ManagementObjectSearcher("root\CIMV2", "SELECT * FROM SoftwareLicensingProduct")
    
                For Each queryObj As ManagementObject In searcher.[Get]()
                    SKUID = Convert.ToString(queryObj("ID"))
                    Dim LicStatus As Integer = Convert.ToInt32(queryObj("LicenseStatus"))
    
                    ' Zero means the license is not active, 1 means it is. Only 1 Windows key should be active so stop the loop when we don't get a zero.
                    If LicStatus <> 0 Then
                        Exit For
                    End If
                Next
            Catch generatedExceptionName As Exception
            End Try
    
            ' Activate (Uses Loop to retry for KMS)
            Dim [loop] As Integer = 1
            Dim success As Boolean = False
    
            classInstance = New ManagementObject("root\CIMV2", "SoftwareLicensingProduct.ID='" + SKUID + "'", Nothing)
            outParams = classInstance.InvokeMethod("Activate", Nothing, Nothing)
            success = True
            classInstance.InvokeMethod("ClearKeyManagementServiceMachine", Nothing, Nothing)
        End Sub
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  7. CODYQX4

    CODYQX4 MDL Developer

    Sep 4, 2009
    4,804
    45,138
    150
    I don't know VB but I would get rid of the loop. It was heavily tied to toolkit settings and looping activation is only recommended for KMS, and you could put it in your own loop if needed in that case or change things around so it does work. I don't see any remaining toolkit specific code there that would break it but I don't know VB too well to see if this works.

    Also, when doing KMS activation, this assumes you are running KMS Keygen already. Another thing is I hardcoded 127.0.0.1 for KMS Host here but AutoKMS will read it from ini file (Which is 127.0.0.1 unless using a real server anyway).

    Are you using this in the AfterBurner thing you made?
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  8. flare4000

    flare4000 MDL Senior Member

    Apr 23, 2010
    414
    111
    10
    Ya I'm trying to eliminate the use of slmgr I might just use your code for the loop and convert the 50% of the kms in my app to C# lol since the loop is important for kms
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  9. CODYQX4

    CODYQX4 MDL Developer

    Sep 4, 2009
    4,804
    45,138
    150
    I did the same thing since I didn't want to use ospp.vbs or slmgr.vbs, which just use WMI in the first place.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  10. ileve

    ileve MDL Novice

    May 31, 2010
    3
    0
    0
    Your code is very enlightening. You use
    Code:
    Settings.AutoKMSSettings
    which comes from another library, i guess. Is it also available in C# somewhere? I searched but i didn't find anything.