<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>The thread that gets the DLL_PROCESS_DETACH notification is not necessarily the one that got the DLL_PROCESS_ATTACH notification</title><link>http://blogs.msdn.com/oldnewthing/archive/2009/06/26/9804500.aspx</link><description>Don't do things with thread affinity.</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>re: The thread that gets the DLL_PROCESS_DETACH notification is not necessarily the one that got the DLL_PROCESS_ATTACH notification</title><link>http://blogs.msdn.com/oldnewthing/archive/2009/06/26/9804500.aspx#9805494</link><pubDate>Fri, 26 Jun 2009 17:08:30 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9805494</guid><dc:creator>John</dc:creator><description>&lt;p&gt;This is why the only thing I ever do in DllMain is InitializeCriticalSection and DeleteCriticalSection; pretty much anything else can be lazy-initialized if needed.&lt;/p&gt;
</description></item><item><title>re: The thread that gets the DLL_PROCESS_DETACH notification is not necessarily the one that got the DLL_PROCESS_ATTACH notification</title><link>http://blogs.msdn.com/oldnewthing/archive/2009/06/26/9804500.aspx#9805551</link><pubDate>Fri, 26 Jun 2009 17:58:31 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9805551</guid><dc:creator>Tihiy</dc:creator><description>&lt;P&gt;&amp;gt;must call DestroyWindow from the same window&lt;/P&gt;
&lt;P&gt;you meant same thread?&lt;/P&gt;
&lt;DIV class=post&gt;[&lt;I&gt;Fixed, thanks. -Raymond&lt;/I&gt;]&lt;/DIV&gt;</description></item><item><title>re: The thread that gets the DLL_PROCESS_DETACH notification is not necessarily the one that got the DLL_PROCESS_ATTACH notification</title><link>http://blogs.msdn.com/oldnewthing/archive/2009/06/26/9804500.aspx#9805967</link><pubDate>Sat, 27 Jun 2009 00:00:53 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9805967</guid><dc:creator>Ben Voigt [C++ MVP]</dc:creator><description>&lt;P&gt;Given the problem of global hooks running arbitrary code, is it possible to safely create a window from the DLL_THREAD_ATTACH notification (note THREAD not PROCESS)?&lt;/P&gt;
&lt;DIV class=post&gt;[&lt;I&gt;Was never possible to do safely. Global hooks are just one of many problems. -Raymond&lt;/I&gt;]&lt;/DIV&gt;</description></item><item><title>re: The thread that gets the DLL_PROCESS_DETACH notification is not necessarily the one that got the DLL_PROCESS_ATTACH notification</title><link>http://blogs.msdn.com/oldnewthing/archive/2009/06/26/9804500.aspx#9806363</link><pubDate>Sat, 27 Jun 2009 08:46:02 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9806363</guid><dc:creator>Worf</dc:creator><description>&lt;p&gt;Which brings up the question - what is the purpose of DllMain, and what would someone normally use it for?&lt;/p&gt;
&lt;p&gt;If it's not a replacement for Init()/Deinit() type functions, what should one use it for?&lt;/p&gt;
&lt;p&gt;(Especially since there's a default stub if you don't implement one...)&lt;/p&gt;
</description></item><item><title>re: The thread that gets the DLL_PROCESS_DETACH notification is not necessarily the one that got the DLL_PROCESS_ATTACH notification</title><link>http://blogs.msdn.com/oldnewthing/archive/2009/06/26/9804500.aspx#9806392</link><pubDate>Sat, 27 Jun 2009 09:58:16 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9806392</guid><dc:creator>Jeff</dc:creator><description>&lt;p&gt;@Worf&lt;/p&gt;
&lt;p&gt;DllMain can be used to initialize/release a few basic low-level primitives that are explicitly designed to be called during DllMain. &amp;nbsp;MSDN gives examples:&lt;/p&gt;
&lt;p&gt;&amp;quot;For example, DllMain can create synchronization objects such as critical sections and mutexes, and use TLS.&amp;quot;&lt;/p&gt;
&lt;p&gt;However, if you don't have any use for it, then by all means just pretend DllMain doesn't exist. &amp;nbsp;(Do still call DisableThreadLibraryCalls though).&lt;/p&gt;
&lt;p&gt;Additionally, your programming language's runtime will take care of bookkeeping in or around DllMain. &amp;nbsp;For example: the C runtime initializes global variables, and the C++ runtime calls the class constructors for global objects (a compelling reason to avoid having global C++ objects).&lt;/p&gt;
</description></item><item><title>re: The thread that gets the DLL_PROCESS_DETACH notification is not necessarily the one that got the DLL_PROCESS_ATTACH notification</title><link>http://blogs.msdn.com/oldnewthing/archive/2009/06/26/9804500.aspx#9806707</link><pubDate>Sat, 27 Jun 2009 22:02:21 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9806707</guid><dc:creator>Daev</dc:creator><description>&lt;p&gt;Also, if you have an &amp;quot;atexit function&amp;quot; in your DLL written in C, and that DLL is linked with the static version of the C run-time library, then the atexit function is automatically called from DllMain during DLL_PROCESS_DETACH.&lt;/p&gt;
&lt;p&gt;So don't do anything complicated in an atexit function -- in particular, don't call functions which are in some other DLL, 'cause that other DLL might have already been unloaded. &amp;nbsp;(There's no way to guarantee that your DLL gets detached before any DLLs it might be using.)&lt;/p&gt;
&lt;p&gt;This bit me, because I had an atexit function which closed any open sockets in case of process termination. &amp;nbsp;But sockets are in the WinSock DLL ... which wasn't there anymore. &amp;nbsp;Crash!&lt;/p&gt;
</description></item><item><title>re: The thread that gets the DLL_PROCESS_DETACH notification is not necessarily the one that got the DLL_PROCESS_ATTACH notification</title><link>http://blogs.msdn.com/oldnewthing/archive/2009/06/26/9804500.aspx#9807660</link><pubDate>Mon, 29 Jun 2009 00:48:27 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9807660</guid><dc:creator>W</dc:creator><description>&lt;p&gt;The contract of dllmain is one of the most problematic things in Win32. Especially since many programmers don't even realize they are implementing dllmain since their language abstracts that away(static constructors/destructors in C++, initialization/finalization sections in Delphi).&lt;/p&gt;
&lt;p&gt;I'm sure I violated it quite often before learning about it. Probably I'm still violating it in my current programs as it is almost impossible to write code which does not call any external functions.&lt;/p&gt;
</description></item><item><title>re: The thread that gets the DLL_PROCESS_DETACH notification is not necessarily the one that got the DLL_PROCESS_ATTACH notification</title><link>http://blogs.msdn.com/oldnewthing/archive/2009/06/26/9804500.aspx#9808404</link><pubDate>Mon, 29 Jun 2009 12:38:45 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9808404</guid><dc:creator>Anonymous</dc:creator><description>&lt;p&gt;Two other useful things to do in a DllMain:&lt;/p&gt;
&lt;p&gt;* Stash the first argument to DLL_PROCESS_ATTACH somewhere, it is useful.&lt;/p&gt;
&lt;p&gt;* Call DisableThreadLibraryCalls() so you will not be bothered again until the DLL_PROCESS_DETACH.&lt;/p&gt;
</description></item><item><title>re: The thread that gets the DLL_PROCESS_DETACH notification is not necessarily the one that got the DLL_PROCESS_ATTACH notification</title><link>http://blogs.msdn.com/oldnewthing/archive/2009/06/26/9804500.aspx#9808470</link><pubDate>Mon, 29 Jun 2009 13:48:16 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9808470</guid><dc:creator>Mike Dimmick</dc:creator><description>&lt;p&gt;It's one of these things that started out with good intentions, of being a place where you would do initialisation and cleanup. However, the implementation imposed some restrictions, and over time it's become clear that those restrictions stop you doing anything actually useful in it.&lt;/p&gt;
&lt;p&gt;There are other good intentions in Windows - the IsBadXxxPtr family spring to mind. Again, the consequences are more serious than were anticipated when they were designed.&lt;/p&gt;
</description></item><item><title>re: The thread that gets the DLL_PROCESS_DETACH notification is not necessarily the one that got the DLL_PROCESS_ATTACH notification</title><link>http://blogs.msdn.com/oldnewthing/archive/2009/06/26/9804500.aspx#9809540</link><pubDate>Tue, 30 Jun 2009 14:27:22 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9809540</guid><dc:creator>640k</dc:creator><description>&lt;p&gt;Unmanaged code is so last millennium.&lt;/p&gt;
</description></item><item><title>re: The thread that gets the DLL_PROCESS_DETACH notification is not necessarily the one that got the DLL_PROCESS_ATTACH notification</title><link>http://blogs.msdn.com/oldnewthing/archive/2009/06/26/9804500.aspx#9813896</link><pubDate>Thu, 02 Jul 2009 15:11:37 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9813896</guid><dc:creator>Maurits</dc:creator><description>&lt;p&gt;&amp;gt; you must call DestroyWindow from the same thread that created it&lt;/p&gt;
&lt;p&gt;I'll bite. &amp;nbsp;Why?&lt;/p&gt;
</description></item></channel></rss>