我尝试创建一个 COM 对象并从 Qt 中的对象获取接口(interface)。所以 Qt 作为 COM 客户端工作。在我的示例中,COM 服务器是 CANoe COM 服务器。
这是我的 Qt pro 文件的源代码和我尝试建立与 CANoe 连接的 interface.cpp 文件:
专业文件:
# 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()”的 undefined reference
有人知道如何在 Qt 和 COM 服务器之间创建 COM 接口(interface)吗?我在互联网上搜索了几个小时,但没有找到任何解释。仅适用于使用 ATL 访问带有 Visual Studio 的 COM 服务器。但是我不能在 Qt 中使用 ATL。
最佳答案
__uuid
可以方便地获取 COM 类的 GUID,但任何其他方法也可以。除此之外,您的代码应该可以正常工作。
错误消息看起来像 MinGW 链接错误(没有足够的信息可以确定),表明您错过了 MinGW COM 库。
关于c++ - 在 Qt 中创建 COM 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20516153/