Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Using Qt in a .dll



  • Hi. I'm attempting to make a DLL with an entry point that is executed when LoadLibrary is called. In the entry point, the DLL creates a thread, and then I need the thread to connect to my nodejs server with a websocket endpoint. I would like to do this with QTcpSocket. I have tried to do this, but whenever I use any Qt features (such as simply declaring a QTcpSocket), the entry point of my DLL fails to execute when LoadLibrary is called. Is it possible to do this and if so, how can I go about doing it?



  • @psyfl
    I believe you have to create something like a QCoreApplication before you can make any Qt calls, have you done anything like that? Have a read of https://stackoverflow.com/questions/2150488/using-a-qt-based-dll-in-a-non-qt-application, or https://stackoverflow.com/questions/17222584/how-to-create-a-qt-shared-library-for-a-non-qt-application ?

    but whenever I use any Qt features (such as simply declaring a QTcpSocket),
    the entry point of my DLL fails to execute when LoadLibrary is called

    If it fails this early, another possibility might be failing to find any further dependent libraries to load, though I wonder if you'd get a specific error message for that if it happens.



  • @JonB is right. I made some DLLs to be called by non Qt code. The problem is any code that uses the Qt event loop will always need an event loop. This means at the very least your DLL needs to startup a QCoreApplication.



  • @MrShawn
    I think it's even deeper than that. I believe you need to have created a QCoreApplication before you can create anything derived from QObject (regardless of Qt event loop or not). And since OP's QTcpSocket is derived from QObject I don't think you can create one (stack or heap) until Qt has been properly "initialized" via QCoreApplication. That may be why it does not appear to work.



  • A good test will be to call your DLL using a basic Qt app after you create your QCoreApplication. The first time I did this I tried to call my DLL from labview and it didn't work but when I tried to use it with my Qt application it worked fine.

    Not to mention debugging will also be easier with QDebug if you debug it that way.


Log in to reply