Dllmain role of Dllmain role

Dllmain role

DllMain function is the default entry point DLL module. This function is invoked when Windows loads DLL module. The system first calls the constructor of a global object and then calls global function DLLMain . DLLMain DLL function not only in the chain will be called when the connection is loaded into the process, when the DLL module and separation processes (and other times) is also called.

DLL entry point function: DllMain, Note: The case is the difference between the ( export only resource DLL can not DllMain function).

Prototype:

 
BOOL APIENTRY DllMain( HMODULE hModule,  
                       DWORD  ul_reason_for_call,  
                       LPVOID lpReserved  
                     )  
{  
    return TRUE;  
}  


Parameters meaning:

①hModule parameters: DLL instance handle points to itself;

②ul_reason_for_call parameters: Indicates the reason DLL is called, you can have the following four values:


1. DLL_PROCESS_ATTACH:
when the DLL is first >> << calling process, resulting in DllMain function is called,

Meanwhile ul_reason_for_call value DLL_PROCESS_ATTACH,

If the same process again later call this DLL, the operating system will only increase the frequency of use of DLL,

DLL_PROCESS_ATTACH DllMain function calls will not use the DLL.


2.DLL_PROCESS_DETACH:
When the DLL is unmapped from the process's address space, system calls its DllMain, ul_reason_for_call value passed is DLL_PROCESS_DETACH.
★ If the end of the process because the call TerminateProcess, the system will not be used to call the DLL's DllMain DLL_PROCESS_DETACH function. This means that the DLL is no chance to perform any clean-up work before the end of the process.


3.DLL_THREAD_ATTACH:
When a process creates a thread, view the current system all the DLL file image is mapped to the process address space,

And with the value DLL_THREAD_ATTACH calling DllMain function DLL. 

Newly created thread responsible for the implementation of the DLL's DllMain function,

Only when all the DLL have finished processing the notification, the system only allows the thread starts executing its thread function.


4.DLL_THREAD_DETACH:
If a thread calls ExitThread to end the thread (the thread function returns, the system will automatically call ExitThread),

View all current system DLL file image is mapped to the process space,

And with DLL_THREAD_DETACH to call the DllMain function,

Inform all DLL to perform thread-level cleanup.
★ Note: If the end of the thread is a thread because the system calls the TerminateThread,

DLL_THREAD_DETACH value system will not be used to call all the DLL's DllMain function.

 

③lpReserved parameters: Hermit loading load and display, this parameter value is not the same, showing zero load time, if the user wishes to define a load in the form of dynamic link libraries can be accomplished by determining the value of this parameter.

 

Implicit dynamic library header files lib shows the need to load dll file dll file when the program is running, but also the content loaded into multiple programs use memory only a dll dll
  dynamic libraries to load only need to show dll file to run the program not only load the dll dll loaded only when using library functions loadlibrary
  delay loaded dLL is actually implicit dynamic libraries to load and display the combined load required header files lib dll file dll file but does not load the program is running, use the program just load the dll, this time the principle and the principle of display of the same load. Compiler options to configure link> delay load dll need to fill in a loaded DLL files
need to know the path of dynamic libraries can () to specify the path to the dynamic library program is run by setdlldirctrory

 

DllMain function is the default entry point DLL module. This function is invoked when Windows loads DLL module. The system first calls the constructor of a global object and then calls global function DLLMain . DLLMain DLL function not only in the chain will be called when the connection is loaded into the process, when the DLL module and separation processes (and other times) is also called.

DLL entry point function: DllMain, Note: The case is the difference between the ( export only resource DLL can not DllMain function).

Prototype:

 
BOOL APIENTRY DllMain( HMODULE hModule,  
                       DWORD  ul_reason_for_call,  
                       LPVOID lpReserved  
                     )  
{  
    return TRUE;  
}  


Parameters meaning:

①hModule parameters: DLL instance handle points to itself;

②ul_reason_for_call parameters: Indicates the reason DLL is called, you can have the following four values:


1. DLL_PROCESS_ATTACH:
when the DLL is first >> << calling process, resulting in DllMain function is called,

Meanwhile ul_reason_for_call value DLL_PROCESS_ATTACH,

If the same process again later call this DLL, the operating system will only increase the frequency of use of DLL,

DLL_PROCESS_ATTACH DllMain function calls will not use the DLL.


2.DLL_PROCESS_DETACH:
When the DLL is unmapped from the process's address space, system calls its DllMain, ul_reason_for_call value passed is DLL_PROCESS_DETACH.
★ If the end of the process because the call TerminateProcess, the system will not be used to call the DLL's DllMain DLL_PROCESS_DETACH function. This means that the DLL is no chance to perform any clean-up work before the end of the process.


3.DLL_THREAD_ATTACH:
When a process creates a thread, view the current system all the DLL file image is mapped to the process address space,

And with the value DLL_THREAD_ATTACH calling DllMain function DLL. 

Newly created thread responsible for the implementation of the DLL's DllMain function,

Only when all the DLL have finished processing the notification, the system only allows the thread starts executing its thread function.


4.DLL_THREAD_DETACH:
If a thread calls ExitThread to end the thread (the thread function returns, the system will automatically call ExitThread),

View all current system DLL file image is mapped to the process space,

And with DLL_THREAD_DETACH to call the DllMain function,

Inform all DLL to perform thread-level cleanup.
★ Note: If the end of the thread is a thread because the system calls the TerminateThread,

DLL_THREAD_DETACH value system will not be used to call all the DLL's DllMain function.

 

③lpReserved parameters: Hermit loading load and display, this parameter value is not the same, showing zero load time, if the user wishes to define a load in the form of dynamic link libraries can be accomplished by determining the value of this parameter.

 

Implicit dynamic library header files lib shows the need to load dll file dll file when the program is running, but also the content loaded into multiple programs use memory only a dll dll
  dynamic libraries to load only need to show dll file to run the program not only load the dll dll loaded only when using library functions loadlibrary
  delay loaded dLL is actually implicit dynamic libraries to load and display the combined load required header files lib dll file dll file but does not load the program is running, use the program just load the dll, this time the principle and the principle of display of the same load. Compiler options to configure link> delay load dll need to fill in a loaded DLL files
need to know the path of dynamic libraries can () to specify the path to the dynamic library program is run by setdlldirctrory

 

Guess you like

Origin www.cnblogs.com/xiangtingshen/p/11465002.html