Panel użytkownika
Nazwa użytkownika:
Hasło:
Nie masz jeszcze konta?

Jak użyć Win32_TemperatureProbe Class

Ostatnio zmodyfikowano 2011-06-07 13:27
Autor Wiadomość
GoldWolf
Temat założony przez niniejszego użytkownika
Jak użyć Win32_TemperatureProbe Class
» 2011-06-06 12:55:50
Jakaś mała podpowiedź jak wyciągnąć z tej kasy info o temp. CPU?
http://msdn.microsoft.com/en-us/library/aa394493(VS.85).aspx

Trafiłem też na coś takiego:
http://www.c-plusplus.de/forum/82851

Nadal szukam, żeby nie było ;-).
P-33904
DejaVu
» 2011-06-06 13:31:50
P-33905
GoldWolf
Temat założony przez niniejszego użytkownika
» 2011-06-06 14:16:17
Na to trafiłem, ale nie wiele mi to na razie daje, dodatkowo mam błąd jakiś. Jeszcze szukam innego rozwiązania, ale nic w sieci nie znalazłem.
P-33906
DejaVu
» 2011-06-06 14:17:11
Nie ma innego rozwiązania niż dostęp poprzez WMI.
P-33907
GoldWolf
Temat założony przez niniejszego użytkownika
» 2011-06-06 14:26:25
Unhandled exception at 0x00412058 in konsola.exe: 0xC0000005: Access violation reading location 0xfeeefef6.

Jutro się tym zajmę. Tak ale może z Biosu będę te dane zaczytywać dodając kod Assemblera. Miałem już kiedyś takie rozwiązanie, ale służyło do odczyty innych parametrów.

Program się odpala tylko jest błąd jw.
P-33908
GoldWolf
Temat założony przez niniejszego użytkownika
» 2011-06-07 08:53:29
Dobra doszedłem do punktu 4, jakieś wskazówki by się przydały.

http://msdn.microsoft.com/en-us/library/aa389762%28v=vs.85%29.aspx

Na razie tak to wygląda w kodzie:
C/C++
#define _WIN32_DCOM
#include <iostream>
using namespace std;
#include <wbemidl.h>
#include <windows.h>
# pragma comment(lib, "wbemuuid.lib")

#include <conio.h>

int main() {
   
    HRESULT hr;
    hr = CoInitializeEx( 0, COINIT_MULTITHREADED );
    if( FAILED( hr ) ) {
        cout << "Failed to initialize COM library. Error code = 0x"
        << hex << hr << endl;
        return hr;
    }
   
    hr = CoInitializeSecurity(
    NULL, // Security descriptor   
    - 1, // COM negotiates authentication service
    NULL, // Authentication services
    NULL, // Reserved
    RPC_C_AUTHN_LEVEL_DEFAULT, // Default authentication level for proxies
    RPC_C_IMP_LEVEL_IMPERSONATE, // Default Impersonation level for proxies
    NULL, // Authentication info
    EOAC_NONE, // Additional capabilities of the client or server
    NULL ); // Reserved
   
    if( FAILED( hr ) ) {
        cout << "Failed to initialize security. Error code = 0x"
        << hex << hr << endl;
        CoUninitialize();
        return hr; // Program has failed.
    }
   
    IWbemLocator * pLoc = 0;
    //HRESULT hr;
   
    hr = CoCreateInstance( CLSID_WbemLocator, 0,
    CLSCTX_INPROC_SERVER, IID_IWbemLocator,( LPVOID * ) & pLoc );
   
    if( FAILED( hr ) ) {
        cout << "Failed to create IWbemLocator object. Err code = 0x"
        << hex << hr << endl;
        CoUninitialize();
        return hr; // Program has failed.
    }
   
    IWbemServices * pSvc = 0;
   
    // Connect to the root\default namespace with the current user.
    hr = pLoc->ConnectServer(
    BSTR( L"ROOT\\DEFAULT" ),
    NULL, NULL, 0, NULL, 0, 0, & pSvc );
   
    if( FAILED( hr ) ) {
        cout << "Could not connect. Error code = 0x"
        << hex << hr << endl;
        pLoc->Release();
        CoUninitialize();
        return hr; // Program has failed.
    }
   
    cout << "Connected to WMI" << endl;
   
    //IWbemServices *pSvc = 0;
    //IWbemLocator *pLoc = 0;
   
    // Set the proxy so that impersonation of the client occurs.
    HRESULT hres;
    hres = CoSetProxyBlanket( pSvc,
    RPC_C_AUTHN_WINNT,
    RPC_C_AUTHZ_NONE,
    NULL,
    RPC_C_AUTHN_LEVEL_CALL,
    RPC_C_IMP_LEVEL_IMPERSONATE,
    NULL,
    EOAC_NONE
    );
   
    if( FAILED( hres ) ) {
        cout << "Could not set proxy blanket. Error code = 0x"
        << hex << hres << endl;
        pSvc->Release();
        pLoc->Release();
        CoUninitialize();
        return hres; // Program has failed.
    }
   
    getch();
    return 0;
}

