通过GetProcAddress使用NtAllocateVirtualMemory()将无法编译

时间:2014-10-16 21:11:57

标签: c++ winapi

我试图将NtAllocateVirtualMemory用于项目,并且我确信其他人已经取得了成功,但这不会在VSC ++ 2010上编译,也不会编译。在两个编译器上都说

FARPROC:呼叫

的参数太多了

有谁知道如何编译这段代码?谢谢你的时间。

FARPROC NtAllocateVirtualMemory;

NtAllocateVirtualMemory = GetProcAddress(GetModuleHandle("NTDLL.DLL"), "NtAllocateVirtualMemory");

printf( "NtAllocateVirtualMemory %08x\n", NtAllocateVirtualMemory);

ReturnCode = NtAllocateVirtualMemory(GetCurrentProcess(),
                                     &BaseAddress,
                                     0,
                                     &RegionSize,
                                     MEM_COMMIT | MEM_RESERVE,
                                     PAGE_EXECUTE_READWRITE);

1 个答案:

答案 0 :(得分:2)

您需要将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);