title: CreateThread inside another thread
body: <p>I am having an issue creating a thread inside of another thread. Normally I would be able to do this, but the reason for this issue is because I've <code>Incremented Reference Count</code> of the DLL which starts these threads. I need to start multiple threads inside this DLL. How can I get around this and be able to issue multiple <code>CreateThread()</code>'s when needed in my project without experiencing problems because of the <code>Incremented Reference Count</code> in my DLL?</p>
  
  <p>Here is the function I've written to Increment Reference Count in my DLL file:</p>
  
  <pre><code>BOOL IncrementReference( HMODULE hModule )
  {
      if ( hModule == NULL )
          return FALSE;
  
      TCHAR ModulePath[ MAX_PATH + 1 ];
      if ( GetModuleFileName( hModule , ModulePath , MAX_PATH ) == 0 )
          return FALSE;
  
      if ( LoadLibrary( ModulePath ) == NULL )
          return FALSE;
  
      return TRUE;
  }
  </code></pre>
  
  <p>As requested, here is a PoC program to recreate the issue I am facing. I am really hoping this will help you guys point me to a solution. Also, take note, the DLL is being unloading due to conditions in the application which I am targeting (hooks that are already set in that application), so Incrementing the Reference Count is required for my thread to run in the first place.</p>
  
  <p>Also, I can't run more than one operation in the main thread as it has its own functionality to take care of and another thread is required on the side to take care of something else. They must also run simultaneously, hence I need to fix this issue of making more than one thread in an Incremented DLL.</p>
  
  <pre><code>// dllmain.cpp : Defines the entry point for the DLL application.
  #pragma comment( linker , "/Entry:DllMain" )
  #include &lt;Windows.h&gt;
  #include &lt;process.h&gt;
  
  UINT CALLBACK SecondThread( PVOID pParam )
  {
      MessageBox( NULL , __FUNCTION__ , "Which Thread?" , 0 );
      return 0;
  }
  
  UINT CALLBACK FirstThread( PVOID pParam )
  {
    MessageBox( NULL , __FUNCTION__ , "Which Thread?" , 0 );
    _beginthreadex(0, 0, &amp;SecondThread, 0, 0, 0);
    return 0;
  }
  
  BOOL IncrementReference( HMODULE hModule )
  {
      if ( hModule == NULL )
          return FALSE;
  
      TCHAR ModulePath[ MAX_PATH + 1 ];
      if ( GetModuleFileName( hModule , ModulePath , MAX_PATH ) == 0 )
          return FALSE;
  
      if ( LoadLibrary( ModulePath ) == NULL )
          return FALSE;
  
      return TRUE;
  }
  
  BOOL APIENTRY DllMain( HMODULE hModule,
                         DWORD  ul_reason_for_call,
                         LPVOID lpReserved
                       )
  {
      switch (ul_reason_for_call)
      {
          case DLL_PROCESS_ATTACH:
          {
              if (IncrementReference(0))    
                  _beginthreadex(0, 0, &amp;FirstThread, 0, 0, 0);
          }
          break;
      }
      return TRUE;
  }
  </code></pre>
  
  <p>As you can see, the code never executes the <code>SecondThread</code> function. The question is, why? And what can be done to fix it?</p>