通过GetProcAddress使用NtAllocateVirtualMemory()将不会编译
Using NtAllocateVirtualMemory() via GetProcAddress will not compile
我正在尝试为一个项目使用NtAllocateVirtualMemory,我相信其他人已经成功了,但这不会在VSC++2010上编译,也不会在mingw上编译。在两个编译器上都显示
FARPROC:调用的参数太多
有人知道我如何编译这些代码吗?谢谢你抽出时间。
FARPROC NtAllocateVirtualMemory;
NtAllocateVirtualMemory = GetProcAddress(GetModuleHandle("NTDLL.DLL"), "NtAllocateVirtualMemory");
printf( "NtAllocateVirtualMemory %08xn", NtAllocateVirtualMemory);
ReturnCode = NtAllocateVirtualMemory(GetCurrentProcess(),
&BaseAddress,
0,
&RegionSize,
MEM_COMMIT | MEM_RESERVE,
PAGE_EXECUTE_READWRITE);
您需要将结果从GetProcAddress
强制转换为正确类型的函数指针。在这种情况下:
typedef NTSTATUS WINAPI (*PNtAllocateVirtualMemory)(HANDLE ProcessHandle,
PVOID *BaseAddress,
ULONG_PTR ZeroBits,
PSIZE_T RegionSize,
ULONG AllocationType,
ULONG Protect);
FARPROC NAVM = GetProcAddress(...);
PNtAllocateVirtualMemory NtAllocateVirtualMemory = (PNtAllocateVirtualMemory)NAVM;
...
当然,简单地使用VirtualAlloc
会容易得多。
VirtualAlloc(&BaseAddress, RegionSize, MEM_COMMIT | MEM_RESERVE,
PAGE_EXECUTE_READWRITE);