使用WMI的QCroreApplication QApplication [英] QCroreApplication QApplication with WMI

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

问题描述

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



我已经尝试过以下链接的代码:



http://msdn.microsoft。当我将应用程序复制并运行为win32控制台应用程序时,系统会将该应用程序复制到/ com / en-us / library / aa390423%28v = VS.85%29.aspx



工作得很好。



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



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



显示我的错误,如当我改变QCroreApplication到QApplication以下源代码不工作,并给我错误的无法初始化COM库。错误代码= 0x80010106



任何建议或帮助

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

#pragma comment(lib,wbemuuid.lib)

// Qt包括
#include< QtCore>
#include< QtGui>


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

//主要功能
int main(int a_argc,char * a_argv [])
// ***************************** *****
{
//初始化应用程序
QApplication anapplication(a_argc,a_argv);

HRESULT hres;

// 步骤1: - - - - - - - - - - - - - - - - - - - - - - - -----
//初始化COM。------------------------------------ ------

hres = CoInitializeEx(0,COINIT_MULTITHREADED);
if(FAILED(hres))
{
cout<以初始化COM库。错误代码= 0x
<<<<< hres<<&end;
return 1; //程序失败
}
$ b b //步骤2:-------------------------------------------- ------
//设置常规COM安全级别--------------------------
//注意:如果您使用的是Windows 2000,则需要通过在pAuthList中使用
// a SOLE_AUTHENTICATION_LIST结构来指定 -
//用户的默认身份验证凭据。 CoInitializeSecurity的参数------------------------

hres = CoInitializeSecurity(
NULL,
- 1,// COM验证
NULL,//验证服务
NULL,//保留
RPC_C_AUTHN_LEVEL_DEFAULT,//默认验证
RPC_C_IMP_LEVEL_IMPERSONATE,//默认模拟
NULL ,//认证信息
EOAC_NONE,//附加功能
NULL //保留
);


if(FAILED(hres))
{
cout< 无法初始化安全性。错误代码= 0x
<< hex<< hres<< endl;
CoUninitialize();
return 1; //程序失败。
}

//第3步:-------------------------------- -------------------
//获取WMI的初始定位符------------------- ------

IWbemLocator * pLoc = NULL;

hres = CoCreateInstance(
CLSID_WbemLocator,
0,
CLSCTX_INPROC_SERVER,
IID_IWbemLocator,(LPVOID *)& pLoc);

if(FAILED(hres))
{
cout< 无法创建IWbemLocator对象。
<< Err code = 0x
<< hex<< hres<< endl;
CoUninitialize();
return 1; //程序失败。
}

//第4步:-------------------------------- ---------------------
//通过IWbemLocator :: ConnectServer方法连接到WMI

IWbemServices * pSvc = NULL ;

//使用
连接到root \cimv2命名空间//当前用户并获取指针pSvc
//以进行IWbemServices调用。
hres = pLoc-> ConnectServer(
_bstr_t(LROOT\\CIMV2),// WMI命名空间的对象路径
NULL,//用户名。 user
NULL,//用户密码NULL = current
0,// Locale。NULL表示当前
NULL,//安全标志
0,// Authority Kerberos)
0,//上下文对象
& pSvc //指向IWbemServices代理的指针
);

if(FAILED(hres))
{
cout< 无法连接。错误代码= 0x
<< hex<< hres<< endl;
pLoc-> Release();
CoUninitialize();
return 1; //程序失败。
}

cout<< 连接到ROOT \\CIMV2 WMI命名空间< endl;


//第5步:--------------------------------- -----------------
//在代理上设置安全级别--------------------- ----

hres = CoSetProxyBlanket(
pSvc,//表示要设置的代理
RPC_C_AUTHN_WINNT,// RPC_C_AUTHN_xxx
RPC_C_AUTHZ_NONE,// RPC_C_AUTHZ_xxx
NULL,//服务器主体名称
RPC_C_AUTHN_LEVEL_CALL,// RPC_C_AUTHN_LEVEL_xxx
RPC_C_IMP_LEVEL_IMPERSONATE,// RPC_C_IMP_LEVEL_xxx
NULL,//客户端身份
EOAC_NONE //代理功能
) ;

if(FAILED(hres))
{
cout< 无法设置代理覆盖。错误代码= 0x
<< hex<< hres<< endl;
pSvc-> Release();
pLoc-> Release();
CoUninitialize();
return 1; //程序失败。
}

//第6步:-------------------------------- ------------------
//使用IWbemServices指针来请求WMI ----

//例如,获取操作系统的名称
IEnumWbemClassObject * pEnumerator = NULL;
hres = pSvc-> ExecQuery(
bstr_t(WQL),
bstr_t(SELECT * FROM Win32_USBControllerDevice),
WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,

& pEnumerator);

// ExecQuery(LWQL,LSELECT * FROM Win32_LogicalDisk,WBEM_FLAG_FORWARD_ONLY,NULL,& iter);

if(FAILED(hres))
{
cout< 查询操作系统名称失败。
<< 错误代码= 0x
<< hex<< hres<< endl;
pSvc-> Release();
pLoc-> Release();
CoUninitialize();
return 1; //程序失败。
}

//第7步:-------------------------------- -----------------
//在步骤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;

//获取Name属性的值
/ * hr = pclsObj-> Get(LVolumeName,0,& vtProp,0,0);
wcout<< VolumeName:< vtProp.bstrVal<< endl;
VariantClear(& vtProp); * /


if(FAILED(pclsObj-> Get(LAntecedent,0,& vtProp,0,0) ))
{
cout<<未找到指定的属性。<< endl;
}
else
{
wcout<< vtProp.bstrVal< endl;
}

pclsObj-> Release();
}

//清除
// ========

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


// return 0; //程序成功完成。

//执行应用程序
int anInt = anapplication.exec();

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


解决方案

您收到的错误是由于COM已经初始化的事实。 QApplication在其构造函数中调用OleInitialize,但QCoreApplication不会,因此当您使用QApplication时,您会得到错误0x80010106(RPC_E_CHANGED_MODE)。有关进一步阅读,请参阅 CoInitializeEx 的文档。以下文章应提供更多见解。 / p>

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

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

the same code i ut into the Qt application ,

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

But if i use QCroreApplication anApplication ( argc, argv ); it is working

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

Any suggestions or help

#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;
}
//-----------------------------------------------------------------------------

解决方案

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.

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

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