[SOLVED] Using an external DLL created in Visual Studio/windows



  • Hi

    I've successfully downloaded and compiled with Mingw, Qt and OpenCV and have them working nicely together for a prototype application.

    For a new test (targeting windows only) I want to integrate into my prototype, a DLL written by a third party. The DLL was written in C++ and compiled on windows with Visual Studio 2012. I have the DLL, LIB and .H files. The header file contains comments stating that "All exported library functions and data structures conform to ANSI "C" linkage semantics" and I see that it seems to use windows types like DWORD.

    So do I need to setup a new environment whereby I compile OpenCV and Qt with Visual Studio (instead of Mingw as before) or can I use the new DLL within my existing environment? If so, how?

    Thanks



  • You need to use the Qt with Visual Studio setup to use the dll created using visual studio.



  • "QLibrary":http://doc-snapshot.qt-project.org/qt5-5.3/qlibrary.html gives you all of the functionality to load dynamic libraries and resolve functions. It also has some great examples in the docs.

    You could compile the dll in visual studio and dynamically load the library using Qt in your Qt environment.



  • hi dvez43 & Dheerendra

    thank you for your responses. They seem to contradict each other though?

    I've also just spent an hour letting nmake do its thing to compile the Qt src with Visual Studio 2012 only for it to fail with a fatal error at the very end ("python" is not recognized.)

    should I be able to have success with pre-compiled binaries of Qt and OpenCV for Visual Studio 2012 or is it really best to compile everything locally?


  • Moderators

    I see no point in compiling yourself a configuration that binaries are provided for unless you want to tweak any settings.

    When linking statically everything from compiler version, compiler and linker settings to runtime libraries must match, otherwise it's playing with fire.

    When dynamically loading a dll (eg. via QLibrary) mixing different compiler versions is ok as long as you stick to the C interface. It can still be dangerous if these modules link statically to different C runtimes though.

    That being said and considering the information you gave, my choice (not the only one but the easiest) would be to do the following:
    Install VS2012(VC11)
    Install Qt library compiled with VS2012(VC11) (just use online installer, it's the easiest and fastest way).
    If OpenCV provides binary for VS2012(VC11) install that, otherwise compile it yourself.
    Link to the 3rd party library however you like.



  • I have struggled enough do this mix and match to experiment. It is simply not worth it. My suggestion is to go with matching all your compilers in all directions.



  • Chris Kawa - I have done as you suggested. Kept it simple and used the pre-built (32bit, MSVC2012) for Qt and OpenCV and my previous project compiles and works fine still. So onward to trying to load my third party DLL.

    I've tried using QLibrary

    @
    QLibrary myDLL("devicecontrol_api");

    typedef void (*MyPrototype)();
    MyPrototype qInit = (MyPrototype) myDLL.resolve("initialize");
    if (qInit)
       qInit();
    

    @

    but resolve is returning NULL. I do have a header file for the library, and I think in fact dllexport is not included (initialize is an existing function in the DLL)

    With the DLL, the .LIB and .H, is there an alternate approach besides QLibrary?



  • edit:
    so instead of QLibrary, I added the LIB to LIBS in my PRO file and included the header file in HEADERS.
    Within my MainWindow test application, intellisense seems to recognize the contained functions but if I try to compile I get unresolved external symbol errors.



  • success!

    so QLibrary wasn't working, but in the end, having properly pointed to the headers, linked the LIB and then ensured the DLL was in the runtime folder, I am not able to successfully use my external DLL.

    (seems like it was a very convoluted trial and error to arrive at what now seems pretty straight-forward, but I guess so it goes.)


Log in to reply
 

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