Results 1 to 14 of 14

Thread: Any .dll Tutorials?

  1. #1
    Join Date
    Jun 2011
    Posts
    203
    Thanks
    7
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Any .dll Tutorials?

    Hi,

    I'm looking for tutorials on how to make .dlls and can't find any. Any suggestions?

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Any .dll Tutorials?

    Try this. Maybe it will help you get started.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. #3
    Join Date
    Jun 2011
    Posts
    203
    Thanks
    7
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Any .dll Tutorials?

    haha, I tried googling, found some sites and youtube videos, but they all lack a more comprehensive overview. I'm piecing it together, but the process currently is more frustrating than enlightening.

  4. #4
    Join Date
    Jun 2011
    Posts
    203
    Thanks
    7
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Any .dll Tutorials?

    EDIT: Feel free to diregard delete post.
    Last edited by Atomic_Sheep; 24th September 2017 at 05:34.

  5. #5
    Join Date
    Jun 2011
    Posts
    203
    Thanks
    7
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Any .dll Tutorials?

    Back at it a week later. So far I have learned that to create functions that are exported in a library you need to write something like:

    Qt Code:
    1. extern "C" __declspec(dllexport)
    To copy to clipboard, switch view to plain text mode 

    In front of every function that you want exported.

    and then you need something like:

    Qt Code:
    1. extern "C" __declspec(dllimport)
    To copy to clipboard, switch view to plain text mode 

    Whenever you want to use the function from a DLL once you have properly linked it.

    I tried this method with a simple qDebug line spit out, but didn't get a result. Everything compiled fine, but I think I must have forgotten something e.g. something called an entry point or some way of loading the DLL into my application. Not sure what yet but I think this is irrelevant because:

    If I'm correct, this will only work for Windows. To be platform independent, you need to for example use this example:

    https://wiki.qt.io/How_to_create_a_l...an_application

    Which uses QLibrary.

    My code at the moment is:

    mydll.h
    Qt Code:
    1. #ifndef MYDLL_H
    2. #define MYDLL_H
    3.  
    4. #include <QDebug>
    5.  
    6. #include "mydll_global.h"
    7.  
    8. #if defined MYDLL
    9. #define MYDLL_COMMON_DLLSPEC Q_DECL_EXPORT
    10. #else
    11. #define MYDLL_COMMON_DLLSPEC Q_DECL_IMPORT
    12. #endif
    13.  
    14. MYDLL_COMMON_DLLSPEC void HelloWorld();
    15.  
    16. #endif // MYDLL_H
    To copy to clipboard, switch view to plain text mode 

    mydll_global.h
    Qt Code:
    1. #ifndef MYDLL_GLOBAL_H
    2. #define MYDLL_GLOBAL_H
    3.  
    4. #include <QtCore/qglobal.h>
    5.  
    6. #if defined MYDLL
    7. #define MYDLL_COMMON_DLLSPEC Q_DECL_EXPORT
    8. #else
    9. #define MYDLL_COMMON_DLLSPEC Q_DECL_IMPORT
    10. #endif
    11.  
    12. #endif // MYDLL_GLOBAL_H
    To copy to clipboard, switch view to plain text mode 

    mydll.cpp
    Qt Code:
    1. #include "mydll.h"
    2.  
    3. void HelloWorld()
    4. {
    5. qDebug() << "HelloWorld";
    6. }
    To copy to clipboard, switch view to plain text mode 

    and then in my actual application:

    main.cpp
    Qt Code:
    1. #include <QLibrary>
    2. int main(int argc, char *argv[])
    3. {
    4. QLibrary MyDLL("MyDLL.dll");
    5. if(!MyDLL.load())
    6. {
    7. qDebug() << MyDLL.errorString();
    8. }
    9. else if(MyDLL.load())
    10. {
    11. qDebug() << "Library 'MyDLL.dll' loaded successfully";
    12. MyDLL.resolve("HelloWorld");
    13. }
    14. }
    To copy to clipboard, switch view to plain text mode 

    What am I doing wrong? Why am not seeing the "HelloWorld" qDebug entry?

  6. #6
    Join Date
    Jun 2011
    Posts
    203
    Thanks
    7
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Any .dll Tutorials?

    Ah, I think I'm starting to get it, using ->resolve incorrectly! Reading up on function pointers .
    Last edited by Atomic_Sheep; 1st October 2017 at 06:15.

  7. #7
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Any .dll Tutorials?

    Ah, I think I'm starting to get it, using ->resolve incorrectly! Reading up on function pointers
    While reading on function pointer is certainly good, I'd like to point out that this is not the usual way shared libs are used, even-though, its one way to use them - however, it is very cumbersome, whats more, if your libs get extended, you will have to edit your code that extract the functions from your DLL - so not that recommended for regular cases.
    Usually you specify in your build configuration (I suppose you use qmake in this case, so it will be the LIBS qmake variable) which libs (DLLs) you are linking to, and the linker does the job for you.
    In your code all you have to do is to include the correct header files, and simply use the classes and functions from your DLL as if they where part of your application's code.

    So your main.cpp would look like this:
    Qt Code:
    1. #include <mydll.h> //provided mydll.h is in your header search path, othewize you need to specify it with "path/to/mydll.h"
    2. int main(int argc, char *argv[])
    3. {
    4. HelloWorld();
    5. }
    To copy to clipboard, switch view to plain text mode 

    and in your *.pro file you would add:
    Qt Code:
    1. INCLUDEPATH += -I/path/to/mydll/headers
    2. LIBS += -L/path/to/mydll -lmydll //note -L for seachpatth and -l for the lib itself
    To copy to clipboard, switch view to plain text mode 

    qmake docs might be of help:
    http://doc.qt.io/qt-5/qmake-variable-reference.html
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  8. #8
    Join Date
    Jun 2011
    Posts
    203
    Thanks
    7
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Any .dll Tutorials?

    Quote Originally Posted by high_flyer View Post
    While reading on function pointer is certainly good, I'd like to point out that this is not the usual way shared libs are used, even-though, its one way to use them - however, it is very cumbersome, whats more, if your libs get extended, you will have to edit your code that extract the functions from your DLL - so not that recommended for regular cases.
    Ah didn't think of this. But looks like the code that is provided in:

    https://wiki.qt.io/How_to_create_a_l...an_application

    won't need much changes if the .dll is modified? You're loading the same .dll and if you're calling a function by ->resolve, you still need the function to be there in the .dll. I'm obviously missing something.

    Quote Originally Posted by high_flyer View Post
    Usually you specify in your build configuration (I suppose you use qmake in this case, so it will be the LIBS qmake variable) which libs (DLLs) you are linking to, and the linker does the job for you.
    I tried doing it this way before - that's when I posted that message regarding disregard because I stumbled onto an alternative solution i.e. using QLibrary, but got nowhere. I must have missed something somewhere, but my code resulted in no qDebug hello world statement. That was another reason why now I'm trying to use the QLibrary code. That and the fact that it's cross platform. I'm sure it's possible to write code this way that's cross platform as well, but I figure it will require slightly more digging to get everything to work. I was kind of hoping with QLibrary, I wouldn't have to do any additional digging and just have code that does what I want it to.

    EDIT: Tried doing it the conventional way again, but not getting anything spat out in qDebug so nothing has changed from my previous attempts.
    Last edited by Atomic_Sheep; 5th October 2017 at 10:20.

  9. #9
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Any .dll Tutorials?

    won't need much changes if the .dll is modified? You're loading the same .dll and if you're calling a function by ->resolve, you still need the function to be there in the .dll. I'm obviously missing something.
    You have to call resolve and assign a function pointer per function you want to use.
    As I said, this is possible, but tedious - but if it suits you, by all mean do it.
    There is another problem with using resolve():
    The symbol must be exported as a C function from the library. This means that the function must be wrapped in an extern "C" if the library is compiled with a C++ compiler. On Windows you must also explicitly export the function from the DLL using the __declspec(dllexport) compiler directive
    Which again, adds quite a bit of overhead when you are writing your DLL (In your case you wrote simple function, what if you start exporting classes?) - and it will pose a problem if you want to use 3rd party lib that is not written by you and which is not exposing symbols with extern "C".

    I tried doing it this way before - that's when I posted that message regarding disregard because I stumbled onto an alternative solution i.e. using QLibrary, but got nowhere. I must have missed something somewhere, but my code resulted in no qDebug hello world statement.
    Post your code, maybe we can help.

    That and the fact that it's cross platform.
    hah?
    Yes, the QLibrary API is cross platform, but the usage is not because built libs in general are not crossplatform, since they are an executable (in C++, or any language not running on a their own runtime like java).
    For this to be cross platform your statement for loading the lib can't be:
    Qt Code:
    1. QLibrary MyDLL("MyDLL.dll");
    To copy to clipboard, switch view to plain text mode 
    since *.dll is a windows thing, under linux it would be *.so.

    In that respect using the pro file approach or the QLibraray approach has no effect on the "cross platformness" and more that using QLibrary has.

    Tried doing it the conventional way again, but not getting anything spat out in qDebug so nothing has changed from my previous attempts.
    Then ask.

    You are free to use QLibrary of course, if you don't see the merits of linking "normally" now, you will see them as time passes and you get more proficient with programming and when the complexity of your projects will grow.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  10. #10
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Any .dll Tutorials?

    but not getting anything spat out in qDebug so nothing has changed from my previous attempts.
    Have you looked at this Microsoft documentation?

    The QLibrary mechanism is really, really the hard way to do it unless you are trying to develop a plugin architecture where you do not know the libraries at link time.

    Consider how you use the Qt DLLs in your applications. You -don't- manually load those libraries before using them. Instead, you link to the corresponding export libraries (the .lib counterpart to the .dll) and that tells the linker the names of the symbols that are exported from the libraries and reserves locations for them when it resolves the symbols at run time. In your code, you do not need to do anything; the linker and run-time loader do all the heavy work for you with the result that when you want to create a QPushButton and call methods on it, you can simply do it.

    Behind the scenes, the Qt header files contain all of the declspec() magic (in a cross-platform portable way), and the binaries built from the Qt source are configured correctly so you can simply use them.

    As for your qDebug() problem, it is likely this:

    If you are compiling your HelloWorld file with the Visual C++ compiler and have not declared the function as extern "C" in both the header and source code files, then the compiler will generate a "mangled" name that combines the name of the method with its signature (argument and return value types). These are the unintelligible "XYZ_MyFunction" style names you see in the debugger. So, if you've generated a C++ object file without the extern "C" declaration, your function is exported with a mangled name, and looking for it as "HelloWorld" will fail because there is no exported function by that name.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  11. #11
    Join Date
    Jun 2011
    Posts
    203
    Thanks
    7
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Any .dll Tutorials?

    Quote Originally Posted by high_flyer View Post
    Yes, the QLibrary API is cross platform, but the usage is not because built libs in general are not crossplatform, since they are an executable (in C++, or any language not running on a their own runtime like java).
    For this to be cross platform your statement for loading the lib can't be:
    Qt Code:
    1. QLibrary MyDLL("MyDLL.dll");
    To copy to clipboard, switch view to plain text mode 
    since *.dll is a windows thing, under linux it would be *.so.
    Ah I that makes sense.

    Quote Originally Posted by d_stranz View Post
    Have you looked at this Microsoft documentation?
    Thanks, reading through it now.

    Hearing what you guys are saying, QLibrary isn't the best way forward it seems. So reverting back to trying to get a more conventional way of going about things.


    Added after 4 minutes:


    EDIT:

    Quote Originally Posted by d_stranz View Post
    Consider how you use the Qt DLLs in your applications. You -don't- manually load those libraries before using them. Instead, you link to the corresponding export libraries (the .lib counterpart to the .dll) and that tells the linker the names of the symbols that are exported from the libraries and reserves locations for them when it resolves the symbols at run time. In your code, you do not need to do anything; the linker and run-time loader do all the heavy work for you with the result that when you want to create a QPushButton and call methods on it, you can simply do it.
    Yep figured this trick out already, helping me with understanding how things work a great deal.

    But it does make me wonder, why I need to include:

    Quote Originally Posted by high_flyer View Post
    Qt Code:
    1. LIBS += -L/path/to/mydll -lmydll //note -L for seachpatth and -l for the lib itself
    To copy to clipboard, switch view to plain text mode 
    I'm including the .dll in the location of the .exe, if I don't include the LIBS .pro file, nothing is affected. Seems like just including the header is enough, just like the use of any of Qt functionality.

    But for now, I have one main question. Everywhere, including the Microsoft documentation, .dll are composed of classes. Then you call a function from a class by doing:

    Qt Code:
    1. MathLibrary::Functions::AddMultiply(a, b)
    To copy to clipboard, switch view to plain text mode 

    Where MathLibrary is the namesapce, Functions is the class and then AddMultiply is the actual function called.

    I know this is related to the hidden 'this' pointer, but it seems like .dlls can't be made without classes? I've not seen one example of a .dll that exports just functions without them belonging to a class.

    I've been trying to make my .dll by avoiding using classes and it seems that this is my greatest pitfall that's causing my problem. I just don't understand the technical reason behind why you can't simply export a naked function. Maybe I'm just getting hung up on the definition that is in my head about .dll that is that they are simply a collection of functions and to properly implement them, usage of classes is mandatory and hence the use of the hidden 'this' pointer which makes everything work.

    Will post more questions as they arise.
    Last edited by Atomic_Sheep; 6th October 2017 at 07:13.

  12. #12
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Any .dll Tutorials?

    I'm including the .dll in the location of the .exe, if I don't include the LIBS .pro file, nothing is affected. Seems like just including the header is enough, just like the use of any of Qt functionality.
    the location of the exeutable is by default part of the search path for libraries which is why in that case you don't need to specify the -L option.
    If you place/deploy your DLL anywhere else you will need it.
    Again, its a question of project complexity and best practices if you put everything in one folder or not.

    I've been trying to make my .dll by avoiding using classes and it seems that this is my greatest pitfall that's causing my problem. I just don't understand the technical reason behind why you can't simply export a naked function. Maybe I'm just getting hung up on the definition that is in my head about .dll that is that they are simply a collection of functions and to properly implement them, usage of classes is mandatory and hence the use of the hidden 'this' pointer which makes everything work.
    Of course you can write DLL that export simple functions.
    Most of the WINAPI is done that way.
    In your example from the beginning of this thread it seems you also managed to build a dll which only exported a simple function - so I am confused as to what you wrote here.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  13. #13
    Join Date
    Jun 2011
    Posts
    203
    Thanks
    7
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Any .dll Tutorials?

    Quote Originally Posted by high_flyer View Post
    Of course you can write DLL that export simple functions.
    Most of the WINAPI is done that way.
    In your example from the beginning of this thread it seems you also managed to build a dll which only exported a simple function - so I am confused as to what you wrote here.
    It never worked. It compiled but never spat out Hello World. So basically after reviewing a bunch of sources, I noticed that every .dll ever created in these examples had functions as part of a class. The hidden this pointer was from here:

    http://www.learncpp.com/cpp-tutorial...-this-pointer/

    But I hadn't read it in a while and now that I refreshed my memory, it doesn't seem to be of relevance in this case so my mistake.

    But looks like I've managed to add the library without errors using the -L -l method. For some reason if I do something like:

    Qt Code:
    1. LIBS += "C:/Users/MyUser/Docs/MyDLL.dll"
    To copy to clipboard, switch view to plain text mode 

    The program exits with ".exe exited with code -1073741515" which basically says that the library wasn't found at least according to what I found on this forum.

    But having added the library, I don't get a QDebug Hello World message as expected . Even though I can find the function in the hints box when I start typing the name of the function and it's at the start of main(){}.

    What's more, if I add the HelloWorld() function in my .dll to namespace mynamespace and then call it in main of my program as mynamespace::HelloWorld(), I get an error:

    error: undefined reference to `_imp___ZN14...' and that doesn't have many hits in google.
    Last edited by Atomic_Sheep; 11th October 2017 at 14:12.

  14. #14
    Join Date
    Jun 2011
    Posts
    203
    Thanks
    7
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Any .dll Tutorials?

    Well, my .dll now works. The problem was that I was compiling my .dll in release mode and my project that was using the .dll was in debug mode.

Similar Threads

  1. Qt Tutorials
    By vivaladav in forum Qt Programming
    Replies: 3
    Last Post: 24th November 2016, 12:56
  2. QT tutorials
    By T3AB4GG3R117 in forum Newbie
    Replies: 6
    Last Post: 20th April 2016, 16:49
  3. New C++ tutorials at MEGA C++ tutorials
    By studentri23 in forum General Programming
    Replies: 2
    Last Post: 15th December 2012, 16:44
  4. Tutorials
    By SharonAngel in forum Newbie
    Replies: 1
    Last Post: 30th November 2010, 13:23
  5. I can't see the code in the Qt Tutorials
    By choucete in forum Newbie
    Replies: 1
    Last Post: 3rd February 2009, 01:31

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.