How to Create DLL in Qt for usage in 3rd party application e.g. (Metatrader 4) ? Please help!
-
The Metatrader 4 is a 32-bit application.
Therefore, I need to create a 32-bit DLL to be used in the Metatrader 4.Here are my steps.
- I create a new project > Library > C++ Library > Shared Library
Below is my code for tcpclient_global.h
@#ifndef TCPCLIENT_GLOBAL_H
#define TCPCLIENT_GLOBAL_H#include <QtCore/qglobal.h>
#if defined(TCPCLIENT_LIBRARY)
define TCPCLIENTSHARED_EXPORT Q_DECL_EXPORT
#else
define TCPCLIENTSHARED_EXPORT Q_DECL_IMPORT
#endif
#endif // TCPCLIENT_GLOBAL_H
@Below is my code for tcpclient_global.h
@#ifndef TCPCLIENT_H
#define TCPCLIENT_H#include "tcpclient_global.h"
TCPCLIENTSHARED_EXPORT int connectClose(){
return 2;
}
class TCPCLIENTSHARED_EXPORT TCPClient
{public:
TCPClient();
};#endif // TCPCLIENT_H
@code for tcpclient.cpp
@#include "tcpclient.h"TCPClient::TCPClient()
{
}
@I build with Desktop Qt 5.4.0 MSVC2013 32 bit
At Metatrader 4 my code to use the DLL is as below.
@#import "TCPClient.dll"
int connectClose();
#importint OnInit()
{
Alert("Before ConnectClose");
int closeStatus=connectClose();
Alert("After ConnectClose");return(INIT_SUCCEEDED);
}
@
When the metatrader 4 code run till the connectClose(), the program stops immediately.
the Alert "Before ConnectClose"" is displayed successfully, but the Alert "After ConnectClose" is not displayed.
The Alert "After ConnectClose" should be display. and the closeStatus value should be 2 if the DLL creation and linking is successful.Please help!
I am not sure whther I did it wrong at the creation of the DLL or at the metatrader 4. -
[quote author="hskoglund" date="1424725084"]Hi, just guessing but maybe the culprit is the calling convention (__stdcall vs __cdecl) try including this in your .pro file:
@
QMAKE_CFLAGS += /Gz
QMAKE_CXXFLAGS += /Gz
@[/quote]
Yes, dear hskoglund, you are right.
I realized that the metatrader 4 indeed need the calling convention __stdcall
After adding in the calling convention __stdcall into the functions in my header file and QMAKE_CFLAGS += /Gz QMAKE_CXXFLAGS += /Gz into the .pro file. The DLL still cant work properly.
I have done test using other DLLs on the metatrader 4 software,which can work properly. After change to my DLL, then the code just stop after the function is called from DLL without giving any error.
So I am suspecting is my DLL created in QT that is the culprit.Below is my code:
@#-------------------------------------------------TCPClient.pro
#-------------------------------------------------
QT += network
QT -= gui
TARGET = TCPClient
TEMPLATE = libDEFINES += TCPCLIENT_LIBRARY
SOURCES += tcpclient.cpp
HEADERS += tcpclient.h
tcpclient_global.hunix {
target.path = /usr/lib
INSTALLS += target
}QMAKE_CFLAGS += /Gz
QMAKE_CXXFLAGS += /Gz
@@//#-------------------------------------------------
//#
//# tcpclient.h
//#
//#-------------------------------------------------#ifndef TCPCLIENT_H
#define TCPCLIENT_H#include "tcpclient_global.h"
TCPCLIENTSHARED_EXPORT void __stdcall connectClose();
#endif // TCPCLIENT_H
@@//#-------------------------------------------------
//#
//# tcpclient_global.h
//#
//#-------------------------------------------------
#ifndef TCPCLIENT_GLOBAL_H
#define TCPCLIENT_GLOBAL_H#include <QtCore/qglobal.h>
#if defined(TCPCLIENT_LIBRARY)
define TCPCLIENTSHARED_EXPORT Q_DECL_EXPORT
#else
define TCPCLIENTSHARED_EXPORT Q_DECL_IMPORT
#endif
#endif // TCPCLIENT_GLOBAL_H
@@//#-------------------------------------------------
//#
//# tcpclient.cpp
//#
//#-------------------------------------------------#include "tcpclient.h"
void __stdcall connectClose(){
}
@