Basit Anwer Basit Anwer - 10 months ago 75
C++ Question

Find number of Physical CPU Sockets in server machine

I have a system with 4 Physical Processor sockets. Running Windows 2003, I would like to programmatically find the number of sockets using C++. Is this possible and if so, how?

Answer Source

For Windows 7 and 2008 server there is the GetActiveProcessorGroupCount function. But you have 2003 server, so it's not an option.

In C++ this requires to write WMI consumer code, which is a clumsy and boring (D)COM stuff.

One nice solution would be to run systeminfo command and parse the output, but be careful as the output's column header is localized to system's locale.


Just found a much nicer solution, which makes use of the command-line interface to WMI.

Run the following command and parse output, there is one line per socket

> wmic.exe cpu get
AddressWidth  Architecture  Availability  Caption                               ConfigManagerErrorCode  ConfigManagerUserConfig  CpuStatus  CreationClassName  CurrentClockSpeed  CurrentVoltage  DataWidth  Description                           DeviceID  ErrorCleared  ErrorDescription  ExtClock  Family  InstallDate  L2CacheSize  L2CacheSpeed  L3CacheSize  L3CacheSpeed  LastErrorCode  Level  LoadPercentage  Manufacturer  MaxClockSpeed  Name                                             NumberOfCores  NumberOfLogicalProcessors  OtherFamilyDescription  PNPDeviceID  PowerManagementCapabilities  PowerManagementSupported  ProcessorId       ProcessorType  Revision  Role  SocketDesignation  Status  StatusInfo  Stepping  SystemCreationClassName  SystemName       UniqueId  UpgradeMethod  Version  VoltageCaps  
64            9             3             Intel64 Family 6 Model 23 Stepping 6                                                   1          Win32_Processor    2532               33              64         Intel64 Family 6 Model 23 Stepping 6  CPU0                                      421       2                                               0            0                            6      1               GenuineIntel  2532           Intel(R) Core(TM)2 Duo CPU     T9400  @ 2.53GHz  2              2                                                                                            FALSE                     BFEBFBFF00010676  3              5894      CPU   CPU Socket #0      OK      3                     Win32_ComputerSystem     CHBROSSO-WIN7VM            1                       2            

Running an exe and parsing output in C++ should the easy part. You can also use POCO library or Boost.Process to have cleaner code.

(this is untested code)

//get wmic program output 
FILE* pipe = _popen("wmic.exe cpu get", "r");
if (!pipe) throw std::exception("error");
char buffer[128];
std::string output;
while(!feof(pipe)) {
  if(fgets(buffer, 128, pipe) != NULL)
      output += buffer;

//split lines to a vector<string>
std::stringstream oss(output);
std::vector<std::string> processor_description; std::string buffer;
while (std::getline(oss, buffer))
//processor_description has n+1 elements, n being nb of sockets, +1 is the header of columns