Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Qt DLL (MinGW) for VB (VBA)
QtWS25 Last Chance

Qt DLL (MinGW) for VB (VBA)

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 2 Posters 3.2k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    MChe
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • hskoglundH Offline
      hskoglundH Offline
      hskoglund
      wrote on last edited by
      #2

      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 :-)

      M 1 Reply Last reply
      2
      • hskoglundH hskoglund

        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 :-)

        M Offline
        M Offline
        MChe
        wrote on last edited by MChe
        #3

        @hskoglund Thx, It's work!

        But I want to use Qt features in VBA (QWidgets, signals and slots, etc.).

        DLLTestOne.pro

        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.

        M 1 Reply Last reply
        0
        • M MChe

          @hskoglund Thx, It's work!

          But I want to use Qt features in VBA (QWidgets, signals and slots, etc.).

          DLLTestOne.pro

          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.

          M Offline
          M Offline
          MChe
          wrote on last edited by
          #4

          Sorry, I have its problem for all Qt apps on PC. I will try to fix it.

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved