Results 1 to 4 of 4

Thread: [RESOLVED] Get CPU id (Machine number) without any late bindings (Option strict = On)

  1. #1

    Thread Starter
    Hyperactive Member pourkascheff's Avatar
    Join Date
    Apr 2020
    Location
    LocalHost
    Posts
    347

    Resolved [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.)

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    109,991

    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.

  3. #3
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    3,012

    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.

  4. #4

    Thread Starter
    Hyperactive Member pourkascheff's Avatar
    Join Date
    Apr 2020
    Location
    LocalHost
    Posts
    347

    Re: Get CPU id (Machine number) without any late bindings (Option strict = On)

    Quote Originally Posted by ChrisE View Post
    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
  •  



Click Here to Expand Forum to Full Width