QT application programming: QT calls COM components under windows

1. Environmental introduction

Operating system:  win10 64 bit

QT version:  5.12.6

Visual Studio IDE version:   2017

Sample project download link:   https://download.csdn.net/download/xiaolong1126626497/14919797

Two, create a COM component

In order to facilitate testing, I use QT to create COM components, which are then called by QT itself. In theory, the calling process of COM components generated by any language should be the same.

The method of QT creating COM is here:

(1) Use QT to generate COM components under VS2017:       https://blog.csdn.net/xiaolong1126626497/article/details/112556866

(2) COM components generated under QtCreate:   https://blog.csdn.net/xiaolong1126626497/article/details/112550412

 

The current project for creating COM components is generated using VS2017+QT plug-in.

In order to facilitate registration, you can write a script in the directory:

  Installation code:

cd /d %~dp0
@regsvr32 ActiveQtServer_vs2017.dll
@exit

  Uninstall code:

cd /d %~dp0
@regsvr32 /u ActiveQtServer_vs2017.dll
@exit

Three, create a project in QtCreate to call COM components

3.1 Create a project

3.2 Writing test code

#include <QApplication>
#include <QAxObject>
#include <QDebug>
#include <QFile>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

     QAxObject *mpAxObj;
    mpAxObj = new QAxObject();

    //指定调用的COM组件类ID,这个ID要填正确.
    mpAxObj->setControl("{fe8bb3a1-998e-4318-b4ee-4263a1cc06a2}");

    //导出支持调用的函数接口
    QString DOC = mpAxObj->generateDocumentation();
    QFile outFile("com_function.html");
    outFile.open(QIODevice ::ReadWrite|QIODevice ::Text);
    QTextStream TS(&outFile);
    TS<<DOC<<endl;

    //调用COM组件函数接口: 获取版本号
    QString result = mpAxObj->dynamicCall("VersionNumber()").toString();
    qDebug()<<"VersionNumber:"<<result;

    //调用COM组件函数接口: 求和计算
    QVariantList params ={100,200};
    int sum_val = mpAxObj->dynamicCall("sum(int, int)", params).toInt();
    qDebug()<<"sum:"<<sum_val;

    //调用COM组件函数接口: 显示界面
    mpAxObj->dynamicCall("show()");

    return a.exec();
}

3.3 Project pro file

QT       += core gui
QT += axcontainer
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

CONFIG += c++11

# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS

# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0

SOURCES += \
    main.cpp

HEADERS +=

FORMS +=

# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

3.4 Run test

Note: How to call the operation failure, remember to copy all the files that the COM component dll depends on to the directory where the test program is currently running to prevent the operation failure if the dependent files are not found.

 

 

 

Guess you like

Origin blog.csdn.net/xiaolong1126626497/article/details/112978956