通过GetProcAddress使用NtAllocateVirtualMemory()将不会编译

Using NtAllocateVirtualMemory() via GetProcAddress will not compile

本文关键字:编译 GetProcAddress 使用 NtAllocateVirtualMemory 通过      更新时间:2023-10-16

我正在尝试为一个项目使用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);