Z tego wszystkiego do przypadkowo trafiłem na program do monitorowania obciążenia CPU, a tak przynajmniej sądzę, że to jest to ;-), przyda się.

C/C++
CoInitialize( NULL );
if( CoInitializeSecurity( NULL, - 1, NULL, NULL, RPC_C_AUTHN_LEVEL_PKT,
RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE, 0 ) != S_OK )
     return 0;

IWbemLocator * pIWbemLocator = NULL;
IWbemServices * pWbemServices = NULL;
IEnumWbemClassObject * pEnumObject = NULL;

BSTR bstrNamespace =( L"root\\cimv2" );

if( CoCreateInstance(
CLSID_WbemAdministrativeLocator,
NULL,
CLSCTX_INPROC_SERVER | CLSCTX_LOCAL_SERVER,
IID_IUnknown,
( void ** ) & pIWbemLocator ) != S_OK )
     return 0;

if( pIWbemLocator->ConnectServer( bstrNamespace, NULL, NULL, NULL, 0,
NULL, NULL, & pWbemServices ) != S_OK )
     return 0;

HRESULT hRes;
BSTR strQuery =( L"Select * from win32_Processor" );
BSTR strQL =( L"WQL" );
hRes = pWbemServices->ExecQuery( strQL, strQuery,
WBEM_FLAG_RETURN_IMMEDIATELY, NULL, & pEnumObject );
if( hRes != S_OK )
{
    //    MessageBox("Could not execute Query");
    return 0;
}

hRes = pEnumObject->Reset();
if( hRes != S_OK )
{
    //  MessageBox("Could not Enumerate");
    return 0;
}

ULONG uCount = 1, uReturned;
IWbemClassObject * pClassObject = NULL;
hRes = pEnumObject->Next( WBEM_INFINITE, uCount, & pClassObject, & uReturned );
if( hRes != S_OK )
{
    //MessageBox("Could not Enumerate");
    return 0;
}

VARIANT v;
BSTR strClassProp = SysAllocString( L"LoadPercentage" );
hRes = pClassObject->Get( strClassProp, 0, & v, 0, 0 );
if( hRes != S_OK )
{
    // MessageBox("Could not Get Value");
    return 0;
}
//BSTR Bstr = &v;
SysFreeString( strClassProp );
_bstr_t bstrPath = & v; //Just to convert BSTR to ANSI
//_bstr_t bstrPath(Bstr);
char * strPath =( char * ) bstrPath;

if( SUCCEEDED( hRes ) )
     MessageBoxA( NULL, strPath, 0, 0 );
else
     MessageBoxA( NULL, "Error in getting object", 0, 0 );

VariantClear( & v );
pIWbemLocator->Release();
pWbemServices->Release();
pEnumObject->Release();
pClassObject->Release();
CoUninitialize();
P-33928
GoldWolf
Temat założony przez niniejszego użytkownika
» 2011-06-07 10:02:49
Dobra trafiłem na rozwiązanie, to co wcześniej pokazywało obciążenie procka, może też pokazywać temp. Tylko jest jakiś błąd:
C/C++
//bazuje na tym - http://msdn.microsoft.com/en-us/library/aa389762%28v=vs.85%29.aspx
#define _WIN32_DCOM
#include <iostream>
using namespace std;
#include <wbemidl.h>
#include <windows.h>
#include <Winuser.h>
#include <comdef.h>
# pragma comment(lib, "wbemuuid.lib")

#include <conio.h>

