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. Using dll creating in QT in QT app with winapi function
Forum Updated to NodeBB v4.3 + New Features

Using dll creating in QT in QT app with winapi function

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 3 Posters 381 Views 1 Watching
  • 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.
  • T Offline
    T Offline
    TomNow99
    wrote on last edited by TomNow99
    #1

    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.

    1 Reply Last reply
    0
    • B Offline
      B Offline
      Bonnie
      wrote on last edited by
      #2

      You've never exported AddSum.

      1 Reply Last reply
      1
      • T Offline
        T Offline
        TomNow99
        wrote on last edited by TomNow99
        #3

        @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

        mrjjM 1 Reply Last reply
        0
        • T TomNow99

          @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

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

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

          1 Reply Last reply
          1
          • B Offline
            B Offline
            Bonnie
            wrote on last edited by Bonnie
            #5

            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.

            1 Reply Last reply
            2
            • T Offline
              T Offline
              TomNow99
              wrote on last edited by
              #6

              @Bonnie @mrjj Thank you. Solution is:

              extern "C" DLLPROBA_EXPORT int AddSum(int a, int b);
              
              1 Reply Last reply
              1

              • Login

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