Описание тега dllmain
In a Windows DLL, the DllMain function is automatically run when the DLL is loaded, just before it is unloaded (provided the process either explicitly unloads the DLL or exits cleanly) and whenever a process thread is started or exits cleanly.
DllMain is a placeholder for the function name defined by the library. The actual name must be specified when building the DLL. Recommended practice, where possible, is for a library to provide an explicit initialization function for the caller to use rather than using DllMain.
The DllMain function must be used cautiously, as only certain Win32 API functions are safe to call from DllMain. In particular, it must not call API functions that are not in kernel32.dll
and it must not cause another DLL to be loaded either directly or indirectly. Communication with other threads or processes is also prohibited.
It is known to be safe to create synchronization objects and to use TLS (Thread Local Storage) in DllMain, so these are the most common uses. Another common use is to allocate per-thread objects. In C++, the runtime library will typically create global and static objects for your DLL using DllMain.