如何在VC下得到CPU温度和风扇转速?
如题。在网上找了一下午也未果,好像可以通过WMI获取,但是只找到VBS的示例而且不能顺利运行。
请成功过的大侠帮个忙,最好是有示例,谢谢!
[解决办法]
这个地方有下载
http://www.pudn.com/downloads63/sourcecode/delphi_control/detail221058.html
如果不能下 留邮箱 我给你发过去
[解决办法]
俺也想看看,最近正研究呢。
[解决办法]
帮顶and学习。
[解决办法]
Dim u, s, CPUTemperatureSet mCPU=GetObject("winmgmts:{impersonationLevel=impersonate}!root\wmi").ExecQuery("Select CurrentTemperature From MSAcpi_ThermalZoneTemperature") For Each u In mCPU s=s&u.CurrentTemperature NextSet mCPU=Nothing CPUTemperature=(s-2732)/10 MsgBox "The Current Temperature of Your CPU is: "&CPUTemperature&"℃"
[解决办法]
HRESULT hres;
// Step 1: --------------------------------------------------
// Initialize COM. ------------------------------------------
hres = CoInitializeEx(0, COINIT_MULTITHREADED);
if (!FAILED(hres))
{
// Step 2: --------------------------------------------------
// Set general COM security levels --------------------------
// Note: If you are using Windows 2000, you need to specify -
// the default authentication credentials for a user by using
// a SOLE_AUTHENTICATION_LIST structure in the pAuthList ----
// parameter of CoInitializeSecurity ------------------------
hres = CoInitializeSecurity(NULL, -1, // COM authentication
NULL, // Authentication services
NULL, // Reserved
RPC_C_AUTHN_LEVEL_DEFAULT, // Default authentication
RPC_C_IMP_LEVEL_IMPERSONATE, // Default Impersonation
NULL, // Authentication info
EOAC_NONE, // Additional capabilities
NULL // Reserved
);
if (!FAILED(hres))
{
// Step 3: ---------------------------------------------------
// Obtain the initial locator to WMI -------------------------
IWbemLocator *pLoc = NULL;
hres = CoCreateInstance( CLSID_WbemLocator,0,CLSCTX_INPROC_SERVER,IID_IWbemLocator,(LPVOID *) &pLoc);
if (!FAILED(hres))
{
// CString sql;
// Step 4: -----------------------------------------------------
// Connect to WMI through the IWbemLocator::ConnectServer method
IWbemServices *pSvc = NULL;
// Connect to the root\cimv2 namespace with
// the current user and obtain pointer pSvc
// to make IWbemServices calls.
hres = pLoc->ConnectServer(
_bstr_t(L"ROOT\\CIMV2"), // Object path of WMI namespace
NULL, // User name. NULL = current user
NULL, // User password. NULL = current
0, // Locale. NULL indicates current
NULL, // Security flags.
0, // Authority (e.g. Kerberos)
0, // Context object
&pSvc // pointer to IWbemServices proxy
);
if (!FAILED(hres))
{
hres = CoSetProxyBlanket(
pSvc, // Indicates the proxy to set
RPC_C_AUTHN_WINNT, // RPC_C_AUTHN_xxx
RPC_C_AUTHZ_NONE, // RPC_C_AUTHZ_xxx
NULL, // Server principal name
RPC_C_AUTHN_LEVEL_CALL, // RPC_C_AUTHN_LEVEL_xxx
RPC_C_IMP_LEVEL_IMPERSONATE, // RPC_C_IMP_LEVEL_xxx
NULL, // client identity
EOAC_NONE // proxy capabilities
);
if (!FAILED(hres))
{
// Step 6: --------------------------------------------------
// Use the IWbemServices pointer to make requests of WMI ----
// For example, get the name of the operating system
IEnumWbemClassObject* pEnumerator = NULL;
//////////////////////CPU:检测单个
str="CPU NOT FOUND";
hres = pSvc->ExecQuery( bstr_t("WQL"), bstr_t("Select * from Win32_Processor"),
WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY, NULL, &pEnumerator);
if (!FAILED(hres))
{
// Step 7: -------------------------------------------------
// Get the data from the query in step 6 -------------------
IWbemClassObject *pclsObj;
ULONG uReturn = 0;
if(pEnumerator)//while
{
HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1,
&pclsObj, &uReturn);
if(0 != uReturn)
{
VARIANT vtProp;
VariantInit(&vtProp);
hr = pclsObj->Get(L"NAME", 0, &vtProp, 0, 0);
// Get the value of the MacAddress property
if(vtProp.boolVal)
{
//_bstr_t bstrYourString = vtProp.bstrVal;
//str = (LPCTSTR)bstrYourString;
str=(LPCTSTR)(_bstr_t)vtProp.bstrVal;
}
hr = pclsObj->Get(L"MaxClockSpeed", 0, &vtProp, 0, 0);
// Get the value of the MacAddress property
if(vtProp.boolVal)
{
str.Format ("%s %d MHz",str,vtProp.iVal );
}
VariantClear(&vtProp);
}
}//end while
pEnumerator->Release();
}
///////////////////////
}
else//step6
{
m_update_time.SetWindowTextA ("Could not set proxy blanket.");
pSvc->Release();
pLoc->Release();
CoUninitialize();
// break;
}
}
else//step5
{
m_update_time.SetWindowTextA ("Could not connect.");
pLoc->Release();
CoUninitialize();
}
}
else//step4
{
m_update_time.SetWindowTextA ("Failed to create IWbemLocator object.");
CoUninitialize();
}
}
else//step3
{
m_update_time.SetWindowTextA ("Failed to initialize security.");
CoUninitialize();
}
}
else//step2
{
m_update_time.SetWindowTextA ("Failed to initialize COM library.");
}
一个比较完整的过程。
具体参数,请用WMI TOOL查看。