我想使用 __ asm 块在我的 c ++ 应用程序(msvc visual studio)中调用 WriteProcessMemory 。
#include <windows.h>
#include <stdio.h>
#include <iostream>
#include <memory>
const char* TextVar = "loon";
int main(){
auto addrof = std::addressof(TextVar);
unsigned int address = (unsigned int)addrof;
HANDLE ProcessHandle = GetCurrentProcess();
//WriteProcessMemory( HANDLE hProcess, LPVOID lpBaseAddress, LPCVOID lpBuffer, SIZE_T nSize, SIZE_T *lpNumberOfBytesWritten );
__asm
{
xor eax, eax // clear eax
push 0x74736554 // push "Test"
mov eax, esp // mov "Test" to eax
push 0x0 // lpNumberOfBytesWritten
push 0x4 // nSize [strlen("Test") = 4]
push eax // lpBuffer - "Test"
push [address] // lpBaseAddress - address of TextVar
push ProcessHandle // hProcess - Handle of current process
call WriteProcessMemory
push DWORD PTR[TextVar]
call printf //Print TextVar
}
return 0;
}
但是我明白了:
Test.exe中的地址0x753F288E(ucrtbase.dll)引发异常:0xC0000005:读取访问冲突位于0x74736554。
// 0x753F288E-printf地址
// 0x74736554-我的文字-[测试]
我在做什么错?据我了解,WPM将文本写为TextVar中的地址。但是如何解决呢?
PS-我不想将c ++中的局部变量用作WPM的缓冲区。
答案 0 :(得分:-2)
我相信Windows API使用Pascal调用约定,将参数从左到右推入堆栈。使用C调用约定,您似乎在做相反的事情。