Windows上获取cpu info, cpuid, cpu id 方法整理

2023-05-16


1. 使用cmd获取cpu id


    在 CMD中输入如下命令:

wmic cpu get processorid

2. 使用源代码编译获取 cpu id:(借码)三个源代码文件

调试通过

原文链接1

原文链接2

//get_cpu_id.h

//get_cpu_id.h

#pragma once

#include <windows.h>
#include <stdio.h>
#include <string>
#include <intrin.h>

// same function as:    wmic cpu get processorid

class CGetCPUId
{
public:
    CGetCPUId();
    virtual ~CGetCPUId();

public:
    std::string            GetId();

};

//get_cpu_id.cpp

//get_cpu_id.cpp


#include "get_cpu_id.h"

CGetCPUId::CGetCPUId()
{
}

CGetCPUId::~CGetCPUId()
{

}

std::string CGetCPUId::GetId()
{/** only win32
    std::string strCPUId;

    unsigned long s1, s2;
    char buf[32] = { 0 };

    __asm
    {
        mov eax, 01h
        xor edx, edx
        cpuid
        mov s1, edx
        mov s2, eax
    }

    if (s1)
    {
        memset(buf, 0, 32);
        sprintf_s(buf, 32, "%08X", s1);
        strCPUId += buf;
    }

    if (s2)
    {
        memset(buf, 0, 32);
        sprintf_s(buf, 32, "%08X", s2);
        strCPUId += buf;
    }

    __asm
    {
        mov eax, 03h
        xor ecx, ecx
        xor edx, edx
        cpuid
        mov s1, edx
        mov s2, ecx
    }

    if (s1)
    {
        memset(buf, 0, 32);
        sprintf_s(buf, 32, "%08X", s1);
        strCPUId += buf;
    }

    if (s2)
    {
        memset(buf, 0, 32);
        sprintf_s(buf, 32, "%08X", s2);
        strCPUId += buf;
    }

    return strCPUId;
    */

    std::string strCPUId;

    unsigned long s1, s2;
    char buf[32] = { 0 };
    INT32 Infobuf[4];
#if defined(_WIN64)
    // 64位下不支持内联汇编. 应使用__cpuid、__cpuidex等Intrinsics函数。
    __cpuid(Infobuf, 1);
    s1 = Infobuf[3];
    s2 = Infobuf[0];
#else
    __asm
    {
        mov eax, 01h
        xor ecx, ecx
        xor edx, edx
        cpuid
        mov s1, edx
        mov s2, ecx
    }
#endif
    if (s1)
    {
        memset(buf, 0, 32);
        sprintf_s(buf, 32, "%08X", s1);
        strCPUId += buf;
    }

    if (s2)
    {
        memset(buf, 0, 32);
        sprintf_s(buf, 32, "%08X", s2);
        strCPUId += buf;
    }


#if defined(_WIN64)
    // 64位下不支持内联汇编. 应使用__cpuid、__cpuidex等Intrinsics函数。
    __cpuid(Infobuf, 3);
    s1 = Infobuf[3];
    s2 = Infobuf[0];
#else
    __asm
    {
        mov eax, 03h
        xor ecx, ecx
        xor edx, edx
        cpuid
        mov s1, edx
        mov s2, ecx
    }
#endif
    if (s1)
    {
        memset(buf, 0, 32);
        sprintf_s(buf, 32, "%08X", s1);
        strCPUId += buf;
    }

    if (s2)
    {
        memset(buf, 0, 32);
        sprintf_s(buf, 32, "%08X", s2);
        strCPUId += buf;
    }

    return strCPUId;

}

//main_app.cpp

//main_app.cpp

#include "get_cpu_id.h"
#include <iostream>

using namespace std;

int main(int argc, char* argv[])
{
    CGetCPUId        GetCPUId;

    cout << "CPUID:" << GetCPUId.GetId() << endl;
    getchar();

    return 0;
}



3. 获取cpu 其他信息

一个cpp文件

原文链接3

//Micro_cpu_info_app.cpp

//Micro_cpu_info_app.cpp

// Micro_CPUID_sample_app.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
// InstructionSet.cpp
// Compile by using: cl /EHsc /W4 InstructionSet.cpp
// processor: x86, x64
// Uses the __cpuid intrinsic to get information about
// CPU extended instruction set support.

#include <iostream>
#include <vector>
#include <bitset>
#include <array>
#include <string>
#include <intrin.h>

