zoukankan      html  css  js  c++  java
  • C++ 嵌入汇编 获取CPU信息

     1 #include "windows.h" 
     2 #include "iostream"
     3 #include "string"
     4 
     5 using namespace std;
     6 
     7 //用来存储信息
     8 DWORD deax;
     9 DWORD debx;
    10 DWORD decx;
    11 DWORD dedx;
    12 
    13 void ExeCPUID(DWORD veax)//初始化CPU
    14 {
    15 __asm
    16 {
    17    mov eax,veax
    18     cpuid
    19     mov deax,eax
    20     mov debx,ebx
    21     mov decx,ecx
    22     mov dedx,edx
    23 }
    24 }
    25 
    26 long GetCPUFreq()//获取CPU频率,单位: MHZ
    27 {
    28 int start1,start2;
    29 _asm rdtsc
    30    _asm mov start1,eax
    31    Sleep(50);
    32 _asm rdtsc
    33    _asm mov start2,eax
    34    return (start2-start1)/50000;
    35 }
    36 
    37 string GetManID()//获取制造商信息
    38 {
    39 char ID[25];//存储制造商信息
    40 memset(ID,0,sizeof(ID));//先清空数组 ID
    41 ExeCPUID(0);//初始化
    42 memcpy(ID+0,&debx,4);//制造商信息前四个字符复制到数组
    43 memcpy(ID+4,&dedx,4);//中间四个
    44 memcpy(ID+8,&decx,4);//最后四个
    45 //如果返回 char * ,会出现乱码;故返回 string 值
    46 return string(ID);
    47 }
    48 
    49 string GetCPUType()
    50 {
    51 const DWORD id = 0x80000002; //从0x80000002开始,到0x80000004结束
    52 char CPUType[49];//用来存储CPU型号信息
    53 memset(CPUType,0,sizeof(CPUType));//初始化数组
    54 
    55 for(DWORD t = 0 ; t < 3 ; t++ )
    56 {
    57    ExeCPUID(id+t);
    58    //每次循环结束,保存信息到数组
    59    memcpy(CPUType+16*t+ 0,&deax,4);
    60    memcpy(CPUType+16*t+ 4,&debx,4);
    61    memcpy(CPUType+16*t+ 8,&decx,4);
    62    memcpy(CPUType+16*t+12,&dedx,4);
    63 }
    64 
    65 return string(CPUType);
    66 }
    67 
    68 void main() 
    69 { 
    70 cout<<"本机CPU信息如下:"<<endl;
    71 cout<<"CPU 主 频: "<<GetCPUFreq()<<" MHZ"<<endl;
    72 cout<<"CPU 制造商: "<<GetManID()<<endl;
    73 cout<<"CPU 型 号: "<<GetCPUType()<<endl;
    74 cin.get();
    75 
    76 }
     
  • 相关阅读:
    mysql给数据库字段赋值为随机数
    利用lList集合中的subList进行分页
    redis中分页缓存数据
    ios账号第三方登录,判断是否是Ios账号
    通过ip查询ip地址
    MySQL
    排序算法
    139. 单词拆分
    138. 复制带随机指针的链表
    137. 只出现一次的数字 II
  • 原文地址:https://www.cnblogs.com/msdn1433/p/3666706.html
Copyright © 2011-2022 走看看