2022-12-11 Windows下如何用C/C++获取cpu数量和物理核数

Windows下如何用C/C++获取cpu数量和物理核数

  • 前言
  • 一、WMI是什么?
  • 二、使用步骤
  • 总结


前言

最近看到个有趣的题目: Windows下如何用C/C++获取cpu数量和物理核数, 感觉比较有意思, 就搜索一下相关问题, 进行回复.


一、WMI是什么?

看这里, 介绍的非常详细, 我们只需用 wmic cpu get XXXX 命令, 就可获取相应的CPU信息.

二、使用步骤

以下是 win10 环境能提供的所有CPU参数, 可谓相当齐全.

#include 
#include 
#include 

// 命令调取 CpuID
#define CpuNum "wmic cpu get DeviceID"
// 命令调取 Cpu 物理核数
#define CpuCoreNum "wmic cpu get NumberOfCores"
// 命令调取 Cpu 逻辑核数
#define CpuLogicalCoreNum "wmic cpu get NumberOfLogicalProcessors"

#define __AddressWidth "wmic cpu get AddressWidth"
#define __Architecture "wmic cpu get Architecture"
#define __AssetTag "wmic cpu get AssetTag"
#define __Availability "wmic cpu get Availability"
#define __Caption "wmic cpu get Caption"
#define __Characteristics "wmic cpu get Characteristics"
#define __ConfigManagerErrorCode "wmic cpu get ConfigManagerErrorCode"
#define __ConfigManagerUserConfig "wmic cpu get ConfigManagerUserConfig"
#define __CpuStatus "wmic cpu get CpuStatus"
#define __CreationClassName "wmic cpu get CreationClassName"
#define __CurrentClockSpeed "wmic cpu get CurrentClockSpeed"
#define __CurrentVoltage "wmic cpu get CurrentVoltage"
#define __DataWidth "wmic cpu get DataWidth"
#define __Description "wmic cpu get Description"
#define __DeviceID "wmic cpu get DeviceID"
#define __ErrorCleared "wmic cpu get ErrorCleared"
#define __ErrorDescription "wmic cpu get ErrorDescription"
#define __ExtClock "wmic cpu get ExtClock"
#define __Family "wmic cpu get Family"
#define __InstallDate "wmic cpu get InstallDate"
#define __L2CacheSize "wmic cpu get L2CacheSize"
#define __L2CacheSpeed "wmic cpu get L2CacheSpeed"
#define __L3CacheSize "wmic cpu get L3CacheSize"
#define __L3CacheSpeed "wmic cpu get L3CacheSpeed"
#define __LastErrorCode "wmic cpu get LastErrorCode"
#define __Level "wmic cpu get Level"
#define __LoadPercentage "wmic cpu get LoadPercentage"
#define __Manufacturer "wmic cpu get Manufacturer"
#define __MaxClockSpeed "wmic cpu get MaxClockSpeed"
#define __Name "wmic cpu get Name"
#define __NumberOfCores "wmic cpu get NumberOfCores"
#define __NumberOfEnabledCore "wmic cpu get NumberOfEnabledCore"
#define __NumberOfLogicalProcessors "wmic cpu get NumberOfLogicalProcessors"
#define __OtherFamilyDescription "wmic cpu get OtherFamilyDescription"
#define __PartNumber "wmic cpu get PartNumber"
#define __PNPDeviceID "wmic cpu get PNPDeviceID"
#define __PowerManagementCapabilities "wmic cpu get PowerManagementCapabilities"
#define __PowerManagementSupported "wmic cpu get PowerManagementSupported"
#define __ProcessorId "wmic cpu get ProcessorId"
#define __ProcessorType "wmic cpu get ProcessorType"
#define __Revision "wmic cpu get Revision"
#define __Role "wmic cpu get Role"
#define __SecondLevelAddressTranslationExtensions                              \
    "wmic cpu get SecondLevelAddressTranslationExtensions"
#define __SerialNumber "wmic cpu get SerialNumber"
#define __SocketDesignation "wmic cpu get SocketDesignation"
#define __Status "wmic cpu get Status"
#define __StatusInfo "wmic cpu get StatusInfo"
#define __Stepping "wmic cpu get Stepping"
#define __SystemCreationClassName "wmic cpu get SystemCreationClassName"
#define __SystemName "wmic cpu get SystemName"
#define __ThreadCount "wmic cpu get ThreadCount"
#define __UniqueId "wmic cpu get UniqueId"
#define __UpgradeMethod "wmic cpu get UpgradeMethod"
#define __Version "wmic cpu get Version"
#define __VirtualizationFirmwareEnabled                                        \
    "wmic cpu get VirtualizationFirmwareEnabled"
#define __VMMonitorModeExtensions "wmic cpu get VMMonitorModeExtensions"
#define __VoltageCaps "wmic cpu get VoltageCaps"

// 获取 Cpu 信息
void getCpuInformation(const char *command)
{
    // 获取windows命令回执
    FILE *winCommand = popen(command, "r");
    char buf[100] = {};

    if (!winCommand)
    {
        perror("popen");
        exit(EXIT_FAILURE);
    }
    else
    {
        // 输出命令回执
        while (fgets(buf, sizeof(buf) - 1, winCommand) != 0)
        {
            printf("%s", buf);
            memset(buf, 0, sizeof(buf));
        }

        pclose(winCommand);
    }
}

int main()
{
    // 具体核数要数ID个数
    getCpuInformation(CpuNum);
    // 具体物理核数直接给出
    getCpuInformation(CpuCoreNum);
    // 具体逻辑核数直接给出
    getCpuInformation(CpuLogicalCoreNum);

    return 0;
}

总结

对于底层硬件信息的获取, 和具体环境有直接关系, 各个系统都需根据系统实现进行操作, 也就是说, 不可移植.

你可能感兴趣的:(笔记,c++,c语言,Windows,CPU物理核数,WMI调用)