How can we make this code work with the strict on option?

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?

0
source share
1 answer

You can use ManagementObjectSearcher from the WMI classes located inside the System.Management.dll assembly.
(And you need to add the appropriate link).

So you can write SystemSerialNumber as

 Private Function SystemSerialNumber(computer As String) As String Dim wmi = New ManagementObjectSearcher(computer & "\root\cimv2", "select * from Win32_BaseBoard") Dim boards = New List(Of String)() For Each board In wmi.Get() Dim temp = board.Properties("SerialNumber").Value?.ToString() If Not String.IsNullOrEmpty(temp) Then boards.Add(temp) End If Next board Return String.Join(", ", boards) End Function 

The CpuId function is a little more complicated because you want to set the Impersonate flags, but you can still write a method around the NET shell classes for WMI interfaces.

 Private Function CpuId(computer As String) As String Dim cpu = New List(Of String)() Dim options = New ConnectionOptions() options.Impersonation = System.Management.ImpersonationLevel.Impersonate Dim scope = New ManagementScope(computer & "\root\cimv2", options) scope.Connect() Dim query = New ObjectQuery("Select * from Win32_Processor") Dim wmi = New ManagementObjectSearcher(scope, query) For Each m As ManagementObject In wmi.Get() Dim temp = m.Properties("ProcessorId").Value?.ToString() If Not String.IsNullOrEmpty(temp) Then cpu.Add(temp) End If Next Return String.Join(", ", cpu) End Function 
+2
source

Source: https://habr.com/ru/post/1303129/


All Articles