Windows 10 Activation Status with VB.Net

~OSD~ 2,151 Reputation points
2021-03-11T14:03:45.66+00:00

Hi,

Considering to check if Windows 10 is Activated or not using VB.Net with SoftwareLicensingProduct.
All I need to check if Windows is activated "Message Box: YES" else "Message Box: NO".
OR there exists better approach?
Currently, I working with following sample code and I can't figure it out why I am getting three message boxes with the message that windows is activated.

Try  
            Dim searcher As New ManagementObjectSearcher(  
                  "root\CIMV2",  
                  "SELECT * FROM SoftwareLicensingProduct WHERE LicenseStatus = 1")  
            Dim myCollection As ManagementObjectCollection  
            Dim myObject As ManagementObject  
            myCollection = searcher.Get()  
            If myCollection.Count = 0 Then  
                MsgBox("Windows is not activated")  
                searcher.Dispose()  
            Else  
                For Each myObject In myCollection  
                    MsgBox("Windows is activated")  
                    searcher.Dispose()  
                Next  
            End If  
            searcher.Dispose()  
        Catch ex As Exception  
            MsgBox(ex.Message, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly)  
        End Try  

Ref: https://learn.microsoft.com/en-us/previous-versions/windows/desktop/sppwmi/softwarelicensingproduct
76787-image.png

VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,662 questions
0 comments No comments
{count} votes