int main() {
   
   
    CoInitialize( NULL );
    if( CoInitializeSecurity( NULL, - 1, NULL, NULL, RPC_C_AUTHN_LEVEL_PKT,
    RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE, 0 ) != S_OK )
         return 0;
   
    IWbemLocator * pIWbemLocator = NULL;
    IWbemServices * pWbemServices = NULL;
    IEnumWbemClassObject * pEnumObject = NULL;
   
    //BSTR bstrNamespace = (L"root\\cimv2");
    BSTR bstrNamespace =( L"root\\WMI" );
   
    if( CoCreateInstance(
    CLSID_WbemAdministrativeLocator,
    NULL,
    CLSCTX_INPROC_SERVER | CLSCTX_LOCAL_SERVER,
    IID_IUnknown,
    ( void ** ) & pIWbemLocator ) != S_OK )
         return 0;
   
    if( pIWbemLocator->ConnectServer( bstrNamespace, NULL, NULL, NULL, 0,
    NULL, NULL, & pWbemServices ) != S_OK )
         return 0;
   
    HRESULT hRes;
   
    //BSTR strQuery = (L"Select * from win32_Processor");
    BSTR strQuery =( L"Select * from MSAcpi_ThermalZoneTemperature" );
    BSTR strQL =( L"WQL" );
    hRes = pWbemServices->ExecQuery( strQL, strQuery,
    WBEM_FLAG_RETURN_IMMEDIATELY, NULL, & pEnumObject );
    if( hRes != S_OK )
    {
        MessageBox( 0, "Could not execute Query", 0, 0 );
        return 0;
    }
   
    hRes = pEnumObject->Reset();
    if( hRes != S_OK )
    {
        MessageBox( 0, "Could not Enumerate", 0, 0 );
        return 0;
    }
    //TU JEST PROBLEM
    ULONG uCount = 1, uReturned;
    IWbemClassObject * pClassObject = NULL;
    hRes = pEnumObject->Next( WBEM_INFINITE, uCount, & pClassObject, & uReturned );
    if( hRes != S_OK )
    {
        MessageBox( 0, "Could not Enumerate - 123", 0, 0 );
        return 0;
    }
   
    VARIANT v;
    BSTR strClassProp = SysAllocString( L"CurrentTemperature" );
    hRes = pClassObject->Get( strClassProp, 0, & v, 0, 0 );
    if( hRes != S_OK )
    {
        MessageBox( 0, "Could not Get Value", 0, 0 );
        return 0;
    }
    SysFreeString( strClassProp );
    _bstr_t bstrPath = & v; //Just to convert BSTR to ANSI
    char * strPath =( char * ) bstrPath;
   
    if( SUCCEEDED( hRes ) )
         MessageBoxA( NULL, strPath, 0, 0 );
    else
         MessageBoxA( NULL, "Error in getting object", 0, 0 );
   
    VariantClear( & v );
    pIWbemLocator->Release();
    pWbemServices->Release();
    pEnumObject->Release();
    pClassObject->Release();
    CoUninitialize();
   
   
    getch();
    return 0;
}
Tutaj jest chyba podpowiedź, ale nie rozkminiłem jej jeszcze.
http://www.eggheadcafe.com/microsoft/Win32-WMI/30861878/cpu-temperature-in-c-using-wmi.aspx

Jak dobrze rozumiem, chodzi o to, że mam kilka rdzeni i każdy ma inną temp., jednak mimo to jest pomiar ogólnej temperatury całego procka. Jednak tak sobie myślę, że rozbicie na poszczególne rdzenie to tez była by niezła gratka.

Tutaj znalazłem jeszcze jeden:
C/C++
#define _WIN32_DCOM
#include <iostream>
using namespace std;
#include <comdef.h>
#include <Wbemidl.h>

# pragma comment(lib, "wbemuuid.lib")

