I'm trying to bring calc.exe to show a messagebox, but calc.exe always crashes as soon as I execute my program. So I tried to inject the code to my own process in order to see debugging messages. Doing so gives me the exception "Access violation at... Cannot execute..." pointing to pData->msg
. Then I found out that pThread
and pData
get the same address. How is this possible? I actually set lpAddress
of VirtualAllocEx
to pPage
and pPage + 128
to not get the same start address.
// Allocate page
void *pPage = VirtualAllocEx(hProcess, NULL, 256, MEM_RESERVE, PAGE_EXECUTE_READWRITE);
// Commit memory for thread procedure
void *pThread = VirtualAllocEx(hProcess, pPage, 128, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
// Commit memory for thread data
void *pData = VirtualAllocEx(hProcess, (void*)((long long)pPage + 128), 128, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
// WriteProcessMemory, do stuff
// Release memory
VirtualFreeEx(hProcess, pPage, 256, MEM_RELEASE);