//getCPUNumber.cpp
1 //用来存储信息 2 DWORD deax; 3 DWORD debx; 4 DWORD decx; 5 DWORD dedx; 6 7 void ExeCPUID(DWORD veax)//初始化CPU 8 { 9 __asm 10 { 11 mov eax,veax 12 cpuid 13 mov deax,eax 14 mov debx,ebx 15 mov decx,ecx 16 mov dedx,edx 17 } 18 } 19 20 long GetCPUFreq()//获取CPU频率,单位: MHZ 21 { 22 int start1,start2; 23 _asm rdtsc 24 _asm mov start1,eax 25 Sleep(50); 26 _asm rdtsc 27 _asm mov start2,eax 28 return (start2-start1)/50000; 29 } 30 31 string GetManID()//获取制造商信息 32 { 33 char ID[25];//存储制造商信息 34 memset(ID,0,sizeof(ID));//先清空数组 ID 35 ExeCPUID(0);//初始化 36 memcpy(ID+0,&debx,4);//制造商信息前四个字符复制到数组 37 memcpy(ID+4,&dedx,4);//中间四个 38 memcpy(ID+8,&decx,4);//最后四个 39 //如果返回 char * ,会出现乱码;故返回 string 值 40 return string(ID); 41 } 42 43 string GetCPUType() 44 { 45 const DWORD id = 0x80000002; //从0x80000002开始,到0x80000004结束 46 char CPUType[49];//用来存储CPU型号信息 47 memset(CPUType,0,sizeof(CPUType));//初始化数组 48 49 for(DWORD t = 0 ; t < 3 ; t++ ) 50 { 51 ExeCPUID(id+t); 52 //每次循环结束,保存信息到数组 53 memcpy(CPUType+16*t+ 0,&deax,4); 54 memcpy(CPUType+16*t+ 4,&debx,4); 55 memcpy(CPUType+16*t+ 8,&decx,4); 56 memcpy(CPUType+16*t+12,&dedx,4); 57 } 58 59 return string(CPUType); 60 } 61 62 63 CString GetMac_SN() 64 { 65 unsigned long s1,s2; 66 CString str; 67 68 __asm 69 { 70 mov eax,0x01 71 xor edx,edx 72 cpuid 73 mov s1,edx 74 mov s2,eax 75 } 76 77 str.Format(_T("%08X%08X"), s1, s2); 78 79 return str; 80 }
//使用上面的函数
1 void CgetCPUNumberDlg::OnBnClickedButton2() 2 { 3 // TODO: 在此添加控件通知处理程序代码; 4 5 CString strCPUID = GetMac_SN(); 6 7 CEdit* pEdit = (CEdit*)GetDlgItem(IDC_EDIT1); 8 pEdit->SetWindowText(strCPUID); 9 10 CEdit* pEdit2 = (CEdit*)GetDlgItem(IDC_EDIT2); 11 12 long nFreq = GetCPUFreq(); //主频; 13 string strMaker = GetManID(); //制造商; 14 string strModel = GetCPUType(); //型号; 15 16 CString str("本机CPU信息如下:\r\n\r\n"); 17 CString strTemp; 18 strTemp.Format(_T("%d"), nFreq); 19 20 str += "主频:"; 21 str += strTemp; 22 23 str += "\r\n\r\n"; 24 str += "制造商:"; 25 str += CString(strMaker.c_str()); 26 27 str += "\r\n\r\n"; 28 str += "型号:"; 29 str += CString(strModel.c_str()); 30 31 32 pEdit2->SetWindowText(str); 33 34 }
//复制到剪贴板
1 void CgetCPUNumberDlg::OnBnClickedButton1() 2 { 3 // TODO: 在此添加控件通知处理程序代码; 4 5 BOOL ret = OpenClipboard(); 6 if (ret != TRUE) 7 { 8 AfxMessageBox(_T("打开剪贴板失败!")); 9 return; 10 } 11 12 EmptyClipboard(); 13 14 HGLOBAL hMem = GlobalAlloc(GMEM_MOVEABLE, 1024); 15 16 LPVOID pMem = GlobalLock(hMem); 17 18 char* pBuf = (char*)pMem; 19 20 CString strCPUID; 21 GetDlgItemText(IDC_EDIT1, strCPUID); 22 23 int i = 0; 24 int len = strCPUID.GetLength(); 25 char chArray[1024] = {0}; 26 27 for (i = 0; i < len; ++ i ) 28 { 29 chArray[i] = strCPUID.GetAt(i); 30 } 31 chArray[i] = '\0'; 32 33 strcpy_s(pBuf, (len + 1) * sizeof(char), chArray); //把EDIT控件中的数据拷贝至指针指向的内存区域; 34 35 GlobalUnlock(hMem); 36 37 38 HANDLE hClipboard = SetClipboardData(CF_TEXT, hMem); 39 40 CloseClipboard(); 41 }