首页 > 编程语言 >c++ 直接读取 cpu id

c++ 直接读取 cpu id

时间:2024-02-21 09:33:59浏览次数:27  
标签:CPUInfo InfoType int unsigned c++ cpu include id mov

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  
}

 

标签:CPUInfo,InfoType,int,unsigned,c++,cpu,include,id,mov
From: https://www.cnblogs.com/okgogo2000/p/18024478

相关文章

  • C++多线程 第八章 设计并发代码
    第八章设计并发代码数据划分工作在处理开始前在线程间划分数据方面,C++与MPI或OpenMP的方式较为相似.一个任务被分成一个并行任务集,工作的线程独立运行这些任务.并且在最后的化简步骤中合并这些结果.尽管这种方法是很有效的,但是只有在数据可以实现划分时,才可如此.考虑这......
  • andorid开发--记账本(六)
    完成了记录支出页面的逻辑编写此时项目结构adapter适配器包RecordPagerAdapter.javapackagecom.example.myapplication.adapter;importandroidx.annotation.NonNull;importandroidx.annotation.Nullable;importandroidx.fragment.app.Fragment;importandroidx.fra......
  • Android家庭记账本开发第五天:ListAdapter适配器的编写
    昨天讲了数据库相关的操作现在来看我们当初在MainActivity的Java文件当中的initData方法:1@SuppressLint("Range")2privatevoidinitData(){3helper=newDBHelper(MainActivity.this);4list=newArrayList<>();5SQLiteDatabasedb=h......
  • IDEA插件推荐:免费好用!
    IDEA是一款功能强大的集成开发环境(IDE),它可以帮助开发人员更加高效地编写、调试和部署软件应用程序。我们在编写完接口代码后需要进行接口调试等操作,一般需要打开额外的调试工具。今天给大家介绍一款IDEA插件:Apipost-Helper-2.0。代码写完直接编辑器内调试、还支持生成接口文档、......
  • 【C++】判断回文字符串。回文指的是顺读和逆读都一样的字符串。例如,“tot”和“otto”
    //判断字符串是否是回文字符串(考虑大小写,空格和标点符号)boolpalindrome1(string&str){stringret;for(auto&c:str){if(isalpha(c)){if(isupper(c)){ret.push_back(tolower(c));}else{ret.push_back(c);}......
  • RFID射频信号的耦合与传输工作频率,增加读写成功,规避读写失败
    RFID射频信号的耦合与传输工作频率:RFID读写器通过天线RFID标签的工作频率通常分为低频、高频和超高频三种。如下:1、低频标签的工作频率为125kHz,识别距离一般在10cm以内。2、高频标签的工作频率为13.56MHz,识别距离一般在1米以内。3、超高频标签的工作频率为860MHz—960MHz,......
  • solidity实现批量转账
    直接贴代码://SPDX-License-Identifier:MITpragmasolidity^0.8.0;contractBatchTransfer{eventTransfer(addressindexedfrom,addressindexedto,uint256value);constructor()payable{}functionbatchTransfer(addresspayable[]memory_r......
  • orchard core 开启openid 使用uniapp结合oidc-client 作为客户端连接
    官方的项目地址:https://github.com/onestar1/OrchardSkills.OrchardCore.OIDC.Vue.js/tree/main/OrchardSkills.OrchardCore.MaterialDesignTheme操作步骤:单独clonehttps://github.com/OrchardSkills/OrchardSkills.OrchardCore.MaterialDesignTheme1、打开Recipes目录下......
  • C++ 模板的笔记2
    C++模板的笔记2关于可变参函数模板借鉴了一部分笔记,感谢大佬类模板中的嵌套类模板可以嵌套其他类模板,就像普通类可以嵌套其他普通类一样。嵌套的类模板可以访问外部类模板的成员,包括私有成员。示例:#include<iostream>usingnamespacestd;template<typenameT>classO......
  • Android 《ViewPager 实现引导页》
    布局文件activity_launch_simple.xml<?xmlversion="1.0"encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xm......