2012-04-10 56 views
1

我跟了這MSDN文章,顯示從本地計算機http://msdn.microsoft.com/en-us/library/windows/desktop/aa390423%28v=vs.85%29.aspx查詢WMI的D3DKMDT_VIDEO_OUTPUT_TECHNOLOGY

顯示的示例獲取操作系統的名稱,但對我來說,我查詢檢索WMI數據的例子「選擇*來自WmiMonitorConnectionParams「並且想要檢索D3DKMDT_VIDEO_OUTPUT_TECHNOLOGY http://msdn.microsoft.com/en-us/library/windows/hardware/ff546605%28v=vs.85%29.aspx

在這種情況下,我應該如何使用IWbemClassObject :: Get? VARIANT變量的哪一個成員存儲我後面的結果?

更一般地說,在每個查詢和VARIANT的哪個成員存儲結果之後,我們應該如何知道在Get方法中使用哪些標誌?這在哪裏記錄?

謝謝。

回答

2

是的,您必須使用IWbemClassObject接口來檢索數據,關於要使用的變體成員,它取決於WMI屬性的類型,在這種情況下VideoOutputTechnology屬性是uint32值,因此您必須使用uintVal成員。

入住此示例代碼

#include "stdafx.h" 
#define _WIN32_DCOM 
#include <iostream> 
using namespace std; 
#include <comdef.h> 
#include <Wbemidl.h> 
# pragma comment(lib, "wbemuuid.lib") 


// Monitor's basic connection parameters 

