Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / October 2007

Tip: Looking for answers? Try searching our database.

DllMain

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
George - 19 Oct 2007 08:01 GMT
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
David Lowndes - 19 Oct 2007 10:39 GMT
>Is that correct understanding?

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

Dave
George - 21 Oct 2007 08:10 GMT
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

> >Is that correct understanding?
>
> Yes that's essentially it - to the best of my understanding of course.
>
> Dave
David Lowndes - 21 Oct 2007 09:40 GMT
>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
George - 21 Oct 2007 09:49 GMT
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 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
[quoted text clipped - 7 lines]
>
> Dave
David Lowndes - 21 Oct 2007 10:15 GMT
>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.

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 - 21 Oct 2007 10:31 GMT
I agree, thanks Dave.

regards,
George

> >I have such questions after reading Ben's answer,
> >
[quoted text clipped - 8 lines]
>
> Dave
Bob Milton - 19 Oct 2007 17:52 GMT
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

> Hello everyone,
>
[quoted text clipped - 29 lines]
> thanks in advance,
> George
George - 21 Oct 2007 08:08 GMT
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

> George,
>     DLL_THREAD_ATTACH will be called for every already loaded DLL when a new
[quoted text clipped - 36 lines]
> > thanks in advance,
> > George
Ben Voigt [C++ MVP] - 19 Oct 2007 21:01 GMT
> Hello everyone,
>
[quoted text clipped - 20 lines]
> 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.
George - 21 Oct 2007 08:14 GMT
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

> > Hello everyone,
> >
[quoted text clipped - 27 lines]
> each time the use count changes from 1 to 0.  This could happen multiple
> times in a single run.
Ben Voigt [C++ MVP] - 22 Oct 2007 14:11 GMT
> Thanks Ben,
>
[quoted text clipped - 7 lines]
> DLL_PROCESS_ATTACH
> value, right?

I think that is correct, yes.

> regards,
> George
[quoted text clipped - 34 lines]
>> each time the use count changes from 1 to 0.  This could happen multiple
>> times in a single run.
George - 23 Oct 2007 09:21 GMT
Thanks for your confirmation, Ben!

regards,
George

> > Thanks Ben,
> >
[quoted text clipped - 48 lines]
> >> each time the use count changes from 1 to 0.  This could happen multiple
> >> times in a single run.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.