Search code examples
c#c++qtdllqnetworkaccessmanager

QNetworkAccessManager->get freezes when called in DLL


I have a dll where I want to do some networking, this dll is called from an C# application as an unmanaged DLL. All initialization works fine but it freezes everytime the ->get() is supposed to run. I have this code:

.........
QUrl path(remotePath);
QNetworkRequest request(path);

currentFile.setFileName(localPath);
if(!currentFile.open(QIODevice::WriteOnly)){
doCallback("failed to open: " + localPath);
}
doCallback("before get: " + remotePath);
QNetworkReply* reply = this->manager->get(request);
doCallback("after get: " + localPath);
...........

The "before get" callback is executed fine but never the one "after get" so I guess it completely freezes when manager is trying the Get() method. Have I missed something or is this just plain impossible through an DLL?


Solution

  • Okey so I got this working and I thought I would share my knowledge. With my unmanaged dll I exported an init function which creates a new QCoreApplication, through this I can later use the event loop by using the QCoreApplication object as parent to my class which executes all networking.

    QCoreApplication* coreApp = 0;
    extern "C" __declspec(dllexport) void initdll()
    {
        if (!coreApp) {
            int argc    = 0;
            coreApp = new QCoreApplication(argc, 0);
        }
        if(!MyFunc){
            MyFunc = new QtDll(coreApp);
        }
    }
    

    By calling this init function in my C# (or whatever) application before I tried any networking I got it working with the code in my first post by adding an exec of the event loop.

    QNetworkReply* reply = this->manager->get(request);
    currentReply = reply;
    QEventLoop eLoop;
    connect(reply, SIGNAL(readyRead()), this, SLOT(slotReadyRead()));
    eLoop.exec( QEventLoop::ExcludeUserInputEvents );
    

    Thanks Peter for your feedback.