c++windowskernel32getprocaddress

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


I'm trying to find the address of the SetProcessDEPPolicy function of the Windows API in kernel32 (see my problem here and the first answer I got).

I've never written a Windows C++ program before so I'm a bit lost but I have this so far:

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

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

I'm getting the following error on line 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") ;

I couldn't find any good ideas on how to solve this from Google.

(Once I get this to compile, how can I print the address in the pointer?)

EDIT: Added Cyclone's suggestion from the comment, getting same error Invalid conversion from HANDLE to HINSTANCE


Solution

  • This is how you should do it:

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