0

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

Community
  • 1
  • 1
Juicy
  • 11,840
  • 35
  • 123
  • 212
  • 1
    `FARPROC* funcAddr = (FARPROC *) ` – Cyclonecode Jan 26 '15 at 20:39
  • @Cyclone Thanks for pointing that out, but get exactly same error by declaring it as pointer. `Invalid conversaion from HANDLE to HINSTANCE` – Juicy Jan 26 '15 at 20:41
  • The error is caused by passing an invalid parameter to `GetProcAddress()`, I believe. Just to make sure, call that function in one step and then convert it to a `FARPROC*` in a second one. See the documentation at https://msdn.microsoft.com/en-us/library/windows/desktop/ms683199%28v=vs.85%29.aspx, the function returns a `HMODULE`, not a `HANDLE`. – Ulrich Eckhardt Jan 26 '15 at 20:45
  • Also check out the return type of `GetProcAddress()` and stop listening to @Cyclone as long as they suggest blindly casting in order to silence the compiler. For use with `GetProcAddress()`, you will need a cast, but you should be able to use `reinterpret_cast`, I believe. – Ulrich Eckhardt Jan 26 '15 at 20:50

1 Answers1

3

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;
}
Axalo
  • 2,953
  • 4
  • 25
  • 39