QLibrary does not load



  • I'm trying to write a simple example of loading a dll file, and the load fails.
    I'm using qt 5.1.1 qt creator 3.0.1.
    my compiler is: Microsoft Visual C++ Compiler 11.0 (amd64)

    the pro file:
    @
    QT += core

    greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
    
    TARGET = rrrr
    
    SOURCES += main.cpp
    

    @

    the main file is:
    @
    #include <QLibrary>

    int main()
    {
    
        bool is;
        QLibrary lib("C:\A.dll");
        is = lib.load();
        is = lib.isLoaded();
    
        return 0;
    }@
    

    The A.dll file of-course exists inside C.
    I get the value false for the variable "is" while debbuging.


  • Moderators

    Hi,

    Does A.dll depends on anyother dll's? If yes, are they present in the same path ?



  • No. is doesn't depend on another dll.



  • @
    QLibrary lib("C:\A.dll");
    @

    Should be:
    @
    QLibrary lib("C:\A.dll");
    @

    Use double slash in strings.



  • OK, I did so, but it still doesn't work.



  • What is the error string provided by:
    @
    QLibrary::errorString()
    @

    after you load the library?



  • Cannot load library C:\A.dll:



  • You could try with the Windows SDK's LoadLibrary function (#include <Windows.h>). Then get the error identifier using GetLastError:

    @
    #include <Windows.h>
    #include <string>
    #include <QDebug>

    std::string getLastErrorAsString();

    int main()
    {
    HMODULE libraryHandle = ::LoadLibrary( "C:\A.dll" );
    if ( libraryHandle == 0 )
    {
    const DWORD errorCode = ::GetLastError();
    qDebug() << "error: code(" << errorCode << "), description(" << getLastErrorAsString() << ')';
    }

    return 0;
    

    }

    std::string getLastErrorAsString()
    {
    //Get the error message, if any.
    DWORD errorMessageID = ::GetLastError();
    if(errorMessageID == 0)
    return "No error message has been recorded";

    LPSTR messageBuffer = nullptr;
    size_t size = FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
                                 NULL, errorMessageID, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR)&messageBuffer, 0, NULL);
    
    std::string message(messageBuffer, size);
    
    //Free the buffer.
    LocalFree(messageBuffer);
    
    return message;
    

    }
    @


  • Moderators



  • [quote author="napajejenunedk0" date="1401015515"]You could try with the Windows SDK's LoadLibrary function (#include <Windows.h>). Then get the error identifier using GetLastError:

    @
    #include <Windows.h>
    #include <string>
    #include <QDebug>

    std::string getLastErrorAsString();

    int main()
    {
    HMODULE libraryHandle = ::LoadLibrary( "C:\A.dll" );
    if ( libraryHandle == 0 )
    {
    const DWORD errorCode = ::GetLastError();
    qDebug() << "error: code(" << errorCode << "), description(" << getLastErrorAsString() << ')';
    }

    return 0;
    

    }

    std::string getLastErrorAsString()
    {
    //Get the error message, if any.
    DWORD errorMessageID = ::GetLastError();
    if(errorMessageID == 0)
    return "No error message has been recorded";

    LPSTR messageBuffer = nullptr;
    size_t size = FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
                                 NULL, errorMessageID, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR)&messageBuffer, 0, NULL);
    
    std::string message(messageBuffer, size);
    
    //Free the buffer.
    LocalFree(messageBuffer);
    
    return message;
    

    }
    @[/quote]

    the error massage is:
    error: code <126>, description<the specified module could not be found.



  • Just to be sure: You are not trying to load a 64-Bit DLL from a 32-Bit process or vice versa, right? If so, it clearly cannot work.

    Secondly, does your "A.dll" have any further dependencies? Check this with Dependency Walker, if not sure! And make sure that all required dependencies are present! Note that Windows will look for the dependencies in the directory where your EXE file is located and in some system directories (like "C:\Windows\System32"), but not in the directory where "A.dll" is located.

    Last but not least: Putting the DLL file directly into "C:" is pretty unusual. Non-elevated processes don't have write-access there. It's even possible the Virtual Store actually copied the DLL to another place, like "C:\Users\UserName\AppData\Local\VirtualStore\A.dll" rather than "C:\A.dll". So in the Explorer it may appear to be stored at "C:\A.dll" while it fact it is not! So try loading the DLL from a more "safe" place, like your "Desktop" directory...



  • Thank you very much!

    The problem was that the dll was a 32-Bit DLL, and the process was 64.
    I changed the dll and now its working :).


Log in to reply
 

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