Public Class Form1 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load lblSystemSerialNumbers.Text = SystemSerialNumber() lblCpuIds.Text = CpuId() End Sub Private Function SystemSerialNumber() As String ' Get the Windows Management Instrumentation object. Dim wmi As Object = GetObject("WinMgmts:") ' Get the "base boards" (mother boards). Dim serial_numbers As String = "" Dim mother_boards As Object = wmi.InstancesOf("Win32_BaseBoard") For Each board As Object In mother_boards serial_numbers &= ", " & board.SerialNumber Next board If serial_numbers.Length > 0 Then serial_numbers = serial_numbers.Substring(2) Return serial_numbers End Function Private Function CpuId() As String Dim computer As String = "." Dim wmi As Object = GetObject("winmgmts:" & _ "{impersonationLevel=impersonate}!\\" & _ computer & "\root\cimv2") Dim processors As Object = wmi.ExecQuery("Select * from Win32_Processor") Dim cpu_ids As String = "" For Each cpu As Object In processors cpu_ids = cpu_ids & ", " & cpu.ProcessorId Next cpu If cpu_ids.Length > 0 Then cpu_ids = cpu_ids.Substring(2) Return cpu_ids End Function End Class
This code will retrieve the CPU ID and motherboard ID. How can I guarantee that this will work even if the strict option is enabled.
Why could this be a problem?
OK, we will see. Type wmi - Object. This wmi does not necessarily support methods such as InstancesOf and SerialNumber.
So how can we get this out?
I think the object we got from GetObject is not just a clean object. I think we need ctype or direct to apply it to a more suitable type. This more suitable type will support methods such as InstancesOf, SerialNumber, etc.
However, what are the relevant types?
source share