Home > Blockchain >  check and protect a thread in c
check and protect a thread in c

Time:11-06

Currently, I created a simple thread to clear memory:

BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
{
    switch (ul_reason_for_call)
    {
    case DLL_PROCESS_ATTACH:
        CreateThread(0, 0, test, 0, 0, 0);
        break;
    case DLL_THREAD_ATTACH:
    case DLL_THREAD_DETACH:
    case DLL_PROCESS_DETACH:
        break;
    }

    return TRUE;
}

Code:

DWORD WINAPI test(LPVOID lpvParam) {
memo: 
    Sleep(10000);
    SetProcessWorkingSetSize(GetCurrentProcess(), 102400, 614400);
    goto memo;  
}

Is there any way to protect this thread? And prevent it from being paused with some external program (such as Process Hacker 2)?

Example: if the thread is running, returns true, if it is externally paused or interrupted, returns false and closes the program with ExitProcess()?

I tried different methods like

std::thread
thread.join().
thread.joinable()

None of them worked.

CodePudding user response:

prevent thread from being paused with some external program (such as Process Hacker 2)?

Instead of creating a new thread to run your code periodically, create a waitable timer with CreateWaitableTimer and schedule it with SetWaitableTimer. The timer runs the callback in the thread that called SetWaitableTimer, so that there is no other thread to pause.

That also solves the problem that threads must not be created in DllMain.

  • Related