int main( int argc, char ** argv )
{
    CoInitializeEx( 0, COINIT_MULTITHREADED ); // Initialize COM
    try
    {
        HRESULT hres;
       
        hres = CoInitializeSecurity( NULL, - 1, NULL, NULL, RPC_C_AUTHN_LEVEL_DEFAULT,
        RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE, NULL );
        if( FAILED( hres ) )
        {
            cout << "Failed to initialize security. Error code = 0x"
            << hex << hres << endl;
            CoUninitialize();
            return 1; // Program has failed.
        }
       
        IWbemLocator * pLoc = NULL;
        hres = CoCreateInstance( CLSID_WbemLocator, 0, CLSCTX_INPROC_SERVER, IID_IWbemLocator,( LPVOID * ) & pLoc );
       
        if( FAILED( hres ) )
        {
            cout << "Failed to create IWbemLocator object."
            << " Err code = 0x"
            << hex << hres << endl;
            CoUninitialize();
            return 1; // Program has failed.
        }
       
        IWbemServices * pSvc = NULL;
       
        // Connect to the root\WMI namespace with
        // the current user and obtain pointer pSvc
        // to make IWbemServices calls.
        hres = pLoc->ConnectServer( _bstr_t( L"ROOT\\WMI" ), NULL, NULL, 0, NULL, 0, 0, & pSvc );
        if( FAILED( hres ) )
        {
            cout << "Could not connect. Error code = 0x"
            << hex << hres << endl;
            pLoc->Release();
            CoUninitialize();
            return 1; // Program has failed.
        }
       
        hres = CoSetProxyBlanket( pSvc, RPC_C_AUTHN_WINNT, RPC_C_AUTHZ_NONE, NULL,
        RPC_C_AUTHN_LEVEL_CALL, RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE );
       
        if( FAILED( hres ) )
        {
            cout << "Could not set proxy blanket. Error code = 0x"
            << hex << hres << endl;
            pSvc->Release();
            pLoc->Release();
            CoUninitialize();
            return 1; // Program has failed.
        }
       
        IEnumWbemClassObject * pEnumerator = NULL;
       
        hres = pSvc->ExecQuery( bstr_t( "WQL" ), bstr_t( "SELECT * FROM MSAcpi_ThermalZoneTemperature" ),
        WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY, NULL, & pEnumerator );
       
        if( FAILED( hres ) )
        {
            cout << "Query for operating system name failed."
            << " Error code = 0x"
            << hex << hres << endl;
            pSvc->Release();
            pLoc->Release();
            CoUninitialize();
            return 1; // Program has failed.
        }
       
        IWbemClassObject * pclsObj;
        ULONG uReturn = 0;
        SAFEARRAY * allNames;
        while( pEnumerator )
        {
            HRESULT hr = pEnumerator->Next( WBEM_INFINITE, 1, & pclsObj, & uReturn );
            if( 0 == uReturn )
            {
                break;
            }
            /*pclsObj->GetNames(NULL,0,NULL,&allNames);
            for (LONG a = allNames->rgsabound[0].lLbound; a < allNames->rgsabound[0].lLbound + allNames->rgsabound[0].cElements; a++)
            {   
            BSTR name;  
            hres = SafeArrayGetElement(allNames, &a, &name); 
            wcout << name << endl;
            }*/
            VARIANT vtProp;
            VariantInit( & vtProp );
           
            hr = pclsObj->Get( L"CurrentTemperature", 0, & vtProp, 0, 0 );
            wcout << "Current CPU Temperature : " <<( vtProp.intVal - 2732 ) / 10.0 << endl;
           
            VariantClear( & vtProp );
            pclsObj->Release();
        }
       
        // Cleanup
        // ========
       
        pSvc->Release();
        pLoc->Release();
        pEnumerator->Release();
        /*pclsObj->Release();*/
    }
    // In case there was an error, print it out...
    catch( _com_error err )
    {
        printf( "Error occurred: %S", err.ErrorMessage() );
    }
    CoUninitialize();
    return 0;
}
Nic mi nie robi, teraz to mi się już mózg zagotował, jakby co to może, komuś na jednordzeniowym zadziała.
P-33929
GoldWolf
Temat założony przez niniejszego użytkownika
» 2011-06-07 13:27:23
Dobra już skumałem o co chodzi przesyłam poprawny kod, już był wysyłany, ale tak dla podsumowania dam co i jak.
C/C++
//bazuje na tym - http://msdn.microsoft.com/en-us/library/aa389762%28v=vs.85%29.aspx
#define _WIN32_DCOM
#include <iostream>
using namespace std;
#include <wbemidl.h>
#include <windows.h>
#include <Winuser.h>
#include <comdef.h>
# pragma comment(lib, "wbemuuid.lib")

#include <conio.h>

