2015-08-28 101 views
0

我已經閱讀了大多數這樣的問題。但是,在使用C++獲取WMI中cpu使用的價值時,仍然存在一些問題。如何使用C++在WMI中獲取CPU使用率?

我已經嘗試了兩種方法來解決這個問題:

  1. 查詢PercentProcessorTimeWin32_PerfFormattedData_PerfOS_Processor。但值值遠大於我想要的。它可以達到10802692。我的代碼一些件:

hres = m_pWbemSvc->ExecQuery(
bstr_t("WQL"), 
bstr_t("SELECT * FROM Win32_PerfFormattedData_PerfOS_Processor where Name='_Total' "), 
WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY, 
NULL, 
&m_pEnumClsObj 
); 

hres = m_pWbemClsObj->Get(L"PercentProcessorTime", 0, &vtProp, 0, 0); 
     wcout << "CPU Usage : " << vtProp.ullVal << endl; 
     VariantClear(&vtProp); 
  • 得到一些數據形式Win32_PerfRawData_PerfOS_Processor,然後使用formula。然而,當我嘗試該方法中,我總是獲取有關PercentProcessorTimeTimeStamp_Sys100NS。那相同的值就是N1=N2D1=D2。我認爲必須有我的代碼中一些錯誤的
  • 式 - (1 - ((N 2 - N 1)/(D2 - D1)))×100

    unsigned __int64 N1; 
    unsigned __int64 D1; 
    unsigned __int64 N2; 
    unsigned __int64 D2; 
    bool result = false; 
    
    
    if (getCPUData(&N1, &D1)) 
    { 
        Sleep(1000); 
    
        if (getCPUData(&N2, &D2)) 
        { 
         result = true; 
        } 
    } 
    
    //(1 - ((N2 - N1)/(D2 - D1))) * 100; 
    

    bool WMI_Util::getCPUData(unsigned __int64 *N, unsigned __int64 *D) 
    { 
    HRESULT hres; 
    ULONG uReturn = 0; 
    VARIANT vtProp; 
    m_pEnumClsObj = NULL; 
    bool result = false; 
    
    
    hres = m_pWbemSvc->ExecQuery(
        bstr_t("WQL"), 
        bstr_t("SELECT * FROM Win32_PerfRawData_PerfOS_Processor where Name='_Total' "), 
        WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY, 
        NULL, 
        &m_pEnumClsObj 
        ); 
    
    if (FAILED(hres)) 
    { 
        wcout << L"Query for operating system name failed." 
         << L" Error code = 0x" 
         << hex << hres << endl; 
        m_pWbemSvc->Release(); 
        m_pWbemLoc->Release(); 
        CoUninitialize(); 
    } 
    else{ 
    
        m_pWbemClsObj = NULL; 
    
        while (m_pEnumClsObj) 
        { 
         hres = m_pEnumClsObj->Next(WBEM_INFINITE, 1, &m_pWbemClsObj, &uReturn); 
    
         if (0 == uReturn) 
          break; 
    
         hres = m_pWbemClsObj->Get(L"PercentProcessorTime", 0, &vtProp, 0, 0); 
         *N = vtProp.ullVal; 
         VariantClear(&vtProp); 
    
         hres = m_pWbemClsObj->Get(L"TimeStamp_Sys100NS", 0, &vtProp, 0, 0); 
         *D = vtProp.ullVal; 
         VariantClear(&vtProp); 
    
         m_pWbemClsObj->Release(); 
    
        } 
        result = true; 
    } 
    return result; 
    ReleaseWMI(); 
    } 
    

    至於WMI的初始化,我在MSDN的step1-5後面。我需要在兩個不同的WMI連接中獲得它們的值嗎?在目前的情況下,我只是在兩個不同的時間查詢課程。這就是爲什麼我總是得到相同的價值?

    回答

    0

    我的建議是使用'vtProp.bstrVal'而不是'vtProp.ullVal'。 我實現了非常相似的功能,正如你所說的在數值字段中得到了常量值,但我在字符串字段中得到了正確的值。

    這裏是我的方法(沒有調試印刷):

    HRESULT WMI_sdk_services::GetCpuUsage(int &cpuUsage) 
    { 
        bool shouldUninitializeComAfterWmiRequest; //out parameter 
    
        HRESULT hres = PrepareEnumWbemClassObject(true, shouldUninitializeComAfterWmiRequest, L"SELECT * FROM Win32_PerfFormattedData_PerfOS_Processor where Name='_Total'"); 
    
        IWbemClassObject *pclsObj = NULL; 
        ULONG uReturn = 0; 
    
        if(SUCCEEDED(hres)){ 
    
        while (pEnumerator) 
        { 
         HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1, 
          &pclsObj, &uReturn); 
    
         if(0 == uReturn) 
         { 
          break; 
         } 
    
         VARIANT vtProp; 
    
         // Get the value of the 'PercentProcessorTime' property 
         hr = pclsObj->Get(L"PercentProcessorTime", 0, &vtProp, 0, 0); 
    
         if (WBEM_S_NO_ERROR != hr) { 
    
          if(pclsObj){ 
           VariantClear(&vtProp); 
           pclsObj->Release(); pclsObj = NULL; 
          } 
          break; 
         } 
         cpuUsage = std::stoi(vtProp.bstrVal); 
    
         VariantClear(&vtProp); 
    
         pclsObj->Release(); pclsObj = NULL;   
        } 
        } 
    
        return hres;  
    } 
    

    另一句話:你在這裏總的CPU使用率不是你的進程的CPU使用率。

    +0

    謝謝你的回答。我改變了我的方式來獲取它的價值,就像你說的一樣。但我還有一個問題:爲什麼這個值看起來比在Windows任務管理器中顯示的CPU使用率稍小一點? – spartawhy117