class InstructionSet
{
    // forward declarations
    class InstructionSet_Internal;

public:
    // getters
    static std::string Vendor(void) { return CPU_Rep.vendor_; }
    static std::string Brand(void) { return CPU_Rep.brand_; }

    static bool SSE3(void) { return CPU_Rep.f_1_ECX_[0]; }
    static bool PCLMULQDQ(void) { return CPU_Rep.f_1_ECX_[1]; }
    static bool MONITOR(void) { return CPU_Rep.f_1_ECX_[3]; }
    static bool SSSE3(void) { return CPU_Rep.f_1_ECX_[9]; }
    static bool FMA(void) { return CPU_Rep.f_1_ECX_[12]; }
    static bool CMPXCHG16B(void) { return CPU_Rep.f_1_ECX_[13]; }
    static bool SSE41(void) { return CPU_Rep.f_1_ECX_[19]; }
    static bool SSE42(void) { return CPU_Rep.f_1_ECX_[20]; }
    static bool MOVBE(void) { return CPU_Rep.f_1_ECX_[22]; }
    static bool POPCNT(void) { return CPU_Rep.f_1_ECX_[23]; }
    static bool AES(void) { return CPU_Rep.f_1_ECX_[25]; }
    static bool XSAVE(void) { return CPU_Rep.f_1_ECX_[26]; }
    static bool OSXSAVE(void) { return CPU_Rep.f_1_ECX_[27]; }
    static bool AVX(void) { return CPU_Rep.f_1_ECX_[28]; }
    static bool F16C(void) { return CPU_Rep.f_1_ECX_[29]; }
    static bool RDRAND(void) { return CPU_Rep.f_1_ECX_[30]; }

    static bool MSR(void) { return CPU_Rep.f_1_EDX_[5]; }
    static bool CX8(void) { return CPU_Rep.f_1_EDX_[8]; }
    static bool SEP(void) { return CPU_Rep.f_1_EDX_[11]; }
    static bool CMOV(void) { return CPU_Rep.f_1_EDX_[15]; }
    static bool CLFSH(void) { return CPU_Rep.f_1_EDX_[19]; }
    static bool MMX(void) { return CPU_Rep.f_1_EDX_[23]; }
    static bool FXSR(void) { return CPU_Rep.f_1_EDX_[24]; }
    static bool SSE(void) { return CPU_Rep.f_1_EDX_[25]; }
    static bool SSE2(void) { return CPU_Rep.f_1_EDX_[26]; }

    static bool FSGSBASE(void) { return CPU_Rep.f_7_EBX_[0]; }
    static bool BMI1(void) { return CPU_Rep.f_7_EBX_[3]; }
    static bool HLE(void) { return CPU_Rep.isIntel_ && CPU_Rep.f_7_EBX_[4]; }
    static bool AVX2(void) { return CPU_Rep.f_7_EBX_[5]; }
    static bool BMI2(void) { return CPU_Rep.f_7_EBX_[8]; }
    static bool ERMS(void) { return CPU_Rep.f_7_EBX_[9]; }
    static bool INVPCID(void) { return CPU_Rep.f_7_EBX_[10]; }
    static bool RTM(void) { return CPU_Rep.isIntel_ && CPU_Rep.f_7_EBX_[11]; }
    static bool AVX512F(void) { return CPU_Rep.f_7_EBX_[16]; }
    static bool RDSEED(void) { return CPU_Rep.f_7_EBX_[18]; }
    static bool ADX(void) { return CPU_Rep.f_7_EBX_[19]; }
    static bool AVX512PF(void) { return CPU_Rep.f_7_EBX_[26]; }
    static bool AVX512ER(void) { return CPU_Rep.f_7_EBX_[27]; }
    static bool AVX512CD(void) { return CPU_Rep.f_7_EBX_[28]; }
    static bool SHA(void) { return CPU_Rep.f_7_EBX_[29]; }

    static bool PREFETCHWT1(void) { return CPU_Rep.f_7_ECX_[0]; }

    static bool LAHF(void) { return CPU_Rep.f_81_ECX_[0]; }
    static bool LZCNT(void) { return CPU_Rep.isIntel_ && CPU_Rep.f_81_ECX_[5]; }
    static bool ABM(void) { return CPU_Rep.isAMD_ && CPU_Rep.f_81_ECX_[5]; }
    static bool SSE4a(void) { return CPU_Rep.isAMD_ && CPU_Rep.f_81_ECX_[6]; }
    static bool XOP(void) { return CPU_Rep.isAMD_ && CPU_Rep.f_81_ECX_[11]; }
    static bool TBM(void) { return CPU_Rep.isAMD_ && CPU_Rep.f_81_ECX_[21]; }

