怎样用C语言获取硬件信息
1个回答
展开全部
这个需要相关的库以及API。
给你一个获取cpu信息的函数:
DWORD MyCPUSpeed()
{
BSTR bstrMyCpuName=NULL;
BSTR bstrMyPropName = NULL;
VARIANT pVal;
char szText[256];
int nCpuNum=0;
DWORD dwCpuSpeed=0;
IWbemClassObject *pCpuInst=NULL;
IWbemQualifierSet* pQualSet = NULL;
bstrMyPropName = SysAllocString(L"CurrentClockSpeed");
if(bstrMyPropName == NULL)
{
return dwCpuSpeed;
}
while(1)
{
memset(szText,0,sizeof(szText));
sprintf(szText,"Win32_Processor.DeviceID=\"CPU%d\"", nCpuNum);
bstrMyCpuName = _bstr_t(szText);
if(bstrMyCpuName == NULL)
{
break;
}
if(pSvc->GetObject(bstrMyCpuName, 0L, NULL, &pCpuInst, NULL) != S_OK)
{
break;
}
if(pCpuInst->Get(bstrMyPropName, 0L, &pVal, NULL, NULL) == S_OK)
{
nCpuNum++;
dwCpuSpeed += pVal.lVal;
}
VariantClear(&pVal);
SysFreeString(bstrMyCpuName);
bstrMyCpuName = NULL;
}
if(bstrMyPropName != NULL)
{
SysFreeString(bstrMyPropName);
bstrMyPropName = NULL;
}
if(bstrMyCpuName != NULL)
{
SysFreeString(bstrMyCpuName);
bstrMyCpuName = NULL;
}
memset(szText,0,sizeof(szText));
sprintf(szText, "CPU个数: %d, CPU频率: %ld Mhz", nCpuNum, dwCpuSpeed);
cout<<"------------------------"<<szText<<"-------------------------"<<endl;
return dwCpuSpeed;
}
给你一个获取cpu信息的函数:
DWORD MyCPUSpeed()
{
BSTR bstrMyCpuName=NULL;
BSTR bstrMyPropName = NULL;
VARIANT pVal;
char szText[256];
int nCpuNum=0;
DWORD dwCpuSpeed=0;
IWbemClassObject *pCpuInst=NULL;
IWbemQualifierSet* pQualSet = NULL;
bstrMyPropName = SysAllocString(L"CurrentClockSpeed");
if(bstrMyPropName == NULL)
{
return dwCpuSpeed;
}
while(1)
{
memset(szText,0,sizeof(szText));
sprintf(szText,"Win32_Processor.DeviceID=\"CPU%d\"", nCpuNum);
bstrMyCpuName = _bstr_t(szText);
if(bstrMyCpuName == NULL)
{
break;
}
if(pSvc->GetObject(bstrMyCpuName, 0L, NULL, &pCpuInst, NULL) != S_OK)
{
break;
}
if(pCpuInst->Get(bstrMyPropName, 0L, &pVal, NULL, NULL) == S_OK)
{
nCpuNum++;
dwCpuSpeed += pVal.lVal;
}
VariantClear(&pVal);
SysFreeString(bstrMyCpuName);
bstrMyCpuName = NULL;
}
if(bstrMyPropName != NULL)
{
SysFreeString(bstrMyPropName);
bstrMyPropName = NULL;
}
if(bstrMyCpuName != NULL)
{
SysFreeString(bstrMyCpuName);
bstrMyCpuName = NULL;
}
memset(szText,0,sizeof(szText));
sprintf(szText, "CPU个数: %d, CPU频率: %ld Mhz", nCpuNum, dwCpuSpeed);
cout<<"------------------------"<<szText<<"-------------------------"<<endl;
return dwCpuSpeed;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询