• 0

[VB.NET] How many CPU cores?


Question

9 answers to this question

Recommended Posts

  • 0
  jameswjrose said:
Hey gang,

Is there a way within VB.NET 2005 to detect how many cores the CPU has? and/or how many CPUs are on the system?

Thanks kindly

You'll probably have to utilize p/invoke to leverage the Win32 API. I'm fairly certain there's no native .NET way to do this.

  • 0
  jameswjrose said:
Thanks, at least I feel better on not finding it. Last I checked (a few years ago) there were no Windows APIs available to denote CPU info. Intel had a DLL we could use but...

Thanks.

System.Environment.ProcessorCount will get you that.

  • 0
  rnvrajagopal said:
System.Environment.ProcessorCount will get you that.

Great. Thanks kindly. Now I have to try this at home, where I have a dual core to see if it reports 1 or 2 (what is the API denoting as a CPU... a core or a chip... <shrug> But thank you very much!

  • 0

That doesn't distinguish between CPUs and cores. AFAIK, you have to look at CPUID to determine if the reported CPUs are indeed cores on a chip or separate CPUs. With the ProcessorCount property, I'd hazard that it doesn't distinguish between cores or CPUs or HyperThreading.

  • 0

On Windows Vista and using the .NET WMI API, it should be possible to detect. (and thus in a .NET, non-P/Invoke way)

However, on XP and earlier, the WMI Win32_Processor objects didn't distinguish as well between logical (cores, HyperThreading...) and physical processors. Executing the WMI query "SELECT * FROM Win32_Processor" should return two items for a single dual core CPU on XP, but only one item in that case on Vista.

Here's the code that should work on Vista: (untested)

' Requires an import for System.Management

Dim scope As New ManagementScope("\\.\root\cimv2")
scope.Connect()

Dim objectQuery As New ObjectQuery("SELECT * FROM Win32_Processor")
Dim searcher As New ManagementObjectSearcher(scope, objectQuery)
Dim cpu As ManagementObject

For Each cpu In searcher.Get()
	Console.WriteLine("Processor: " &amp; cpu["Name"])
	Console.WriteLine("Manufacturer: " &amp; cpu["Manufacturer"])
	Console.WriteLine("Clock frequency: " &amp; cpu["MaxClockSpeed"] &amp; " MHz")
	Console.WriteLine("NumberOfLogicalProcessors: " &amp; cpu["NumberOfLogicalProcessors"])
	Console.WriteLine("NumberOfCores: " &amp; cpu["NumberOfCores"])
	Console.WriteLine()
Next cpu

However, obviously having Windows Vista as a requirement if your software is going to be spread is a bit harsh. :p

More info on the Win32_Processor WMI class.

If using P/Invoke, I only found GetLogicalProcessorInformation as an "easy" way out, but that requires Windows XP x64 or Vista too, so it's not much better as for the software requirements.

Edited by Jugalator
  • 0
  azcodemonkey said:
That doesn't distinguish between CPUs and cores. AFAIK, you have to look at CPUID to determine if the reported CPUs are indeed cores on a chip or separate CPUs. With the ProcessorCount property, I'd hazard that it doesn't distinguish between cores or CPUs or HyperThreading.

I was curious to this myself, so i asked my m8 to test a simple app on his Dual Core system and it returned 2 and 1 on mine because i only have a P4. So it does look at cores? If you want to dig deep and get lots of information look at the System.Management namepsace in .NET.

  • 0
  scottb said:
I was curious to this myself, so i asked my m8 to test a simple app on his Dual Core system and it returned 2 and 1 on mine because i only have a P4. So it does look at cores? If you want to dig deep and get lots of information look at the System.Management namepsace in .NET.

I just mean that you wouldn't be able to tell it was indeed 2 cores on 1 die, 2 separate cpus, or a hyperthreaded cpu.

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.