#pragma argsused 
int main(int argc, char* argv[]) 
{ 
    BSTR strNetworkResource; 
    //To use a WMI remote connection set localconn to false and configure the values of the pszName, pszPwd and the name of the remote machine in strNetworkResource 
    strNetworkResource = L"\\\\.\\root\\WMI"; 

    COAUTHIDENTITY *userAcct = NULL ; 
    COAUTHIDENTITY authIdent; 

    // Initialize COM. ------------------------------------------ 

    HRESULT hres; 
    hres = CoInitializeEx(0, COINIT_MULTITHREADED); 
    if (FAILED(hres)) 
    { 
     cout << "Failed to initialize COM library. Error code = 0x" << hex << hres << endl; 
     cout << _com_error(hres).ErrorMessage() << endl; 
     cout << "press enter to exit" << endl; 
     cin.get();  
     return 1;     // Program has failed. 
    } 

    // Set general COM security levels -------------------------- 

     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)) 
    { 
     cout << "Failed to initialize security. Error code = 0x" << hex << hres << endl; 
     cout << _com_error(hres).ErrorMessage() << endl; 
     CoUninitialize(); 
     cout << "press enter to exit" << endl; 
     cin.get();  
     return 1;     // Program has failed. 
    } 

    // Obtain the initial locator to WMI ------------------------- 

    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; 
     cout << _com_error(hres).ErrorMessage() << endl; 
     CoUninitialize();  
     cout << "press enter to exit" << endl; 
     cin.get();  
     return 1;     // Program has failed. 
    } 

    // Connect to WMI through the IWbemLocator::ConnectServer method 

    IWbemServices *pSvc = NULL; 

     hres = pLoc->ConnectServer(
      _bstr_t(strNetworkResource),  // 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)) 
    { 
     cout << "Could not connect. Error code = 0x" << hex << hres << endl;  
     cout << _com_error(hres).ErrorMessage() << endl; 
     pLoc->Release(); 
     CoUninitialize(); 
     cout << "press enter to exit" << endl; 
     cin.get();   
     return 1;    // Program has failed. 
    } 

    cout << "Connected to root\\WMI WMI namespace" << endl; 

    // Set security levels on the proxy ------------------------- 
     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)) 
    { 
     cout << "Could not set proxy blanket. Error code = 0x" << hex << hres << endl; 
     cout << _com_error(hres).ErrorMessage() << endl; 
     pSvc->Release(); 
     pLoc->Release(); 
     CoUninitialize(); 
     cout << "press enter to exit" << endl; 
     cin.get();  
     return 1;    // Program has failed. 
    } 

    // Use the IWbemServices pointer to make requests of WMI ---- 

    IEnumWbemClassObject* pEnumerator = NULL; 
    hres = pSvc->ExecQuery(L"WQL", L"SELECT * FROM WmiMonitorConnectionParams", 
    WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY, NULL, &pEnumerator); 

    if (FAILED(hres)) 
    { 
     cout << "ExecQuery failed" << " Error code = 0x" << hex << hres << endl; 
     cout << _com_error(hres).ErrorMessage() << endl; 
     pSvc->Release(); 
     pLoc->Release(); 
     CoUninitialize(); 
     cout << "press enter to exit" << endl; 
     cin.get();  
     return 1;    // Program has failed. 
    } 


    // Get the data from the WQL sentence 
    IWbemClassObject *pclsObj = NULL; 
    ULONG uReturn = 0; 

    while (pEnumerator) 
    { 
     HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1, &pclsObj, &uReturn); 

     if(0 == uReturn || FAILED(hr)) 
      break; 

     VARIANT vtProp; 

       hr = pclsObj->Get(L"Active", 0, &vtProp, 0, 0);// Boolean 
       if (!FAILED(hr)) 
       { 
        if ((vtProp.vt==VT_NULL) || (vtProp.vt==VT_EMPTY)) 
        wcout << "Active : " << ((vtProp.vt==VT_NULL) ? "NULL" : "EMPTY") << endl; 
        else 
        wcout << "Active : " << (vtProp.boolVal ? "True" : "False") << endl; 
       } 
       VariantClear(&vtProp); 

       hr = pclsObj->Get(L"InstanceName", 0, &vtProp, 0, 0);// String 
       if (!FAILED(hr)) 
       { 
        if ((vtProp.vt==VT_NULL) || (vtProp.vt==VT_EMPTY)) 
        wcout << "InstanceName : " << ((vtProp.vt==VT_NULL) ? "NULL" : "EMPTY") << endl; 
        else 
        wcout << "InstanceName : " << vtProp.bstrVal << endl; 
       } 
       VariantClear(&vtProp); 

       hr = pclsObj->Get(L"VideoOutputTechnology", 0, &vtProp, 0, 0);// Uint32 
       if (!FAILED(hr)) 
       { 
        if ((vtProp.vt==VT_NULL) || (vtProp.vt==VT_EMPTY)) 
        wcout << "VideoOutputTechnology : " << ((vtProp.vt==VT_NULL) ? "NULL" : "EMPTY") << endl; 
        else 
        wcout << "VideoOutputTechnology : " << vtProp.uintVal << endl; 
       } 
       VariantClear(&vtProp); 


     pclsObj->Release(); 
     pclsObj=NULL; 
    } 

    // Cleanup 

    pSvc->Release(); 
    pLoc->Release(); 
    pEnumerator->Release(); 
    if (pclsObj!=NULL) 
    pclsObj->Release(); 

    CoUninitialize(); 
    cout << "press enter to exit" << endl; 
    cin.get(); 
    return 0; // Program successfully completed. 
} 

您也可以使用像wmi delphi code creator它可以幫助你探索WMI和生成C++代碼訪問WMI數據的工具。

+0

嗨RRUZ, 感謝您的答覆,一切順利,直到第一次調用pEnumerator-> Next,總是返回WBEM_E_INVALID_CLASS。 – 2012-04-10 15:52:36

+2

'WmiMonitorConnectionParams' wmi類是在Windows Vista中引入的,因此請檢查您的Windows版本。此外,如果你使用自己的代碼(而不是樣本)檢查命名空間必須是'root \ WMI' – RRUZ 2012-04-10 15:54:06

+0

Aargh,沒有意識到它是在Vista中引入的,我的機器正在運行XP。在XP中有這樣的方法嗎? – 2012-04-10 15:58:26