Accepted answer
  1. Castorix31 83,106 Reputation points
    2021-03-12T11:27:35.67+00:00

    A test with the method used by Windows =>
    (add a Button for the click)

    Public Class Form1
    
        Public Enum HRESULT As Integer
            S_OK = 0
            S_FALSE = 1
            E_NOINTERFACE = &H80004002
            E_NOTIMPL = &H80004001
            E_FAIL = &H80004005
            E_UNEXPECTED = &H8000FFFF
            E_OUTOFMEMORY = &H8007000E
        End Enum
    
        <DllImport("Slc.dll", SetLastError:=True, CharSet:=CharSet.Unicode)>
        Public Shared Function SLOpen(ByRef phSLC As IntPtr) As HRESULT
        End Function
    
        <DllImport("Slc.dll", SetLastError:=True, CharSet:=CharSet.Unicode)>
        Public Shared Function SLClose(phSLC As IntPtr) As HRESULT
        End Function
    
        <DllImport("Slc.dll", SetLastError:=True, CharSet:=CharSet.Unicode)>
        Public Shared Function SLGetLicensingStatusInformation(hSLC As IntPtr, pAppID As IntPtr, pProductSkuId As IntPtr, pwszRightName As String,
                                                               ByRef pnStatusCount As UInteger, ByRef ppLicensingStatus As IntPtr) As HRESULT
        End Function
    
        <StructLayout(LayoutKind.Sequential)>
        Public Structure SL_LICENSING_STATUS
            Public SkuId As Guid ' SKU id
            Public eStatus As SLLICENSINGSTATUS '  licensing status, see SLLICENSINGSTATUS
            Public dwGraceTime As UInteger ' grace time In minute
            Public dwTotalGraceDays As UInteger '  pre-defined grace days In license
            Public hrReason As HRESULT ' the Error Of unlicensed status
            Public qwValidityExpiration As UInt64 ' Validity expiration day
        End Structure
    
        Public Enum SLLICENSINGSTATUS
            SL_LICENSING_STATUS_UNLICENSED
            SL_LICENSING_STATUS_LICENSED
            SL_LICENSING_STATUS_IN_GRACE_PERIOD
            SL_LICENSING_STATUS_NOTIFICATION
            SL_LICENSING_STATUS_LAST
        End Enum
    
        <DllImport("Slc.dll", SetLastError:=True, CharSet:=CharSet.Unicode)>
        Public Shared Function SLGetInstalledProductKeyIds(hSLC As IntPtr, ByRef pProductSkuId As Guid, ByRef pnProductKeyIds As UInteger, ByRef ppProductKeyIds As IntPtr) As HRESULT
        End Function
    
        Public WINDOWS_SLID As New Guid("55C92734-D682-4D71-983E-D6EC3F16059F")
    
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim hr As HRESULT = CheckWindowsActivation()
            If (hr = HRESULT.S_OK) Then
                MessageBox.Show("Windows is activated", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information)
            Else
                MessageBox.Show("Windows is not activated", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information)
            End If
    
        End Sub
    
        Private Function CheckWindowsActivation() As HRESULT
            Dim hSLC As IntPtr = IntPtr.Zero
            Dim hr As HRESULT = SLOpen(hSLC)
            If (hr = HRESULT.S_OK) Then
                Dim nStatusCount As UInteger = 0
                Dim pSL_LICENSING_STATUS As IntPtr = IntPtr.Zero
                Dim pLastStatus As SL_LICENSING_STATUS = New SL_LICENSING_STATUS()
                Dim pGuid As IntPtr = IntPtr.Zero
                pGuid = Marshal.AllocHGlobal(Marshal.SizeOf(WINDOWS_SLID))
                Marshal.StructureToPtr(WINDOWS_SLID, pGuid, False)
                hr = SLGetLicensingStatusInformation(hSLC, pGuid, IntPtr.Zero, Nothing, nStatusCount, pSL_LICENSING_STATUS)
                ' hr = &HC004F015 SL_E_PRODUCT_SKU_NOT_INSTALLED
                If (hr = HRESULT.S_OK) Then
                    Dim nStructSize As Integer = Marshal.SizeOf(GetType(SL_LICENSING_STATUS))
                    Dim lStatus(nStatusCount) As SL_LICENSING_STATUS
                    For i As Integer = 0 To nStatusCount - 1
                        lStatus(i) = Marshal.PtrToStructure(New IntPtr(pSL_LICENSING_STATUS.ToInt32() + (nStructSize * i)), GetType(SL_LICENSING_STATUS))
                        Dim nPKeyIds As UInteger = 0
                        Dim pPKeyIds As IntPtr = IntPtr.Zero
                        hr = SLGetInstalledProductKeyIds(hSLC, lStatus(i).SkuId, nPKeyIds, pPKeyIds)
                        If (hr = HRESULT.S_OK And nPKeyIds <> 0) Then
                            ' {E0C42288-980C-4788-A014-C080D2E1926E} Windows 10 Education
                            pLastStatus = lStatus(i)
                        End If
                    Next
                    If (pLastStatus.eStatus = SLLICENSINGSTATUS.SL_LICENSING_STATUS_LICENSED) Then
                        hr = HRESULT.S_OK
                    Else
                        hr = HRESULT.S_FALSE
                    End If
                End If
                Marshal.FreeHGlobal(pGuid)
                SLClose(hSLC)
            End If
            Return hr
        End Function
    End Class
    
    0 comments No comments

3 additional answers

Sort by: Most helpful
  1. Hauck, Oliver 1 Reputation point
    2021-03-11T14:13:08.767+00:00

    The reason you get the message box thrice is that the collection that your query returns contains three items (three SoftwareLicensingProducts) and you loop through each of these items.
    You could amend your messagebox like this to also include the name of the activated product: MsgBox(myObject.Name & " is activated")

    best regards
    Oliver


  2. Xingyu Zhao-MSFT 5,361 Reputation points
    2021-03-12T02:43:13.173+00:00

    Hi @~OSD~ ,
    In order to check if Windows 10 activation status, you can try the following code:

            Dim strCmdText As String = "/C slmgr /xpr"  
            System.Diagnostics.Process.Start("CMD.exe", strCmdText)  
    

    You can use 'slmgr /xpr' command to see if Windows is activated, and you will see a message that says Windows is activated if it is, or be given a date if it's not permanently activated, or see an error if no product key has been provided.

    Best Regards,
    Xingyu Zhao
    *
    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


  3. ~OSD~ 2,151 Reputation points
    2021-03-15T19:39:15.407+00:00

    Is it possible to read the output of the SLMGR.VBS /XPR?

    77915-image.png

    Then search for the text if activated show dialog1 else show dialog2.?