<?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>In theory, there is no difference between theory and practice. But, in practice, there is. : Platform</title><link>http://blogs.msdn.com/oleglv/archive/tags/Platform/default.aspx</link><description>Tags: Platform</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>DllMain : a horror story</title><link>http://blogs.msdn.com/oleglv/archive/2003/10/28/56142.aspx</link><pubDate>Tue, 28 Oct 2003 12:10:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:56142</guid><dc:creator>oleglv</dc:creator><slash:comments>2</slash:comments><comments>http://blogs.msdn.com/oleglv/comments/56142.aspx</comments><wfw:commentRss>http://blogs.msdn.com/oleglv/commentrss.aspx?PostID=56142</wfw:commentRss><description>&lt;body xmlns="http://www.w3.org/1999/xhtml"&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-INDENT: 0.5in"&gt;
        &lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma"&gt;&lt;a href="http://blogs.gotdotnet.com/olegl/PermaLink.aspx/d1610664-8d0b-4579-a20c-712516e08423"&gt;Last
        time&lt;/a&gt; I was talking about DllMain and what nasty things can occur if you misuse
        it. I have also mentioned that it may not be one of those "I'm always careful it can
        never happen to me" situations - things can get out of hand very quickly. 
        &lt;O:P&gt;&lt;/O:P&gt;
        &lt;/span&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-INDENT: 0.5in"&gt;
        &lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma"&gt;Keep in mind that OS loader has
        been evolving over time. OS creators know that not all DLLs are well-behaved and they
        have been trying to do their best to minimize the impact of poorly-written DllMains,
        however it is still more than possible to shot oneself in the foot. 
        &lt;O:P&gt;&lt;/O:P&gt;
        &lt;/span&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-INDENT: 0.5in"&gt;
        &lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma"&gt;Let me give you a very simple example
        as to how easy this can be (behavior may vary on different OS's, I'm running this
        on Windows XP SP1). 
        &lt;O:P&gt;&lt;/O:P&gt;
        &lt;/span&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-INDENT: 0.5in"&gt;
        &lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma"&gt;Consider the following:&lt;O:P&gt;&lt;/O:P&gt;
        &lt;/span&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-INDENT: 0.5in"&gt;
        &lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma"&gt;
        &lt;O:P&gt;&amp;#160;&lt;/O:P&gt;
        &lt;/span&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; mso-layout-grid-align: none"&gt;
        &lt;span style="FONT-SIZE: 8pt; COLOR: green; FONT-FAMILY: Tahoma"&gt;/////////////////////////////////////////////////////////////////////&lt;O:P&gt;&lt;/O:P&gt;
        &lt;/span&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; mso-layout-grid-align: none"&gt;
        &lt;span style="FONT-SIZE: 8pt; COLOR: green; FONT-FAMILY: Tahoma"&gt;Dll2.cpp&lt;O:P&gt;&lt;/O:P&gt;
        &lt;/span&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; mso-layout-grid-align: none"&gt;
        &lt;span style="FONT-SIZE: 8pt; COLOR: green; FONT-FAMILY: Tahoma"&gt;/////////////////////////////////////////////////////////////////////&lt;O:P&gt;&lt;/O:P&gt;
        &lt;/span&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; mso-layout-grid-align: none"&gt;
        &lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: Tahoma"&gt;HMODULE g_Module;&lt;O:P&gt;&lt;/O:P&gt;
        &lt;/span&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; mso-layout-grid-align: none"&gt;
        &lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: Tahoma"&gt;TCHAR g_tclpszFileName[256];&lt;O:P&gt;&lt;/O:P&gt;
        &lt;/span&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; mso-layout-grid-align: none"&gt;
        &lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: Tahoma"&gt;BOOL APIENTRY DllMain( HINSTANCE
        hModule, 
        &lt;O:P&gt;&lt;/O:P&gt;
        &lt;/span&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; mso-layout-grid-align: none"&gt;
        &lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: Tahoma"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/span&gt;DWORD&lt;span style="mso-spacerun: yes"&gt;&amp;#160; &lt;/span&gt;ul_reason_for_call, 
        &lt;O:P&gt;&lt;/O:P&gt;
        &lt;/span&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; mso-layout-grid-align: none"&gt;
        &lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: Tahoma"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/span&gt;LPVOID
        lpReserved&lt;O:P&gt;&lt;/O:P&gt;
        &lt;/span&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; mso-layout-grid-align: none"&gt;
        &lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: Tahoma"&gt;&lt;span style="mso-tab-count: 5"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/span&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;#160;&lt;/span&gt;)&lt;O:P&gt;&lt;/O:P&gt;
        &lt;/span&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; mso-layout-grid-align: none"&gt;
        &lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: Tahoma"&gt;{&lt;O:P&gt;&lt;/O:P&gt;
        &lt;/span&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; mso-layout-grid-align: none"&gt;
        &lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: Tahoma"&gt;&lt;span style="mso-tab-count: 1"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/span&gt;&lt;span style="COLOR: blue"&gt;if&lt;/span&gt; (
        DLL_PROCESS_ATTACH == ul_reason_for_call )&lt;O:P&gt;&lt;/O:P&gt;
        &lt;/span&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; mso-layout-grid-align: none"&gt;
        &lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: Tahoma"&gt;&lt;span style="mso-tab-count: 1"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/span&gt;{&lt;O:P&gt;&lt;/O:P&gt;
        &lt;/span&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; mso-layout-grid-align: none"&gt;
        &lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: Tahoma"&gt;&lt;span style="mso-tab-count: 2"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/span&gt;printf("Dll2:DllMain\r\n");&lt;O:P&gt;&lt;/O:P&gt;
        &lt;/span&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; mso-layout-grid-align: none"&gt;
        &lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: Tahoma"&gt;&lt;span style="mso-tab-count: 2"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/span&gt;g_Module
        = hModule;&lt;O:P&gt;&lt;/O:P&gt;
        &lt;/span&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; mso-layout-grid-align: none"&gt;
        &lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: Tahoma"&gt;&lt;span style="mso-tab-count: 2"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/span&gt;::GetModuleFileName(
        g_Module, g_tclpszFileName, 255 );&lt;O:P&gt;&lt;/O:P&gt;
        &lt;/span&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; mso-layout-grid-align: none"&gt;
        &lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: Tahoma"&gt;&lt;span style="mso-tab-count: 1"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/span&gt;}&lt;O:P&gt;&lt;/O:P&gt;
        &lt;/span&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; mso-layout-grid-align: none"&gt;
        &lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: Tahoma"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;#160;&amp;#160; &lt;/span&gt;&lt;span style="mso-tab-count: 1"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/span&gt;&lt;span style="COLOR: blue"&gt;return&lt;/span&gt; TRUE;&lt;O:P&gt;&lt;/O:P&gt;
        &lt;/span&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; mso-layout-grid-align: none"&gt;
        &lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: Tahoma"&gt;}&lt;O:P&gt;&lt;/O:P&gt;
        &lt;/span&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; mso-layout-grid-align: none"&gt;
        &lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: Tahoma"&gt;
        &lt;O:P&gt;&amp;#160;&lt;/O:P&gt;
        &lt;/span&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; mso-layout-grid-align: none"&gt;
        &lt;span style="FONT-SIZE: 8pt; COLOR: blue; FONT-FAMILY: Tahoma"&gt;extern&lt;/span&gt;&lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: Tahoma"&gt; "C" &lt;span style="COLOR: blue"&gt;__declspec&lt;/span&gt;(&lt;span style="COLOR: blue"&gt;dllexport&lt;/span&gt;) &lt;span style="COLOR: blue"&gt;void&lt;/span&gt; WINAPIV
        OutputModuleInfo2(&lt;span style="COLOR: blue"&gt;void&lt;/span&gt;)&lt;O:P&gt;&lt;/O:P&gt;
        &lt;/span&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; mso-layout-grid-align: none"&gt;
        &lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: Tahoma"&gt;{&lt;O:P&gt;&lt;/O:P&gt;
        &lt;/span&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; mso-layout-grid-align: none"&gt;
        &lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: Tahoma"&gt;&lt;span style="mso-tab-count: 1"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/span&gt;printf("Enetering
        Dll2::OutputModuleInfo2\r\n");&lt;O:P&gt;&lt;/O:P&gt;
        &lt;/span&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; mso-layout-grid-align: none"&gt;
        &lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: Tahoma"&gt;&lt;span style="mso-tab-count: 1"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/span&gt;printf("Name:
        %s\r\nHandle 0x%x\r\n", g_tclpszFileName, g_Module );&lt;O:P&gt;&lt;/O:P&gt;
        &lt;/span&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; mso-layout-grid-align: none"&gt;
        &lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: Tahoma"&gt;}&lt;O:P&gt;&lt;/O:P&gt;
        &lt;/span&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; mso-layout-grid-align: none"&gt;
        &lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: Tahoma"&gt;
        &lt;O:P&gt;&amp;#160;&lt;/O:P&gt;
        &lt;/span&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; TEXT-INDENT: 0.5in"&gt;
        &lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: Tahoma"&gt;
        &lt;O:P&gt;&amp;#160;&lt;/O:P&gt;
        &lt;/span&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; mso-layout-grid-align: none"&gt;
        &lt;span style="FONT-SIZE: 8pt; COLOR: green; FONT-FAMILY: Tahoma"&gt;///////////////////////////////////////////////////////////////////// 
        &lt;O:P&gt;&lt;/O:P&gt;
        &lt;/span&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; mso-layout-grid-align: none"&gt;
        &lt;span style="FONT-SIZE: 8pt; COLOR: green; FONT-FAMILY: Tahoma"&gt;// Dll1.cpp &lt;b style="mso-bidi-font-weight: normal"&gt;-
        NEVER do this&lt;/b&gt; 
        &lt;O:P&gt;&lt;/O:P&gt;
        &lt;/span&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; mso-layout-grid-align: none"&gt;
        &lt;span style="FONT-SIZE: 8pt; COLOR: green; FONT-FAMILY: Tahoma"&gt;/////////////////////////////////////////////////////////////////////&lt;O:P&gt;&lt;/O:P&gt;
        &lt;/span&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; mso-layout-grid-align: none"&gt;
        &lt;span style="FONT-SIZE: 8pt; COLOR: blue; FONT-FAMILY: Tahoma"&gt;typedef&lt;/span&gt;&lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: Tahoma"&gt; &lt;span style="COLOR: blue"&gt;void&lt;/span&gt; (WINAPIV
        *LPFOUTPUTMODULEINFOFUNC) (&lt;span style="COLOR: blue"&gt;void&lt;/span&gt;);&lt;O:P&gt;&lt;/O:P&gt;
        &lt;/span&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; mso-layout-grid-align: none"&gt;
        &lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: Tahoma"&gt;HMODULE g_Module;&lt;O:P&gt;&lt;/O:P&gt;
        &lt;/span&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; mso-layout-grid-align: none"&gt;
        &lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: Tahoma"&gt;TCHAR g_tclpszFileName[256];&lt;O:P&gt;&lt;/O:P&gt;
        &lt;/span&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; mso-layout-grid-align: none"&gt;
        &lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: Tahoma"&gt;BOOL APIENTRY DllMain( HINSTANCE
        hModule, 
        &lt;O:P&gt;&lt;/O:P&gt;
        &lt;/span&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; mso-layout-grid-align: none"&gt;
        &lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: Tahoma"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/span&gt;DWORD&lt;span style="mso-spacerun: yes"&gt;&amp;#160; &lt;/span&gt;ul_reason_for_call, 
        &lt;O:P&gt;&lt;/O:P&gt;
        &lt;/span&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; mso-layout-grid-align: none"&gt;
        &lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: Tahoma"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/span&gt;LPVOID
        lpReserved&lt;O:P&gt;&lt;/O:P&gt;
        &lt;/span&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; mso-layout-grid-align: none"&gt;
        &lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: Tahoma"&gt;&lt;span style="mso-tab-count: 5"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/span&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;#160;&lt;/span&gt;)&lt;O:P&gt;&lt;/O:P&gt;
        &lt;/span&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; mso-layout-grid-align: none"&gt;
        &lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: Tahoma"&gt;{&lt;O:P&gt;&lt;/O:P&gt;
        &lt;/span&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; mso-layout-grid-align: none"&gt;
        &lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: Tahoma"&gt;&lt;span style="mso-tab-count: 1"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/span&gt;&lt;span style="COLOR: blue"&gt;if&lt;/span&gt; (
        DLL_PROCESS_ATTACH == ul_reason_for_call )&lt;O:P&gt;&lt;/O:P&gt;
        &lt;/span&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; mso-layout-grid-align: none"&gt;
        &lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: Tahoma"&gt;&lt;span style="mso-tab-count: 1"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/span&gt;{&lt;O:P&gt;&lt;/O:P&gt;
        &lt;/span&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; mso-layout-grid-align: none"&gt;
        &lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: Tahoma"&gt;&lt;span style="mso-tab-count: 2"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/span&gt;printf("Dll1:DllMain\r\n");&lt;O:P&gt;&lt;/O:P&gt;
        &lt;/span&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 1in; TEXT-INDENT: 0.5in; mso-layout-grid-align: none"&gt;
        &lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: Tahoma"&gt;g_Module = hModule;&lt;O:P&gt;&lt;/O:P&gt;
        &lt;/span&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; mso-layout-grid-align: none"&gt;
        &lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: Tahoma"&gt;&lt;span style="mso-tab-count: 2"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/span&gt;::GetModuleFileName(
        g_Module, g_tclpszFileName, 255 );&lt;O:P&gt;&lt;/O:P&gt;
        &lt;/span&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; mso-layout-grid-align: none"&gt;
        &lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: Tahoma"&gt;&lt;span style="mso-tab-count: 2"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/span&gt;
        &lt;O:P&gt;&lt;/O:P&gt;
        &lt;/span&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; mso-layout-grid-align: none"&gt;
        &lt;b style="mso-bidi-font-weight: normal"&gt;&lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: Tahoma"&gt;&lt;span style="mso-tab-count: 2"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/span&gt;&lt;span style="COLOR: green"&gt;//
        Load Dll2 - never EVER do this&lt;O:P&gt;&lt;/O:P&gt;
        &lt;/span&gt;&lt;/span&gt;&lt;/b&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; mso-layout-grid-align: none"&gt;
        &lt;b style="mso-bidi-font-weight: normal"&gt;&lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: Tahoma"&gt;&lt;span style="mso-tab-count: 2"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/span&gt;HMODULE
        hModule1 = ::LoadLibrary("Dll2.dll");&lt;O:P&gt;&lt;/O:P&gt;
        &lt;/span&gt;&lt;/b&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; mso-layout-grid-align: none"&gt;
        &lt;b style="mso-bidi-font-weight: normal"&gt;&lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: Tahoma"&gt;&lt;span style="mso-tab-count: 2"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/span&gt;LPFOUTPUTMODULEINFOFUNC
        lpOutputModuleInfo1Func = (LPFOUTPUTMODULEINFOFUNC)::GetProcAddress( hModule1,"OutputModuleInfo2");&lt;O:P&gt;&lt;/O:P&gt;
        &lt;/span&gt;&lt;/b&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; mso-layout-grid-align: none"&gt;
        &lt;b style="mso-bidi-font-weight: normal"&gt;&lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: Tahoma"&gt;&lt;span style="mso-tab-count: 2"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/span&gt;lpOutputModuleInfo1Func();&lt;O:P&gt;&lt;/O:P&gt;
        &lt;/span&gt;&lt;/b&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; mso-layout-grid-align: none"&gt;
        &lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: Tahoma"&gt;
        &lt;O:P&gt;&amp;#160;&lt;/O:P&gt;
        &lt;/span&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; mso-layout-grid-align: none"&gt;
        &lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: Tahoma"&gt;&lt;span style="mso-tab-count: 1"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/span&gt;}&lt;O:P&gt;&lt;/O:P&gt;
        &lt;/span&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; mso-layout-grid-align: none"&gt;
        &lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: Tahoma"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;/span&gt;&lt;span style="mso-tab-count: 1"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/span&gt;&lt;span style="COLOR: blue"&gt;return&lt;/span&gt; TRUE;&lt;O:P&gt;&lt;/O:P&gt;
        &lt;/span&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; mso-layout-grid-align: none"&gt;
        &lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: Tahoma"&gt;}&lt;O:P&gt;&lt;/O:P&gt;
        &lt;/span&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; mso-layout-grid-align: none"&gt;
        &lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: Tahoma"&gt;
        &lt;O:P&gt;&amp;#160;&lt;/O:P&gt;
        &lt;/span&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; mso-layout-grid-align: none"&gt;
        &lt;span style="FONT-SIZE: 8pt; COLOR: blue; FONT-FAMILY: Tahoma"&gt;extern&lt;/span&gt;&lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: Tahoma"&gt; "C" &lt;span style="COLOR: blue"&gt;__declspec&lt;/span&gt;(&lt;span style="COLOR: blue"&gt;dllexport&lt;/span&gt;)&lt;span style="mso-spacerun: yes"&gt;&amp;#160; &lt;/span&gt;&lt;span style="COLOR: blue"&gt;void&lt;/span&gt; WINAPIV
        OutputModuleInfo1(&lt;span style="COLOR: blue"&gt;void&lt;/span&gt;)&lt;O:P&gt;&lt;/O:P&gt;
        &lt;/span&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; mso-layout-grid-align: none"&gt;
        &lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: Tahoma"&gt;{&lt;O:P&gt;&lt;/O:P&gt;
        &lt;/span&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; mso-layout-grid-align: none"&gt;
        &lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: Tahoma"&gt;&lt;span style="mso-tab-count: 1"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/span&gt;printf("Enetering
        Dll1::OutputModuleInfo1\r\n");&lt;O:P&gt;&lt;/O:P&gt;
        &lt;/span&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; mso-layout-grid-align: none"&gt;
        &lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: Tahoma"&gt;&lt;span style="mso-tab-count: 1"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/span&gt;printf("Name:
        %s\r\nHandle 0x%x\r\n", g_tclpszFileName, g_Module );&lt;O:P&gt;&lt;/O:P&gt;
        &lt;/span&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; mso-layout-grid-align: none"&gt;
        &lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: Tahoma"&gt;}&lt;O:P&gt;&lt;/O:P&gt;
        &lt;/span&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; mso-layout-grid-align: none"&gt;
        &lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: Tahoma"&gt;
        &lt;O:P&gt;&amp;#160;&lt;/O:P&gt;
        &lt;/span&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; mso-layout-grid-align: none"&gt;
        &lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: Tahoma"&gt;
        &lt;O:P&gt;&amp;#160;&lt;/O:P&gt;
        &lt;/span&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; mso-layout-grid-align: none"&gt;
        &lt;span style="FONT-SIZE: 8pt; COLOR: green; FONT-FAMILY: Tahoma"&gt;/////////////////////////////////////////////////////////////////////&lt;O:P&gt;&lt;/O:P&gt;
        &lt;/span&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; mso-layout-grid-align: none"&gt;
        &lt;span style="FONT-SIZE: 8pt; COLOR: green; FONT-FAMILY: Tahoma"&gt;// Main.cpp 
        &lt;O:P&gt;&lt;/O:P&gt;
        &lt;/span&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; mso-layout-grid-align: none"&gt;
        &lt;span style="FONT-SIZE: 8pt; COLOR: green; FONT-FAMILY: Tahoma"&gt;/////////////////////////////////////////////////////////////////////&lt;O:P&gt;&lt;/O:P&gt;
        &lt;/span&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; mso-layout-grid-align: none"&gt;
        &lt;span style="FONT-SIZE: 8pt; COLOR: blue; FONT-FAMILY: Tahoma"&gt;extern&lt;/span&gt;&lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: Tahoma"&gt; "C" &lt;span style="COLOR: blue"&gt;__declspec&lt;/span&gt;(&lt;span style="COLOR: blue"&gt;dllimport&lt;/span&gt;) &lt;span style="COLOR: blue"&gt;void&lt;/span&gt; WINAPIV
        OutputModuleInfo1(&lt;span style="COLOR: blue"&gt;void&lt;/span&gt;);&lt;O:P&gt;&lt;/O:P&gt;
        &lt;/span&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; mso-layout-grid-align: none"&gt;
        &lt;span style="FONT-SIZE: 8pt; COLOR: blue; FONT-FAMILY: Tahoma"&gt;extern&lt;/span&gt;&lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: Tahoma"&gt; "C" &lt;span style="COLOR: blue"&gt;__declspec&lt;/span&gt;(&lt;span style="COLOR: blue"&gt;dllimport&lt;/span&gt;) &lt;span style="COLOR: blue"&gt;void&lt;/span&gt; WINAPIV
        OutputModuleInfo2(&lt;span style="COLOR: blue"&gt;void&lt;/span&gt;);&lt;O:P&gt;&lt;/O:P&gt;
        &lt;/span&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; mso-layout-grid-align: none"&gt;
        &lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: Tahoma"&gt;
        &lt;O:P&gt;&amp;#160;&lt;/O:P&gt;
        &lt;/span&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; mso-layout-grid-align: none"&gt;
        &lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: Tahoma"&gt;
        &lt;O:P&gt;&amp;#160;&lt;/O:P&gt;
        &lt;/span&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; mso-layout-grid-align: none"&gt;
        &lt;span style="FONT-SIZE: 8pt; COLOR: blue; FONT-FAMILY: Tahoma"&gt;int&lt;/span&gt;&lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: Tahoma"&gt; _tmain(&lt;span style="COLOR: blue"&gt;int&lt;/span&gt; argc,
        _TCHAR* argv[])&lt;O:P&gt;&lt;/O:P&gt;
        &lt;/span&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; mso-layout-grid-align: none"&gt;
        &lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: Tahoma"&gt;{&lt;O:P&gt;&lt;/O:P&gt;
        &lt;/span&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; mso-layout-grid-align: none"&gt;
        &lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: Tahoma"&gt;&lt;span style="mso-tab-count: 1"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/span&gt;OutputModuleInfo1();&lt;O:P&gt;&lt;/O:P&gt;
        &lt;/span&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; mso-layout-grid-align: none"&gt;
        &lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: Tahoma"&gt;&lt;span style="mso-tab-count: 1"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/span&gt;OutputModuleInfo2();&lt;O:P&gt;&lt;/O:P&gt;
        &lt;/span&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; mso-layout-grid-align: none"&gt;
        &lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: Tahoma"&gt;&lt;span style="mso-tab-count: 1"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/span&gt;&lt;span style="COLOR: blue"&gt;return&lt;/span&gt; 0;&lt;O:P&gt;&lt;/O:P&gt;
        &lt;/span&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; mso-layout-grid-align: none"&gt;
        &lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: Tahoma"&gt;}&lt;O:P&gt;&lt;/O:P&gt;
        &lt;/span&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"&gt;
        &lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt;
        &lt;O:P&gt;&amp;#160;&lt;/O:P&gt;
        &lt;/span&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-INDENT: 0.25in"&gt;
        &lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma"&gt;What's wrong with this? Let me
        count the ways. On top of non-existent error-handling and the fact that we have an
        un-paired LoadLibrary() call, this code has a very fundamental problem. &lt;span style="mso-spacerun: yes"&gt;&amp;#160;&lt;/span&gt;Let's
        just say that depending on how this code is compiled, it may 
        &lt;O:P&gt;&lt;/O:P&gt;
        &lt;/span&gt;
    &lt;/p&gt;
    &lt;ul style="MARGIN-TOP: 0in" type="disc"&gt;
        &lt;li class="MsoNormal" style="MARGIN: 0in 0in 0pt; mso-list: l1 level1 lfo1; tab-stops: list .5in"&gt;
            &lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma"&gt;Run and produce results you expect 
            &lt;O:P&gt;&lt;/O:P&gt;
            &lt;/span&gt; 
        &lt;/li&gt;
        &lt;li class="MsoNormal" style="MARGIN: 0in 0in 0pt; mso-list: l1 level1 lfo1; tab-stops: list .5in"&gt;
            &lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma"&gt;Run and produce results you don't
            expect&lt;O:P&gt;&lt;/O:P&gt;
            &lt;/span&gt; 
        &lt;/li&gt;
        &lt;li class="MsoNormal" style="MARGIN: 0in 0in 0pt; mso-list: l1 level1 lfo1; tab-stops: list .5in"&gt;
            &lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma"&gt;Blow up with AV&lt;O:P&gt;&lt;/O:P&gt;
            &lt;/span&gt;
        &lt;/li&gt;
    &lt;/ul&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.25in"&gt;
        &lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma"&gt;
        &lt;O:P&gt;&amp;#160;&lt;/O:P&gt;
        &lt;/span&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.25in"&gt;
        &lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma"&gt;That's right. 
        &lt;O:P&gt;&lt;/O:P&gt;
        &lt;/span&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.25in"&gt;
        &lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma"&gt;Let's dig into it.&lt;O:P&gt;&lt;/O:P&gt;
        &lt;/span&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.25in"&gt;
        &lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma"&gt;
        &lt;O:P&gt;&amp;#160;&lt;/O:P&gt;
        &lt;/span&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-INDENT: 0.25in"&gt;
        &lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma"&gt;First let's see what this code
        is supposed to do in the first place. You'll have to bear with me here - it's after
        midnight and I haven't been able to come up with something brilliantly meaningful,
        but this will just have to do for now.&lt;O:P&gt;&lt;/O:P&gt;
        &lt;/span&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-INDENT: 0.25in"&gt;
        &lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma"&gt;As you can see, we are dealing
        with two DLLs and one EXE that uses those DLLs. First DLL (inventively called Dll2)
        - gets its own HMODULE in DLLMain(DLL_PROCESS_ATTACH), gets its name based on that
        and stores them away in global variables. Exported function OutputModuleInfo2 simply
        prints that out using printf.&lt;O:P&gt;&lt;/O:P&gt;
        &lt;/span&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-INDENT: 0.25in"&gt;
        &lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma"&gt;Dll1 is almost identical, except
        it dynamically calls into Dll2 right after collecting its own information. It's a
        little weird, but this is just a primitive example after all.&lt;O:P&gt;&lt;/O:P&gt;
        &lt;/span&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-INDENT: 0.25in"&gt;
        &lt;ST1:PLACE w:st="on"&gt;
            &lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma"&gt;Main&lt;/span&gt; 
        &lt;/ST1:PLACE&gt;
        &lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma"&gt;is a console appliccation that
        is statically bruit against export libraries produced by the build of the first two
        DLLs and calls both OutputModuleInfo1 and OutputModuleInfo2. 
        &lt;O:P&gt;&lt;/O:P&gt;
        &lt;/span&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-INDENT: 0.25in"&gt;
        &lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma"&gt;Simple enough? Let's roll.&lt;O:P&gt;&lt;/O:P&gt;
        &lt;/span&gt;
    &lt;/p&gt;
    &lt;ol style="MARGIN-TOP: 0in" type="1" start="1"&gt;
        &lt;li class="MsoNormal" style="MARGIN: 0in 0in 0pt; mso-list: l0 level1 lfo2; tab-stops: list .5in"&gt;
            &lt;b style="mso-bidi-font-weight: normal"&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma"&gt;It
            works! It works!&lt;/span&gt;&lt;/b&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma"&gt; Let's
            compile everything, but make sure that all three binaries use CRT(C/C++ runtime) dynamically
            (/MD compiler option) and that Dll2.lib appears &lt;u&gt;before&lt;/u&gt; Dll1.lib in linker options
            pertaining to additional input libraries for our console app (something like &lt;i style="mso-bidi-font-style: normal"&gt;link.exe
            main.obj /out:main.exe dll2.lib dll1.lib&lt;/i&gt;). Turns out that is important - we'll
            see why in a little bit. When you run the application, it outputs:&lt;br style="mso-special-character: line-break" /&gt;
            &lt;br style="mso-special-character: line-break" /&gt;
            &lt;O:P&gt;&lt;/O:P&gt;
            &lt;/span&gt;
        &lt;/li&gt;
    &lt;/ol&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in"&gt;
        &lt;i style="mso-bidi-font-style: normal"&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma"&gt;Dll2:DllMain&lt;O:P&gt;&lt;/O:P&gt;
        &lt;/span&gt;&lt;/i&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in"&gt;
        &lt;i style="mso-bidi-font-style: normal"&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma"&gt;Dll1:DllMain&lt;O:P&gt;&lt;/O:P&gt;
        &lt;/span&gt;&lt;/i&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in"&gt;
        &lt;i style="mso-bidi-font-style: normal"&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma"&gt;Enetering
        Dll2::OutputModuleInfo2&lt;O:P&gt;&lt;/O:P&gt;
        &lt;/span&gt;&lt;/i&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in"&gt;
        &lt;i style="mso-bidi-font-style: normal"&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma"&gt;Name:
        c:\Temp\KillDllMain\Dll2.dll&lt;O:P&gt;&lt;/O:P&gt;
        &lt;/span&gt;&lt;/i&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in"&gt;
        &lt;i style="mso-bidi-font-style: normal"&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma"&gt;Handle
        0x10000000&lt;O:P&gt;&lt;/O:P&gt;
        &lt;/span&gt;&lt;/i&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in"&gt;
        &lt;i style="mso-bidi-font-style: normal"&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma"&gt;Enetering
        Dll1::OutputModuleInfo1&lt;O:P&gt;&lt;/O:P&gt;
        &lt;/span&gt;&lt;/i&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in"&gt;
        &lt;i style="mso-bidi-font-style: normal"&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma"&gt;Name:
        c:\Temp\KillDllMain\Dll1.dll&lt;O:P&gt;&lt;/O:P&gt;
        &lt;/span&gt;&lt;/i&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in"&gt;
        &lt;i style="mso-bidi-font-style: normal"&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma"&gt;Handle
        0x320000&lt;O:P&gt;&lt;/O:P&gt;
        &lt;/span&gt;&lt;/i&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in"&gt;
        &lt;i style="mso-bidi-font-style: normal"&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma"&gt;Enetering
        Dll2::OutputModuleInfo2&lt;O:P&gt;&lt;/O:P&gt;
        &lt;/span&gt;&lt;/i&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in"&gt;
        &lt;i style="mso-bidi-font-style: normal"&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma"&gt;Name:
        c:\Temp\KillDllMain\Dll2.dll&lt;O:P&gt;&lt;/O:P&gt;
        &lt;/span&gt;&lt;/i&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in"&gt;
        &lt;i style="mso-bidi-font-style: normal"&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma"&gt;Handle
        0x10000000&lt;O:P&gt;&lt;/O:P&gt;
        &lt;/span&gt;&lt;/i&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in"&gt;
        &lt;i style="mso-bidi-font-style: normal"&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma"&gt;
        &lt;O:P&gt;&amp;#160;&lt;/O:P&gt;
        &lt;/span&gt;&lt;/i&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; TEXT-INDENT: 0.5in"&gt;
        &lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma"&gt;As you see, things seem to be working
        fine. One thing that is worth pointing out is that - as you can see from the output
        - DllMain for Dll2.dll was called before DllMain of Dll1.dll. Why? Well, technically,
        there's no explicit guarantee as to the order of these things - it is all in the loader's
        hands. &lt;a href="http://blogs.gotdotnet.com/olegl/PermaLink.aspx/d1610664-8d0b-4579-a20c-712516e08423"&gt;As
        I mentioned before&lt;/a&gt;, the loader looks at static dependencies and builds a list
        of DllMains to be called based on that. But what happens if the order really doesn't
        matter? From loader's perspective, Main.exe depends on Dll1 and Dll2 and there's no
        reason to choose one over the other (remember, the fact that Dll1 does in fact load
        Dll2 is our little dirty secret). 
        &lt;br /&gt;
        &lt;span style="mso-tab-count: 1"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/span&gt;Well,
        turns out that the loader seems to be preserving the order in which the imported DLLs
        are listed in the Imports Section of the loading executable. You can read all about
        the low-level details in &lt;a href="http://msdn.microsoft.com/msdnmag/issues/02/03/PE2/default.aspx"&gt;Matt
        Pietrek's article&lt;/a&gt;, but for the purpose of this discussion let's just say that
        each PE file (EXE or DLL) knows what binaries it "references" - that is what external
        functions it imports - and that a list of those binaries, together with referenced
        functions is linked into its PE header. Microsoft Linker seems to build that header
        based on the order in which export libraries are supplied, which is why we built our
        app the way we did. 
        &lt;O:P&gt;&lt;/O:P&gt;
        &lt;/span&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt"&gt;
        &lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma"&gt;&lt;span style="mso-tab-count: 2"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/span&gt;What
        happens if change that? Let's see.&lt;O:P&gt;&lt;/O:P&gt;
        &lt;/span&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-INDENT: 0.25in"&gt;
        &lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma"&gt;
        &lt;O:P&gt;&amp;#160;&lt;/O:P&gt;
        &lt;/span&gt;
    &lt;/p&gt;
    &lt;ol style="MARGIN-TOP: 0in" type="1" start="2"&gt;
        &lt;li class="MsoNormal" style="MARGIN: 0in 0in 0pt; mso-list: l0 level1 lfo2; tab-stops: list .5in"&gt;
            &lt;b style="mso-bidi-font-weight: normal"&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma"&gt;Huh?
            But...&lt;/span&gt;&lt;/b&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma"&gt; So now let's
            build the same code, only this time supply export libraries in the opposite order
            (something like &lt;i style="mso-bidi-font-style: normal"&gt;link.exe main.obj /out:main.exe
            dll1.lib dll2.lib).&lt;/i&gt; Let's run it:&lt;br style="mso-special-character: line-break" /&gt;
            &lt;br style="mso-special-character: line-break" /&gt;
            &lt;O:P&gt;&lt;/O:P&gt;
            &lt;/span&gt;
        &lt;/li&gt;
    &lt;/ol&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in"&gt;
        &lt;i style="mso-bidi-font-style: normal"&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma"&gt;Dll1:DllMain&lt;O:P&gt;&lt;/O:P&gt;
        &lt;/span&gt;&lt;/i&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in"&gt;
        &lt;i style="mso-bidi-font-style: normal"&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma"&gt;Enetering
        Dll2::OutputModuleInfo2&lt;O:P&gt;&lt;/O:P&gt;
        &lt;/span&gt;&lt;/i&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in"&gt;
        &lt;b style="mso-bidi-font-weight: normal"&gt;&lt;i style="mso-bidi-font-style: normal"&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma"&gt;Name:&lt;O:P&gt;&lt;/O:P&gt;
        &lt;/span&gt;&lt;/i&gt;&lt;/b&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in"&gt;
        &lt;b style="mso-bidi-font-weight: normal"&gt;&lt;i style="mso-bidi-font-style: normal"&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma"&gt;Handle
        0x0&lt;O:P&gt;&lt;/O:P&gt;
        &lt;/span&gt;&lt;/i&gt;&lt;/b&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in"&gt;
        &lt;i style="mso-bidi-font-style: normal"&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma"&gt;Dll2:DllMain&lt;O:P&gt;&lt;/O:P&gt;
        &lt;/span&gt;&lt;/i&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in"&gt;
        &lt;i style="mso-bidi-font-style: normal"&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma"&gt;Enetering
        Dll1::OutputModuleInfo1&lt;O:P&gt;&lt;/O:P&gt;
        &lt;/span&gt;&lt;/i&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in"&gt;
        &lt;i style="mso-bidi-font-style: normal"&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma"&gt;Name:
        c:\Temp\KillDllMain\Dll1.dll&lt;O:P&gt;&lt;/O:P&gt;
        &lt;/span&gt;&lt;/i&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in"&gt;
        &lt;i style="mso-bidi-font-style: normal"&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma"&gt;Handle
        0x10000000&lt;O:P&gt;&lt;/O:P&gt;
        &lt;/span&gt;&lt;/i&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in"&gt;
        &lt;i style="mso-bidi-font-style: normal"&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma"&gt;Enetering
        Dll2::OutputModuleInfo2&lt;O:P&gt;&lt;/O:P&gt;
        &lt;/span&gt;&lt;/i&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in"&gt;
        &lt;i style="mso-bidi-font-style: normal"&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma"&gt;Name:
        c:\Temp\KillDllMain\Dll2.dll&lt;O:P&gt;&lt;/O:P&gt;
        &lt;/span&gt;&lt;/i&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in"&gt;
        &lt;i style="mso-bidi-font-style: normal"&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma"&gt;Handle
        0x320000&lt;O:P&gt;&lt;/O:P&gt;
        &lt;/span&gt;&lt;/i&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in"&gt;
        &lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma"&gt;
        &lt;O:P&gt;&amp;#160;&lt;/O:P&gt;
        &lt;/span&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; TEXT-INDENT: 0.5in"&gt;
        &lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma"&gt;Interesting... As you see, this
        time DllMain from Dll1 got called first. That loaded Dll2 and its OutputModuleInfo2
        got called ... before its DllMain! No wonder it printed what it did. Note that the
        second call into OutputModuleInfo2 went through just fine because Dll2's DllMain was
        called already. 
        &lt;O:P&gt;&lt;/O:P&gt;
        &lt;/span&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; TEXT-INDENT: 0.5in"&gt;
        &lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma"&gt;So why in the world is OS loader
        acting so dumb? Doesn't it know we are loading Dll2? We have explicitly called LoadLibrary
        after all, which loaded it from disk, laid it out in memory, resolved its exports
        etc. Why wasn't DllMain called? If you experiment a little, you will find out that
        in most cases DllMain of dynamically loaded libraries &lt;b style="mso-bidi-font-weight: normal"&gt;will&lt;/b&gt; be
        called, even if the "illegal" LoadLibrary is used to load it. The only case that will
        not take place is when OS loader already "knows" about that DLL but hasn't yet called
        DllMain on it, which is exactly what happened here. 
        &lt;O:P&gt;&lt;/O:P&gt;
        &lt;/span&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; TEXT-INDENT: 0.5in"&gt;
        &lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma"&gt;Main.exe &lt;b style="mso-bidi-font-weight: normal"&gt;statically&lt;/b&gt; depends
        on Dll2.dll, so it's already in the loader's plan. It turns out, the loader is not
        so willing to change its original plan created based on static dependencies. If new
        binaries get thrown in, the loader will stop and dutifully load them; but if the binary
        is in fact the "old" one - that is already in the plan - the loader will just skip
        it. 
        &lt;O:P&gt;&lt;/O:P&gt;
        &lt;/span&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; TEXT-INDENT: 0.5in"&gt;
        &lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma"&gt;Why? My guess is that this works
        pretty well for most scenarios. The loader is still trying to be nice and compensate
        for our bad behavior. Once we attempt to load something it already knows about, it
        simply preserves its current plan - I suspect doing otherwise would cause all kinds
        of nasty consequences.&lt;span style="mso-spacerun: yes"&gt;&amp;#160; &lt;/span&gt;Mind you, we are
        on no position to complain - we are not supposed to call LoadLibrary from DllMain
        in the first place. Keep in mind, these are my speculations - I'm not trying to give
        a precise recipe as to how the OS loader can be mistreated, I'm just saying that &lt;b style="mso-bidi-font-weight: normal"&gt;it
        can be done&lt;/b&gt;. 
        &lt;O:P&gt;&lt;/O:P&gt;
        &lt;/span&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; TEXT-INDENT: 0.5in"&gt;
        &lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma"&gt;So...there you go. In this particular
        situation you "just" got the wrong value printed out, but you can imagine that this
        can easily cause a wide range of nastiness - AVs for instance. Speaking of which...&lt;O:P&gt;&lt;/O:P&gt;
        &lt;/span&gt;
    &lt;/p&gt;
    &lt;ol style="MARGIN-TOP: 0in" type="1" start="3"&gt;
        &lt;li class="MsoNormal" style="MARGIN: 0in 0in 0pt; mso-list: l0 level1 lfo2; tab-stops: list .5in"&gt;
            &lt;b style="mso-bidi-font-weight: normal"&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma"&gt;What???
            How did that happen?...&lt;/span&gt;&lt;/b&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma"&gt; Let's
            build the whole thing again, only this time let's use static CRT (/MT or /ML compiler
            options). Why should it matter, right? 
            &lt;br /&gt;
            Now let's run it:&lt;br /&gt;
            &lt;br /&gt;
            &lt;i style="mso-bidi-font-style: normal"&gt;Dll1:DllMain&lt;br style="mso-special-character: line-break" /&gt;
            &lt;br style="mso-special-character: line-break" /&gt;
            &lt;/i&gt;
            &lt;O:P&gt;&lt;/O:P&gt;
            &lt;/span&gt;
        &lt;/li&gt;
    &lt;/ol&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in"&gt;
        &lt;b style="mso-bidi-font-weight: normal"&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma"&gt;.&lt;/span&gt;&lt;/b&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma"&gt;..
        and then... whoa... 
        &lt;O:P&gt;&lt;/O:P&gt;
        &lt;/span&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in"&gt;
        &lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma"&gt;
        &lt;O:P&gt;&amp;#160;&lt;/O:P&gt;
        &lt;/span&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in"&gt;
        &lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: Tahoma"&gt;First-chance exception at 0x77f57bd2
        (ntdll.dll) in MainApp.exe: 0xC0000005: Access violation reading location 0x00000010.&lt;O:P&gt;&lt;/O:P&gt;
        &lt;/span&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in"&gt;
        &lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma"&gt;
        &lt;O:P&gt;&amp;#160;&lt;/O:P&gt;
        &lt;/span&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.25in"&gt;
        &lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma"&gt;&lt;span style="mso-tab-count: 1"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/span&gt;But
        why? If you look at the stack, you will see the following:&lt;O:P&gt;&lt;/O:P&gt;
        &lt;/span&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.25in"&gt;
        &lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma"&gt;
        &lt;O:P&gt;&amp;#160;&lt;/O:P&gt;
        &lt;/span&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.25in"&gt;
        &lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma"&gt;&lt;span style="mso-tab-count: 1"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/span&gt;&lt;/span&gt;&lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: Tahoma"&gt;ntdll.dll!_RtlAllocateHeap@12()&lt;span style="mso-spacerun: yes"&gt;&amp;#160; &lt;/span&gt;+
        0x24&lt;span style="mso-tab-count: 1"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/span&gt;
        &lt;O:P&gt;&lt;/O:P&gt;
        &lt;/span&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.25in"&gt;
        &lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: Tahoma"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;#160;&lt;/span&gt;&lt;span style="mso-tab-count: 1"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/span&gt;Dll2.dll!_heap_alloc(unsigned
        int size=0x00000018)&lt;span style="mso-spacerun: yes"&gt;&amp;#160; &lt;/span&gt;Line 212&lt;span style="mso-tab-count: 1"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/span&gt;C&lt;O:P&gt;&lt;/O:P&gt;
        &lt;/span&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.25in"&gt;
        &lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: Tahoma"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;#160;&lt;/span&gt;&lt;span style="mso-tab-count: 1"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/span&gt;Dll2.dll!_nh_malloc(unsigned
        int size=0x00000018, int nhFlag=0x00000000)&lt;span style="mso-spacerun: yes"&gt;&amp;#160; &lt;/span&gt;Line
        113&lt;span style="mso-tab-count: 1"&gt; &lt;/span&gt;C&lt;O:P&gt;&lt;/O:P&gt;
        &lt;/span&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.25in"&gt;
        &lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: Tahoma"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;#160;&lt;/span&gt;&lt;span style="mso-tab-count: 1"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/span&gt;Dll2.dll!malloc(unsigned
        int size=0x00000018)&lt;span style="mso-spacerun: yes"&gt;&amp;#160; &lt;/span&gt;Line 54 + 0xf&lt;span style="mso-tab-count: 1"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/span&gt;C&lt;O:P&gt;&lt;/O:P&gt;
        &lt;/span&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.25in"&gt;
        &lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: Tahoma"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;#160;&lt;/span&gt;&lt;span style="mso-tab-count: 1"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/span&gt;Dll2.dll!_mtinitlocknum(int
        locknum=0x00000011)&lt;span style="mso-spacerun: yes"&gt;&amp;#160; &lt;/span&gt;Line 251 + 0x7&lt;span style="mso-tab-count: 1"&gt;&amp;#160; &lt;/span&gt;C&lt;O:P&gt;&lt;/O:P&gt;
        &lt;/span&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.25in"&gt;
        &lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: Tahoma"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;#160;&lt;/span&gt;&lt;span style="mso-tab-count: 1"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/span&gt;Dll2.dll!_lock(int
        locknum=0x00000011)&lt;span style="mso-spacerun: yes"&gt;&amp;#160; &lt;/span&gt;Line 311 + 0x6&lt;span style="mso-tab-count: 1"&gt;&amp;#160;&amp;#160; &lt;/span&gt;C&lt;O:P&gt;&lt;/O:P&gt;
        &lt;/span&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.25in"&gt;
        &lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: Tahoma"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;#160;&lt;/span&gt;&lt;span style="mso-tab-count: 1"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/span&gt;Dll2.dll!_lock_file2(int
        i=0x00000001, void * s=0x00346b68)&lt;span style="mso-spacerun: yes"&gt;&amp;#160; &lt;/span&gt;Line
        267 + 0x9&lt;span style="mso-tab-count: 1"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/span&gt;C&lt;O:P&gt;&lt;/O:P&gt;
        &lt;/span&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.25in"&gt;
        &lt;b style="mso-bidi-font-weight: normal"&gt;&lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: Tahoma"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;#160;&lt;/span&gt;&lt;span style="mso-tab-count: 1"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/span&gt;Dll2.dll!printf(const
        char * format=0x0034204c, ...)&lt;span style="mso-spacerun: yes"&gt;&amp;#160; &lt;/span&gt;Line 57
        + 0xd&lt;span style="mso-tab-count: 1"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/span&gt;C&lt;O:P&gt;&lt;/O:P&gt;
        &lt;/span&gt;&lt;/b&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.25in"&gt;
        &lt;b style="mso-bidi-font-weight: normal"&gt;&lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: Tahoma"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;#160;&lt;/span&gt;&lt;span style="mso-tab-count: 1"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/span&gt;Dll2.dll!OutputModuleInfo2()&lt;span style="mso-spacerun: yes"&gt;&amp;#160; &lt;/span&gt;Line
        30 + 0xa&lt;span style="mso-tab-count: 1"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/span&gt;C++&lt;O:P&gt;&lt;/O:P&gt;
        &lt;/span&gt;&lt;/b&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.25in"&gt;
        &lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: Tahoma"&gt;&amp;gt;&lt;span style="mso-tab-count: 1"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/span&gt;Dll1.dll!DllMain(HINSTANCE__
        * hModule=0x10000000, unsigned long ul_reason_for_call=0x00000001, void * lpReserved=0x0012fd30)&lt;span style="mso-spacerun: yes"&gt;&amp;#160; &lt;/span&gt;Line
        30 + 0x5&lt;span style="mso-tab-count: 1"&gt;&amp;#160; &lt;/span&gt;C++&lt;O:P&gt;&lt;/O:P&gt;
        &lt;/span&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.25in"&gt;
        &lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: Tahoma"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;#160;&lt;/span&gt;&lt;span style="mso-tab-count: 1"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/span&gt;Dll1.dll!_DllMainCRTStartup(void
        * hDllHandle=0x10000000, unsigned long dwReason=0x00000001, void * lpreserved=0x0012fd30)&lt;span style="mso-spacerun: yes"&gt;&amp;#160; &lt;/span&gt;Line
        297 + 0xd&lt;span style="mso-tab-count: 1"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/span&gt;C&lt;O:P&gt;&lt;/O:P&gt;
        &lt;/span&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.25in"&gt;
        &lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: Tahoma"&gt;&lt;span style="mso-tab-count: 1"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/span&gt;
        &lt;O:P&gt;&lt;/O:P&gt;
        &lt;/span&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-INDENT: 0.25in"&gt;
        &lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma"&gt;So this is caused by calling "printf"
        from Dll2's &lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma; mso-fareast-font-family: 'Times New Roman'; mso-ansi-language: EN-US; mso-fareast-language: EN-US; mso-bidi-language: AR-SA"&gt;OuputModuleInfo2&lt;/span&gt;,
        which is sort of strange. If you look some more, you will find that the&amp;#160;CRT&amp;#160;internal
        global&amp;#160;_crtheap is NULL, which means that CRT has no heap. Why? You guessed it
        - static CRT allocates its heap in DllMain of the owning DLL! If our case DllMain
        wasn't called yet, so naturally - no heap.&lt;O:P&gt;&lt;/O:P&gt;
        &lt;/span&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-INDENT: 0.25in"&gt;
        &lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma"&gt;Ouch. (Incidentally, this means
        that just about any CRT call will AV - it's awfully difficult to do anything without
        allocating any memory...)&lt;O:P&gt;&lt;/O:P&gt;
        &lt;/span&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.25in; TEXT-INDENT: 0.25in"&gt;
        &lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma"&gt;
        &lt;O:P&gt;&amp;#160;&lt;/O:P&gt;
        &lt;/span&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.25in"&gt;
        &lt;b style="mso-bidi-font-weight: normal"&gt;&lt;span style="FONT-FAMILY: Tahoma"&gt;Moral&lt;O:P&gt;&lt;/O:P&gt;
        &lt;/span&gt;&lt;/b&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-INDENT: 0.25in"&gt;
        &lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma"&gt;OK, this is much longer than I
        intended... but here's the moral: &lt;/span&gt;&lt;b style="mso-bidi-font-weight: normal"&gt;&lt;span style="FONT-SIZE: 14pt; COLOR: red; FONT-FAMILY: Tahoma"&gt;be
        careful&lt;/span&gt;&lt;/b&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma"&gt;. OS loader is
        not dumb, and it is as forgiving as it gets, but sometimes it won't be there to help
        - simply because it has no idea what your intentions are.&lt;O:P&gt;&lt;/O:P&gt;
        &lt;/span&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-INDENT: 0.25in"&gt;
        &lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma"&gt;OK, I think I'm officially done
        with the topic - I'm feeling much better now :)&lt;/span&gt;&lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: Tahoma"&gt; 
        &lt;O:P&gt;&lt;/O:P&gt;
        &lt;/span&gt;
    &lt;/p&gt;
