c++ 直接读取 cpu id

c++ 直接读取 cpu id

复制代码
#include <iostream>
using namespace std;
#include  <string>
#include <comutil.h>
#include "Windows.h"
#include <atlconv.h>
#include <intrin.h>
#include <cctype>
#include <iomanip>

char* get_cpuid(char *pCpuId);
void getcpuid(unsigned int *CPUInfo, unsigned int InfoType);
void getcpuidex(unsigned int *CPUInfo, unsigned int InfoType, unsigned int ECXValue);

int main()
{
    char pCpuId[32] = "";
    get_cpuid(pCpuId);
    cout << pCpuId << endl;
    system("pause");
        return 1;
}

char* get_cpuid(char *pCpuId)
{

    int dwBuf[4];
    getcpuid((unsigned int *)dwBuf, 1);
    sprintf(pCpuId, "%08X", dwBuf[3]);
    sprintf(pCpuId + 8, "%08X", dwBuf[0]);
    return pCpuId;
}

void getcpuid(unsigned int *CPUInfo, unsigned int InfoType)
{
#if defined(__GNUC__)// GCC  
    __cpuid(InfoType, CPUInfo[0], CPUInfo[1], CPUInfo[2], CPUInfo[3]);
#elif defined(_MSC_VER)// MSVC  
#if _MSC_VER >= 1400 //VC2005才支持__cpuid  
    __cpuid((int*)(void*)CPUInfo, (int)(InfoType));
#else //其他使用getcpuidex  
    getcpuidex(CPUInfo, InfoType, 0);
#endif  
#endif  
}

void getcpuidex(unsigned int *CPUInfo, unsigned int InfoType, unsigned int ECXValue)
{
#if defined(_MSC_VER) // MSVC  
#if defined(_WIN64) // 64位下不支持内联汇编. 1600: VS2010, 据说VC2008 SP1之后才支持__cpuidex.  
    __cpuidex((int*)(void*)CPUInfo, (int)InfoType, (int)ECXValue);
#else  
    if (NULL == CPUInfo)
        return;
    _asm {
        // load. 读取参数到寄存器.  
        mov edi, CPUInfo;
        mov eax, InfoType;
        mov ecx, ECXValue;
        // CPUID  
        cpuid;
        // save. 将寄存器保存到CPUInfo  
        mov[edi], eax;
        mov[edi + 4], ebx;
        mov[edi + 8], ecx;
        mov[edi + 12], edx;
    }
#endif  
#endif  
}
复制代码

 

posted on   okgogo2000  阅读(191)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

点击右上角即可分享
微信分享提示