QT DLL Summary (ii)--Create and use QT DLL (reprint)

Source: Internet
Author: User
Tags define function

Let's talk about the method of calling the QT Dynamic link library, mainly including:

1, explicit link DLL, call DLL global function, using QT Qlibrary method

2. Show link DLL, call DLL class object, member function. (A call to a class member function can be implemented through an object)

① uses the method of virtual function table, which is also the method used by COM, using QT's qlibrary technology call;

② is called directly with GetProcAddress.

the generated DLL plug-in class object directly with Qt's Qpluginloader class

3, the implicit link DLL: Also uses the QT Qlibrary method

For these three methods, the following detailed narration

Detailed classification narrative

Prerequisites : Two project Files directory

1. Testdll Project: Testdll_global.h,testdll.h,testdll.cpp

2, Testmain EXE Application project: main.cpp

Testdll_global.h File source code is always the same

CPP Code
    1. #ifndef Testdll_global_h
    2. #define Testdll_global_h
    3. #include <QtCore/qglobal.h>
    4. #ifdef Testdll_lib
    5. # define Testdll_export Q_decl_export
    6. #else
    7. # define Testdll_export Q_decl_import
    8. #endif
    9. #endif//Testdll_global_h

Explicit chaining of DLLs is more flexible than implicit linking at some point. For example, if a DLL cannot be found at run time, the program can display an error message and continue to run. Explicit linking is also useful when you want to provide plug-in services for your program

1, using the display link , call the DLL in the global function , only need a TestDLL.dll.

Normally Windows programs that call DLLs are divided into three steps (three functions): LoadLibrary (), getprocadress (), FreeLibrary ()

where the LoadLibrary () function is used to load the specified DLL file into the calling program's memory (DLL does not have its own memory!). )

The GetProcAddress () function retrieves the address of the output library function in the specified dynamic-link library (DLL) for invocation

FreeLibrary () releases the space occupied by the DLL

The Qlibrary class for QT shows the steps for linking calls to a DLL: Load (), resolve (const char * symbol), unload (), and VC steps similar

TestDLL.h source code in the TestDll.dll project

CPP Code
    1. #ifndef Testdll_h
    2. #define Testdll_h
    3. #include "testdll_global.h"
    4. Class Testdll_export Testdll
    5. {
    6. Public
    7. Testdll ();
    8. ~testdll ();
    9. Private
    10. };
    11. extern "C" Testdll_export void HelloWorld ();
    12. extern "C" Testdll_export int Add (int a,int b);
    13. #endif//Testdll_h

TestDLL.cpp source code in the TestDll.dll project

CPP Code
    1. #include  <iostream>  
    2. #include   "TestDll.h"   
    3.   
    4. Testdll::testdll ()   
    5. {  
    6.  & nbsp;
    7. }  
    8.   
    9. Testdll::~testdll ()   
    10. {  
    11. & nbsp; 
    12. }  
    13.   
    14. Void helloworld ()   
    15. {   
    16.     std::cout <<  "hello,world!";   
    17. }  
    18. int add (int a,int b)   
    19. {   
    20.     return a + b;  
    21. }  

Note: 1) after a successful DLL project is established, you can use the command "Dumpbin-exports DllTest.dll" in the VS Command Prompt (You can also view it using the depends program in the VC toolkit)
Note: 2) You must use the extern "C" link tag, otherwise the C + + compiler will produce a decorated function name so that the name of the exported function will no longer be HelloWorld, but a name like "[Email protected]@ @UAEXXZ". Why is the name not HelloWorld? This is because C + + in order to support the overload of the function, the parameter type information of the function and the return value type information is added to the function name at compile time, so that the overloaded function with the same name in the code will be separated from each other after compiling, and the function name will be able to find the corresponding function after the same processing. See this article for more information dynamic Link Library study notes

Testmain Project Main.cpp

