Difference of calling a function in Dllmain by Createthread or calling directly

Asked

Viewed 63 times

2

What is the difference of calling a function in the following ways:

The first way creating 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 by 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;
}

1 answer

0


It’s bad practice to make calls from API within the DllMain()!

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

Every time the program that uses the DLL is executed/finalised or when a thread is fired/completed, the code of the DllMain() is carried out, therefore the operations carried out by it should be carefully assessed.

After all, what is the purpose of firing a thread within the DllMain() ?

References:

https://blogs.msdn.microsoft.com/oldnewthing/20040127-00/? p=40873

https://stackoverflow.com/questions/1688290/creating-a-thread-in-dllmain

Browser other questions tagged

You are not signed in. Login or sign up in order to post.