Skip to content

Commit

Permalink
fix(src/ProcessStopper.cpp) Remake trigger.
Browse files Browse the repository at this point in the history
    - Fix false event triggering.
    - Fix detection of active app.
  • Loading branch information
AmeliePick committed Dec 20, 2024
1 parent 4fb5892 commit 92c46bb
Showing 1 changed file with 28 additions and 26 deletions.
54 changes: 28 additions & 26 deletions src/ProcessStopper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,57 +8,59 @@

#include <Windows.h>
#pragma comment(lib, "Synchronization.lib")
#pragma comment(lib, "Winmm.lib")

typedef LONG(NTAPI* NtProcess)(HANDLE ProcessHandle);

NtProcess NtSuspendProcess = (NtProcess)GetProcAddress(GetModuleHandle(L"ntdll"), "NtSuspendProcess");
NtProcess NtResumeProcess = (NtProcess)GetProcAddress(GetModuleHandle(L"ntdll"), "NtResumeProcess");
static NtProcess NtSuspendProcess = (NtProcess)GetProcAddress(GetModuleHandle(L"ntdll"), "NtSuspendProcess");
static NtProcess NtResumeProcess = (NtProcess)GetProcAddress(GetModuleHandle(L"ntdll"), "NtResumeProcess");

static HANDLE suspendedProcess = NULL;
static bool runThread = false;
static bool isKeyPressed = false;
static MMRESULT _timer = NULL;


void CALLBACK WorkCallback(PTP_CALLBACK_INSTANCE Instance, PVOID Parameter, PTP_WORK Work)
VOID CALLBACK WorkCallback(UINT uTimerID, UINT uMsg, DWORD_PTR dwUser, DWORD_PTR dw1, DWORD_PTR dw2)
{
if (suspendedProcess != NULL)
{
NtResumeProcess(suspendedProcess);
CloseHandle(suspendedProcess);
suspendedProcess = NULL;

return;
}

bool cmp = runThread;
WaitOnAddress(&runThread, &cmp, sizeof(bool), 2000); // 2 sec delay for key pressing.
if (runThread == false) return; // The key was released.
if (!isKeyPressed) return;

DWORD processID = 0;
GetWindowThreadProcessId(GetForegroundWindow(), &processID);

suspendedProcess = OpenProcess(THREAD_ALL_ACCESS, TRUE, processID);
NtSuspendProcess(suspendedProcess);
GetWindowThreadProcessId((HWND)dwUser, &processID);
NtSuspendProcess(suspendedProcess = OpenProcess(THREAD_ALL_ACCESS | PROCESS_SUSPEND_RESUME, TRUE, processID));
}


LRESULT keyboardProcessing(int code, WPARAM w, LPARAM l)
{
if (((PKBDLLHOOKSTRUCT)l)->vkCode != VK_ESCAPE) return CallNextHookEx(NULL, code, w, l);

if (w == WM_KEYUP && runThread)
if (w == WM_KEYUP)
{
runThread = false;
WakeByAddressAll(&runThread);
isKeyPressed = false;
if (_timer != NULL) // If the user simulates input and sending UP event without DOWN before.
{
timeKillEvent(_timer);
_timer = NULL;
}
}
else if (!runThread) // Don't create the thread while the key is pressed.
else if (!isKeyPressed)
{
runThread = true;
SubmitThreadpoolWork(CreateThreadpoolWork((PTP_WORK_CALLBACK)WorkCallback, NULL, NULL));
isKeyPressed = true;

if (suspendedProcess != NULL)
{
NtResumeProcess(suspendedProcess);
CloseHandle(suspendedProcess);
suspendedProcess = NULL;
return CallNextHookEx(NULL, code, w, l);
}
_timer = timeSetEvent(2000, 0, WorkCallback, (DWORD_PTR)GetForegroundWindow(), TIME_CALLBACK_FUNCTION | TIME_KILL_SYNCHRONOUS | TIME_ONESHOT);
}

return CallNextHookEx(NULL, code, w, l);
}


int main()
{
ShowWindow(GetConsoleWindow(), SW_HIDE);
Expand Down

0 comments on commit 92c46bb

Please sign in to comment.