[WMI] Help

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

  1. flare4000

    flare4000 MDL Senior Member

    Apr 23, 2010
    414
    114
    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
     
  2. Calistoga

    Calistoga MDL Senior Member

    Jul 25, 2009
    421
    199
    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
    114
    10
    I keep getting COM error
     
  4. CODYQX4

    CODYQX4 MDL Developer

    Sep 4, 2009
    4,813
    45,775
    150
    #4 CODYQX4, Dec 7, 2010
    Last edited: Apr 15, 2019
    .
     
  5. CODYQX4

    CODYQX4 MDL Developer

    Sep 4, 2009
    4,813
    45,775
    150
    #5 CODYQX4, Dec 7, 2010
    Last edited: Apr 15, 2019
    .
     
  6. flare4000

    flare4000 MDL Senior Member

    Apr 23, 2010
    414
    114
    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
     
  7. CODYQX4

    CODYQX4 MDL Developer

    Sep 4, 2009
    4,813
    45,775
    150
    #7 CODYQX4, Dec 7, 2010
    Last edited: Apr 15, 2019
    .
     
  8. flare4000

    flare4000 MDL Senior Member

    Apr 23, 2010
    414
    114
    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
     
  9. CODYQX4

    CODYQX4 MDL Developer

    Sep 4, 2009
    4,813
    45,775
    150
    #9 CODYQX4, Dec 7, 2010
    Last edited: Apr 15, 2019
    .
     
  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.