After all the debate that went on in the comments, I think it’s better to summarize my positions in the “real” answer.
, , DLL DllMain LoadLibrary. , DllMain LoadLibrary, , DllMain:
LoadLibrary DLL . DLL, DLL_PROCESS_ATTACH, DLL. , , , , LoadLibrary. . DllMain , DLL .
entry-point . LoadLibrary LoadLibraryEx ( , ), DLL. , DLL , . entry-point FreeLibrary ( , FreeLibrary) , , DLL , .
( )
, , ; , . this this, , , DllMain, . Raymond Chen.
, .
, , DllMain, DllMain dll; , dll. , , /, ( ). ( , ) , DllMain.
- (, , , ):
BOOL WINAPI CRTDllMain(
__in HINSTANCE hinstDLL,
__in DWORD fdwReason,
__in LPVOID lpvReserved
)
{
BOOL ret=FALSE;
switch(fdwReason)
{
case DLL_PROCESS_ATTACH:
init_CRT();
construct_globals();
ret = DllMain(hinstDLL, fdwReason, lpvReserved);
break;
case DLL_PROCESS_DETACH:
ret = DllMain(hinstDLL, fdwReason, lpvReserved);
destruct_globals();
cleanup_CRT();
break;
case DLL_THREAD_ATTACH:
init_TLS_CRT();
construct_TLS_globals();
ret = DllMain(hinstDLL, fdwReason, lpvReserved);
break;
case DLL_THREAD_DETACH:
ret = DllMain(hinstDLL, fdwReason, lpvReserved);
destruct_TLS_globals();
cleanup_TLS_CRT();
break;
default:
ret = DllMain(hinstDLL, fdwReason, lpvReserved);
}
return ret;
}
: , , .
, : DllMain (.. dll, , MSDN DllMain), LoadLibrary , DllMain.
, , LoadLibrary DllMain, , , , MAINTAINER , .
delayload: , , - DLL DllMain: , , LoadLibrary, , .
, , dll, , , , , .
DLL DllMain, , ; , DllMain 32, , , dll , LoadLibrary, dll ( , dll , , , , dll ).
,
, DllMain . , , - - , , " ". .
, DllMain, . -, , , -, . OS Loader . , . , DllMains . , , , MSDN - , .
, , , . - DllMain DLL_PROCESS_ATTACH, , . , DllMain , , GetProcAddress , , . , AV.
( , )
, Linux vs Windows: Linux, , -.
DllMain (_init _fini), - , ! - CRT, , , _init, , __ attribute__ ( - "" DllMain Win32). _fini.
_init , dll ( dlopen ), , . , -, Linux , (1) DllMain-like, (2), Linux, , dll.
"" DLL, kernel32.dll, DllMain.
, DllMain, (.. "" DllMain, CRT) ( / ), DLL, , ( LoadLibrary), ( DLL, LoadLibrary).
dll, , - doh! - . : , ( DllMain, , DllMain).
If for some reason this is not viable, there are still delay options (with the restrictions I said earlier).
If for some unknown reason you have an inexplicable need to call LoadLibrary in DllMain, well, go ahead, shoot in the foot, this is in your faculties. But do not tell me that I did not warn you.
I forgot: Another fundamental source of information on this topic is the Recommendations on creating DLLs from Microsoft, which in fact speaks almost exclusively about the bootloader, DllMain, locking the bootloader and their interactions; Look at him for more information on this topic.
Adding
, . , , : " , " " dllmain".
: , , , . , DllMain , kernel32. .
, , ,
, , .
, , Microsoft.
, , , , LoadLibrary , . DllMain ( dll, dll DllMain), , .
, DLL (, A.dll B.dll), : , DllMain ? A.dll, , DllMain, B.dll, , B.dll ( DllMain ). , .
, , : DllMain, DllMain - DLL.
, dll_attach, , , , , , .
: : " , x ^ 2 + 1 = 0 ". , ; , , .
- : , , , - +/- sqrt (-1); ( , ), , . , . , , , .
, , , , , "", , DLL DllMain? "" - , , . , , dll DllMain.
PS: DLL2 (ole32.dll, Vista x64) DLL1 (mydll), DLL ?
, (, , 32 ); , , dll, dll (LoadLibrary ).
(2)
, CreateRemoteThread, . Linux Mac dll/shared .
dll ( ) , Linux/Mac, , , , DllMain , kernel32.dll( , ).
, . ( LoadLibrary DLL) CreateRemoteThread; DllMain IPC (, shared memory, -, init), "" init, dll. DllMain - . - WaitForSingleObject, , CreateRemoteThread. , , ( , LoadLibrary , ), , DllMain, init CreateRemoteThread.
: Windows 2000 DllMain ,
Windows 2000 . DLL , DLL .
, , , . dll, -, , . , dll , , "" .
, , - ( ), LoadLibrary init; , CreateRemoteThread, , .
, , , , . , , , , .