Results 1 to 2 of 2

Thread: How to load DLL(C/C++) in Qt on Windows?

  1. #1
    Join Date
    Oct 2009
    Location
    Brazil Maceió/Alagoas
    Posts
    24
    Thanks
    7
    Qt products
    Qt4 Qt/Embedded Qt Jambi
    Platforms
    Unix/X11 Windows

    Question How to load DLL(C/C++) in Qt on Windows?

    Hello, everyone.

    I need to load a library dynamically (the CapturaECF.dll this in a different directory): "D:\Sweda\lib"

    The result is that the library can not be loaded, the message is always shown.

    my project file:

    --------------------------------------------------
    QT -= gui

    TARGET = capture
    CONFIG += console
    CONFIG -= app_bundle

    TEMPLATE = app

    SOURCES += main.cpp

    LIBS += d:/sweda/lib/CapturaECF.lib
    --------------------------------------------------

    CPP File:

    --------------------------------------------------

    #include <QtCore/QCoreApplication>
    #include <iostream>
    #include <QLibrary>
    using namespace std;

    int main(int argc, char *argv[])
    {
    QCoreApplication a(argc, argv);

    typedef int (*OpenDllPorta)(int porta,int veloc, QString str);

    QLibrary library("CapturaECF");
    library.load();

    bool okLoad = library.load(); // check load DLL file successful or not
    bool ok = library.isLoaded(); // check is DLL file loaded or not

    if (ok) {
    cout << "ok" << endl;
    } else {
    cout << "not ok" << endl;
    cout << library.errorString().toStdString() << " " << library.fileName().toStdString() << endl;
    }


    return a.exec();
    }

    --------------------------------------------------

  2. #2
    Join Date
    Nov 2009
    Posts
    129
    Thanks
    4
    Thanked 29 Times in 29 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to load DLL(C/C++) in Qt on Windows?

    Quote Originally Posted by josecarlosmissias View Post
    I need to load a library dynamically (the CapturaECF.dll this in a different directory): "D:\Sweda\lib"
    [...]
    LIBS += d:/sweda/lib/CapturaECF.lib
    [...]
    QLibrary library("CapturaECF");
    library.load();
    I think there are two problems here. The obvious one you can solve by reading the documentation for QLibrary::fileName-prop — you need to specify the absolute path to the library as part of the name:
    Qt Code:
    1. QLibrary library("d:/sweda/lib/CapturaECF");
    To copy to clipboard, switch view to plain text mode 
    if the library is not in one of the locations the operating system normally searches for dynamically-loaded libraries.

    However, I believe there is a mistake in using both LIBS and QLibrary. LIBS= links libraries (either static libraries or stubs for dynamic libraries) into the executable, so that your code calls the functions normally and any dynamic libraries are loaded automatically during start-up; QLibrary is normally be used when you don’t always want to load the library, or you don’t want the program to fail entirely if the library can’t be loaded. I could be missing something, but I think you probably want to pick one method or the other.

    If you’re getting a Windows error box saying that the application could not be started because CapturaECF.dll could not be loaded, the LIBS entry is the reason. Switching to using only the QLibrary method (with the absolute path included) should fix that; but if you want to use the LIBS method, there are a few alternatives:

    1. Put a copy of the DLL in the program directory.

    2. Set the working directory to the directory containing the DLL. (Use the Projects / Run Settings tab in Qt Creator; set the shortcut properties to do this when running directly from Windows.)

    3. Add the directory containing the DLL to the PATH environment variable.

    4. Use the Windows registry; see this description of how to use HKLM\Software\Microsoft\Windows\CurrentVersion\App Paths.

  3. The following user says thank you to Coises for this useful post:

    josecarlosmissias (20th November 2009)

Similar Threads

  1. Replies: 1
    Last Post: 16th November 2009, 07:10
  2. Qt Jambi, deploying app on Mac & Windows fails
    By ChrisColon in forum Installation and Deployment
    Replies: 2
    Last Post: 16th February 2009, 22:05
  3. Replies: 5
    Last Post: 15th January 2009, 09:03
  4. Gifs don't load in QTextEdit under windows
    By balazsbela in forum Newbie
    Replies: 4
    Last Post: 26th August 2008, 22:28
  5. Qt and windows vista
    By munna in forum General Discussion
    Replies: 8
    Last Post: 11th January 2006, 22:33

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.