    static bool SYSCALL(void) { return CPU_Rep.isIntel_ && CPU_Rep.f_81_EDX_[11]; }
    static bool MMXEXT(void) { return CPU_Rep.isAMD_ && CPU_Rep.f_81_EDX_[22]; }
    static bool RDTSCP(void) { return CPU_Rep.isIntel_ && CPU_Rep.f_81_EDX_[27]; }
    static bool _3DNOWEXT(void) { return CPU_Rep.isAMD_ && CPU_Rep.f_81_EDX_[30]; }
    static bool _3DNOW(void) { return CPU_Rep.isAMD_ && CPU_Rep.f_81_EDX_[31]; }

private:
    static const InstructionSet_Internal CPU_Rep;

    class InstructionSet_Internal
    {
    public:
        InstructionSet_Internal()
            : nIds_{ 0 },
            nExIds_{ 0 },
            isIntel_{ false },
            isAMD_{ false },
            f_1_ECX_{ 0 },
            f_1_EDX_{ 0 },
            f_7_EBX_{ 0 },
            f_7_ECX_{ 0 },
            f_81_ECX_{ 0 },
            f_81_EDX_{ 0 },
            data_{},
            extdata_{}
        {
            //int cpuInfo[4] = {-1};
            std::array<int, 4> cpui;

            // Calling __cpuid with 0x0 as the function_id argument
            // gets the number of the highest valid function ID.
            __cpuid(cpui.data(), 0);
            nIds_ = cpui[0];

            for (int i = 0; i <= nIds_; ++i)
            {
                __cpuidex(cpui.data(), i, 0);
                data_.push_back(cpui);
            }

            // Capture vendor string
            char vendor[0x20];
            memset(vendor, 0, sizeof(vendor));
            *reinterpret_cast<int*>(vendor) = data_[0][1];
            *reinterpret_cast<int*>(vendor + 4) = data_[0][3];
            *reinterpret_cast<int*>(vendor + 8) = data_[0][2];
            vendor_ = vendor;
            if (vendor_ == "GenuineIntel")
            {
                isIntel_ = true;
            }
            else if (vendor_ == "AuthenticAMD")
            {
                isAMD_ = true;
            }

            // load bitset with flags for function 0x00000001
            if (nIds_ >= 1)
            {
                f_1_ECX_ = data_[1][2];
                f_1_EDX_ = data_[1][3];
            }

            // load bitset with flags for function 0x00000007
            if (nIds_ >= 7)
            {
                f_7_EBX_ = data_[7][1];
                f_7_ECX_ = data_[7][2];
            }

            // Calling __cpuid with 0x80000000 as the function_id argument
            // gets the number of the highest valid extended ID.
            __cpuid(cpui.data(), 0x80000000);
            nExIds_ = cpui[0];

            char brand[0x40];
            memset(brand, 0, sizeof(brand));

            for (int i = 0x80000000; i <= nExIds_; ++i)
            {
                __cpuidex(cpui.data(), i, 0);
                extdata_.push_back(cpui);
            }

            // load bitset with flags for function 0x80000001
            if (nExIds_ >= 0x80000001)
            {
                f_81_ECX_ = extdata_[1][2];
                f_81_EDX_ = extdata_[1][3];
            }

            // Interpret CPU brand string if reported
            if (nExIds_ >= 0x80000004)
            {
                memcpy(brand, extdata_[2].data(), sizeof(cpui));
                memcpy(brand + 16, extdata_[3].data(), sizeof(cpui));
                memcpy(brand + 32, extdata_[4].data(), sizeof(cpui));
                brand_ = brand;
            }
        };

        int nIds_;
        int nExIds_;
        std::string vendor_;
        std::string brand_;
        bool isIntel_;
        bool isAMD_;
        std::bitset<32> f_1_ECX_;
        std::bitset<32> f_1_EDX_;
        std::bitset<32> f_7_EBX_;
        std::bitset<32> f_7_ECX_;
        std::bitset<32> f_81_ECX_;
        std::bitset<32> f_81_EDX_;
        std::vector<std::array<int, 4>> data_;
        std::vector<std::array<int, 4>> extdata_;
    };
};

// Initialize static member data
const InstructionSet::InstructionSet_Internal InstructionSet::CPU_Rep;