CPP Code
  1. #include <QtCore/QCoreApplication>
  2. #include <iostream>
  3. #include <QLibrary>
  4. typedef int (*fun) (int,int); Defines the function pointer, int add (int a,int b);
  5. int main (int argc, char *argv[])
  6. {
  7. Qcoreapplication A (argc, argv);
  8. Qlibrary mylib ("TestDll.dll"); DLL file used by the Declaration
  9. int result;
  10. Determine if the load is correct
  11. if (Mylib.load ())
  12. {
  13. Std::cout << "DLL load is ok!" <<std::endl;
  14. Call external function Add ()
  15. Fun add = (fun) mylib.resolve ("Add");
  16. Whether the Add () function was successfully connected
  17. if (add)
  18. {
  19. Std::cout << "Link to add Function is ok!" <<std::endl;
  20. Here the function pointer calls the Add () function in the DLL
  21. result = Add (5,6);
  22. Std::cout << result;
  23. }
  24. Else
  25. Std::cout << "Link to add Function failed!!" <<std::endl;
  26. }
  27. Load failed
  28. Else
  29. Std::cout << "DLL is not loaded!" <<std::endl;
  30. return A.exec ();
  31. }

2. Use display links to invoke class objects, member functions in C + + classes

What if you want to export and explicitly link member functions in a set of C + + classes? Here are two questions. The first is that C + + member function names are decorated (even if the extern "C" tag is specified), and the second is that C + + does not allow pointers to member functions to be converted to other types. These two issues restrict explicit linking of C + + classes. Here are two ways to solve this problem:

① uses the method of virtual function table, which is also the method used by COM, using QT's qlibrary technology call;

② is called directly with GetProcAddress.

the generated DLL plug-in class object directly with Qt's Qpluginloader class

① method of virtual function table, Qlibrary technology Call

TestDll.h Code

CPP Code
    1. #ifndef Testdll_h
    2. #define Testdll_h
    3. #include "testdll_global.h"
    4. Class Testdll_export Testdll
    5. {
    6. Public
    7. Testdll ();
    8. Virtual~testdll ();
    9. virtual void HelloWorld (); Class member functions
    10. Private
    11. };
    12. extern "C" Testdll_export testdll* Gettestdll (); Gets the object of the class Testdll
    13. #endif//Testdll_h

TestDll.cpp Source

CPP Code
    1. #include <iostream>
    2. #include "TestDll.h"
    3. Testdll::testdll ()
    4. {
    5. }
    6. Testdll::~testdll ()
    7. {
    8. }
    9. void Testdll::helloworld ()
    10. {
    11. Std::cout << "hello,world!";
    12. }
    13. testdll* Gettestdll ()
    14. {
    15. return new Testdll ();
    16. }

Main.cpp source code in the Testmain project

CPP Code
  1. #include <QtCore/QCoreApplication>
  2. #include <iostream>
  3. #include <QLibrary>
  4. #include ". /testdll/testdll.h "//header file still needs to be added, otherwise the Testdll class cannot be resolved
  5. typedef testdll* (*gettestdll);//define function pointers, get class Testdll objects;
  6. int main (int argc, char *argv[])
  7. {
  8. Qcoreapplication A (argc, argv);
  9. Qlibrary mylib ("TestDll.dll"); DLL file used by the Declaration
  10. int result;
  11. Determine if the load is correct
  12. if (Mylib.load ())
  13. {
  14. Gettestdll Gettestdll = (gettestdll) mylib.resolve ("Gettestdll");
  15. if (Gettestdll)
  16. {
  17. Testdll *testdll = Gettestdll ();
  18. Testdll->helloworld ();
  19. Delete Testdll;
  20. }
  21. }
  22. Load failed
  23. Else
  24. Std::cout << "DLL is not loaded!" <<std::endl;
  25. return A.exec ();
  26. }

This method is used by users to easily create plugins for your program. Its disadvantage is that the memory that creates the object must be allocated in the DLL

② to call member functions in class objects directly with GetProcAddress

