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!
Glad you got it sorted. I thought I'd post these anyway, just because there are some good thoughts there about checking for Windows activation. https://stackoverflow.com/questions/1552392/determine-genuine-windows-installation-in-c-sharp and https://stackoverflow.com/questions/18717389/detect-with-c-sharp-if-windows-os-has-invalid-license Hopefully, they will help.
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.
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");