DllMain, DLL_PROCESS_ATTACH, and DLL_PROCESS_DETACH in MFC Extension DLLs

  • Thread starter Thread starter Boris
  • Start date Start date
B

Boris

With MFC Extension DLL that exports classes through __declspec(dllexport)

DLL_PROCESS_ATTACH/DLL_PROCESS_DETACH
1. Does each a process loads dll only once and unloads dll only once during
its life-time?

DLL_THREAD_ATTACH/DLL_THREAD_DETACH
2. Does each a thread loads dll only once and unloads dll only once during
its life-time? Does DLL_THREAD_DETACH occurs only when thread terminates?


Thank you,
-Boris
 
Boris said:
With MFC Extension DLL that exports classes through
__declspec(dllexport)

DLL_PROCESS_ATTACH/DLL_PROCESS_DETACH
1. Does each a process loads dll only once and unloads dll only once
during its life-time?

If the DLL is statically loaded (i.e. NOT by LoadLibrary), then yes. If the
DLL is loaded by LoadLibrary, it can be loaded and unloadded multiple times.
Since you're exposing classes by __declspec(dllexport), I would expect that
the DLL is statically loaded.
DLL_THREAD_ATTACH/DLL_THREAD_DETACH
2. Does each a thread loads dll only once and unloads dll only once
during its life-time?

DLL_THREAD_ATTACH will be called each time a thread is created. Threads
that existed before the DLL was loaded will never call DLL_THREAD_ATTACH.
For a statically linked DLL, this means that the main thread will never call
thread attach (but it will call process attach).
Does DLL_THREAD_DETACH occurs only when thread
terminates?

DLL_THREAD_DETACH will be called each time a thread other than the main
tread terminates. It may be called on threads for which no
DLL_THREAD_ATTACH call was received. The main thread (or any threadd that
directly calls ExitProcess) will result in a DLL_PROCESS_DETACH call instead
of the thread detach.

-cd
 
Back
Top