Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Getting the number of processors

1 view
Skip to first unread message

Saucer Man

unread,
Jan 1, 2010, 12:39:23 PM1/1/10
to
What's the best way to get the number of processors? I was thinking of just
getting it from the environment variable.

--
Thanks.


Bob Butler

unread,
Jan 1, 2010, 12:56:05 PM1/1/10
to

"Saucer Man" <sauc...@nospam.net> wrote in message
news:uLnKelwi...@TK2MSFTNGP04.phx.gbl...


> What's the best way to get the number of processors? I was thinking of
> just getting it from the environment variable.

If WMI is an option then this may be a start:

Sub main()
MsgBox "Count processors=" & CountProcessors & vbCrLf & _
"Query processors=" & QueryProcessors & vbCrLf & _
"Logical Processors=" & QueryLogicalProcessors
End Sub

Public Function CountProcessors() As Long
Dim oWMI As Object
Dim cCPUs As Object
Set oWMI = GetObject("winmgmts://./root/cimv2")
Set cCPUs = oWMI.execquery("Select * from Win32_Processor")
CountProcessors = cCPUs.Count
Set cCPUs = Nothing
Set oWMI = Nothing
End Function

Public Function QueryProcessors() As Long
Dim oWMI As Object
Dim cCPUs As Object
Dim oCPU As Object
Set oWMI = GetObject("winmgmts://./root/cimv2")
Set cCPUs = oWMI.execquery("Select NumberOfProcessors from
Win32_ComputerSystem")
For Each oCPU In cCPUs
QueryProcessors = oCPU.NumberOfProcessors
Next
Set cCPUs = Nothing
Set oWMI = Nothing
End Function

Public Function QueryLogicalProcessors() As Long
Dim oWMI As Object
Dim cCPUs As Object
Dim oCPU As Object
Set oWMI = GetObject("winmgmts://./root/cimv2")
Set cCPUs = oWMI.execquery("Select NumberOfLogicalProcessors from
Win32_ComputerSystem")
For Each oCPU In cCPUs
QueryLogicalProcessors = oCPU.NumberOfLogicalProcessors
Next
Set cCPUs = Nothing
Set oWMI = Nothing
End Function

The NumberOfLogicalProcessors vs NumberOfProcessors is discussed starting
here:
http://msdn.microsoft.com/en-us/library/aa394102(VS.85).aspx

Ralph

unread,
Jan 1, 2010, 1:15:35 PM1/1/10
to
Saucer Man wrote:
> What's the best way to get the number of processors? I was thinking
> of just getting it from the environment variable.

Probably.

There is a general disdain for Environmental Variables. Mainly because they
are so easily changed for a particular session (which is preceived as a
security risk), and partly they just don't seem as cool as snooping the
Registry for system info. On the other hand, they are convenient and require
no special permissions to read.

So it depends on how critical this information is for your application, ie,
what's the downside if the User lies?

Frankly, if a system variable is available - use it. Unless you have an
overwhelming reason not to.

-ralph


Wolfgang Enzinger

unread,
Jan 1, 2010, 2:27:58 PM1/1/10
to
On Fri, 1 Jan 2010 09:56:05 -0800, Bob Butler wrote:

>If WMI is an option then [...]

and if not, then:

Type SYSTEM_INFO
wProcessorArchitecture As Integer
wReserved As Integer
dwPageSize As Long
lpMinimumApplicationAddress As Long
lpMaximumApplicationAddress As Long
dwActiveProcessorMask As Long
dwNumberOfProcessors As Long '<<<-------
dwProcessorType As Long
dwAllocationGranularity As Long
wProcessorLevel As Integer
wProcessorRevision As Integer
End Type

Declare Sub GetSystemInfo Lib "kernel32" (lpSystemInfo As SYSTEM_INFO)

[...]
Dim sInfo As SYSTEM_INFO, n As Long
GetSystemInfo sInfo
n = sInfo.dwNumberOfProcessors
[...]

I'm not 100% sure but I think that the number of logical processors is
returned here.

Saucer Man

unread,
Jan 1, 2010, 4:32:15 PM1/1/10
to

Thanks for all the responses!


Dee Earley

unread,
Jan 27, 2010, 7:14:00 AM1/27/10
to
On 01/01/2010 17:39, Saucer Man wrote:
> What's the best way to get the number of processors? I was thinking of just
> getting it from the environment variable.

The official method GetLogicalProcessorInformation().

--
Dee Earley (dee.e...@icode.co.uk)
i-Catcher Development Team

iCode Systems

0 new messages