Load a DLL More Than Once?

后端 未结 3 1361
醉酒成梦
醉酒成梦 2021-01-04 21:44

I\'m using the LoadLibrary function to load a DLL in Windows. My question is this: If I call this method more than once for the same DLL, do I get handles to different insta

相关标签:
3条回答
  • 2021-01-04 21:52

    If the DLL is already loaded, LoadLibrary will simply return the address of the library in memory. However, DllMain is not called again with DLL_PROCESS_ATTACH when the second load is attempted. Handles in the sense of libraries are just memory locations, so the value you get the second time around should be the same as the first.

    As far as linux SO files go, I don't see why they would load twice either. However, someone else will have to weigh in on this to give you a proper answer.

    0 讨论(0)
  • 2021-01-04 21:53

    The MSDN documentation states:

    The system maintains a per-process reference count on all loaded modules. Calling LoadLibrary increments the reference count. Calling the FreeLibrary or FreeLibraryAndExitThread function decrements the reference count. The system unloads a module when its reference count reaches zero or when the process terminates (regardless of the reference count).

    So it would appear that loading the module more than once (without matching calls to FreeLibrary) will return the same handle.

    0 讨论(0)
  • 2021-01-04 22:02

    For Linux shared objects, from the dlopen(3) manpage:

    If the same library is loaded again with dlopen(), the same file handle is returned. The dl library maintains reference counts for library handles, so a dynamic library is not deallocated until dlclose() has been called on it as many times as dlopen() has succeeded on it. The _init() routine, if present, is only called once. But a subsequent call with RTLD_NOW may force symbol resolution for a library earlier loaded with RTLD_LAZY.

    0 讨论(0)
提交回复
热议问题