从 HANDLE 到 HINSTANCE 的无效转换(获取内核函数的地址)

Invalid conversion from HANDLE to HINSTANCE (Getting a kernel function's address)

我正在尝试在 kernel32 (see my problem here and the first answer I got) 中找到 Windows API 的 SetProcessDEPPolicy 函数的地址。

我以前从未编写过 Windows C++ 程序,所以我有点迷茫,但到目前为止我有这个:

#include <windows.h>
#include <iostream>

int main(int argc, char* argv[])
{
    HANDLE kernel32 = GetModuleHandle("kernel32");
    FARPROC* funcAddr = (FARPROC *) GetProcAddress(kernel32, "SetProcessDEPPolicy");
    std::cout << "@ ";
}

我在第 7 行收到以下错误:

C:\Documents and Settings\John\Desktop>c++ finddep.cpp -o finddep.exe finddep.cpp: In function 'int main(int, char**)': finddep.cpp:7:79: error: invalid conversion from 'HANDLE {aka void*}' to 'HINSTA NCE' [-fpermissive]   FARPROC funcAddr = (FARPROC *) GetProcAddress(kernel32, "SetProcessDEPPolicy") ;
                                                                               ^
In file included from c:\mingw\include\windows.h:50:0,
from finddep.cpp:1: c:\mingw\include\winbase.h:1675:27: error:   initializing argument 1 of 'int (__ attribute__((__stdcall__)) * GetProcAddress(HINSTANCE, LPCSTR))()' [-fpermissive ]  WINBASEAPI FARPROC WINAPI GetProcAddress(HINSTANCE,LPCSTR);
^ finddep.cpp:7:79: error: cannot convert 'int (__attribute__((__stdcall__)) **)() ' to 'FARPROC {aka int (__attribute__((__stdcall__)) *)()}' in initialization   FARPROC funcAddr = (FARPROC *) GetProcAddress(kernel32, "SetProcessDEPPolicy") ;

我无法从 Google 中找到任何解决此问题的好主意。

(一旦我得到这个编译,我怎样才能在指针中打印地址?)

编辑:从评论中添加了 Cyclone 的建议,得到同样的错误 Invalid conversion from HANDLE to HINSTANCE

您应该这样做:

#include <windows.h>
#include <iostream>

int main(int argc, char* argv[])
{
    HMODULE kernel32 = GetModuleHandleA("kernel32");
    FARPROC *funcAddr = (FARPROC *)GetProcAddress(kernel32, "SetProcessDEPPolicy");
    std::cout << "@" << funcAddr;
}