[Visual Basic] Fastest way to check if OS is Activated?

Discussion in 'Mixed Languages' started by TheRavenBlue, Jul 30, 2017.

  1. TheRavenBlue

    TheRavenBlue MDL Novice

    Jul 28, 2017
    5
    19
    0
    #1 TheRavenBlue, Jul 30, 2017
    Last edited: Jul 30, 2017
    Hi!

    So I'm currently working on a tool called WinTool, I have ran into a problem.
    I am checking if Windows is activated using this code:
    Here is a GIF of it freezing when I load the form where I run the "check" code:
    gyazo.com/572aa9579cfc7d206fe9046ce12fe94b


    Code:
        Public Function WindowsStatus() As String
            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
                    Return ("Windows Is Not Activated")
                    searcher.Dispose()
                Else
                    For Each myObject In myCollection
                        Return ("Windows Is Activated")
                        searcher.Dispose()
                    Next
                End If
                searcher.Dispose()
            Catch ex As Exception
                MsgBox(ex.Message, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly)
            End Try
        End Function
    It does the job, but there is an issue and that is that the program freezes for a short while (like 1 or 2 seconds).
    Is there a faster way to check if Windows is activated without freezing?
    Note: I'm trying to learn VB.Net so feel free to leave reference to where you can find a solution to the problem so I can check them out!


    Thank you!


    EDIT: I solved it, I just made a splash screen and loaded all info into there!
     
  2. Michaela Joy

    Michaela Joy MDL Crazy Lady

    Jul 26, 2012
    3,645
    3,941
    120
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  3. QuantumBug

    QuantumBug MDL Developer

    Mar 7, 2012
    1,485
    1,322
    60
    #3 QuantumBug, Sep 27, 2017
    Last edited: Sep 27, 2017
    Here is a better method for you, I wrote this up ages ago.

    Code:
        Public Function getlicensestatuswmi() As String
    
            Dim s As String = String.Empty
    
            Try
                Dim objMgmt As System.Management.ManagementObject
                Dim LicenseStatus As New System.Management.ManagementObjectSearcher("root\CIMV2", "SELECT LicenseStatus FROM SoftwareLicensingProduct WHERE PartialProductKey <> null AND ApplicationId='55c92734-d682-4d71-983e-d6ec3f16059f'")
                For Each objMgmt In LicenseStatus.Get
                    Select Case objMgmt("LicenseStatus")
                        Case "0"
                            s = "Unlicensed"
                        Case "1"
                            s = "Licensed"
                        Case "2"
                            s = "Out-Of-Box Grace"
                        Case "3"
                            s = "Out-Of-Tolerance Grace"
                        Case "4"
                            s = "Non Genuine Grace"
                        Case "5"
                            s = "Notification"
                        Case "6"
                            s = "Extended Grace"
                        Case Else
                            s = "Unknown License Status"
                    End Select
                Next
            Catch
                s = "Error getting License Status"
            End Try
            Return s.ToString()
        End Function
    
    This should also be quicker, the more you use "SELECT * FROM", the more work each routine needs to do.

    Regards, QB.
     
  4. MugiwaraDono

    MugiwaraDono MDL Novice

    Dec 1, 2017
    1
    0
    0
    I am surprised people are still coding in VB.net nowadays. You should do something like C# or Java.
     
  5. Alphawaves

    Alphawaves Super Moderator/Developer
    Staff Member

    Aug 11, 2008
    5,891
    20,335
    180

    Code:
    public bool IsLicensed(bool Licensed = false)
       {
           try
           {              
               foreach (ManagementObject Obj in new ManagementObjectSearcher("root\\CIMV2", "SELECT LicenseStatus FROM SoftwareLicensingProduct WHERE LicenseStatus = 1").Get())
               {
                   Licensed = true;
               }
           }
           catch (ManagementException) { Licensed = false; }
           return Licensed;
       }
    Usage:
    Code:
    if(IsLicenced())
               MessageBox.Show("Windows is Licensed");
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...