Original: http://blog.csdn.net/wanghaihao_1/article/details/51098451
1. The static link library differs from the Dynamic link library:
Static link library: The instructions in Lib are included directly in the resulting EXE file.
Dynamic link library: DLL does not have to be included in the final exe, EXE file can be dynamically referenced and unloaded DLL files when executing.
At the same time, the static link library can no longer contain other dynamic-link libraries or static libraries, while the dynamic-link library may contain other dynamic or static libraries.
2.vc++ supported DLLs:
DLL is compiled with specific programming language and compiler independent, dynamic link library everywhere, VC + + support three kinds of DLLs: Non-MFC dynamic Library, MFC rules DLL and MFC extension DLL. DLL export functions (or variables, classes) are available for application invocation; DLL intrinsics can only be used within DLL programs and applications cannot invoke them.
3. How to declare the exported function:
One is to add "_declspec (dllexport)" Between the function declaration type and the functional name.
Another way to use the module definition (. def) file declaration is to add a module file to the Library project in the following format:
Library project Name
Exports the name of the exported function
4.DLL Call Mode:
A static call that is completed by the compilation system to load the DLL and unload the DLL at the end of the application.
Another dynamic invocation is the way that programmers load and unload DLLs (DLL load-dll function addresses get-dll released) using API functions.
5. All library projects must be in release mode at compile time:
Build-set Active configuration-Select the release mode of the Library project
6. Example:
I. Function----Create a dynamic-link library (MFC rules DLLs)
1. New--projects--mfc AppWizard (DLL)--regular DLL using shared MFC DLL//named Mfc_dll
2. def file added: function name (add_new)
3. h file added: external function declaration//Summation function, function named Add_new
extern "C" __declspec (dllexport) int __stdcall add_new (int a,int b);
4. cpp file added: external function implementation
extern "C" __declspec (dllexport) int __stdcall add_new (int a,int b)
{
return a+b;
}
5. Build--set Active Configuration--win32 Release--ok
6. Build
7. The Dll,lib and root h files in the release folder under the root directory are required
Second, the function----call the dynamic link library (Mfc_dll.dll and mfc_dll.lib to the directory where the project is located)
Static call (. h can be written to a. cpp file)
1. New--projects--win32 Console Application--an Empty Project
2. Add h file: (test.h)
#pragma comment (lib, "Mfc_dll.lib")//tell the compiler DLL the path and file name of the corresponding LIB file
extern "C" _declspec (dllimport) int _stdcall add_new (int a,int b);//Declaration Import function
3. Add CPP File: (main.cpp)
#include "test.h"
int main ()
{
Cout<<add_new (10,3);
return 0;
}
Dynamic invocation
#include <stdio.h>
#include <windows.h>
typedef INT (* lpaddfun) (int, int);//define a function pointer type that is identical to the Add_new function accept parameter type and return value
int main ()
{
HINSTANCE hdll;//Handle
Lpaddfun addfun;//function pointer
Hdll=loadlibrary ("DllTest.dll");//dynamic load DLL module handle
if (hdll)
{
Addfun= (Lpaddfun) GetProcAddress (hDLL, "add_new");//Get the address of the function in the loaded DLL module
if (Addfun)
{
int Result=addfun (2,3);
printf ("%d", result); } freelibrary (hDLL);//release DLL modules that have already been loaded
}
return 0;
}
III. variables----Creating dynamic-link libraries (Non-MFC DLLs)
1. New---Projects---Win32 dynamic-link library----An empty project (Sample)
2. Add Sample.h
#ifndef Sample_h
#define Sample_h
extern int Dllglobalvar;
#endif
3. Add Sample.cpp
#include "sample.h"
#include <windows.h>
int Dllglobalvar;
BOOL Apientry DllMain (HANDLE hmodule,dword ul_reason_for_call,lpvoid lpreserved)
When Windows loads a DLL, it needs an entry function, just as a console or DOS program requires the main function, and the Win32 program needs the WinMain function. So introduce a function version of the default DllMain that does nothing. is an intrinsic function of a DLL
VC + + 6.0 How to create and invoke a dynamic link library