title: Get CPU Temperature
body: <p>I want to get the CPU temperature.
  Below is what I've done using C++ and WMI. I'm reading MSAcpi_ThermalZoneTemperature, but it's always the same and it's not the CPU temperature at all. </p>
  
  <p>Is there any way to get the real temperature of the CPU without having to write drivers? Or are there any libs which I can use? Thank you in advance.</p>
  
  <pre><code>#define _WIN32_DCOM
  #include &lt;iostream&gt;
  using namespace std;
  #include &lt;comdef.h&gt;
  #include &lt;Wbemidl.h&gt;
  
  #pragma comment(lib, "wbemuuid.lib")
  
  HRESULT GetCpuTemperature(LPLONG pTemperature)
  {
          if (pTemperature == NULL)
                  return E_INVALIDARG;
  
          *pTemperature = -1;
          HRESULT ci = CoInitialize(NULL);
          HRESULT hr = CoInitializeSecurity(NULL, -1, NULL, NULL, RPC_C_AUTHN_LEVEL_DEFAULT, RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE, NULL);
          if (SUCCEEDED(hr))
          {
                  IWbemLocator *pLocator;
                  hr = CoCreateInstance(CLSID_WbemAdministrativeLocator, NULL, CLSCTX_INPROC_SERVER, IID_IWbemLocator, (LPVOID*)&amp;pLocator);
                  if (SUCCEEDED(hr))
                  {
                          IWbemServices *pServices;
                          BSTR ns = SysAllocString(L"root\\WMI");
                          hr = pLocator-&gt;ConnectServer(ns, NULL, NULL, NULL, 0, NULL, NULL, &amp;pServices);
                          pLocator-&gt;Release();
                          SysFreeString(ns);
                          if (SUCCEEDED(hr))
                          {
                                  BSTR query = SysAllocString(L"SELECT * FROM MSAcpi_ThermalZoneTemperature");
                                  BSTR wql = SysAllocString(L"WQL");
                                  IEnumWbemClassObject *pEnum;
                                  hr = pServices-&gt;ExecQuery(wql, query, WBEM_FLAG_RETURN_IMMEDIATELY | WBEM_FLAG_FORWARD_ONLY, NULL, &amp;pEnum);
                                  SysFreeString(wql);
                                  SysFreeString(query);
                                  pServices-&gt;Release();
                                  if (SUCCEEDED(hr))
                                  {
                                          IWbemClassObject *pObject;
                                          ULONG returned;
                                          hr = pEnum-&gt;Next(WBEM_INFINITE, 1, &amp;pObject, &amp;returned);
                                          pEnum-&gt;Release();
                                          if (SUCCEEDED(hr))
                                          {
                                                  BSTR temp = SysAllocString(L"CurrentTemperature");
                                                  VARIANT v;
                                                  VariantInit(&amp;v);
                                                  hr = pObject-&gt;Get(temp, 0, &amp;v, NULL, NULL);
                                                  pObject-&gt;Release();
                                                  SysFreeString(temp);
                                                  if (SUCCEEDED(hr))
                                                  {
                                                          *pTemperature = V_I4(&amp;v);
                                                  }
                                                  VariantClear(&amp;v);
                                          }
                                  }
                          }
                          if (ci == S_OK)
                          {
                                  CoUninitialize();
                          }
                  }
          }
          return hr;
  }
  
  int main(int argc, char **argv)
  {
          LONG temp;
          GetCpuTemperature(&amp;temp);
          printf("temp=%lf\n", ((double)temp / 10 - 273.15));
          getc(stdin);
          return 0;
  }
  </code></pre>