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
- #ifndef Testdll_global_h
- #define Testdll_global_h
- #include <QtCore/qglobal.h>
- #ifdef Testdll_lib
- # define Testdll_export Q_decl_export
- #else
- # define Testdll_export Q_decl_import
- #endif
- #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
- #ifndef Testdll_h
- #define Testdll_h
- #include "testdll_global.h"
- Class Testdll_export Testdll
- {
- Public
- Testdll ();
- ~testdll ();
- Private
- };
- extern "C" Testdll_export void HelloWorld ();
- extern "C" Testdll_export int Add (int a,int b);
- #endif//Testdll_h
TestDLL.cpp source code in the TestDll.dll project
CPP Code
- #include <iostream>
- #include "TestDll.h"
-
- Testdll::testdll ()
- {
- & nbsp;
- }
-
- Testdll::~testdll ()
- {
- & nbsp;
- }
-
- Void helloworld ()
- {
- std::cout << "hello,world!";
- }
- int add (int a,int b)
- {
- return a + b;
- }
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
- #include <QtCore/QCoreApplication>
- #include <iostream>
- #include <QLibrary>
- typedef int (*fun) (int,int); Defines the function pointer, int add (int a,int b);
- int main (int argc, char *argv[])
- {
- Qcoreapplication A (argc, argv);
- Qlibrary mylib ("TestDll.dll"); DLL file used by the Declaration
- int result;
- Determine if the load is correct
- if (Mylib.load ())
- {
- Std::cout << "DLL load is ok!" <<std::endl;
- Call external function Add ()
- Fun add = (fun) mylib.resolve ("Add");
- Whether the Add () function was successfully connected
- if (add)
- {
- Std::cout << "Link to add Function is ok!" <<std::endl;
- Here the function pointer calls the Add () function in the DLL
- result = Add (5,6);
- Std::cout << result;
- }
- Else
- Std::cout << "Link to add Function failed!!" <<std::endl;
- }
- Load failed
- Else
- Std::cout << "DLL is not loaded!" <<std::endl;
- return A.exec ();
- }
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
- #ifndef Testdll_h
- #define Testdll_h
- #include "testdll_global.h"
- Class Testdll_export Testdll
- {
- Public
- Testdll ();
- Virtual~testdll ();
- virtual void HelloWorld (); Class member functions
- Private
- };
- extern "C" Testdll_export testdll* Gettestdll (); Gets the object of the class Testdll
- #endif//Testdll_h
TestDll.cpp Source
CPP Code
- #include <iostream>
- #include "TestDll.h"
- Testdll::testdll ()
- {
- }
- Testdll::~testdll ()
- {
- }
- void Testdll::helloworld ()
- {
- Std::cout << "hello,world!";
- }
- testdll* Gettestdll ()
- {
- return new Testdll ();
- }
Main.cpp source code in the Testmain project
CPP Code
- #include <QtCore/QCoreApplication>
- #include <iostream>
- #include <QLibrary>
- #include ". /testdll/testdll.h "//header file still needs to be added, otherwise the Testdll class cannot be resolved
- typedef testdll* (*gettestdll);//define function pointers, get class Testdll objects;
- int main (int argc, char *argv[])
- {
- Qcoreapplication A (argc, argv);
- Qlibrary mylib ("TestDll.dll"); DLL file used by the Declaration
- int result;
- Determine if the load is correct
- if (Mylib.load ())
- {
- Gettestdll Gettestdll = (gettestdll) mylib.resolve ("Gettestdll");
- if (Gettestdll)
- {
- Testdll *testdll = Gettestdll ();
- Testdll->helloworld ();
- Delete Testdll;
- }
- }
- Load failed
- Else
- Std::cout << "DLL is not loaded!" <<std::endl;
- return A.exec ();
- }
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
- #ifndef Testdll_h
- #define Testdll_h
- #include "testdll_global.h"
- Class Testdll_export Testdll
- {
- Public
- Testdll ();
- ~testdll ();
- void HelloWorld (); Class member functions
- Private
- };
- extern "C" Testdll_export int Add (int a,int b); Custom external functions
- #endif//Testdll_h
TestDll.cpp Source
CPP Code
- #include <iostream>
- #include "TestDll.h"
- Testdll::testdll ()
- {
- }
- Testdll::~testdll ()
- {
- }
- void Testdll::helloworld ()
- {
- Std::cout << "hello,world!";
- }
- int add (int a,int b)
- {
- return a + B;
- }
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
- #include <qtcore/qcoreapplication>
- #include <iostream>
- #include <qlibrary>
- #include "... /testdll/testdll.h "&NBSP;&NBSP;
- //Introduce TestDll.lib file, and the above 2, 3 steps work similarly
- //#pragma Comment (lib, ". /debug/testdll.lib ")
- Int main (int argc, char *argv[])
- {
- qcoreapplication a (ARGC,&NBSP;ARGV);
- int result = add (5,6);
- std::cout << result;
- testdll dll;
- dll.helloworld ();
- return a.exec ();
- }
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)