473,769 Members | 1,674 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

DllMain

Hello everyone,
From MSDN,

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

parameter fdwReason of DllMain has four values,

DLL_PROCESS_ATT ACH
DLL_PROCESS_DET ACH
DLL_THREAD_ATTA CH
DLL_THREAD_DETA CH

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_ATT ACH: will be called only once when the process loads the DLL
for the 1st time
DLL_PROCESS_DET ACH: will be called only once when the process unloads the
DLL (when process stops)
DLL_THREAD_ATTA CH: will be called every time when a thread inside the
current process loads the DLL
DLL_THREAD_DETA CH: will be called every time when a thread inside the
current process unloads the DLL

Is that correct understanding?
thanks in advance,
George
Oct 19 '07 #1
10 2351
>Is that correct understanding?

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

Dave
Oct 19 '07 #2
George,
DLL_THREAD_ATTA CH 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_DETA CH - every loaded DLL is called when the thread exits.
Bob

"George" <Ge****@discuss ions.microsoft. comwrote in message
news:92******** *************** ***********@mic rosoft.com...
Hello everyone,
From MSDN,

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

parameter fdwReason of DllMain has four values,

DLL_PROCESS_ATT ACH
DLL_PROCESS_DET ACH
DLL_THREAD_ATTA CH
DLL_THREAD_DETA CH

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_ATT ACH: will be called only once when the process loads the
DLL
for the 1st time
DLL_PROCESS_DET ACH: will be called only once when the process unloads the
DLL (when process stops)
DLL_THREAD_ATTA CH: will be called every time when a thread inside the
current process loads the DLL
DLL_THREAD_DETA CH: will be called every time when a thread inside the
current process unloads the DLL

Is that correct understanding?
thanks in advance,
George

Oct 19 '07 #3

"George" <Ge****@discuss ions.microsoft. comwrote in message
news:92******** *************** ***********@mic rosoft.com...
Hello everyone,
From MSDN,

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

parameter fdwReason of DllMain has four values,

DLL_PROCESS_ATT ACH
DLL_PROCESS_DET ACH
DLL_THREAD_ATTA CH
DLL_THREAD_DETA CH

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_ATT ACH: will be called only once when the process loads the
DLL
for the 1st time
DLL_PROCESS_DET ACH: 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_ATT ACH is called
each time the use count changes from 0 to 1 and DLL_PROCESS_DET ACH is called
each time the use count changes from 1 to 0. This could happen multiple
times in a single run.
Oct 19 '07 #4
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_ATTA CH value (support DLL is loaded before creating thread) --
even if the thread does not use the DLL at all, right?
regards,
George

"Bob Milton" wrote:
George,
DLL_THREAD_ATTA CH 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_DETA CH - every loaded DLL is called when the thread exits.
Bob

"George" <Ge****@discuss ions.microsoft. comwrote in message
news:92******** *************** ***********@mic rosoft.com...
Hello everyone,
From MSDN,

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

parameter fdwReason of DllMain has four values,

DLL_PROCESS_ATT ACH
DLL_PROCESS_DET ACH
DLL_THREAD_ATTA CH
DLL_THREAD_DETA CH

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_ATT ACH: will be called only once when the process loads the
DLL
for the 1st time
DLL_PROCESS_DET ACH: will be called only once when the process unloads the
DLL (when process stops)
DLL_THREAD_ATTA CH: will be called every time when a thread inside the
current process loads the DLL
DLL_THREAD_DETA CH: will be called every time when a thread inside the
current process unloads the DLL

Is that correct understanding?
thanks in advance,
George


Oct 21 '07 #5
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_ATT ACH value, right?
regards,
George

"David Lowndes" wrote:
Is that correct understanding?

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

Dave
Oct 21 '07 #6
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_ATT ACH
value, right?
regards,
George

"Ben Voigt [C++ MVP]" wrote:
>
"George" <Ge****@discuss ions.microsoft. comwrote in message
news:92******** *************** ***********@mic rosoft.com...
Hello everyone,
From MSDN,

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

parameter fdwReason of DllMain has four values,

DLL_PROCESS_ATT ACH
DLL_PROCESS_DET ACH
DLL_THREAD_ATTA CH
DLL_THREAD_DETA CH

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_ATT ACH: will be called only once when the process loads the
DLL
for the 1st time
DLL_PROCESS_DET ACH: 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_ATT ACH is called
each time the use count changes from 0 to 1 and DLL_PROCESS_DET ACH is called
each time the use count changes from 1 to 0. This could happen multiple
times in a single run.
Oct 21 '07 #7
>I have such questions after reading Ben's answer,
>
>If LoadLibrary and FreeLibrary are used, then DLL_PROCESS_ATT ACH is called
each time the use count changes from 0 to 1 and DLL_PROCESS_DET ACH 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
Oct 21 '07 #8
I agree, thanks Dave.
regards,
George

