Qt DLL (MinGW) for VB (VBA)
-
Hello!
I want to create Qt DLL for VB (VBA).
I find out how I can do it via c++ (https://stackoverflow.com/questions/2720246/how-to-create-dll-using-gcc-compiler-mingw-for-visual-basic). I use 'gcc fooBar.cpp -shared -Wl,--kill-at -o fooBar.dll'. It's work in VBA (Excel).
But I can't do it with Qt :( Have anybody example source of Qt DLL for VBA? How can I use something like 'gcc fooBar.cpp -shared -Wl,--kill-at -o fooBar.dll' in Qt Creator?
-
Hi, it's actually easier with Qt, you don't have to specify any command line switches to gcc. You start by creating a Library (C++ library), Standard Library, then select the Qt modules you need (I tried just with QtCore).
I just tested with a global function in my untitled.cpp (also I used extern "C" to simplify):extern "C" __declspec(dllexport) int __stdcall square_int(int x) { return x * x; }
Build it in Release mode (not Debug). To make the untitled.dll loadable by Excel, all the Qt and MinGW .dlls needed, like libwinpthread-1.dll, libgcc_s_dw2-1.dll, Qt5Core.dll etc. needs to be visible in the PATH environment variable.
In Excel, test the following VBA code in Book1 - Sheet1:
Private Declare Function square_int Lib _ "C:\Projects\build-untitled-Desktop_Qt_5_6_2_MinGW_32bit-Release\release\untitled.dll" Alias "square_int@4" _ (ByVal x As Long) As Long Sub test() Debug.Print square_int(5) End Sub
In the Immediate window in Excel's VBA window ' 25' should appear!
P.S. (I can't figure out right now how to call the VBA code from inside a cell in the spreadsheet,
=square_int(5)
does not work, but maybe something similar :-) -
@hskoglund Thx, It's work!
But I want to use Qt features in VBA (QWidgets, signals and slots, etc.).
QT += widgets network TARGET = DLLTestOne TEMPLATE = lib DEFINES += DLLTESTONE_LIBRARY SOURCES += \ dlltestone.cpp HEADERS += \ dlltestone.h \ dlltestone_global.h unix { target.path = /usr/lib INSTALLS += target }
dlltestone.h
#ifndef DLLTESTONE_H #define DLLTESTONE_H #include "dlltestone_global.h" class DLLTESTONESHARED_EXPORT DLLTestOne { public: DLLTestOne(); }; #endif // DLLTESTONE_H
dlltestone_global.h
#ifndef DLLTESTONE_GLOBAL_H #define DLLTESTONE_GLOBAL_H #include <QtCore/qglobal.h> #include <QApplication> #if defined(DLLTESTONE_LIBRARY) # define DLLTESTONESHARED_EXPORT Q_DECL_EXPORT #else # define DLLTESTONESHARED_EXPORT Q_DECL_IMPORT #endif #endif // DLLTESTONE_GLOBAL_H
dlltestone.cpp
#include "dlltestone.h" DLLTestOne::DLLTestOne() { } extern "C" __declspec(dllexport) int __stdcall square_int(int x) { char arg0[] = "programName1"; char arg1[] = "arg"; char arg2[] = "another arg"; char* argv[] = { &arg0[0], &arg1[0], &arg2[0], NULL }; int argc = (int)(sizeof(argv) / sizeof(argv[0])) - 1; QApplication a(argc, &argv[0]); return a.exec(); }
If I run 'square_int' in VBA, I have error: The application failed to start because it could not find or load the Qt platform plugin "windows" in "".
When I run simple Qt exe, It's run ok. I try to create folder "platform" with "qwindows.dll" everywhere, I haven't result :(
VBA:
Private Declare Function square_int Lib _ "P:\Mydocuments\QtProject\DLLTestOne\build-DLLTestOne-Desktop_Qt_5_9_0_MinGW_32bit-Release\release\DLLTestOne.dll" Alias "square_int@4" _ (ByVal x As Long) As Long Sub test() Debug.Print square_int(5) End Sub
Setting QT_QPA_PLATFORM_PLUGIN_PATH doesn't help.