2

I try to create a COM object and get the interface from the object in Qt. So Qt works as the COM client. In my example the COM server is the CANoe COM server.

Here is my source code of the Qt pro file and my interface.cpp file where I try to establish the connection to CANoe:

pro file:

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

}

When I try to build the program I get the following error code: undefined reference to '_GUID const&_mingw_uuidof()'

Does someone have an idea how to create the COM interface between Qt and a COM Server? I've searched for hours in the Internet but doesn't find any explanations. Only for accessing a COM Server with Visual Studio by using ATL. But I can't use ATL in Qt.

la-ga
  • 160
  • 2
  • 13

1 Answers1

1

__uuid is a convenience to obtain an GUID for a COM class, but any other method will work too. Other than that, your code should work.

The error message looks like a MinGW link error (not enough info in question to be sure), suggesting that you missed a MinGW COM library.

MSalters
  • 173,980
  • 10
  • 155
  • 350
  • 1
    Thank you for your answer, MSalters. What alternatives do I have to `__uuidof`? Maybe then the error will be solved? Do you need some additional info about the libraries? I added `CONFIG += qaxcontainer` in the pro file. This was explaned in the [Qt documentation](http://qt-project.org/doc/qt-4.8/activeqt-container.html). – la-ga Dec 11 '13 at 10:51
  • 1
    Well, the trivial one is ` ... = {LONG,GUID,STRING,WITH,ALL,BITS}` - an IID is just a 16 byte struct. I'm not familiar with the mingw libraries, I've always used Visual Studio for COM (often without ATL). `qaxcontainer` is needed when hosting ActiveX objects in Qt. ActiveX requires COM, but not the other way around. So you probably do not need it. – MSalters Dec 11 '13 at 10:59
  • 1
    I've searched in the program dcomcnfg after the IID and found one for CANoe. But when I insert it in my code as the IID I geht the error "invalid suffix "xxx" on integer constant". The suffix is the part behind the first alphabetic character in the IID. - I've read that I need the ole32.lib for `__uuidof`. Is there a possibility to insert the VS library in Qt? Maybe this would be a way to avoid the error - Thank you for your help – la-ga Dec 11 '13 at 13:51
  • 1
    @la-ga: You need to format the IID initializer as C++ code. I.e. `0x` as the hex prefix, and with 11 terms (1x32 bits, 2x16, and 8x8). – MSalters Dec 11 '13 at 14:13
  • 1
    Ok - thank you so much. Now CANoe starts when I create an instance of the COM Object. – la-ga Dec 12 '13 at 10:15