Loading DLL from DLL?

What is the best way to load a dll from a dll?

My problem is that I cannot load the DLL on process_attach, and I cannot load the DLL from the main program, because I do not control the main source of the program. And therefore, I cannot also call the non-dllmain function.

+5
source share
4 answers

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.

- (, , , ):

/* This is actually the function that the linker marks as entrypoint for the dll */
BOOL WINAPI CRTDllMain(
  __in  HINSTANCE hinstDLL,
  __in  DWORD fdwReason,
  __in  LPVOID lpvReserved
)
{
    BOOL ret=FALSE;
    switch(fdwReason)
    {
        case DLL_PROCESS_ATTACH:
            /* Init the global CRT structures */
            init_CRT();
            /* Construct global objects and static fields */
            construct_globals();
            /* Call user-supplied DllMain and get from it the return code */
            ret = DllMain(hinstDLL, fdwReason, lpvReserved);
            break;
        case DLL_PROCESS_DETACH:
            /* Call user-supplied DllMain and get from it the return code */
            ret = DllMain(hinstDLL, fdwReason, lpvReserved);
            /* Destruct global objects and static fields */
            destruct_globals();
            /* Destruct the global CRT structures */
            cleanup_CRT();
            break;
        case DLL_THREAD_ATTACH:
            /* Init the CRT thread-local structures */
            init_TLS_CRT();
            /* The same as before, but for thread-local objects */
            construct_TLS_globals();
            /* Call user-supplied DllMain and get from it the return code */
            ret = DllMain(hinstDLL, fdwReason, lpvReserved);
            break;
        case DLL_THREAD_DETACH:
            /* Call user-supplied DllMain and get from it the return code */
            ret = DllMain(hinstDLL, fdwReason, lpvReserved);
            /* Destruct thread-local objects and static fields */
            destruct_TLS_globals();
            /* Destruct the thread-local CRT structures */
            cleanup_TLS_CRT();
            break;
        default:
            /* ?!? */
            /* Call user-supplied DllMain and get from it the return code */
            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, , .

, , , , . , , , , .

+77

- DLL lib . , DLL Windows. , , DLL DLL. Windows . A.DLL B.DLL, A.DLL, B.DLL A.DLL.

+13

. DLL , . , . . DLL .

+5

LoadLibrary GetProcAddress , / dll, / , , .

0

All Articles