"David Lowndes" wrote:
I have such questions after reading Ben's answer,
If LoadLibrary and FreeLibrary are used, then DLL_PROCESS_ATT ACH is called
each time the use count changes from 0 to 1 and DLL_PROCESS_DET ACH 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
Oct 21 '07 #9

"George" <Ge****@discuss ions.microsoft. comwrote in message
news:E3******** *************** ***********@mic rosoft.com...
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_ATT ACH
value, right?
I think that is correct, yes.
>

regards,
George

"Ben Voigt [C++ MVP]" wrote:
>>
"George" <Ge****@discuss ions.microsoft. comwrote in message
news:92******* *************** ************@mi crosoft.com...
Hello everyone,
From MSDN,

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

parameter fdwReason of DllMain has four values,

DLL_PROCESS_ATT ACH
DLL_PROCESS_DET ACH
DLL_THREAD_ATTA CH
DLL_THREAD_DETA CH

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_ATT ACH: will be called only once when the process loads the
DLL
for the 1st time
DLL_PROCESS_DET ACH: 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_ATT ACH is
called
each time the use count changes from 0 to 1 and DLL_PROCESS_DET ACH is
called
each time the use count changes from 1 to 0. This could happen multiple
times in a single run.

Oct 22 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
10226
by: Michael Roper | last post by:
In Jesse Liberty's "Programming in C#" he says that "...an assembly must have exactly one entry point: DLLMain, WinMain, or Main. DLLMain is the entry point for DLLs..." In the MSDN ".Net Framework Developer's Guide" it says that "...each assembly can have only one entry point (that is, DllMain, WinMain, or Main..." So we've got "must" vs. "can" and "DLLMain" vs. "DllMain."
0
1640
by: Arno Huetter | last post by:
Hi there, I invoke a native DLL function from managed code. This works fine, but it seems that the DLL's DllMain function is never called upon attaching / detaching. Does anyone have an idea why that could be the case? Thanks in advance! Kind regards,
7
2430
by: Adam | last post by:
I have a managed cpp wrapper. Im using this in a native dll as a static variable. I need to free this library when the dll is done being used. The perfect place to do this is DllMain for DLL_PROCESS_DETACH, but I can't do this when touching managed code, even if its just calling "delete someObject;". Any recommendations on how to free this object (it needs to be freed when the dll is being unloaded from its caller). Since the CRT is...
3
3090
by: Jozsef Bekes | last post by:
Hi All, is there a mechanism just like DllMain in VC6, a function that gets called whenever an assembly gets loaded? Thank you for all answers. Jozsi
9
3599
by: Ulrich Proeller | last post by:
I currently hunting a problem with a MixedMode dll, which used to work well, until the .NET Framework 2.0 is installed on a machine. Since the, the dll fail when loaded stating that there is a problem with the "LoaderLock" bug. Thus, I made sure that the dll is initialized properly before any other call into the dll is made. I also gave my dll a empty DllMain as it was proposed by Microsoft. BOOL WINAPI DllMain(HINSTANCE hModule, DWORD...
2
6273
by: Vincent Fatica | last post by:
I can get Explorer to load (via LoadLibrary()) my DLL using VirtualAlloc(), WriteProcessMemory(), and CreateRemoteThread() (a technique discussed here occasionally). But I'd really like to jusy leave my DLL attached to Explorer until Explorer terminates (typically logoff or shutdown). But I notice that DllMain(DLL_PROCESS_DETACH) is not called when Explorer terminates after my DLL has been loaded as described above. Why isn't...
1
3204
by: =?Utf-8?B?U2VhbiBDb25uZXJ5?= | last post by:
Hi, Is it possible to define DllMain in a static library for use in dlls? The reason is that I am defining a platform abstraction for being loaded as a shared library and I figured the simplest way is to implement DllMain in a static library, which then calls mydllmain. I define this library (call it platform.lib) which has DllMain implemented with what should be unresolved references to mydllmain. I then link it into a bunch of...
3
1380
by: Abubakar | last post by:
Hi, I have a dll that gets called by an exe, I just placed a little code in its dlmain method and put a breakpoint on the code only to discover that its getting called nearly hundreds of times. I suspect each time some exported function is getting called the dllmain also gets called. Why is it happening and why should it happen? Regards, ...ab
0
2665
by: Question123 | last post by:
Hi I am developing windows c# application in which i have given reference of third party DLL. "Do not attempt to run managed code inside a DllMain or image initialization function since doing so can cause the application to hang." Does anybody knows why this error comes.?? plz help me..
0
9589
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9423
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10045
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9994
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9863
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8870
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6673
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5298
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5447
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.