// Print out supported instruction set extensions
int main()
{
    auto& outstream = std::cout;

    auto support_message = [&outstream](std::string isa_feature, bool is_supported) {
        outstream << isa_feature << (is_supported ? " supported" : " not supported") << std::endl;
    };

    std::cout << InstructionSet::Vendor() << std::endl;
    std::cout << InstructionSet::Brand() << std::endl;

    support_message("3DNOW", InstructionSet::_3DNOW());
    support_message("3DNOWEXT", InstructionSet::_3DNOWEXT());
    support_message("ABM", InstructionSet::ABM());
    support_message("ADX", InstructionSet::ADX());
    support_message("AES", InstructionSet::AES());
    support_message("AVX", InstructionSet::AVX());
    support_message("AVX2", InstructionSet::AVX2());
    support_message("AVX512CD", InstructionSet::AVX512CD());
    support_message("AVX512ER", InstructionSet::AVX512ER());
    support_message("AVX512F", InstructionSet::AVX512F());
    support_message("AVX512PF", InstructionSet::AVX512PF());
    support_message("BMI1", InstructionSet::BMI1());
    support_message("BMI2", InstructionSet::BMI2());
    support_message("CLFSH", InstructionSet::CLFSH());
    support_message("CMPXCHG16B", InstructionSet::CMPXCHG16B());
    support_message("CX8", InstructionSet::CX8());
    support_message("ERMS", InstructionSet::ERMS());
    support_message("F16C", InstructionSet::F16C());
    support_message("FMA", InstructionSet::FMA());
    support_message("FSGSBASE", InstructionSet::FSGSBASE());
    support_message("FXSR", InstructionSet::FXSR());
    support_message("HLE", InstructionSet::HLE());
    support_message("INVPCID", InstructionSet::INVPCID());
    support_message("LAHF", InstructionSet::LAHF());
    support_message("LZCNT", InstructionSet::LZCNT());
    support_message("MMX", InstructionSet::MMX());
    support_message("MMXEXT", InstructionSet::MMXEXT());
    support_message("MONITOR", InstructionSet::MONITOR());
    support_message("MOVBE", InstructionSet::MOVBE());
    support_message("MSR", InstructionSet::MSR());
    support_message("OSXSAVE", InstructionSet::OSXSAVE());
    support_message("PCLMULQDQ", InstructionSet::PCLMULQDQ());
    support_message("POPCNT", InstructionSet::POPCNT());
    support_message("PREFETCHWT1", InstructionSet::PREFETCHWT1());
    support_message("RDRAND", InstructionSet::RDRAND());
    support_message("RDSEED", InstructionSet::RDSEED());
    support_message("RDTSCP", InstructionSet::RDTSCP());
    support_message("RTM", InstructionSet::RTM());
    support_message("SEP", InstructionSet::SEP());
    support_message("SHA", InstructionSet::SHA());
    support_message("SSE", InstructionSet::SSE());
    support_message("SSE2", InstructionSet::SSE2());
    support_message("SSE3", InstructionSet::SSE3());
    support_message("SSE4.1", InstructionSet::SSE41());
    support_message("SSE4.2", InstructionSet::SSE42());
    support_message("SSE4a", InstructionSet::SSE4a());
    support_message("SSSE3", InstructionSet::SSSE3());
    support_message("SYSCALL", InstructionSet::SYSCALL());
    support_message("TBM", InstructionSet::TBM());
    support_message("XOP", InstructionSet::XOP());
    support_message("XSAVE", InstructionSet::XSAVE());
}

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Windows上获取cpu info, cpuid, cpu id 方法整理 的相关文章

