
如上圖,作業系統可以獲取該CPU型號和記憶體大小的資訊。
那么用BCB該如何獲取呢?
試用了_SYSTEM_INFO和GetSystemInfo,發現回傳的CPU型號資訊是586,而不是core2,怎么辦?
uj5u.com熱心網友回復:
以前用內嵌匯編方式獲取,好象使用指令cpuid不過我感覺windows這些引數一般的注冊表里就可以直接找到
uj5u.com熱心網友回復:
記得可以用 WMI 獲取所有硬體設備資訊,還可以用 JCL 庫(JEDI Library, 開源) ,里面有 Hardware 單元, 能獲硬體資訊。
uj5u.com熱心網友回復:
就是這個cpuid
uj5u.com熱心網友回復:
我來助你classCPUInfo.cpp
#include "classCPUInfo.h" // 取CPU資訊
// #include "classCPUInfo.cpp" // 取CPU資訊
#ifndef _classCPUInfo_cpp_
#define _classCPUInfo_cpp_
/*
www.plm.hk
*/
//-----------------------------------------------------------------
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
classCCPUInfo::classCCPUInfo()
{
cpuname=new char[128];
temp=new char[128];
char OEMString[13];
//OEMString=new char[13];
int iEAXValue,iEBXValue,iECXValue,iEDXValue;
//獲取CPU的品牌:
_asm
{
mov eax,0
cpuid
mov DWORD PTR OEMString,ebx
mov DWORD PTR OEMString+4,edx
mov DWORD PTR OEMString+8,ecx
mov BYTE PTR OEMString+12,0
}
Name=new char[15];
strcpy(Name,OEMString);
// 更多CPU資訊:
_asm
{
mov eax,1
cpuid
mov iEAXValue,eax
mov iEBXValue,ebx
mov iECXValue,ecx
mov iEDXValue,edx
}
MMX=bool(iEDXValue & 0x800000);
iFamily=(0xf00 & iEAXValue)>>8;
FPU=bool(iEDXValue & 0x1);
//H_DMAT(); // 取得CPU資訊,速度稍慢
}
classCCPUInfo::~classCCPUInfo()
{
delete []Name;
delete []cpuname;
delete []temp;
//delete []OEMString;
}
int classCCPUInfo::GetSpeed()
{
int PriorityClass, Priority;
HANDLE hThread,hProcess;
hThread=GetCurrentThread();
hProcess=GetCurrentProcess();
PriorityClass = GetPriorityClass(hProcess);
Priority = GetThreadPriority(hThread);
SetPriorityClass(hProcess, REALTIME_PRIORITY_CLASS);
SetThreadPriority(hThread,THREAD_PRIORITY_TIME_CRITICAL);
long lEAXValue,lEDXValue;
SleepEx(50,false);
_asm
{
xor eax,eax
rdtsc
mov lEAXValue,eax
mov lEDXValue,edx
}
if(SleepEx(500,false)==0)
{
_asm
{
xor eax,eax
rdtsc
sub eax,lEAXValue
sbb edx,lEDXValue
mov lEAXValue, eax
mov lEDXValue, edx
}
}
SetThreadPriority(hThread, Priority);
SetPriorityClass(hProcess, PriorityClass);
return lEAXValue/(1000.0*500);
}
bool classCCPUInfo::withMMX()
{
return MMX;
}
int classCCPUInfo::GetFamily()
{
return iFamily;
}
int classCCPUInfo::GetName(char *name)
{
if(name==NULL) return -1;
strcpy(name,Name);
return 0;
}
int classCCPUInfo::GetTypeName(char *type)
{
// EAX的8到11位表明是幾86:
// 3 - 386
// 4 - i486
// 5 - Pentium
// 6 - Pentium Pro Pentium II
// 2 - Dual Processors
if(type==NULL) return -1;
switch(iFamily)
{
case 2: strcpy(type,"Dual Processors");break;
case 3: strcpy(type,"386");break;
case 4: strcpy(type,"486");break;
case 5: strcpy(type,"Pentium");break;
case 6: strcpy(type,"P2/3,celeron,PPro");break;
case 15: strcpy(type,"P4,P4Celeron");break;
default: strcpy(type,"Unknown Type");
}
return 0;
}
bool classCCPUInfo::hasFPU()
{
return FPU;
}
void classCCPUInfo::H_DMAT(){ // 取得CPU資訊,速度稍慢
memset(cpuname,'\0',128);
memset(temp,'\0',128);
GetName(cpuname);
C_OPHS=String(cpuname); // cpu名字
C_AYUQ=GetSpeed(); // 速度
GetTypeName(temp);
C_XLNA=String(temp); // 型別
C_CFGY=hasFPU(); // HasFPU
C_RBZA=withMMX(); // WithMMX
}
#endif
/*
#include "classCPUInfo.h" // 取CPU資訊
classCCPUInfo * F_HEFM; // 取CPU資訊
void __fastcall TForm1::FormCreate(TObject *Sender)
{
F_HEFM=new classCCPUInfo(); // 取CPU資訊
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormClose(TObject *Sender, TCloseAction &Action)
{
delete F_HEFM; // 取CPU資訊
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
F_HEFM->H_DMAT(); // 取得CPU資訊,速度稍慢
Edit1->Text=F_HEFM->C_OPHS; // cpu名字
Edit2->Text=F_HEFM->C_AYUQ; // 速度
Edit3->Text=F_HEFM->C_XLNA; // 型別
CheckBox1->Checked=F_HEFM->C_CFGY; // HasFPU
CheckBox2->Checked=F_HEFM->C_RBZA; // WithMMX
}
*/
uj5u.com熱心網友回復:
classCPUInfo.h
// #include "classCPUInfo.h" // 取CPU資訊
#ifndef _classCPUInfo_H_
#define _classCPUInfo_H_
//-----------------------------------------------------------------
// CPUInfo.h
// by kkm1982
// C++Builder 6,C++Builder 2010,C++Builder XE下直接運行
//----------------------------------------------------------------
class classCCPUInfo
{
private: // 私有的,private則說明其后的識別符號除了類的成員函式之外,用戶定義的其實體不能參考
bool FPU;
char * Name;
bool MMX;
int iFamily;
char * cpuname;
char * temp;
//char * OEMString;
public: // 公有的,public意味著其后的識別符號可以被用戶定義的其實體參考
//---------------------------------------------------------------------------
String C_OPHS; // cpu名字
int C_AYUQ; // 速度
String C_XLNA; // 型別
bool C_CFGY; // HasFPU
bool C_RBZA; // WithMMX
//---------------------------------------------------------------------------
int GetTypeName(char *type);
int GetName(char *name);
int GetFamily();
bool withMMX();
bool hasFPU();
int GetSpeed();
classCCPUInfo();
virtual ~classCCPUInfo();
void H_DMAT(); // 取得CPU資訊,速度稍慢
};
#endif
uj5u.com熱心網友回復:
根據我的一貫風格,注釋也很詳細啊用法樓主應該會了,把.cpp檔案加入工程,然后
#include "classCPUInfo.h" // 取CPU資訊
classCCPUInfo * F_HEFM; // 取CPU資訊
void __fastcall TForm1::FormCreate(TObject *Sender)
{
F_HEFM=new classCCPUInfo(); // 取CPU資訊
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormClose(TObject *Sender, TCloseAction &Action)
{
delete F_HEFM; // 取CPU資訊
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
F_HEFM->H_DMAT(); // 取得CPU資訊,速度稍慢
Edit1->Text=F_HEFM->C_OPHS; // cpu名字
Edit2->Text=F_HEFM->C_AYUQ; // 速度
Edit3->Text=F_HEFM->C_XLNA; // 型別
CheckBox1->Checked=F_HEFM->C_CFGY; // HasFPU
CheckBox2->Checked=F_HEFM->C_RBZA; // WithMMX
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/120961.html
標籤:基礎類
上一篇:這個錯誤怎么改啊?
下一篇:BCB下在Opengl中輸出字符
