title: How to wait for a thread created in DLLmain to finish?
body: <p>I create a new thread in <code>Dllmain()</code> by the <code>CreateThread()</code> API, which does not involve thread synchronization, it is only a separate thread.</p>
  <p><code>Dllmain()</code> invokes <code>WaitForSingleObject(funcThread, INFINITE);</code> to force the main thread to wait for <code>funcThread</code> to finish.</p>
  <p>Then I dynamic link this <code>Dllmain()</code>, but the result shows <code>funcThread()</code> finishes early. I know adding a System Pause will work, but do you have some ways to make it work with only changing the <code>Dllmain()</code> file?</p>
  <p>What is weird is that it has different results when changing the order of <code>printf()</code> and <code>OutputDebugStringW()</code>.</p>
  <p>targetFuction.exe</p>
  <pre class="lang-cpp prettyprint-override"><code>#include &lt;Windows.h&gt;
  #include &lt;iostream&gt;
  
  int main() {
      HINSTANCE hModule = LoadLibrary(L&quot;testDllmain.dll&quot;);
      return 0;
  }
  </code></pre>
  <p>testDllmain.dll</p>
  <pre><code>#include &lt;Windows.h&gt;
  #include &lt;iostream&gt;
  
  DWORD WINAPI function(LPVOID lpParam) {
      for (int i = 0; i &lt; 100; i++) {
          printf(&quot;%d \n&quot;, i);
      }
  
      return 0;
  }
  HANDLE funcThread;
  
  BOOL APIENTRY DllMain(HMODULE hModule,
      DWORD  ul_reason_for_call,
      LPVOID lpReserved
  ) {
      switch (ul_reason_for_call) {
      case DLL_PROCESS_ATTACH:
  
          funcThread = CreateThread(
              NULL,                   // default security attributes
              0,                      // use default stack size  
              function,       // thread function name
              NULL,          // argument to thread function 
              0,                      // use default creation flags 
              NULL);
          OutputDebugStringW(L&quot;attach process!\n&quot;);
  
          printf(&quot;attach process!\n&quot;);
  
          break;
      case DLL_PROCESS_DETACH:
  
          WaitForSingleObject(funcThread, INFINITE);
          OutputDebugStringW(L&quot;detach process!\n&quot;);
          printf(&quot;detach process!\n&quot;);
          // printf(&quot;detach process!\n&quot;);
          // OutputDebugStringW(L&quot;detach process!\n&quot;);
          break;
      case DLL_THREAD_ATTACH:
          printf(&quot;attach thread!\n&quot;);
          OutputDebugStringW(L&quot;attach thread!\n&quot;);
  
          break;
  
      case DLL_THREAD_DETACH:
          OutputDebugStringW(L&quot;detach thread!\n&quot;);
  
          printf(&quot;detach thread!\n&quot;);
          break;
      }
  
      return TRUE;
  }
  </code></pre>
  <p>Output:</p>
  <p><a href="https://i.stack.imgur.com/sX0bY.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/sX0bY.png" alt="image" /></a></p>
  <p><code>OutputDebugStringW()</code> before <code>printf()</code>:</p>
  <p><a href="https://i.stack.imgur.com/wsqr3.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/wsqr3.png" alt="image" /></a></p>
  <p><code>printf()</code> before <code>OutputDebugStringW()</code></p>
  <p><a href="https://i.stack.imgur.com/Ga0gl.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Ga0gl.png" alt="image" /></a></p>
  <p>It should also print 23 to 99 and <code>detach process</code> messages, but all of them are missing.</p>