Basic MFC: some descriptions of static DLL, main (), main global variable, dllMain (), and dllMain global variable, mfcdllmain
In most cases, some function libraries are referenced more or less in today's programs. So understandDynamic library DLLOfLoadAndInitializationTo avoid some dependency problems.
The dynamic library can be loaded dynamically or statically. Now we only track the Static Loading Method to get some sequential results.
Main program entry: int main (char argc, char * argv []); DLL library entry: bool winapi DllMain (DWORD dwReason, LPVOID/* lpReserved */);Note, 1) if the DLL does not declare its own entry function: DllMain, the system will callDefaultEntry function: DllMain 2). If a new sub-thread is created in the program process, the DLL entry function: DllMain 3 will also be called. If the process/thread is forcibly terminated, the entry function will not be called: dllMain(Therefore, in some cases, a memory leak may occur when the thread is forcibly terminated.)DLLTracking code
class CGlobalT{public:CGlobalT(){printf("CGlobalT constructor called\n");}~CGlobalT(){printf("~CGlobalT destructor called\n");}protected:private:};static CGlobalT m_global;BOOL APIENTRY DllMain( HMODULE hModule,DWORD ul_reason_for_call,LPVOID lpReserved){switch (ul_reason_for_call){case DLL_PROCESS_ATTACH:printf("DLL process attached\n");break;case DLL_THREAD_ATTACH:printf("DLL thread attached");break;case DLL_THREAD_DETACH:printf("DLL thread detached");break;case DLL_PROCESS_DETACH:printf("DLL process detached");break;}return TRUE;}
DLL entry functions: DllMain () and static global variables are created:
M_globalYou can trace global object constructor calls and interface calls to determine the call sequence.
Tracking CODE OF THE MAIN PROCESS
#pragma comment(lib,"..\\..\\build\\dllTest.lib")#include "..\..\dllTest\dllTest\dllTest.h"#include "ThreadInterface.h"class CMainGlobal{public:CMainGlobal(){printf("CMainGlobal constructor called\n");}~CMainGlobal(){printf("~CMainGlobal destructor called\n");}protected:private:};static CMainGlobal m_mainGlobal;int main(int argc, char *argv[]){func();CExportClass exportClass;exportClass.DoAction();CThreadInterface threadC;threadC.StartThread();Sleep(500);threadC.ExistThread();return EXIT_SUCCESS;}
The main process entry: main () and static global variable m_mainGlobal are created to determine the call sequence by tracking the global object constructor call and mian call.
Call sequence obtained by tracking
1) Call the DLL global variable constructor
2) DllMain () entry function call: DLL_PROCESS_ATTACH
3) call the main global variable constructor.
4) main () function call 5) DllMain () entry function call: DLL_THREAD_ATTACH. (ThreadC is the thread object and the action after the thread is started)
6) DllMain () entry function call: DLL_THREAD_DETACH
7) Exit the main () function.
8) call the main () global variable destructor.
9) DllMain () entry function call: DLL_PROCESS_DETACH
10) Call the DLL global variable destructor.
Some conclusions drawn from the above tracking sequence: 1) the action of loading static DLL by the Program: global variable initialization, dll entry call,
AverageBefore loading the main global static variable 2) the DLL loading process and the main process call process initialize the global variable first and then call the entry function 3) the interface exit is the opposite of the DLL unloading process, the parameters brought in by the call are different.
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger. If you have any copyright questions, please leave a message.