DllMain

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hello everyone,


From MSDN,

http://msdn2.microsoft.com/en-us/library/ms682583.aspx

parameter fdwReason of DllMain has four values,

DLL_PROCESS_ATTACH
DLL_PROCESS_DETACH
DLL_THREAD_ATTACH
DLL_THREAD_DETACH

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
 
Is that correct understanding?

Yes that's essentially it - to the best of my understanding of course.

Dave
 
George,
DLL_THREAD_ATTACH will be called for every already loaded DLL when a new
thread is created. (This allows the DLL to setup things like thread local
storage). There is no requirement for the thread to load the DLL. Similarly
for DLL_THREAD_DETACH - every loaded DLL is called when the thread exits.
Bob
 
George said:
Hello everyone,


From MSDN,

http://msdn2.microsoft.com/en-us/library/ms682583.aspx

parameter fdwReason of DllMain has four values,

DLL_PROCESS_ATTACH
DLL_PROCESS_DETACH
DLL_THREAD_ATTACH
DLL_THREAD_DETACH

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)

No, these are wrong (others have explained the THREAD messages).

If LoadLibrary and FreeLibrary are used, then DLL_PROCESS_ATTACH is called
each time the use count changes from 0 to 1 and DLL_PROCESS_DETACH is called
each time the use count changes from 1 to 0. This could happen multiple
times in a single run.
 
Thanks Bob,


It is interesting and after reading MSDN reference, I think each time a new
thread is created, DllMain will be invoked (as callback function) with
DLL_THREAD_ATTACH value (support DLL is loaded before creating thread) --
even if the thread does not use the DLL at all, right?


regards,
George
 
Hi David,


I think my previous understanding has some mistakes. I think if a process
calls LoadLibrary again for the DLL after the DLL is already loaded, DllMain
will be invoked again with DLL_PROCESS_ATTACH value, right?


regards,
George
 
Thanks Ben,


I think you mean if we have already load the library, if we call LoadLibrary
again and again, the DllMain will not be called again and again since the
reference counter is already >1, right?

But if the process unloads the DLL (make reference counter to 0), then
LoadLibrary again, the DllMain will be called again with DLL_PROCESS_ATTACH
value, right?


regards,
George
 
I think my previous understanding has some mistakes. I think if a process
calls LoadLibrary again for the DLL after the DLL is already loaded, DllMain
will be invoked again with DLL_PROCESS_ATTACH value, right?

I wouldn't have thought so - but I've never investigated it in that
much details. The MSDN documentation could be construed as saying it
would do that, but I think it really means its only done once - or if
the process has unloaded the DLL and then re-loaded it (which is the
same thing really).

Dave
 
Thanks Dave,


I have such questions after reading Ben's answer,
If LoadLibrary and FreeLibrary are used, then DLL_PROCESS_ATTACH is called
each time the use count changes from 0 to 1 and DLL_PROCESS_DETACH is
called each time the use count changes from 1 to 0. This could happen multiple
times in a single run.


regards,
George
 
I have such questions after reading Ben's answer,
Yes it's reasonable that if a process unloads a DLL and then re-loads
it, it's not really different to the initial load situation, so you
would expect the process attach notification.

Dave
 
I agree, thanks Dave.


regards,
George

David Lowndes said:
Yes it's reasonable that if a process unloads a DLL and then re-loads
it, it's not really different to the initial load situation, so you
would expect the process attach notification.

Dave
 
George said:
Thanks Ben,


I think you mean if we have already load the library, if we call
LoadLibrary
again and again, the DllMain will not be called again and again since the
reference counter is already >1, right?

But if the process unloads the DLL (make reference counter to 0), then
LoadLibrary again, the DllMain will be called again with
DLL_PROCESS_ATTACH
value, right?

I think that is correct, yes.
 
Back
Top