what is good way to converting application .exe to dll file?



  • Hi there,
    we have qt application, which has GUI. But Now we want to implemented something like third party library and remove GUI part. So that User can control our application from his code. Customer code may be contain Qt code or non Qt code.

    which one is good way to create API in qt apps.

    1. create the DLL file for application, and run QCoreApplication a(argc, argv); in separate thread.
    2. create the console application for user. So that, user will start manually or from his/her code and close it.
    3. create various utilities for my application.

    I would like to go with option 1. I want to create separate dll, and I am calling QCoreApplication a(argc, argv); in startAPP method of dll, and exit by stopAPP method of dll.

    startAPP method :

    void startAPP(){
           classPrivateVar = new QCoreApplication ();
           MainWindow w;
           classPrivateVar->exec();
    }
    

    stopAPP method :

    void stopAPP(){
           classPrivateVar->quit();
    }
    

    classPrivateVar->exec(); will make problem or not for User? I mean If user will call the startAPP method from his/her code then classPrivateVar->exec(); will block the main thread or not?

    if any other option are efficient way available then i am ready to walk on that way.


  • Qt Champions 2018

    @Yash001 Is your DLL going to be used only in Qt applications or non Qt as well?



  • @jsulm In most of the case, customer will use dll in non Qt code. So that, I can go with non Qt code.



  • For non Qt code I am trying to call Qcoreapplication with help of another thread.

    Here the code:
    ApplicationThread.h

    #pragma once
    #include <qthread.h>
    #include "squidstat_dll_global.h"
    #include <iostream> 
    
    class SQUIDSTAT_DLLSHARED_EXPORT ApplicationThread:public QThread
    {
    	void run();
    };
    
    class SQUIDSTAT_DLLSHARED_EXPORT ApplicationStarter : public QObject
    {
    	Q_OBJECT
    
    public:
    	ApplicationStarter();
    	~ApplicationStarter();
    public slots:
    	void onStarted();
    };
    

    ApplicationThread.cpp

    #include "ApplicationThread.h"
    #include "MainWindow.h"
    #include "qcoreapplication.h"
    
    
     namespace ToolThreadGlobal
    {
    	static int argc = 1;
    	static char* argv[] = { QString("SquidState.exe").toLocal8Bit().data(),NULL};
    	static QCoreApplication *coreApp = nullptr;
    	static ApplicationThread *toolThread = nullptr;
    };
    
    
    
    void ApplicationThread::run() {
    	if (ToolThreadGlobal::coreApp){
    		std::cout << "\nApplicationThread: thread run method is call";
    		ToolThreadGlobal::coreApp->exec();
    	}
    }
    
    
    ApplicationStarter::ApplicationStarter()
    {
    	if (ToolThreadGlobal::toolThread == nullptr)
    	{
    		std::cout << "\nApplicationstarter Constructor call";
    		ToolThreadGlobal::toolThread = new ApplicationThread();
    		connect(ToolThreadGlobal::toolThread, &ApplicationThread::started, this, &ApplicationStarter::onStarted, Qt::DirectConnection);
    		ToolThreadGlobal::toolThread->start();
    	}
    }
    ApplicationStarter::~ApplicationStarter()
    {
    	std::cout << "\nApplicationStarter:: destructor is called";
    	// Ensure that the thread and the QCoreApplication are cleanly shut down:
    	ToolThreadGlobal::toolThread->quit();
    	ToolThreadGlobal::coreApp->quit();
    	ToolThreadGlobal::coreApp->deleteLater();
    	ToolThreadGlobal::coreApp = nullptr;
    	ToolThreadGlobal::toolThread->terminate();
    	ToolThreadGlobal::toolThread->wait();
    	ToolThreadGlobal::toolThread->deleteLater();
    	ToolThreadGlobal::toolThread = nullptr;
    }
    
    
    void ApplicationStarter::onStarted()
    {
    	std::cout << "\nApplicationStarter::signal received";
    	if (QCoreApplication::instance() == NULL) {
    		std::cout << "\nApplicationStarter::call QcoreApplication";
    		ToolThreadGlobal::coreApp = new QCoreApplication(ToolThreadGlobal::argc, ToolThreadGlobal::argv);
    		std::cout << "\nApplicationStarter::call worker";
    		MainWindow w;
    		std::cout << "\nApplicationStarter::call before exe";
    	}
    }
    
    

    SquidState.h

    #pragma once
    #include "squidstat_dll_global.h"
    #include "ApplicationThread.h"
    
    #ifndef _SQUIDSTAT_H_
    #define _SQUIDSTAT_H_
    
    namespace AISquid {
    
    	class SQUIDSTAT_DLLSHARED_EXPORT SquidStat {
    
    	public:
    		SquidStat();
    		~SquidStat();
    		void startAPP();
    		void closeAPP();
    	};
    }
    
    #endif // !_SQUIDSTAT_H_
    

    SquidState.cpp

    #include <stdlib.h>
    #include "SquidStat.h"
    #include "ApplicationThread.h"
    
    AISquid::SquidStat::SquidStat() {
    	std::cout << "\nSquidStat: SquidStat class object is created";
    };
    AISquid::SquidStat::~SquidStat() {
    	std::cout << "\nSquidStat: SquidStat class object is destriod ";
    };
    
    void AISquid::SquidStat::startAPP()
    {
    };
    void AISquid::SquidStat::closeAPP()
    {	
    };
    

    Testing My dll with help of below code.

    tester.cpp

    #include "SquidStat.h"
    #include "ApplicationThread.h"
    
    void main(int argc, char *argv[])
    {
    	ApplicationStarter start;
    	AISquid::SquidStat obj;
    	obj.startAPP();
    	
    	return;	
    }
    
    

    OutPut of Tester:
    0_1554857416616_285cd514-f416-4d15-817b-51334702ce84-image.png

    What is wrong am I doing here? I am receiving call for slot onStarted(); once ApplicationStarter start; object is destroy.

    Any suggestion would be more appreciable.