我试图让 calc.exe 显示一个消息框,但是一旦我执行我的程序,calc.exe 总是崩溃。所以我尝试将代码注入到我自己的进程中,以便查看调试消息。这样做会给我一个异常“访问冲突......无法执行......”指向pData->msg
。然后我发现pThread
并pData
得到相同的地址。这怎么可能?我实际上设置和lpAddress
不得到相同的起始地址。VirtualAllocEx
pPage
pPage + 128
// 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);