-
Sep 23rd, 2023, 06:56 AM
#1
Thread Starter
Hyperactive Member
[RESOLVED] Get CPU id (Machine number) without any late bindings (Option strict = On)
The idea which is easily found all over the internet is as follows:
Code:
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
Which returns hexadecimal based 16 characters id that works just fine UNTIL you turn on the option strict. This function consists of 3 late binding errors.
Is there any ways to bypass the errors (at least turning them to warnings) or doing it in a different way?
The idea is not to limit users (like licensing) but to restrict them to run your app on a specific hardware. (Differs all-in-one-pcs, laptops, thinclients, minicomputers, various mainboard fabricators, etc.)
-
Sep 23rd, 2023, 07:08 AM
#2
Re: Get CPU id (Machine number) without any late bindings (Option strict = On)
You would need have .NET types that correspond to WMI types. My guess would be that there's a NuGet package for that.
-
Sep 23rd, 2023, 10:37 AM
#3
Re: Get CPU id (Machine number) without any late bindings (Option strict = On)
you have to set a Ref. to ->.NET->System.Management
Code:
Option Strict On
Imports System.Management
Public Class Form2
Private mosObj As New ManagementObjectSearcher("select * from Win32_Processor")
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
For Each moWMIInst As ManagementObject In mosObj.Get()
Debug.WriteLine(moWMIInst("Processorid").ToString())
'for all Data:
'For Each pdObj As PropertyData In moWMIInst.Properties
' Debug.WriteLine("{0}: {1}", pdObj.Name, If(pdObj.Value IsNot Nothing, pdObj.Value.ToString(), "n/a"))
'Next pdObj
Next moWMIInst
End Sub
End Class
to hunt a species to extinction is not logical !
since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.
-
Sep 25th, 2023, 12:32 AM
#4
Thread Starter
Hyperactive Member
Re: Get CPU id (Machine number) without any late bindings (Option strict = On)
 Originally Posted by ChrisE
you have to set a Ref. to ->.NET->System.Management[/CODE]
It worked just fine, I only changed the code to
Code:
Imports System.Management
Public Class Form2
Private mosObj As New ManagementObjectSearcher("select * from Win32_Processor")
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
For Each moWMIInst As ManagementObject In mosObj.Get()
Label1.Text = moWMIInst("Processorid").ToString()
Next moWMIInst
End Sub
End Class
No idea what your latter commented for loop does but thanks anyways. It is working as I expected.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|