Tag Archives: registry

Use the Registry to get CPU/Processor Hardware information using VB.NET and Higher

Click Star to Rate Post
1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
 

This is a simple article/tip of using the Windows System Registry to access the ‘CentralProcessor’ sub-key and extract information about the computers processor. You can get the cpu’s clockspeed, namestring, model, stepping, etc. Shows two ways to access the registry.

Example use is below.


Visual Basic.NET, Visual Basic 2008, and VB 2010

        'The registry path to get the info we are wanting.
        Dim str As String = "HARDWARE\DESCRIPTION\System\CentralProcessor"

        'Returns how many SubKeys are under “CentralProcessor”.
        'The subKey count under this key =’s the‘total cpu’s your system has installed.
        'My system happens to return 2x subkeys since I have a Dual Core system.
        Dim cpuCount As Integer = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(str, False).SubKeyCount
        '
        'Get and add the values for the first cpu.
        '
        Dim cpuMhz As String = Microsoft.Win32.Registry.LocalMachine.OpenSubKey( _
            "HARDWARE\DESCRIPTION\System\CentralProcessor\0").GetValue("~mhz")

        Dim cpuName As String = Microsoft.Win32.Registry.LocalMachine.OpenSubKey( _
            "HARDWARE\DESCRIPTION\System\CentralProcessor\0").GetValue("ProcessorNameString")

        Dim cpuVendorID As String = Microsoft.Win32.Registry.LocalMachine.OpenSubKey( _
            "HARDWARE\DESCRIPTION\System\CentralProcessor\0").GetValue("VendorIdentifier")

        Dim cpuIdentifier As String = Microsoft.Win32.Registry.LocalMachine.OpenSubKey( _
            "HARDWARE\DESCRIPTION\System\CentralProcessor\0").GetValue("Identifier")

Thats all there is to it. You just need to handle the variables that will contain the cpu data extracted. Below is similar example code for VB 05, and higher only since it uses the MY interface to access the registry…


Visual Basic 2005/2008 and VB 2010 Only…

        'The registry path to get the info we are wanting.
        Dim str As String = "HARDWARE\DESCRIPTION\System\CentralProcessor"

        'Returns how many SubKeys are under “CentralProcessor”.
        'The subKey count under this key =’s the‘total cpu’s your system has installed.
        'Of course, 90% or so will only have 1x subkey denoting a‘single cpu system.
        'My system happens to return 2x subkeys since I have a Dual Core system.
        Dim cpuCount As Integer = My.Computer.Registry.LocalMachine.OpenSubKey(str, False).SubKeyCount

        'Get and add the values for the first cpu.

        Dim cpuMhz As String = My.Computer.Registry.LocalMachine.OpenSubKey( _
            "HARDWARE\DESCRIPTION\System\CentralProcessor\0").GetValue("~mhz")

        Dim cpuName As String = My.Computer.Registry.LocalMachine.OpenSubKey( _
            "HARDWARE\DESCRIPTION\System\CentralProcessor\0").GetValue("ProcessorNameString")

        Dim cpuVendorID As String = My.Computer.Registry.LocalMachine.OpenSubKey( _
            "HARDWARE\DESCRIPTION\System\CentralProcessor\0").GetValue("VendorIdentifier")

        Dim cpuIdentifier As String = My.Computer.Registry.LocalMachine.OpenSubKey( _
            "HARDWARE\DESCRIPTION\System\CentralProcessor\0").GetValue("Identifier")

For VB 6.0 it’s not quite this simple to access the registry. Win32 API calls are required to do so. There are examples around the net that shows how to use the Win32 Registry code in VB 6.0. Have fun!

Jason

Find out how many CPU’s/Processors/Cores the computer has installed using VB.NET and Higher

Click Star to Rate Post
1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
 

Its actually very easy with Visual Basic.NET VB 2008, VB.NET 2010, 2013, and Higher to detect the cpu or core count for the computer. Below is the very simple way to do it which will return the total number of logical processors installed in the computer…

        '
        'This works for Visual Basic 2005 and Higher.
        '
        'The code we want is under the System.Environment Namespace and is the ProcessorCount
        'property.
        MsgBox(System.Environment.ProcessorCount.ToString)

What if you want to get the CPU/Processor/Core count with Visual Basic.NET 2002 or 2003?

Well, .NET version 1.1 and older does not have the ProcessorCount property like version 2.0 of the .NET Framework. But that doesn’t mean it can’t be done. Its actually not to awfully hard to do. Below is a way to do it using the Windows system Registry…

        '
        'The registry path to get the info we are wanting.
        Dim str As String = "HARDWARE\DESCRIPTION\System\CentralProcessor"
        '
        'Returns how many SubKeys are under “CentralProcessor”. The subKey count under this key
        '=’s the‘total cpu’s your system has installed. Of course, 90% or so will only have 1x
        'subkey denoting a ‘single cpu system. My system happens to return 2x subkeys since I
        'have a Dual Core system.
        Dim cpuCount As Integer = My.Computer.Registry.LocalMachine.OpenSubKey(str, False).SubKeyCount

        MsgBox(cpuCount.ToString)

The Messagebox will then throw a Dialog with the total number of cpu’s/core’s on the current computer. I do not know though if this works on all Windows operating systems.

You can also use the Windows Management Instrumentation (WMI) and get the total instances of the class.

Below is a simple way to use WMI to return the computers CPU/Core Count…

       '
        'Create a Instrument Class and Object.
        Dim wmiClass As New System.Management.ManagementClass
        Dim wmiObject As New System.Management.ManagementObject
        '
        'Set the PAth to “Win32_Processor” which is where we want to get info.
        wmiClass.Path.RelativePath = "Win32_Processor"
        '
        'Last of all, just check how many instances is available and that will be your cpu/core count.
        MsgBox(wmiClass.GetInstances.Count)

Executing the code above will tell you how many cpus are on the computer since there is a class instance for each CPU/processor/core on the current computer.

Take care 🙂

                       Jason

Revised: 2015