【问题标题】:Querying WMI for D3DKMDT_VIDEO_OUTPUT_TECHNOLOGY查询 D3DKMDT_VIDEO_OUTPUT_TECHNOLOGY 的 WMI
【发布时间】:2012-04-10 09:07:36
【问题描述】:

我关注了这篇 MSDN 文章,该文章显示了从本地计算机 http://msdn.microsoft.com/en-us/library/windows/desktop/aa390423%28v=vs.85%29.aspx 检索 WMI 数据的示例

显示的示例获取操作系统的名称,但在我的情况下,我正在查询“Select * from WmiMonitorConnectionParams”并想要检索 D3DKMDT_VIDEO_OUTPUT_TECHNOLOGY http://msdn.microsoft.com/en-us/library/windows/hardware/ff546605%28v=vs.85%29.aspx

在这种情况下我应该如何使用 IWbemClassObject::Get? VARIANT 变量的哪个成员存储了我想要的结果?

更一般地说,我们应该如何知道在每次查询之后在 Get 方法中使用哪些标志以及 VARIANT 的哪个成员存储结果?这是在哪里记录的?

谢谢。

【问题讨论】:

    标签: c++ windows wmi


    【解决方案1】:

    是的,你必须使用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 数据。

    【讨论】:

    • 嗨 RRUZ,感谢您的回复,一切都很好,直到第一次调用 pEnumerator->Next,它总是返回 WBEM_E_INVALID_CLASS。
    • WmiMonitorConnectionParams wmi 类是在 windows vista 中引入的,因此请检查您的 Windows 版本。此外,如果您使用自己的代码(而不是示例),请检查必须是 root\WMI 的命名空间
    • 啊,没想到是在vista中引入的,我的机器运行的是XP。在 XP 中有没有等效的方法来做到这一点?
    • 如果您查看有关 D3DKMDT_VIDEO_OUTPUT_TECHNOLOGY 枚举的文档,您将看到 Windows vista 中也引入了该枚举。
    猜你喜欢
    • 1970-01-01
    • 2019-10-25
    • 2013-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-01
    相关资源
    最近更新 更多