Difference calling a function in DllMain by CreateThread or calling directly

2

What's the difference in calling a function in the following ways:

The first way to create a thread. Example:

DWORD WINAPI Metodo1(LPVOID)
{
      // Meu código aqui...
      return NULL;
}

int WINAPI DllMain(HMODULE hModule, DWORD dwReason, LPVOID lpvReserved)
{
     switch(dwReason)
     {
     case DLL_PROCESS_ATTACH:
          CreateThread(NULL, NULL, &Metodo1, NULL, NULL, NULL);
          break;
     }
     return true;
}

Second way calling directly on Main. Example:

void Metodo2(void)
{
    // Meu código...
}

int WINAPI DllMain(HMODULE hModule, DWORD dwReason, LPVOID lpvReserved)
{
     switch(dwReason)
     {
     case DLL_PROCESS_ATTACH:
        Metodo2();
        break;
     }
     return true;
}
    
asked by anonymous 04.10.2017 / 20:41

1 answer

0

It is a bad practice to make API calls within DllMain() !

The CreateThread() function is one of the only functions that can be called within DllMain() , however, this should be done very carefully because this practice can easily cause dead-locks in your program.

Whenever the program using DLL is executed / terminated or when a thread is triggered / terminated, the DllMain() code is executed, so the operations performed by it must be carefully evaluated .

After all, what is the attempt to fire a thread within DllMain() ?

References:

link

link

    
05.10.2017 / 00:05