随机推荐

  • 阿里巴巴常用的 12 个后端开发工具

    从手动编码到自动化 xff0c 从重复工作到创新 xff0c 开发人员工具随着技术的发展而不断发展 阿里巴巴集团和阿里巴巴云已通过开源发布和基于云的实施向公众提供其技术 通过在各种业务场景中的多年开发积累了这些技术 本文介绍了一些阿里巴巴开
  • Ubuntu卡在登陆界面循环

    文章目录 一 现象二 原因三 解决措施 一 现象 先卡在这 xff1a a start job is running for hold until boot process finishes up 22s no limit 然后卡在这里 二
  • SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further

    报错 xff1a SLF4J Failed to load class org slf4j impl StaticLoggerBinder SLF4J Defaulting to no operation NOP logger implem
  • AWVS(Acunetix Web Vulnerability Scanner) Ubuntu下安装依赖缺失问题及解决

    最近在安装使用AWVS时 xff0c 安装出现了如下问题 xff0c 报错 Checking os Checking for dependencies dependency libgtk 3 so 0 not found on the sy
  • busybox login: root Login incorrect

    answer The file etc securetty comes with self explained header 34 etc securetty List of terminals on which root is allow
  • 在Macbook Pro上安装支持GPU的TensorFlow

    上一篇博文 在Macbook Pro上为TensorFlow设置GPU 中 xff0c 我们已经为Macbook上的NVIDIA显卡安装了各种驱动 xff0c 保证各种深度学习框架能够使用GPU进行计算 这儿就总结一下在后续安装Tensor
  • EXCEL 2016常用知识--Excel基础操作

    从数据填充开始讲起 xff0c 介绍Excel内置各种功能 xff0c 如筛选 查询 粘贴 单元格类型等 excel 窗口组成介绍 xff1a 1 快速访问工具栏 xff1a 添加你常用的命令 xff0c 方便我们快速操作和访问 xff1b
  • vue安装scss时报The “path“ argument must be of type string. Received undefined

    在安装vue安装scss时报The path argument must be of type string Received undefined 解决方式 xff1a 这个错误是sass loader 版本造成的 xff0c 此时的版本是
  • 利用python语言制作简单的音乐播放器

    from tkinter import from tkinter import filedialog from pygame locals import import time import pygame import sys pygame
  • 支撑程序员的三种精神

    我注意到有三种精神指引着软件开发人员的灵魂 伟大的艺术家精神 xff0c 可信赖的员工精神和自私的实用主义精神 伟大的艺术家精神 如果你听到一种声音说 你不能这样画 xff0c 然后 xff0c 你继续这样画 xff0c 这种反对的声音就会
  • 小白都懂的Python爬虫之网易云音乐下载

    微信又改版了 xff0c 为了方便第一时间看到我们的推送 xff0c 请按照下列操作 xff0c 设置 置顶 xff1a 点击上方蓝色字体 程序员之家 点击右上角 点击 设为星标 可以啦 xff0c 让我们继续相互陪伴 源 网络 目标 偶然
  • VBoxManage命令用法详解

    增加一个新的扩展包 VBoxManage extpack install lt vbox extpack gt 卸载指定扩展包 VBoxManage extpack uninstall lt name gt 显示已安装的扩展包 VBoxMa
  • Ubuntu2204之最小化安装操作系统

    目录 安装操作系统 xff08 跳过创建虚拟机 xff09 验证磁盘分区 配置静态IP 开启root登录 配置yum源 安装操作系统 xff08 跳过创建虚拟机 xff09 选择语言 xff1a 英语 下一步 默认安装ubuntu serv
  • 大数据领域三个大的技术方向资料

    大数据领域三个大的技术方向 xff1a 1 Hadoop大数据开发方向 2 数据挖掘 数据分析 amp 机器学习方向 3 大数据运维 amp 云计算方向 大数据学习什么 Python xff1a Python 的排名从去年开始就借助人工智能
  • 【技术栈】Spring环境配置

    1 创建maven环境 2 导入包 lt https mvnrepository com artifact org springframework spring webmvc gt lt dependency gt lt groupId g
  • mysql授权语句说明grant all privileges、创建用户、删除用户

    mysql的赋权语句 xff1a grant all privileges on to 39 root 39 64 39 39 identified by 39 123456 39 with grant option all privile
  • 视频下载网址

    视频下载网址 小视频下载 http www downfi com video V视频助手 xff1a http v ranks xin Video Grabber https www videograbber net zh Eagleget
  • EXCEL 2016常用知识--Excel函数

    必备常用函数教学 xff0c 包括逻辑函数 查找函数 文本函数 数学函数等 1 Excel计算的两种方式 Excel计算的两种方式 xff1a 公式 xff1a 一些运算符和数值组成的数学表达式 函数 xff1a 是Excel内部设置好的运
  • 【VIM】VIM

    vim version 查看vim版本 输入vim进入 xff0c 默认状态下是normal 模式 xff0c 输入的是命令而不是文本 q 退出 q 强制退出 i 进入编辑状态 xff0c 光标前插入 a 进入编辑状态 xff0c 光标前插
  • Windows上获取cpu info, cpuid, cpu id 方法整理

    1 使用cmd获取cpu id 在 CMD中输入如下命令 xff1a wmic cpu get processorid 2 使用源代码编译获取 cpu id xff1a 借码 三个源代码文件 调试通过 原文链接1 原文链接2 get cpu