How to add third party library .DLL in QT project



  • Hi,

    I want to add third party .dll file in my project. and call exposed function from qt program. I am running QT 5.4 on windows 8.1.

    "Add library" feature in QT opens a form. then asks for type of library (internal, external or system). if we pick external it goes to next screen but provides .lib and .a file as acceptable extensions NOT .DLL

    How can we add .dll in our QT project in windows environment and use it. (Call exposed functions of .DLL from QT program).

    Thanks
    Muhammad Sarfraz


  • Moderators

    Hi, welcome to devnet.

    To link to external library you either need the .dll, the .lib/.a and the header, in which case you use the above wizard, or use "QLibrary":http://doc.qt.io/qt-5/qlibrary.html to load the .dll dynamically and resolve entry points manually.



  • Thanks Chris for your reply.

    In QT 5.4 this wizard has no option to select .DLL type of file. It is fixed to .a or .lib type.
    I am running QT 5.4 on Windows 8.1.

    Am I using wizard correctly or can it be a Bug in QT 5.4 environment ?

    Thanks
    Muhammad


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    If you want to link against your 3rd party library, then it's either the lib or a files. You don't link against dlls



  • Hi SGaist,

    Thanks for your reply.

    Can you please tell me how to use third party .dll in QT ? any example ?

    Thanks
    Muhammad


  • Moderators

    if you use the wizard like already mentioned you link against this DLL file as an dependency.
    Meaning your application can't even start without it and automatically loads it if it's found or displays an error message if not.



  • How can i use third party .DLL ? any example?



  • Hi, just tested using a 3rd party DLL with Qt, it's pretty straightforward:
    I choose to test an old DLL present in Windows XP for the FreeCell game, called cards.dll, if you don't have Windows XP you can download the dll "here":http://www.dlldump.com/download-dll-files_new.php/dllfiles/C/cards.dll/5.1.2600.0/download.html The site looks suspicious but I checked, if you click to the right (Click Here to Download cards.zip) you'll get the exact same cards.dll that's in C:\Windows\System32 in Windows XP.

    For this example I put cards.dll in C:\Downloads. Also you need to know about the function(s) you are calling, in this case I googled for a description of the functions inside cards.dll, I use 2: cdtInit and cdtDraw.

    Create a new Widgets app, first insert CONFIG += c++11 in your .pro file to turn on C++11 features, then goto your mainwindow.cpp, insert these includes:
    @
    #include "QLibrary"
    #include "QTimer"
    #include "windows.h"
    @

    then below ui->setupUi(this); insert this fancy code:
    @
    ((int (WINAPI*)(int*,int*)) QLibrary("C:/Downloads/cards.dll").resolve("cdtInit"))(new int, new int);
    QTimer* pTimer = new QTimer(this);
    pTimer->start(500);
    connect(pTimer,&QTimer::timeout,
    {
    auto GetDC = (int (WINAPI *)(int)) QLibrary("user32.dll").resolve("GetDC");
    auto cdtDraw = (int (WINAPI *)(int,int,int,int,int,int)) QLibrary("C:/Downloads/cards.dll").resolve("cdtDraw");
    cdtDraw(GetDC(0),rand() % 1000,rand() % 1000,rand() % 52,0,0);
    });
    @

    If you do it right you'll get lots of cards drawn on your screen like this: !http://www.tripleboot.org/Pictures/DLLTest.png(Lots of cards)!



  • I just want to add that normally you do not use random 3rd party dll.
    If you are legally allowed to use it you will have access to headers files you need and associated lib files or would be able to build them.

    That means that you do not need to use QLibrary or in other way to resolve calls. you will include appropriate header and link against appropriate lib file
    Dll and call functions like they were in your module. Dll will be loaded by your exe at runtime. You will have to provide it with your code.


  • Lifetime Qt Champion

    I agree with the random part however, that technique is perfectly fine otherwise and Qt uses it for e.g. OpenSSL by default



  • Thanks for your reply Robot Herder, Ant Farmer and Mad Scientist.

    Is there any difference if i want to use this .dll for mobile app (Android and Windows)?

    for example can i use card.dll in Android app and get same results ?

    Thanks and Regards


  • Lifetime Qt Champion

    No you can't that dll uses Win32 specific API


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.