&lt;/body&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=56142" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/oleglv/archive/tags/Platform/default.aspx">Platform</category><category domain="http://blogs.msdn.com/oleglv/archive/tags/C_2B002B00_/default.aspx">C++</category><category domain="http://blogs.msdn.com/oleglv/archive/tags/Tidbits/default.aspx">Tidbits</category></item><item><title>DllMain and life before birth</title><link>http://blogs.msdn.com/oleglv/archive/2003/10/24/56141.aspx</link><pubDate>Sat, 25 Oct 2003 03:47:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:56141</guid><dc:creator>oleglv</dc:creator><slash:comments>5</slash:comments><comments>http://blogs.msdn.com/oleglv/comments/56141.aspx</comments><wfw:commentRss>http://blogs.msdn.com/oleglv/commentrss.aspx?PostID=56141</wfw:commentRss><description>&lt;body xmlns="http://www.w3.org/1999/xhtml"&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-INDENT: 0.5in"&gt;
        &lt;b style="mso-bidi-font-weight: normal"&gt;&lt;span style="FONT-FAMILY: Tahoma"&gt;Preface&lt;O:P&gt;&lt;/O:P&gt;
        &lt;/span&gt;&lt;/b&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-INDENT: 0.5in"&gt;
        &lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma"&gt;OS loader has always intrigued
        me - probably because it works behind the scenes and no-one normally bothers to understand
        what is that is does exactly, until strange or funny things start happening. And they
        do. And then we read through the documentation and we are forced to remember that
        there's more to loading a binary than just slapping it into process address space.
        In fact &lt;a href="http://msdn.microsoft.com/msdnmag/issues/02/02/PE/default.aspx"&gt;there's
        a wonderful article&lt;/a&gt; by Matt Pietrek that discusses those matters. I strongly encourage
        every person who deals with native code to go and read it - it may be quite enlightening
        for you - I know it was for me. When you know how things get loaded, you are less
        likely to forget to re-base your binary, consider early binary binding etc.&lt;O:P&gt;&lt;/O:P&gt;
        &lt;/span&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-INDENT: 0.5in"&gt;
        &lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma"&gt;Every now and then another piece
        of information or a great summary on the subject comes up and I find myself mystified
        with the whole loader topic all over again. This time it was &lt;a href="http://blogs.gotdotnet.com/cbrumme/permalink.aspx/dac5ba4a-f0c8-42bb-a5cf-097efb25d1a9"&gt;a
        very lengthy post&lt;/a&gt; in &lt;a href="http://blogs.gotdotnet.com/cbrumme"&gt;Chris Brumme's
        blog&lt;/a&gt;. As many people have mentioned, the post in question is very long and very
        dense with technical information well, what else did you expect from Chris's blog?
        :) &lt;span style="mso-spacerun: yes"&gt;&amp;#160;&lt;/span&gt;Anyway, in order to absorb the topic
        better and in hopes of getting the whole thing out of my system I decided to write
        things down.&lt;O:P&gt;&lt;/O:P&gt;
        &lt;/span&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-INDENT: 0.5in"&gt;
        &lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma"&gt;
        &lt;O:P&gt;&amp;#160;&lt;/O:P&gt;
        &lt;/span&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-INDENT: 0.5in"&gt;
        &lt;b style="mso-bidi-font-weight: normal"&gt;&lt;span style="FONT-FAMILY: Tahoma"&gt;DllMain
        and OS loader&lt;O:P&gt;&lt;/O:P&gt;
        &lt;/span&gt;&lt;/b&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-INDENT: 0.5in"&gt;
        &lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma"&gt;As we are all well aware now, things
        are not as easy as they seem. In fact are they ever? DllMain which used to be briefly
        discussed in most books on Win32 as a reasonably innocent initialization routine may
        now look like a vicious monster which obeys no rules and causes nasty side-effects.
        But let's get to the source - &lt;a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/dllmain.asp"&gt;MSDN
        reference&lt;/a&gt; 
        &lt;O:P&gt;&lt;/O:P&gt;
        &lt;/span&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-INDENT: 0.5in"&gt;
        &lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma"&gt;It all starts innocently enough.
        The article defines DllMain as an optional entry point into a DLL, called by the system
        when the DLL gets attached to a process or a thread; outlines the somewhat tricky
        but reasonable rules that govern the calls (for instance, calls may be unmatched for
        a thread if it's a main thread of the process or if it was already running when LoadLibrary
        was called), discusses abnormal termination and then 
        &lt;O:P&gt;&lt;/O:P&gt;
        &lt;/span&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-INDENT: 0.5in"&gt;
        &lt;b style="mso-bidi-font-weight: normal"&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma"&gt;...whoa...&lt;/span&gt;&lt;/b&gt; 
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-INDENT: 0.5in"&gt;
        &lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma"&gt;Without missing a heart-beat, it
        carries on describing what you can do there. That is pretty startling as of itself
        since when should you be limited in that regard? - but as you keep reading, things
        just get worse. It turns out, you can do pretty much nothing at all. Calls to LoadLibrary/LoadLibraryEx
        are explicitly prohibited. Other calls into kernel32 are OK. But you can't call into
        User32. And don't use CRT memory management (unless you are&amp;#160;linked statically)
        - use HeapAlloc instead. Oh, and of course don't call anything that would do any such
        nasty things: that would be bad. One last thing - don't read the registry either.
        Have a nice day.&lt;O:P&gt;&lt;/O:P&gt;
        &lt;/span&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-INDENT: 0.5in"&gt;
        &lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma"&gt;The fact that none of this is written
        is &lt;/span&gt;&lt;span style="FONT-SIZE: 11pt; FONT-FAMILY: Tahoma"&gt;big&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma"&gt;, &lt;/span&gt;&lt;b style="mso-bidi-font-weight: normal"&gt;&lt;span style="FONT-SIZE: 11pt; FONT-FAMILY: Tahoma"&gt;bold&lt;/span&gt;&lt;/b&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma"&gt;, &lt;/span&gt;&lt;b style="mso-bidi-font-weight: normal"&gt;&lt;span style="FONT-SIZE: 14pt; COLOR: red; FONT-FAMILY: Tahoma"&gt;maybe
        even red&lt;/span&gt;&lt;/b&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma"&gt; print is truly
        unfortunate -&amp;#160;it really ought to be, because most people simply miss that part.
        So let's say, you have read it all now the question is: &lt;b style="mso-bidi-font-weight: normal"&gt;why&lt;/b&gt;? 
        &lt;O:P&gt;&lt;/O:P&gt;
        &lt;/span&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-INDENT: 0.5in"&gt;
        &lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma"&gt;The thing is, as far as your binary
        is concerned, DllMain gets called at a truly unique moment. By that time OS loader
        has found, mapped and bound the file from disk, but - depending on the circumstances
        - in some sense your binary may not have been "fully born". Things can be tricky.&lt;O:P&gt;&lt;/O:P&gt;
        &lt;/span&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-INDENT: 0.5in"&gt;
        &lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma"&gt;In a nutshell, when DllMain is
        called, OS loader is in a rather fragile state. First off, it has applied a lock on
        its structures to prevent internal corruption while inside that call, and secondly,
        some of your dependencies may not be in a fully loaded state. Before a binary gets
        loaded, OS Loader looks at its static dependencies. If those require additional dependencies,
        it looks at them as well. As a result of this analysis, it comes up with a sequence
        in which DllMains of those binaries need to be called. It's pretty smart about things
        and in most cases you can even get away with not following most of the rules described
        in MSDN - but not always.&lt;O:P&gt;&lt;/O:P&gt;
        &lt;/span&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-INDENT: 0.5in"&gt;
        &lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma"&gt;The thing is, the loading order
        is unknown to you, but more importantly, it's built based on the &lt;b style="mso-bidi-font-weight: normal"&gt;static&lt;/b&gt; import
        information. If some dynamic loading occurs in your DllMain during DLL_PROCESS_ATTACH
        and you're making an outbound call, all bets are off. There is no guarantee that DllMain
        of that binary will be called and therefore if you then attempt to &lt;b style="mso-bidi-font-weight: normal"&gt;GetProcAddress&lt;/b&gt; into
        a function inside that binary, results are completely unpredictable as global variables
        may not have been initialized. Most likely you will get an AV.&lt;O:P&gt;&lt;/O:P&gt;
        &lt;/span&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-INDENT: 0.5in"&gt;
        &lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma"&gt;Another scenario is when you start
        spinning a new thread on DLL_THREAD_ATTACH and wait for it to finish initialization
        via some syncronization technique. This blocks your thread in DllMain, while still
        keeping OS lock. This can lead to deadlocks. 
        &lt;O:P&gt;&lt;/O:P&gt;
        &lt;/span&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-INDENT: 0.5in"&gt;
        &lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma"&gt;Overall, if anything - &lt;strong&gt;&lt;span style="FONT-FAMILY: Tahoma"&gt;anything&lt;/span&gt;&lt;/strong&gt; -
        goes wrong in DllMain of one of the binaries, the whole process may be doomed.&lt;O:P&gt;&lt;/O:P&gt;
        &lt;/span&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-INDENT: 0.5in"&gt;
        &lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma"&gt;The trouble is, definition of "wrong"
        is very, very vague in this case. For instance, developers using MC++ know that you
        shouldn't even dream of having DllMain in your library. And if you do you do, &lt;a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dv_vstechart/html/vcconmixeddllloadingproblem.asp"&gt;you
        may be very, very sorry&lt;/a&gt;. I think CLR folks want to fix this for the "Whidbey"
        release. 
        &lt;O:P&gt;&lt;/O:P&gt;
        &lt;/span&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-INDENT: 0.5in"&gt;
        &lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma"&gt;Chris Brumme lists the following
        things that &lt;b style="mso-bidi-font-weight: normal"&gt;should never, ever be done in
        DllMain&lt;/b&gt;.&lt;O:P&gt;&lt;/O:P&gt;
        &lt;/span&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.75in; TEXT-INDENT: -0.25in; mso-list: l0 level1 lfo1; tab-stops: list .75in"&gt;
        &lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Symbol; mso-fareast-font-family: Symbol; mso-bidi-font-family: Symbol"&gt;&lt;span style="mso-list: Ignore"&gt;·&lt;span style="FONT: 7pt 'Times New Roman'"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;b style="mso-bidi-font-weight: normal"&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma"&gt;Dynamic
        binds&lt;/span&gt;&lt;/b&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma"&gt;. That includes
        LoadLibrary/UnloadLibrary calls or anything that may call implicitly call them&lt;O:P&gt;&lt;/O:P&gt;
        &lt;/span&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.75in; TEXT-INDENT: -0.25in; mso-list: l0 level1 lfo1; tab-stops: list .75in"&gt;
        &lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Symbol; mso-fareast-font-family: Symbol; mso-bidi-font-family: Symbol"&gt;&lt;span style="mso-list: Ignore"&gt;·&lt;span style="FONT: 7pt 'Times New Roman'"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;b style="mso-bidi-font-weight: normal"&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma"&gt;Locking&lt;/span&gt;&lt;/b&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma"&gt; of
        any kind. If you are trying to acquire a lock that is currently help by a thread that
        needs OS loader lock (which you may be holding), you'll deadlock. 
        &lt;O:P&gt;&lt;/O:P&gt;
        &lt;/span&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.75in; TEXT-INDENT: -0.25in; mso-list: l0 level1 lfo1; tab-stops: list .75in"&gt;
        &lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Symbol; mso-fareast-font-family: Symbol; mso-bidi-font-family: Symbol"&gt;&lt;span style="mso-list: Ignore"&gt;·&lt;span style="FONT: 7pt 'Times New Roman'"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;b style="mso-bidi-font-weight: normal"&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma"&gt;Cross-binary
        calls. &lt;/span&gt;&lt;/b&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma"&gt;As been discussed
        the binary youre calling into may not have been initialized or have already been unutilized. 
        &lt;O:P&gt;&lt;/O:P&gt;
        &lt;/span&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.75in; TEXT-INDENT: -0.25in; mso-list: l0 level1 lfo1; tab-stops: list .75in"&gt;
        &lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Symbol; mso-fareast-font-family: Symbol; mso-bidi-font-family: Symbol"&gt;&lt;span style="mso-list: Ignore"&gt;·&lt;span style="FONT: 7pt 'Times New Roman'"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;b style="mso-bidi-font-weight: normal"&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma"&gt;Starting
        new threads and then wait for completion&lt;/span&gt;&lt;/b&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma"&gt;.
        As discussed, thread in question may need to acquire OS lock that you are holding. 
        &lt;O:P&gt;&lt;/O:P&gt;
        &lt;/span&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-INDENT: 0.5in"&gt;
        &lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma"&gt;
        &lt;O:P&gt;&amp;#160;&lt;/O:P&gt;
        &lt;/span&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-INDENT: 0.5in"&gt;
        &lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma"&gt;So, what does this tell us?&lt;O:P&gt;&lt;/O:P&gt;
        &lt;/span&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-INDENT: 0.5in"&gt;
        &lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma"&gt;
        &lt;O:P&gt;&amp;#160;&lt;/O:P&gt;
        &lt;/span&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-INDENT: 0.5in"&gt;
        &lt;b style="mso-bidi-font-weight: normal"&gt;&lt;span style="FONT-FAMILY: Tahoma"&gt;DllMain
        is that gun you can easily shoot yourself with&lt;O:P&gt;&lt;/O:P&gt;
        &lt;/span&gt;&lt;/b&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-INDENT: 0.5in"&gt;
        &lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma"&gt;How many people do you know that
        did stupid things like calling CoInitialize() in DllMain? I know of cases when that
        was done on DLL_THREAD_ATTACH, which not only means that we were risking to hit a
        deadlock, but also that any thread in that process will have COM initialized. What's
        worse, it may be initialized with the wrong threading model. And then people will
        be wondering how the heck they ended up with STA threads in thread pools. Or something
        much more subtle like calling a system function that starts a worker thread as part
        of its execution? How many times did &lt;b style="mso-bidi-font-weight: normal"&gt;you&lt;/b&gt; do
        all those things?&lt;O:P&gt;&lt;/O:P&gt;
        &lt;/span&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-INDENT: 0.5in"&gt;
        &lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma"&gt;Another problem with this is that
        all these horrors can present themselves under very limited circumstances. In most
        cases things do work fine, but a race condition, a slightly modified DLL load order
        or other factors may change everything. Which means you may not even know it until
        your ship. This may be fine for a user application (well, things like that are &lt;b style="mso-bidi-font-weight: normal"&gt;never&lt;/b&gt; fine,
        it's just that the damage may not be substabtial), but this is always bad for servers
        -&amp;#160;especially if you are talking enterprise availability. I don't think this can
        ever become a security threat - one you can fight anyway -&amp;#160;but random crashes
        are just &lt;b style="mso-bidi-font-weight: normal"&gt;not nice&lt;/b&gt;.&lt;O:P&gt;&lt;/O:P&gt;
        &lt;/span&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-INDENT: 0.5in"&gt;
        &lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma"&gt;So let's get back to what we &lt;b style="mso-bidi-font-weight: normal"&gt;can&lt;/b&gt; do
        in DllMain. According to MSDN, "The entry-point function should perform only simple
        initialization or termination tasks."&lt;O:P&gt;&lt;/O:P&gt;
        &lt;/span&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-INDENT: 0.5in"&gt;
        &lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma"&gt;These tasks can only include calls
        to Kernel32 (excluding LoadLibrary/LoadLibraryEx). If you look at what this means
        for you, you will find that this is extremely liming. &lt;span style="mso-spacerun: yes"&gt;&amp;#160;&lt;/span&gt;Further,
        CRT functions, including memory allocations are not safe unless you are statically
        linked. This means that seemingly innocent things something like &lt;i style="mso-bidi-font-style: normal"&gt;g_pMyGlobalObject
        = new CMyGlobalObject()&lt;/i&gt; can theoretically cause all kinds of nasty stuff&lt;span style="mso-spacerun: yes"&gt;&amp;#160; &lt;/span&gt;because
        they will use &lt;b style="mso-bidi-font-weight: normal"&gt;malloc&lt;/b&gt; that is dynamically
        linked from msvcr*.dll.&lt;O:P&gt;&lt;/O:P&gt;
        &lt;/span&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-INDENT: 0.5in"&gt;
        &lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma"&gt;This leaves us with primitive types,
        synchronization objects initialization ... that's about it. And definitely -&amp;#160;&lt;b style="mso-bidi-font-weight: normal"&gt;definitely&lt;/b&gt; -
        no managed code.&lt;O:P&gt;&lt;/O:P&gt;
        &lt;/span&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-INDENT: 0.5in"&gt;
        &lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma"&gt;So what am I saying? There aren't
        too many things that are legal there; it's extremely easy to do illegal stuff - you
        have to always know if what you're calling really does, which is extremely difficult
        if you use something defined elsewhere - C/C++ LIB for instance; the compiler won't
        tell you that you are doing the wrong thing; and the code is likely to run fine in
        most cases... but &lt;b style="mso-bidi-font-weight: normal"&gt;not all&lt;/b&gt; of them. 
        &lt;O:P&gt;&lt;/O:P&gt;
        &lt;/span&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-INDENT: 0.5in"&gt;
        &lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma"&gt;Where options does this leave us
        with?&lt;O:P&gt;&lt;/O:P&gt;
        &lt;/span&gt;
    &lt;/p&gt;
    &lt;ul style="MARGIN-TOP: 0in" type="disc"&gt;
        &lt;li class="MsoNormal" style="MARGIN: 0in 0in 0pt; mso-list: l1 level1 lfo2; tab-stops: list .5in"&gt;
            &lt;b style="mso-bidi-font-weight: normal"&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma"&gt;Just
            say no&lt;/span&gt;&lt;/b&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma"&gt;. Avoid the darn
            thing altogether and link with /noentry. Reconsider the way you deal with globals.
            Do lazy TLS initialization.&lt;span style="mso-spacerun: yes"&gt;&amp;#160; &lt;/span&gt;
            &lt;O:P&gt;&lt;/O:P&gt;
            &lt;/span&gt; 
        &lt;/li&gt;
        &lt;li class="MsoNormal" style="MARGIN: 0in 0in 0pt; mso-list: l1 level1 lfo2; tab-stops: list .5in"&gt;
            &lt;b style="mso-bidi-font-weight: normal"&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma"&gt;Be
            very careful&lt;/span&gt;&lt;/b&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma"&gt;. Sometimes
            you simply have to use it. It's just too ugly not to. Have a full code review. See
            what's being done and what OS does. Make sure that everyone understands that DllMain
            is just different. Read and memorize horror stories about people who didn't know better. 
            &lt;br /&gt;
            One thing you can do here to minimize the damage is disabling calls to your DllMain
            when new threads join/leave the process - this can be done with &lt;b style="mso-bidi-font-weight: normal"&gt;DisableThreadLibraryCalls&lt;/b&gt;.
            This is generally a good idea in all cases where you don't need thread-level initialization
            because OS loader doesn't need to call into your binary every time a new thread is
            born&lt;/span&gt; 
        &lt;/li&gt;
        &lt;li class="MsoNormal" style="MARGIN: 0in 0in 0pt; mso-list: l1 level1 lfo2; tab-stops: list .5in"&gt;
            &lt;b style="mso-bidi-font-weight: normal"&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma"&gt;Be
            afraid. Be very afraid. &lt;/span&gt;&lt;/b&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma"&gt;Well,
            just leave things where they are. Things don't crash right now and you have other
            things to do. Good plan.&lt;O:P&gt;&lt;/O:P&gt;
            &lt;/span&gt;
        &lt;/li&gt;
    &lt;/ul&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.75in"&gt;
        &lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma"&gt;
        &lt;O:P&gt;&amp;#160;&lt;/O:P&gt;
        &lt;/span&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt"&gt;
        &lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma"&gt;
        &lt;O:P&gt;&amp;#160;&lt;/O:P&gt;
        &lt;/span&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt"&gt;
        &lt;b style="mso-bidi-font-weight: normal"&gt;&lt;span style="FONT-FAMILY: Tahoma"&gt;Silver lining
        : DllMain and resource leaks diagnostics&lt;O:P&gt;&lt;/O:P&gt;
        &lt;/span&gt;&lt;/b&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-INDENT: 0.5in"&gt;
        &lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma"&gt;There's one piece of information
        that gets provided through DllMain which you can't possibly get any other way. If
        you review the signature of DllMain, youll notice that the last argument passed in
        despite being called lpReserved actually has some meaning: 
        &lt;O:P&gt;&lt;/O:P&gt;
        &lt;/span&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-INDENT: 0.5in"&gt;
        &lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma"&gt;
        &lt;O:P&gt;&amp;#160;&lt;/O:P&gt;
        &lt;/span&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in"&gt;
        &lt;span style="FONT-SIZE: 8.5pt; COLOR: black; FONT-FAMILY: Verdana"&gt;If &lt;i&gt;fdwReason&lt;/i&gt; is
        DLL_PROCESS_ATTACH, &lt;i&gt;lpvReserved&lt;/i&gt; is NULL for dynamic loads and non-NULL for
        static loads. 
        &lt;O:P&gt;&lt;/O:P&gt;
        &lt;/span&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0.1in 0in 0.1in 0.5in"&gt;
        &lt;span style="FONT-SIZE: 8.5pt; COLOR: black; FONT-FAMILY: Verdana"&gt;If &lt;i&gt;fdwReason&lt;/i&gt; is
        DLL_PROCESS_DETACH, &lt;i&gt;lpvReserved&lt;/i&gt; is NULL if &lt;b&gt;DllMain&lt;/b&gt; has been called by
        using &lt;b&gt;FreeLibrary&lt;/b&gt; and non-NULL if &lt;b&gt;DllMain&lt;/b&gt; has been called during process
        termination.&lt;O:P&gt;&lt;/O:P&gt;
        &lt;/span&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-INDENT: 0.5in"&gt;
        &lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma"&gt;As you see, lpvReserved does tell
        you something. Although I can't see why you would be interested in knowing whether
        your DLL has been statically or dynamically loaded -&amp;#160;there may be uses there,
        I just don't see them - but knowing &lt;u&gt;how&lt;/u&gt; you are being unloaded could be interesting. 
        &lt;O:P&gt;&lt;/O:P&gt;
        &lt;/span&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-INDENT: 0.5in"&gt;
        &lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma"&gt;For one, if you're managing some
        kind of resource in DllMain, which only lives within process context, you can possibly
        skip some clean-up if you knew that the process is dying as it is. This is not too
        valuable because the very nature of DllMain does not make it a very good entry point
        for resource management. 
        &lt;O:P&gt;&lt;/O:P&gt;
        &lt;/span&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-INDENT: 0.5in"&gt;
        &lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma"&gt;There are cases, however, when
        you expect your DLL to be unloaded in a specific way and you can use DllMain to verify
        that it is indeed being unloaded as you expect. For instance, if:&lt;O:P&gt;&lt;/O:P&gt;
        &lt;/span&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 75pt; TEXT-INDENT: -0.25in; mso-list: l2 level1 lfo3; tab-stops: list 75.0pt"&gt;
        &lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Symbol; mso-fareast-font-family: Symbol; mso-bidi-font-family: Symbol"&gt;&lt;span style="mso-list: Ignore"&gt;·&lt;span style="FONT: 7pt 'Times New Roman'"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma"&gt;your
        DLL is in fact a COM server (and has no other uses), &lt;b style="mso-bidi-font-weight: normal"&gt;and&lt;/b&gt; 
        &lt;O:P&gt;&lt;/O:P&gt;
        &lt;/span&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 75pt; TEXT-INDENT: -0.25in; mso-list: l2 level1 lfo3; tab-stops: list 75.0pt"&gt;
        &lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Symbol; mso-fareast-font-family: Symbol; mso-bidi-font-family: Symbol"&gt;&lt;span style="mso-list: Ignore"&gt;·&lt;span style="FONT: 7pt 'Times New Roman'"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma"&gt;the
        COM host is well-behaved &lt;b style="mso-bidi-font-weight: normal"&gt;and&lt;/b&gt; 
        &lt;O:P&gt;&lt;/O:P&gt;
        &lt;/span&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 75pt; TEXT-INDENT: -0.25in; mso-list: l2 level1 lfo3; tab-stops: list 75.0pt"&gt;
        &lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Symbol; mso-fareast-font-family: Symbol; mso-bidi-font-family: Symbol"&gt;&lt;span style="mso-list: Ignore"&gt;·&lt;span style="FONT: 7pt 'Times New Roman'"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma"&gt;all
        of your COM objects have been properly released, 
        &lt;O:P&gt;&lt;/O:P&gt;
        &lt;/span&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-INDENT: 0.5in"&gt;
        &lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma"&gt;then you should expect that you
        will get lpvReserved=NULL - that is unloaded via FreeLibrary. 
        &lt;O:P&gt;&lt;/O:P&gt;
        &lt;/span&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-INDENT: 0.5in"&gt;
        &lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma"&gt;Heres what seems to be happening.
        Every well-behaved COM process should call CoUnintialize() on each thread when it
        gets shut down. Internally that calls DllCanUnloadNow on your binary which returns
        TRUE if all outstanding references are closed. If that's the case, COM will call FreeLibrary,
        which - unless there are other LoadLibrary references outstanding - will unload your
        DLL. That will pass lpvReserved=NULL. If &lt;b style="mso-bidi-font-weight: normal"&gt;any&lt;/b&gt; of
        these conditions is not satisfied, your DLL will reside in the process until it terminates
        and you'll get lpvReserved!=NULL( I'd like to thank 
        &lt;ST1:PERSONNAME w:st="on"&gt;Michael 
Entin&lt;/ST1:PERSONNAME&gt;
        - who really ought to start blogging - for helping me to get all the pieces together).&lt;O:P&gt;&lt;/O:P&gt;
        &lt;/span&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-INDENT: 0.5in"&gt;
        &lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma"&gt;So if - and that's a &lt;/span&gt;&lt;b style="mso-bidi-font-weight: normal"&gt;&lt;span style="FONT-FAMILY: Tahoma"&gt;big
        if&lt;/span&gt;&lt;/b&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma"&gt; - your application
        is well-behaved, and no-one ever messed up loading your DLL with LoadLibrary and forgetting
        to unload it, then lpvReserved!=NULL means that some of your COM objects have not
        been released. There's nothing your code can do about that - except maybe asserting
        - and you will then have to look into that further.&lt;O:P&gt;&lt;/O:P&gt;
        &lt;/span&gt;
    &lt;/p&gt;
    &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-INDENT: 0.5in"&gt;
        &lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma"&gt;This approach is not limited to
        only COM leaks -&amp;#160;theoretically you should expect that when your binary is leaving
        this world, it's not taking anything with it. You can look through the list of globally-managed
        resources and see of they have been disposed if. Be very, very careful there - you
        shouldn't be doing any stuff that may compromise OS loader:&amp;#160;see the four bullets
        above.&lt;O:P&gt;&lt;/O:P&gt;
        &lt;/span&gt;
    &lt;/p&gt;
    &lt;p&gt;
    &lt;/p&gt;
&lt;/body&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=56141" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/oleglv/archive/tags/Platform/default.aspx">Platform</category><category domain="http://blogs.msdn.com/oleglv/archive/tags/C_2B002B00_/default.aspx">C++</category><category domain="http://blogs.msdn.com/oleglv/archive/tags/Tidbits/default.aspx">Tidbits</category></item></channel></rss>