-
-
Save fredemmott/adb9c41d7cff44aecf92bece990f4c3f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define WIN32_LEAN_AND_MEAN | |
#include <Windows.h> | |
#include <detours.h> | |
#include <mutex> | |
std::mutex gStartupMutex; | |
bool gHeapLocked = false; | |
DWORD WINAPI HeapLockingThreadProc(LPVOID ignored) { | |
std::unique_lock lock(gStartupMutex); | |
HeapLock(GetProcessHeap()); | |
gHeapLocked = true; | |
while(true) { | |
Sleep(1000); | |
} | |
} | |
DWORD WINAPI SleepingThreadProc(LPVOID ignored) { | |
while(true) { | |
Sleep(1000); | |
} | |
} | |
int main() { | |
gStartupMutex.lock(); | |
auto getsLock = CreateThread(nullptr, NULL, &HeapLockingThreadProc, nullptr, NULL, nullptr); | |
auto sleeps = CreateThread(nullptr, NULL, &SleepingThreadProc, nullptr, NULL, nullptr); | |
gStartupMutex.unlock(); | |
// spinlock to avoid going anywhere near the heap | |
while (!gHeapLocked) { | |
YieldProcessor(); | |
} | |
DetourTransactionBegin(); | |
DetourUpdateThread(getsLock); | |
DetourUpdateThread(sleeps); | |
DetourTransactionCommit(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment