Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Using dll creating in QT in QT app with winapi function



  • Hello,

    I create very simple dll in QT:

    dllproba.h

    #ifndef DLLPROBA_H
    #define DLLPROBA_H
    
    #include "dllProba_global.h"
    
    int AddSum(int a, int b);
    
    class DLLPROBA_EXPORT DllProba
    {
    public:
        DllProba();
    };
    
    #endif // DLLPROBA_H
    

    dllProba.cpp

    #include "dllproba.h"
    
    DllProba::DllProba(){}
    
    int AddSum(int a, int b)
    {
        return a*b+a;
    }
    
    

    dllProba_global.h

    #ifndef DLLPROBA_GLOBAL_H
    #define DLLPROBA_GLOBAL_H
    
    #include <QtCore/qglobal.h>
    
    #if defined(DLLPROBA_LIBRARY)
    #  define DLLPROBA_EXPORT Q_DECL_EXPORT
    #else
    #  define DLLPROBA_EXPORT Q_DECL_IMPORT
    #endif
    
    #endif // DLLPROBA_GLOBAL_H
    
    

    Next I create QT app with winapi functions:

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include "windows.h"
    #include <QDebug>
    #include "dllproba.h"
    #include "dllProba_global.h"
    
    extern int AddSum( int a, int b );
    
    MainWindow::MainWindow(QWidget *parent)
        : QMainWindow(parent)
        , ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    
        typedef int( * MYPROC )( int, int );
        MYPROC fun;
    
        HINSTANCE hDll = LoadLibrary( L"dllProba" );
        qInfo()<<(hDll?"loadLib 1":"loadLib 0");
    
        if( hDll )
        {
            fun =( MYPROC ) GetProcAddress( hDll, (LPCSTR)"AddSum" );
            qInfo()<<(fun?"loadFun 1":"loadFun 0");
    
            if( fun )
            {
                int L =( fun )( 256,43 );
                qInfo()<<L;
            }
    
            FreeLibrary( hDll );
        }
    
    }
    

    The qDebug() output is:
    loadLib 1
    loadFun 0

    So I have problem with loading function AddSum(int, int ). I try with extern but with no result. What can I do? I have to use winapi functions, so I can't using Qt functions to run AddSum(int, int).

    EDIT error which I get is:
    ERROR_PROC_NOT_FOUND
    127 (0x7F)
    The specified procedure could not be found.



  • You've never exported AddSum.



  • @Bonnie I changed dll to:

    #ifndef DLLPROBA_H
    #define DLLPROBA_H
    
    #include "dllProba_global.h"
    
    int DLLPROBA_EXPORT AddSum(int a, int b);
    
    class DLLPROBA_EXPORT DllProba
    {
    public:
        DllProba();
    };
    
    #endif // DLLPROBA_H
    

    And I try using QT ( to check ), but with no result:

        QLibrary lib("dllProba.dll");
    
        qInfo()<<"loading"<<lib.load();
        qInfo()<<lib.isLoaded();
        typedef int (*FunctionPrototype)(int, int);
        auto function1 = (FunctionPrototype)lib.resolve("AddSum");
    
        qInfo()<<function1;
    

    Output:
    loading true
    true
    false


  • Lifetime Qt Champion

    Hi
    Should it not be
    DLLPROBA_EXPORT int AddSum(int a, int b);
    ?



  • Ah, I've not tried the QLibrary function yet.
    For the win api, refer to this stackoverflow post
    You'll need to add extern "C" to make the function name be just "AddSum" in the dll.
    Or it will be something like "?AddSum@@YAHHH@Z".

    [ADDED] I find out that this is also mentioned in the doc of QLibrary::resolve

    The symbol must be exported as a C function from the library. This means that the function must be wrapped in an extern "C" if the library is compiled with a C++ compiler.



  • @Bonnie @mrjj Thank you. Solution is:

    extern "C" DLLPROBA_EXPORT int AddSum(int a, int b);
    

Log in to reply