1. Why use DLL?
We know that code reuse is an important way to improve development efficiency. We often construct some common functions into relatively independent modules and reuse them in subsequent projects, there are two ways to reuse code:
- White Box multiplexing: For example, ATL and MFC are all released in the form of source code, and the source code is completely exposed to programmers.
There are many disadvantages of white box reuse. If the source code is exposed, it is easy to conflict with our own code, and the update function is troublesome.
- Black box reuse: Such as DLL, static link, and COM component. Compared with white box multiplexing, the advantages of DLL black box multiplexing are obvious. dll is a binary file,
Therefore, the source code is hidden. If explicit calling is used, naming conflicts are generally avoided. The. dll file is relatively independent, so it is feasible to update the function module.
2. How to Create a DLL project? Use vs2012 to create a new Win32 application console program named math,
Select the DLL option.
Create the mymath. h and mymath. cpp files.
Mymath. H is as follows:
# Pragma once # ifdef math_exports # define math_api _ declspec (dllexport) # else # define math_api _ declspec (dllimport) # endifnamespace shun {class math_api cmath {/class public: int add (int A, int B) ;}; extern "C" math_api float PI; // variable extern "C" math_api int getmax (Int &, Int &); // function}
Mymath. cpp is as follows:
# Include "stdafx. H "# include" mymath. H "namespace shun {float Pi = 3.1415; // variable int getmax (Int & A, Int & B) // function {return A> B? A: B;} int cmath: add (int A, int B) // class method {return a + B ;}}
Then generate a solution. After compilation, you can find the math. dll and math. Lib files in the debug folder.
So far, we have successfully created a DLL project.
3. How to implicitly reference a DLL project?
Three elements of implicit calling:. H files,. DLL files, and. Lib files are indispensable.
The advantage of implicit call is that it is as convenient as calling a local function.
3.1 If the project and DLL project are in the same solution, create a blank project for the Win32 console named usemath, and then reference the DLL as follows:
- Project-> properties-> general properties-> framework and reference-> Add new reference-> check math project-> OK
- Project-> properties-> Configuration properties-> VC ++ directory-> include directory-> Add the directory where mymath. H is located
Then add a test. cpp file and enter the following:
# Include <iostream> # include "mymath. H "using namespace STD; int main (char argc, char ** argv) {int A = 1, B = 2; // use Shun: cmath cm for classes; cout <cm. add (1, 2) <Endl; // variable cout <Shun: pI <Endl; // function cout <Shun: getmax (A, B ); getchar (); Return 0 ;}
Run the command and we find that the DLL project has been called ..
3.2 What if the project and DLL project are not in the same solution? To solve this problem, open a new vs2012, create an empty project in the Win32 console, name it usemath, and then reference the DLL as follows:
- Project-> properties-> Configuration properties-> VC ++ directory-> Add the directory where the header file mymath. H is located in "include directory"
- Project-> properties-> Configuration properties-> VC ++ directory-> Add the directory where the header file math. Lib is located in "library directory"
- Project-> properties-> Configuration properties-> linker-> input-> Add "math. lib" to "additional dependencies" (Separate multiple lib with spaces)
- Copy the math. dll file in the debug file of the DLL project to the debug folder of the current project.
Create and add a test. cpp file. Enter the following: (same as before)
# Include <iostream> # include "mymath. H "using namespace STD; int main (char argc, char ** argv) {int A = 1, B = 2; // use Shun: cmath cm for classes; cout <cm. add (1, 2) <Endl; // variable cout <Shun: pI <Endl; // function cout <Shun: getmax (A, B ); getchar (); Return 0 ;}
4. How to display the DLL project call? Show that only one DLL file is required for calling. The advantage of display call is that the module is relatively independent and easy to update. It is a little complicated to use.
As before, create a Win32 console project and put math. dll in the debug folder,
Because you need to use Windows APIs, You need to introduce the windows. h header file.
Add test. cpp as follows:
# Include <windows. h ># include <iostream> using namespace STD; typedef int (* func) (Int &, Int &); int main (INT argc, char * argv []) {int A = 5, B = 10; hmodule hdll = loadlibrary ("math. DLL "); If (hdll! = NULL) {func getmax = (func) getprocaddress (hdll, "getmax"); // function if (getmax! = NULL) {cout <getmax (a, B) <Endl;} float * PPI = (float *) getprocaddress (hdll, "Pi "); // variable if (PPI! = NULL) {cout <* PPI <Endl;} freelibrary (hdll);} getchar ();}
If you are not familiar with function pointers, click here:Function pointer Summary
If you want to display the method of the call class, you can write a function in the DLL to call the function, such:
.
//. H file extern "C" math_api int add_interface (Int &, Int &); // function //. CPP file int add_interface (Int & A, Int & B) {cmath cm; return cm. add (a, B );}
Then, call add_interface like the above call function.
5. Function of extern "C"
We know that extern can be placed before a variable or function to indicate that the definition of a variable or function is in another file, prompting the compiler to find its definition in other modules when it encounters this variable and function.
C ++ Language
To solve the function polymorphism problem, the function name and parameter are combined to generate an intermediate function name.
For example:
void foo( int x, int y );
After the function is compiled by the C compiler, the name in the symbol library is _ Foo, while the C ++ compiler generates names such as _ foo_int_int.(Different compilers may generate different names ).
Therefore, we can use extern "C" before the function to tell the compiler to use the C compilation method, rather than the C ++ compilation method.
Create and use DLL in vs2012