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. How to Create DLL in Qt for usage in 3rd party application e.g. (Metatrader 4) ? Please help!

How to Create DLL in Qt for usage in 3rd party application e.g. (Metatrader 4) ? Please help!

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 1.6k 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.
  • H Offline
    H Offline
    happymic
    wrote on last edited by
    #1

    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.

    1. 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();
    #import

    int OnInit()
    {
    Alert("Before ConnectClose");
    int closeStatus=connectClose();
    Alert("After ConnectClose");

      return(INIT_SUCCEEDED&#41;;
    

    }

    @

    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.

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

      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
      @

      1 Reply Last reply
      1
      • H Offline
        H Offline
        happymic
        wrote on last edited by
        #3

        [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 = lib

        DEFINES += TCPCLIENT_LIBRARY

        SOURCES += tcpclient.cpp

        HEADERS += tcpclient.h
        tcpclient_global.h

        unix {
        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(){

        }

        @

        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