From: http://hi.baidu.com/kalingna/item/e842475b9fa05b05e6c4a591
Connection and difference between static Link Library, dynamic link library, and MFC
Comparison between static library (LIB) and dynamic library (DLL):
First, both static and dynamic libraries share code, which can also be called modular programs;
Static Link Library:
Commands in lib are directly included in the final EXE, so the Lib file is no longer needed during the EXE operation;
Other dynamic or static libraries cannot be included;
Dynamic Link Library:
The DLL file exists independently from the EXE file, so the DLL file must be released at the same time when the EXE file is released;
Other dynamic or static libraries can be included;
Suggestion:
It is best to use the dynamic link library (DLL) to modularize the program. The static link library (LIB) is outdated (the disadvantage can be seen from the application upgrade );
Common DLL viewing tools:
The dumpbin command or the depends tool that comes with VC ++ allows you to view a [DLL file export information] or a [EXE called DLL information].
DLL considerations:
For DLL generation:
Exercise caution when using exetern "C"; // This method is highly readable, but does not support heavy loading. [advantages and disadvantages coexist]
Do not use the module definition (. Def) file; // This method is not flexible enough. [DEF is outdated currently]
Do not forget to use _ declspec (dllexport) and _ declspec (dllimport );
For compilation and running:
Copy the DLL file,. Lib file, and. h file to the EXE main directory (as long as the EXE can see the DLL-related
(Usually, organize the source code hierarchy and set the project-> setting]
For release:
The DLL file must be released together with the EXE file.
Comparison between display (dynamic) and implicit (static) Call methods:
The display (dynamic) Call method is characterized by the programmer using API functions to load and uninstall the DLL. The programmer can decide when to load or not load the DLL file,Flexible call Methods.
The implicit (static) method is characterized by the completion of DLL loading by the compilation system and the uninstallation of DLL at the end of the application. Static call MethodEasy to useBut it is not as flexible as dynamic calling.
Suggestion:
Explicit and implicit are for loadlibrary (". dll;
Please use the implicit (static) method, because the DLL export interface does not use the extern "C" modifier, the display
(Dynamic) The call is very readable. Whether or not extern "C" is used, the interface Name of the implicit call is the same as that of the DLL interface.
The connection and difference between static Link Library, dynamic link library, and MFC:
Using a static or dynamic link library is one thing, and using MFC is another thing! In other words, both lib and DLL can use MFC.
Comparison of Three Types of Dynamic Link Libraries:
Non-MFC:
The exported DLL interface is similar to the Standard C function instead of the MFC class library;
It can be called by MFC or non-MFC programs;
Summary: (the general rule is: This type of DLL is created when the program does not involve MFC)
Regular-MFC:
Similar to the following extension-MFC, which is written using the MFC class library, the most obvious feature is that there is
Cwinapp derivation;
You can use the MFC class library in the exported DLL interface, but the interface itself cannot be an MFC class or its derived class;
It can also be divided into the rule DLL (using MFC in static library) and dynamic link (SHARE)
MFC rule DLL (using MFC in shared DLL );
Can be called in any language that supports DLL (provided that the _ stdcall Convention is used); [I don't want to care about the call Convention]
Summary: (the advantage is that you can use the MFC class library in the DLL Interface) [You can use but cannot export the MFC]
Extension-MFC:
Written using the MFC class library;
Not only can the MFC class library be used in the DLL interface, but the exported interface itself can also be an MFC class or its derived class. It is often used to enhance the existing functions of the MFC;
It can only be called by the MFC program;
Summary: (the advantage is that it can be used to enhance the existing functions of the MFC class library)
Comparison of Two Types of Rule DLL (regular-mfc dll:
The following is a static DLL (using MFC in static library) link to the MFC ):
By default, DLL resources are used in the DLL interface. Therefore, you do not need to switch the resource handle of the module;
The rule DLL generated using this method is large, because the code may be repeatedly included;
CONCLUSION: (the advantage is that resource handle switching is not required for the module, but the DLL is usually large)
Dynamic Link (SHARE) to the MFC rule DLL (using MFC in shared DLL ):
By default, the DLL interface uses the resources of exe. Therefore, in the DLL interface, in order to use its own resources or solve the resource duplication problem (when the DLL and the main application have resources with the same ID ), you must switch the resource handle of the module.
Summary: (the advantage is that the DLL is usually small, but the resource handle switching of the module is required)
When Dynamic Links (Sharing) to the MFC rule DLL (using MFC in shared DLL)There are two methods to switch the module resource handle:
1. Switch the resource module in the DLL Interface Function:
// Switch between DLL interface functions
Extern cshareddllapp theapp;
Void showdlg (void)
{
// Status change
Hinstance save_hinstance = afxgetresourcehandle ();
Afxsetresourcehandle (theapp. m_hinstance );
Cdialog DLG (idd_dll_dialog); // DLL Resource
DLG. domodal ();
// Restore the status
Afxsetresourcehandle (save_hinstance );
// Further operations here will target application resources.
Cdialog dlg1 (idd_dll_dialog); // Application resource
Dlg1.domodal ();
}
Complete the switchover as needed by the application:
// Switch in the Application
Void cshareddllcalldlg: oncalldllbutton ()
{
// Obtain the EXE module handle
Hinstance exe_hinstance = getmodulehandle (null );
// Obtain the DLL module handle
Hinstance dll_hinstance = getmodulehandle ("shareddll. dll ");
Afxsetresourcehandle (dll_hinstance); // Switch Status
Showdlg (); // The DLL dialog box is displayed.
Afxsetresourcehandle (exe_hinstance); // restore the status
Showdlg (); // The EXE dialog box is displayed.
}
About dllmain:
The no-mfc dll and the extension-mfc dll are automatically provided by the MFC;
For regular-MFC, a default version is automatically called in the programming environment when dllmain is not defined;
CONCLUSION: (it is not useless. You can initialize or recycle resources in dllmain)
Description of LIB library files:
-> When to use the Lib Library File
If the program uses an implicit (static) Call to the DLL, the Lib library file is required during compilation.
The dynamic or static Link Library does not need. Lib when the EXE is finally released, and. Lib may only be used during compilation.
-> About exporting. Lib files to a specific directory:
"Project"-> "Settings"-> General-> set in output files;
You can also add commands on the pre-link step and post-build step attribute pages to copy files to the corresponding directories, for example:
Copy debug \ My. Lib (specific directory) \ My. Lib
Copy debug \ My. dll (specific directory) \ My. dll
Reference: http://topic.csdn.net/t/20041222/15/3667256.html
I used the post-build step. When editing the Copy command, note that it does not support/but can only use \;
In addition, make sure that the specified copy directory exists. Otherwise, compilation will fail and it will not be automatically created for you.
-> How to specify the location of the Lib library file to linker
If implicit call is used, the Lib location must be specified during the link. There are two methods:
1) Use # pragma comment (Lib, "XX. lib ");
2) object/library modules on the project-> setting-> link tab: Enter the path of the Lib file, or set the INC and Lib paths in tools-> options-> directories;
// For example, if the DLL file is dllclass. dll, enter release/dllclass. Lib (this is only an example. Note the directory)
The former is better than the latter because when the source file is migrated to another user, the user does not need to set the project-> setting...
About pure resource dll:
Compile "Pure resource DLL": http://hi.baidu.com/pop1210/blog/item/144192b5c6d867da36d3caa5.html
Write "Pure resource DLL" II: http://hi.baidu.com/pop1210/blog/item/39a76acc5b1cf10400e92830.html
Call convention, Def file:
The impact of some call conventions on the DLL is not needed if def is used to export (obsolete) or something. Otherwise, it is complicated.