QCoreApplication QApplication 与 WMI [英] QCoreApplication QApplication with WMI

查看:21
本文介绍了QCoreApplication QApplication 与 WMI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 MSDN 网站上找到了一些 WMI C++ 应用示例.

I found some WMI C++ Application Examples in MSDN website.

我已经尝试了以下链接中的代码

I have tried the code form the below link

http://msdn.microsoft.com/en-us/library/aa390423%28v=VS.85%29.aspx

当我将应用程序复制并作为 win32 控制台应用程序运行时,它运行良好.

when I copied and run the application as win32 console application it worked well.

我放入 Qt 应用程序的相同代码,

the same code I put into the Qt application ,

当我在我的代码中使用 QApplication anApplication ( argc, argv ); 时,它不起作用

When I use QApplication anApplication ( argc, argv ); in my code it is not working

但是如果我使用 QCoreApplication anApplication ( argc, argv ); 它正在工作

But if I use QCoreApplication anApplication ( argc, argv ); it is working

向我显示错误,例如当我将 QCoreApplication 更改为 QApplication 下面的源代码不起作用并给我类似的错误"无法初始化 COM 库.错误代码 = 0x80010106 "

shows me error like "when i change QCoreApplication to QApplication The below source code not works and gives me error like " Failed to initialize COM library. Error code = 0x80010106 "

任何建议或帮助

#define _WIN32_DCOM
#include <iostream>
using namespace std;
#include <comdef.h>
#include <Wbemidl.h>

# pragma comment(lib, "wbemuuid.lib")

// Qt Includes
#include <QtCore>
#include <QtGui>


 //----------------------------------------------------------------------------

// Main Function
int main( int a_argc, char *a_argv[] )
//************************************
{
    // initialize the application
    QApplication anapplication( a_argc, a_argv );

    HRESULT hres;

    // Step 1: --------------------------------------------------
    // Initialize COM. ------------------------------------------

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

    // Step 2: --------------------------------------------------
    // Set general COM security levels --------------------------
    // Note: If you are using Windows 2000, you need to specify -
    // the default authentication credentials for a user by using
    // a SOLE_AUTHENTICATION_LIST structure in the pAuthList ----
    // parameter of CoInitializeSecurity ------------------------

    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;
        CoUninitialize();
        return 1;                    // Program has failed.
    }

    // Step 3: ---------------------------------------------------
    // 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;
        CoUninitialize();
        return 1;                 // Program has failed.
    }

    // Step 4: -----------------------------------------------------
    // Connect to WMI through the IWbemLocator::ConnectServer method

    IWbemServices *pSvc = NULL;

    // Connect to the root\cimv2 namespace with
    // the current user and obtain pointer pSvc
    // to make IWbemServices calls.
    hres = pLoc->ConnectServer(
         _bstr_t(L"ROOT\\CIMV2"), // 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;
        pLoc->Release();     
        CoUninitialize();
        return 1;                // Program has failed.
    }

    cout << "Connected to ROOT\\CIMV2 WMI namespace" << endl;


    // Step 5: --------------------------------------------------
    // 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;
        pSvc->Release();
        pLoc->Release();     
        CoUninitialize();
        return 1;               // Program has failed.
    }

    // Step 6: --------------------------------------------------
    // Use the IWbemServices pointer to make requests of WMI ----

    // For example, get the name of the operating system
    IEnumWbemClassObject* pEnumerator = NULL;
    hres = pSvc->ExecQuery(
        bstr_t("WQL"), 
        bstr_t("SELECT * FROM Win32_USBControllerDevice"),
        WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY, 
        NULL,
        &pEnumerator);

//ExecQuery(L"WQL", L"SELECT * FROM Win32_LogicalDisk", WBEM_FLAG_FORWARD_ONLY, NULL, &iter);

    if (FAILED(hres))
    {
        cout << "Query for operating system name failed."
            << " Error code = 0x" 
            << hex << hres << endl;
        pSvc->Release();
        pLoc->Release();
        CoUninitialize();
        return 1;               // Program has failed.
    }

    // Step 7: -------------------------------------------------
    // Get the data from the query in step 6 -------------------

    IWbemClassObject *pclsObj;
    ULONG uReturn = 0;

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

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

        VARIANT vtProp;

        // Get the value of the Name property
        /*hr = pclsObj->Get(L"VolumeName", 0, &vtProp, 0, 0);
        wcout << " VolumeName : " << vtProp.bstrVal << endl;
        VariantClear(&vtProp);*/


        if (FAILED(pclsObj->Get(L"Antecedent", 0, &vtProp, 0, 0)))
        {
            cout<<"The specified property is not found."<<endl;
        }
        else
        {
            wcout <<vtProp.bstrVal << endl;
        }

        pclsObj->Release();
    }

    // Cleanup
    // ========

    pSvc->Release();
    pLoc->Release();
    pEnumerator->Release();
   // pclsObj->Release();
    CoUninitialize();


   // return 0;   // Program successfully completed.

    //execute the application
    int anInt = anapplication.exec();

    return anInt;
}
//-----------------------------------------------------------------------------

推荐答案

您收到的错误是由于 COM 已经初始化.QApplication 在其构造函数中调用 OleInitialize,但 QCoreApplication 不会,这就是为什么在使用 QApplication 时会收到错误 0x80010106 (RPC_E_CHANGED_MODE).如需进一步阅读,请参阅 CoInitializeEx 的文档.以下文章应该提供更多洞察力.

The error you are receiving is due to the fact that COM is already initialized. QApplication calls OleInitialize in its constructor, but QCoreApplication does not, so that's why you get the error 0x80010106 (RPC_E_CHANGED_MODE) when you use QApplication. For further reading, see the documentation for CoInitializeEx. The following article should provide more insight.

这篇关于QCoreApplication QApplication 与 WMI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