Using dll creating in QT in QT app with winapi function
-
wrote on 27 Jul 2020, 06:46 last edited by TomNow99
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 0So 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. -
wrote on 27 Jul 2020, 07:09 last edited by
You've never exported AddSum.
-
wrote on 27 Jul 2020, 07:12 last edited by 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 -
@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
falseHi
Should it not be
DLLPROBA_EXPORT int AddSum(int a, int b);
? -
wrote on 27 Jul 2020, 07:36 last edited by Bonnie
Ah, I've not tried the QLibrary function yet.
For the win api, refer to this stackoverflow post
You'll need to addextern "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/6