CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Oct 2005
    Posts
    34

    Reading CPU Temp

    Hi

    I trying to get the actual CPU temp in my mfc app, but i have some troubles.
    Here is the code I use to get the cpu temp, but the result always gets a strange value when trying to do (pClassObject and uReturned are NULL afterwards)

    Code:
    hRes = pEnumObject->Next(WBEM_INFINITE,uCount, &pClassObject, &uReturned);
    Here is the whole code I use:

    Code:
    void CFFMpeg_guiDlg::ReadCPUTemp ()
    {
      HRESULT hr = CoInitializeEx (0, COINIT_MULTITHREADED); 
      if (FAILED (hr)) 
      {
        return;
      }
    
      IWbemLocator* pIWbemLocator = NULL;
      IWbemServices* pWbemServices = NULL;
      IEnumWbemClassObject* pEnumObject = NULL;
    
      if (CoCreateInstance (CLSID_WbemLocator, 0, CLSCTX_INPROC_SERVER, IID_IWbemLocator, (LPVOID *) &pIWbemLocator) != S_OK)
        return;
    
      if (pIWbemLocator->ConnectServer ( CComBSTR (L"ROOT\\WMI"), NULL, NULL, 0, NULL, 0, 0, &pWbemServices) != S_OK)
        return;
    
      HRESULT hRes;
      hRes = pWbemServices->ExecQuery (CComBSTR (L"WQL"), CComBSTR (L"SELECT * FROM MSAcpi_ThermalZoneTemperature"),WBEM_FLAG_RETURN_IMMEDIATELY, NULL, &pEnumObject); 
      if (hRes != S_OK)
      {
        return;
      }
    
      hRes = pEnumObject->Reset();
      if (hRes != S_OK)
      {
        return;
      }
    
      ULONG uCount = 1, uReturned;
      IWbemClassObject* pClassObject;
    -->  hRes = pEnumObject->Next(WBEM_INFINITE,uCount, &pClassObject, &uReturned);
      if(hRes != S_OK)
      {
        return;
      }
    
      VARIANT v;
      int i = 0;
      BSTR strClassProp = SysAllocString(L"CurrentTemperature");
      hRes = pClassObject->Get(strClassProp, 0, &v, 0, 0);
      if (hRes != S_OK)
      {
        MessageBox("Could not Get Value");
        return;
      }
    
      SysFreeString(strClassProp);
    //   _bstr_t bstrpath = &v; //just to convert bstr to ansi
    //   char* strpath=(char*)bstrpath;
    // 
    //   if (succeeded(hres))
    //     messagebox(strpath);
    //   else
    //     messagebox("error in getting object");
    
      VariantClear(&v);
      pIWbemLocator->Release();
      pWbemServices->Release();
      pEnumObject->Release();
      pClassObject->Release();
      CoUninitialize();
    }
    thanks for any advice...
    regards
    Erich

  2. #2
    Join Date
    Oct 1999
    Location
    Belgium
    Posts
    440

    Re: Reading CPU Temp

    Just a wild guess ...

    You're comparing return values with S_OK whereas MSDN documentation lists WBEM_S_NO_ERROR

    http://msdn.microsoft.com/en-us/libr...(v=vs.85).aspx

  3. #3
    Join Date
    Oct 2005
    Posts
    34

    Re: Reading CPU Temp

    Hi

    Not only that the return value is not S_OK (WBEM_S_NO_ERROR) the pClassObject is also not initialized and set. The method fails and I have no clue why ....

    regards
    Erich

  4. #4
    Join Date
    Aug 2008
    Location
    Scotland
    Posts
    379

    Re: Reading CPU Temp

    Hi,

    I get error 0x80041003, which mean WBEM_E_ACCESS_DENIED. What error code do you get?

    Alan

  5. #5
    Join Date
    Aug 2008
    Location
    Scotland
    Posts
    379

    Re: Reading CPU Temp

    Adding some security initialization seems to fix it here anyway:
    Code:
    hr = CoInitializeSecurity(NULL, -1, NULL, NULL, RPC_C_AUTHN_LEVEL_PKT,RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE, 0) ;
    if (FAILED (hr)) 
    {
    return;
    }
    Alan

  6. #6
    Join Date
    Oct 2005
    Posts
    34

    Re: Reading CPU Temp

    Hi

    Thanks for your post, but I still get the same result ....
    Return value from pEnumObject->Next.. is 0x8004100c (WBEM_E_NOT_SUPPORTED).
    I tried it here and at home (Intel dualcorfe and AMD 1055t 6x core) ....

    Is there any other method to get the cpu temp? Its a intel dualcore here, so it should be capable of
    getting the cpu temp....

    regards
    Erich
    Last edited by hundsmiachn; February 3rd, 2011 at 07:04 AM.

  7. #7
    Join Date
    Aug 2008
    Location
    Scotland
    Posts
    379

    Re: Reading CPU Temp

    Hi,

    Well, it does sound like WMI doesn't support CPU temperature reading on those 2 PCs. You could check using the simple VBS example shown in this thread:
    http://social.msdn.microsoft.com/forums/en-US/vblanguage/thread/18ce0701-e87d-4414-a8b5-8be3908a21b8/

    That will help work out if the problem is with WMI on your PCs.

    Alan

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured