(i) dynamic link library and static link library
Static link library: The function in Lib is not only connected, all implementations are directly included in the resulting EXE file, but the implementation is not visible.
Dynamic Link libraries: DLLs do not have to be included in the final EXE, only the function name or variable name or class name is linked to the EXE file at static invocation, and the entities of these objects are only imported into the executable from the dynamic library at runtime. The DLL file can be referenced and unloaded dynamically when the EXE file is executed when dynamically invoked.
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.
(ii) Review the 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.
(c) How the exported function is declared:
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
(d) How DLLs are invoked:
In a static call, the compiler completes the load on the DLL and unloads the DLL when the application ends.
In a dynamic call, the programmer uses API functions to load and unload the DLL (DLL loads the-dll function address to get the-dll release) way.
And then we'll write an example to practice the theory mentioned above.
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.
One thing to note is that if you see such a macro definition
#ifdef Kscanbar_exports
#define KSCANBAR_API __declspec (dllexport)
#else
#define KSCANBAR_API __declspec (dllimport)
#endif
is because
This definition typically occurs in a solution that contains multiple projects, which enables the standard method of exporting simpler macros from the DLL. All files in this DLL are compiled with the kscanbar_exports symbol defined on the command line. This symbol should not be defined on any other project that uses this DLL. In this way, any other items in the source file that contain this file will treat the KSCANBAR_API function as imported from the DLL, and this DLL will be treated with the symbol defined by this macro as being exported.
Transfer from http://blog.csdn.net/crich_moon/article/details/6039939
Creation and invocation of dynamic libraries