I think we could simply understand what MSDN described about the four values in the following way after some experiment (I have read the big table and want to extract some brief and simple information to understand to remember),

DLL_PROCESS_ATTACH: will be called only once when the process loads the DLL for the 1st time
DLL_PROCESS_DETACH: will be called only once when the process unloads the DLL (when process stops)
DLL_THREAD_ATTACH: will be called every time when a thread inside the current process loads the DLL
DLL_THREAD_DETACH: will be called every time when a thread inside the current process unloads the DLL

Is that correct understanding?

thanks in advance,
George

10-19-2007

matsp

I'm pretty sure that the THREAD_ATTACH/DETACH are when a thread is STARTED and KILLED, rather than when a thread loads the DLL - the latter wouldn't be meaningfull. A DLL will need to know about all threads using the DLL, so that for example thread-local storage can be set up for each DLL.

It should be pretty easy to test this theory - create a thread in your code, set a breakpoint on DLLmain and see what happens when the thread is created.