读书人

怎么在VC下得到CPU温度和风扇转速

发布时间: 2012-03-14 12:01:12 作者: rapoo

如何在VC下得到CPU温度和风扇转速?
如题。在网上找了一下午也未果,好像可以通过WMI获取,但是只找到VBS的示例而且不能顺利运行。
请成功过的大侠帮个忙,最好是有示例,谢谢!

[解决办法]
这个地方有下载
http://www.pudn.com/downloads63/sourcecode/delphi_control/detail221058.html
如果不能下 留邮箱 我给你发过去
[解决办法]
俺也想看看,最近正研究呢。

[解决办法]
帮顶and学习。
[解决办法]

探讨
这个地方有下载
http://www.pudn.com/downloads63/sourcecode/delphi_control/detail221058.html
如果不能下  留邮箱  我给你发过去

[解决办法]
这个信息很多需要主板等支持.
[解决办法]
我前一段时间研究过这个问题,用wmi获取的数值是有问题的,因为很多主板驱动,准确的说是主板上探测器的驱动并未提供WMI调用接口。很多程序都是自己实现的驱动程序,根据不同的主板探测器来动态实时读取,winbond网站以前有个牛人写了驱动代码,google一下可以找到。或者自己逆向一个类似软件的驱动,直接用他的驱动,如优化大师的驱动,有时间我准备逆向一下,不知有同道否?
[解决办法]
我有调试好的wmi代码,可惜无法用
[解决办法]
这种问题我肯定会第一个选择用WMI,准不准再说
[解决办法]
在Root\Cimv2的Win32_Processor名称空间下

这些信息都是有的



这是我用WMITools看的

Root\wmi下面有温度,Select CurrentTemperature From MSAcpi_ThermalZoneTemperature可以得到各个温度对象

参考这个vbs

VBScript code
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查看。

读书人网 >VC/MFC

热点推荐