int main() {
   
   
    CoInitialize( NULL );
    if( CoInitializeSecurity( NULL, - 1, NULL, NULL, RPC_C_AUTHN_LEVEL_PKT,
    RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE, 0 ) != S_OK )
         return 0;
   
    IWbemLocator * pIWbemLocator = NULL;
    IWbemServices * pWbemServices = NULL;
    IEnumWbemClassObject * pEnumObject = NULL;
   
    BSTR bstrNamespace = SysAllocString( L"root\\cimv2" );
   
   
    if( CoCreateInstance(
    CLSID_WbemAdministrativeLocator,
    NULL,
    CLSCTX_INPROC_SERVER | CLSCTX_LOCAL_SERVER,
    IID_IUnknown,
    ( void ** ) & pIWbemLocator ) != S_OK )
         return 0;
   
    if( pIWbemLocator->ConnectServer( bstrNamespace, NULL, NULL, NULL, 0,
    NULL, NULL, & pWbemServices ) != S_OK )
         return 0;
   
    HRESULT hRes;
   
    //BSTR strQuery = SysAllocString(L"Select * from win32_Processor");
    //BSTR strQuery = SysAllocString(L"SELECT * FROM MSAcpi_ThermalZoneTemperature");
    BSTR strQuery = SysAllocString( L"Select * from Win32_TemperatureProbe" );
    BSTR strQL = SysAllocString( L"WQL" );
    hRes = pWbemServices->ExecQuery( strQL, strQuery,
    WBEM_FLAG_RETURN_IMMEDIATELY, NULL, & pEnumObject );
    if( hRes != S_OK )
    {
        MessageBox( 0, "Could not execute Query", 0, 0 );
        return 0;
    }
   
    hRes = pEnumObject->Reset();
    if( hRes != S_OK )
    {
        MessageBox( 0, "Could not Enumerate", 0, 0 );
        return 0;
    }
   
    ULONG uCount = 1, uReturned;
    IWbemClassObject * pClassObject = NULL;
    hRes = pEnumObject->Next( WBEM_INFINITE, uCount, & pClassObject, & uReturned );
    if( hRes != S_OK )
    {
        MessageBox( 0, "Could not Enumerate - Brak sensora chyba ;-)", 0, 0 );
        return 0;
    }
   
    VARIANT v;
    BSTR strClassProp = SysAllocString( L"Resolution" ); //Trzecia LINIJKA
    hRes = pClassObject->Get( strClassProp, 0, & v, 0, 0 );
    if( hRes != S_OK )
    {
        MessageBox( 0, "Could not Get Value", 0, 0 );
        return 0;
    }
   
    SysFreeString( strClassProp );
    _bstr_t bstrPath = & v; //Just to convert BSTR to ANSI
   
    char * strPath =( char * ) bstrPath;
   
    if( SUCCEEDED( hRes ) )
         MessageBoxA( NULL, strPath, 0, 0 );
    else
         MessageBoxA( NULL, "Error in getting object", 0, 0 );
   
    VariantClear( & v );
    pIWbemLocator->Release();
    pWbemServices->Release();
    pEnumObject->Release();
    pClassObject->Release();
    CoUninitialize();
   
   
    getch();
    return 0;
}

I interesują nas tylko 3 linijki w tym kodzie.

Pierwsza to - BSTR bstrNamespace = (L"root\\cimv2");

Gdy wchodzimy na stronę - http://msdn.microsoft.com/en-us/library/aa394493%28v=VS.85%29.aspx
Na samym dole jest tabelka i pisze nam jaka jest przestrzeń nazw, w tym wypadku - Namespace \root\cimv2  - mam nadzieje, że kumiecie.

Druga to z czego będziemy korzystać jak z Win32_TemperatureProbe, czyli interesuje nas część kodu:
BSTR strQuery = (L"Select * from Win32_TemperatureProbe");  - chyba jasne jak słońce

Ostatnia linijka to z jakiej własności (metody) tej klasy skorzystamy ja z - CurrentReading:
BSTR strClassProp = SysAllocString(L"CurrentReading");

W ten sposób możecie wykorzystać wszystkie (lub większość) tego co oferuje WMI class.

Mogą też pojawić się problemy z zmiennymi, bo każda metoda może przyjmować różne wartości m.in. sint32, uint16, boolean. Być może będzie trzeba jeszcze poprawić.

A na koniec powiem tylko, że płyta którą posiada nie ma sensora temp ;-) lub tego z czego ta klasa bierze dane. By sprawdzić czy taki sensor jest wystarczy ten oto kod
strComputer = "."
Set colTempProbe = GetObject("Winmgmts:"_
    & "{impersonationLevel=impersonate}!\\"_
    & strComputer & "\root\cimv2")._
    InstancesOf("Win32_TemperatureProbe")
Num = 0
For Each obj In colTempProbe     
    WScript.Echo   obj.Name & VBNewLine _
        & obj.DeviceID & VBNewLine _
        & obj.Status & VBNewLine _
        & obj.Resolution & VBNewLine _
        & obj.Tolerance & VBNewLine _
        & obj.Accuracy
    Num = Num +1
Next
If Num = 0 Then
    WScript.Echo "No temperature probe data"
End If
Wkleić do notatnika i zapisać jako Coś.vbs i przekonać się co się pojawi ;-).

Podziękował Jan Wężyk.


PS jedna uwaga Funkcję CoInitializeSecurity wywołuje się tylko raz, najlepiej przy uruchomieniu programu. Można to umieścić w konstruktorze klasy formularza. Szczególnie jest to ważne w przypadku Win7!
P-33935
« 1 »
  Strona 1 z 1