This method, I did not test, for I do not have a big role, but also to use DEF Export DLL functions, interested in reference to this article. Explicit linking of classes in DLLs

the generated DLL plug-in class object directly with Qt's Qpluginloader class

This method, I write a separate summary, please see Qpluginloader simple example VS2008+QT use Qpluginloader Access DLL

3, using the implicit link method, through the Qlibrary class to the DLL class object, the global function call

TestDll.h

CPP Code
    1. #ifndef Testdll_h
    2. #define Testdll_h
    3. #include "testdll_global.h"
    4. Class Testdll_export Testdll
    5. {
    6. Public
    7. Testdll ();
    8. ~testdll ();
    9. void HelloWorld (); Class member functions
    10. Private
    11. };
    12. extern "C" Testdll_export int Add (int a,int b); Custom external functions
    13. #endif//Testdll_h

TestDll.cpp Source

CPP Code
    1. #include <iostream>
    2. #include "TestDll.h"
    3. Testdll::testdll ()
    4. {
    5. }
    6. Testdll::~testdll ()
    7. {
    8. }
    9. void Testdll::helloworld ()
    10. {
    11. Std::cout << "hello,world!";
    12. }
    13. int add (int a,int b)
    14. {
    15. return a + B;
    16. }

Testmain the main.cpp in the project, you need to configure the header file and Lib file slightly

1, the main program in the project introduced TestDll.h header file,

2. Configuration Item Properties: Add TestDLL.lib file directory, select TestDll.lib file directory in linker/general/additional Library diretories D:\VSWorkSpace\Test \debug

3, Configuration Item properties: Add TestDll.lib file, add TestDll.lib in Linker/input/additional Dependencies

Main.cpp Source

CPP Code
    1. #include  <qtcore/qcoreapplication>  
    2. #include  <iostream>  
    3. #include  <qlibrary>  
    4. #include   "... /testdll/testdll.h "&NBSP;&NBSP;
    5. //Introduce TestDll.lib file, and the above 2, 3 steps work similarly   
    6. //#pragma   Comment (lib,  ". /debug/testdll.lib ")   
    7. Int main (int argc, char *argv[])   
    8. {  
    9.     qcoreapplication a (ARGC,&NBSP;ARGV);   
    10.     int result = add (5,6);   
    11.     std::cout << result;  
    12.     testdll dll;  
    13.     dll.helloworld ();   
    14.         return a.exec ();   
    15. }  

Results can be compiled successfully

==========================dll Common Sense =============================
To implicitly link to a DLL, the executable file must obtain the following from the provider of the DLL:

The header file that contains the declaration of the exported function and/or C + + class (. H file)
The import library to link to (. LIB files). (The linker creates an import library when a DLL is generated.) )
The actual DLL (. dll file)

Implicit chaining requires a dynamic connection library to produce. LIB file (import library) and link it to the application's project. The import library contains only the code that loads the DLL and the code that implements the DLL function call. When an external function is found in the import library, the linker's code for this function is notified in the DLL. To resolve external references to DLLs, the linker simply adds information to the executable to tell the system where to look for DLL code when the process starts.

When the system starts a program that contains a dynamic link reference, it uses the information in the program's executable file to locate the required DLL. If the system cannot locate the DLL, it terminates the process and displays a dialog box to report the error. If a DLL is found, the system maps the DLL module to the address space of the process. As with the rest of the program code, the DLL code is mapped to the address space of the process when it is started and is loaded into memory only when needed.

Windows will follow the search order below to locate the DLL

The directory containing the EXE file
Current working directory of the process
Windows system directory (SYSTEM/SYSTEM32). The GetSystemDirectory function retrieves the path to this directory.
The Windows directory. GetWindowsDirectory function retrieves the path to this directory.
A list of directories listed in the PATH environment variable

Transferred from: http://qimo601.iteye.com/blog/1397936

QT DLL Summary (ii)--Create and use QT DLL (reprint)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.