【问题标题】:Create COM object in Qt在 Qt 中创建 COM 对象
【发布时间】:2013-12-11 10:06:27
【问题描述】:

我尝试创建一个 COM 对象并从 Qt 中的对象获取接口。所以 Qt 作为 COM 客户端工作。在我的示例中,COM 服务器是 CANoe COM 服务器。

这是我的 Qt pro 文件的源代码和我的 interface.cpp 文件,我在其中尝试建立与 CANoe 的连接:

专业文件:

# Add more folders to ship with the application, here
folder_01.source = qml/3com_interface
folder_01.target = qml
DEPLOYMENTFOLDERS = folder_01

# Additional import path used to resolve QML modules in Creator's code model
QML_IMPORT_PATH =

# If your application uses the Qt Mobility libraries, uncomment the following
# lines and add the respective components to the MOBILITY variable.
# CONFIG += mobility
# MOBILITY +=

# The .cpp file which was generated for your project. Feel free to hack it.
SOURCES += main.cpp \
    interface.cpp

# Installation path
# target.path =

# Please do not modify the following two lines. Required for deployment.
include(qtquick2applicationviewer/qtquick2applicationviewer.pri)
qtcAddDeployment()

HEADERS += \
    CANoe.h \
    interface.h

CONFIG += qaxcontainer

interface.cpp

#include "interface.h"
#include <QDebug>
#include "objbase.h"
#include "CANoe.h"

#include "windows.h"
#include "winnls.h"
#include "shobjidl.h"
#include "objidl.h"
#include "shlguid.h"

#include "strsafe.h"


Interface::Interface(QObject *parent) :
    QObject(parent)
{
}

void Interface::trigger_slot(const QString &msg)
{


    IApplication* pIApp;
    HRESULT result;

    result = CoInitialize(NULL);

    CLSID clsid;
    result = CLSIDFromProgID(L"CANoe.Application", &clsid);

    if(SUCCEEDED(result))
    {
        qDebug() << "CLSID saved";
    }

    const IID IID_IApplication = __uuidof(IApplication);

    result = CoCreateInstance(clsid, NULL, CLSCTX_LOCAL_SERVER, IID_IApplication, (void**) &pIApp);

    if(SUCCEEDED(result))
    {
        qDebug() << "Connection established";
    }

}

当我尝试构建程序时,我收到以下错误代码: 对'_GUID const&_mingw_uuidof()'的未定义引用

有人知道如何在 Qt 和 COM 服务器之间创建 COM 接口吗?我在互联网上搜索了几个小时,但没有找到任何解释。仅用于使用 ATL 访问带有 Visual Studio 的 COM 服务器。但是我不能在 Qt 中使用 ATL。

【问题讨论】:

    标签: c++ qt interface com com-interface


    【解决方案1】:

    __uuid 可以方便地获取 COM 类的 GUID,但任何其他方法也可以。除此之外,您的代码应该可以工作。

    错误消息看起来像是 MinGW 链接错误(没有足够的信息来确定),表明您错过了 MinGW COM 库。

    【讨论】:

    • 感谢您的回答,MSalters。 __uuidof 有什么替代方法?也许然后错误将得到解决?你需要一些关于图书馆的额外信息吗?我在 pro 文件中添加了CONFIG += qaxcontainer。这在Qt documentation 中有解释。
    • 嗯,最简单的是` ... = {LONG,GUID,STRING,WITH,ALL,BITS}` - IID 只是一个 16 字节的结构。我不熟悉 mingw 库,我一直使用 Visual Studio for COM(通常没有 ATL)。在 Qt 中托管 ActiveX 对象时需要qaxcontainer。 ActiveX 需要 COM,但反之则不然。所以你可能不需要它。
    • 我在程序 dcomcnfg 中搜索了 IID 之后,找到了一个用于 CANoe 的。但是当我将它作为 IID 插入到我的代码中时,我得到了错误“整数常量上的无效后缀“xxx””。后缀是 IID 中第一个字母字符后面的部分。 - 我读到我需要__uuidof 的ole32.lib。是否有可能在 Qt 中插入 VS 库?也许这是避免错误的一种方法 - 感谢您的帮助
    • @la-ga:您需要将 IID 初始化程序格式化为 C++ 代码。 IE。 0x 作为十六进制前缀,有 11 个术语(1x32 位、2x16 和 8x8)。
    • 好的 - 非常感谢。现在,当我创建 COM 对象的实例时,CANoe 就会启动。
    猜你喜欢
    • 2013-04-10
    • 1970-01-01
    • 2011-09-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-05
    • 2018-03-13
    • 1970-01-01
    相关资源
    最近更新 更多