1

is it possible to use an infinite loop in a dll function without using a thread?

here's some example code:

BOOL WINAPI DllMain(HMODULE hModule, DWORD dwReason, LPVOID lpReserved) {
    switch (dwReason)
    {
    case DLL_PROCESS_ATTACH:
        DisableThreadLibraryCalls(hModule);
        GetModuleHandleExA(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_PIN, (LPCSTR)hModule, &hModule);
        CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)myfunction, 0, NULL, 0); //my current method
        myfunction(); //locks the program at runtime if i do it this way (just an example)
    case DLL_THREAD_ATTACH:
        break;
    case DLL_THREAD_DETACH:
        break;
    case DLL_PROCESS_DETACH:
        break;
    }
    return TRUE;
}

and here's an example of the function in the thread:

void myfunction() {
    //begin the infinite loop after 5 seconds
    Sleep(5000);
    for (;;) //set no condition for breaking the loop
    {
        Sleep(500); //this keeps the cpu from spiking
        //call my functions
        function1();
        function2();
        function3();
        function4();
    }
}

this code works well. i just wonder if there are alternatives. for example: can the function be written into the memory of the process? or called at a later time, instead of DLL_PROCESS_ATTACH?

  • Does this answer your question? [How to start a thread in DLLMain?](https://stackoverflow.com/questions/41354455/how-to-start-a-thread-in-dllmain) – Botje Apr 29 '20 at 08:44
  • If you are injecting this DLL from a remote process, consider using [CreateRemoteThread](https://resources.infosecinstitute.com/using-createremotethread-for-dll-injection-on-windows/#gref) to get around this restriction. If the process is loading your DLL voluntarily, have it call some kind of `init` function in your DLL. – Botje Apr 29 '20 at 08:46
  • voluntarily. i'll research your advice. –  Apr 29 '20 at 09:07

1 Answers1

1

First issue

This is undefined behavior:

CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)myfunction, 0, NULL, 0);

The fact that you are casting the function signature of myFunction is likely to corrupt the stack.

Change your signature of myFunction as follows:

DWORD __stdcall myfunction(void*) {

You know you got it right when you can remove the suspicious cast from CreateThread:

CreateThread(NULL, 0, myfunction, 0, NULL, 0);

Second issue

You shouldn't be doing anything of significance in DLL_PROCESS_ATTACH to begin with. And according to Raymond, you definitely shouldn't be creating a thread.

selbie
  • 100,020
  • 15
  • 103
  • 173
  • 1
    If you actually read what Raymond says carefully, and [read the documentation](https://learn.microsoft.com/en-us/windows/win32/dlls/dynamic-link-library-best-practices), calling `CreateThread` in `DllMain` is ok as long as you don't synchronize on the new thread. It is *not recommended* to create a thread, but it is not forbidden either. Just kicking off the thread and moving on is fine. – Remy Lebeau Apr 29 '20 at 10:06