Re: DllMain is called when....

From: Igor Tandetnik (itandetnik_at_mvps.org)
Date: 07/29/04


Date: Thu, 29 Jul 2004 16:21:40 -0400


"Roger" <Roger@discussions.microsoft.com> wrote in message
news:FEDE7415-C932-42CB-8EDE-10E74E5CBD4F@microsoft.com
> 1. called with DLL_PROCESS_ATTACH
> 2. called with DLL_THREAD_ATTACH
> 3. called with DLL_THREAD_ATTACH
> 4. called with DLL_THREAD_DETACH
> 5. called with DLL_PROCESS_DETACH
>
> Most disconcerting is that the last DLL_PROCESS_DETACH is called
> before I call FreeLibrary.(!) Can anyone tell me:
>
> 1. Why there are calls with DLL_THREAD_ATTACH when I am not calling
> LoadLibrary from more than one thread, and additionally I'm not
> accessing the dll from more than the one object in which is it
> created

A newly created thread calls DllMain(DLL_THREAD_ATTACH) for every DLL
currently loaded into the process. This is completely unrelated to
whether this thread ever calls LoadLibrary, or ever referes to anything
from a particular DLL.

> 2. Why there are "unbalanced" calls with DLL_THREAD_ATTACH and
> DLL_THREAD_DETACH

OS makes no attempts to balance them. When a DLL is loaded, there may be
threads running already - they don't call DLL_THREAD_ATTACH. Only a new
thread started after the DLL is loaded calls DLL_THREAD_ATTACH.

By the same token, a thread terminating gracefully calls
DLL_THREAD_DETACH on all DLLs currently loaded into the process right
before terminating. When a DLL is unloaded, only DLL_PROCESS_DETACH is
called - you don't get DLL_THREAD_DETACH for every thread currently
running.

Thus, the following situations are possible:

a. A thread existed before the DLL was loaded, and exited while it was
loaded. Such a thread will execute DLL_PROCESS_DETACH but not
DLL_PROCESS_ATTACH

b. A thread was created while the DLL was loaded, then the DLL is
unloaded while this thread is still running. In this case you will have
DLL_PROCESS_ATTACH for this thread, but not DLL_PROCESS_DETACH.

> 3. Why the call with DLL_PROCESS_DETACH takes place before I call
> FreeLibrary

Are you sure? I find it hard to believe. Is there a possibility of a
reference counting error (FreeLibrary called more times than LoadLibrary
on the same DLL)?

-- 
With best wishes,
    Igor Tandetnik
"For every complex problem, there is a solution that is simple, neat,
and wrong." H.L. Mencken


Relevant Pages

  • Re: IIS permissions error sporadically occurs
    ... I may have found the answer Roger. ... So I believe I did in fact have a name conflict. ... > I am not so sure abont there being no error in their dll. ... > to intercept that and recycle that app. ...
    (microsoft.public.windows.server.security)
  • Re: IIS permissions error sporadically occurs
    ... before removing an old dll it does not ... "Rob" wrote in message ... > I may have found the answer Roger. ... > So I believe I did in fact have a name conflict. ...
    (microsoft.public.windows.server.security)
  • Re: istream::good() in a dll
    ... William DePalo [MVP VC++] wrote: ... > Roger that. ... >> In our library, we have a string handling class, which contains the ... >> a dll, f.gooddoes nothing. ...
    (microsoft.public.dotnet.languages.vc)
  • Re: 16-bit subsystem VirtualDeviceDrivers?
    ... > The switch is /u for regsvr32 to unregister a dll ... > The key value was on this system named VDD, ... >>> I would suggest that you place a copy of the dll named in that reg ... >>> Roger ...
    (microsoft.public.windowsxp.security_admin)