2015-01-26 54 views
0

我試圖在kernel32see my problem here and the first answer I got)中找到Windows API的SetProcessDEPPolicy函數的地址。從HANDLE到HINSTANCE的無效轉換(獲取內核函數的地址)

之前,所以我是有點失落,但我有這到目前爲止,我從來沒有寫過一個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解決此問題的好主意。

(?一旦我得到這個編譯,我怎麼能在指針的地址打印)

編輯:從評論中新增旋風的建議,得到同樣的錯誤Invalid conversion from HANDLE to HINSTANCE

+1

'FARPROC * funcAddr =(FARPROC *)' – Cyclonecode 2015-01-26 20:39:51

+0

@Cyclone感謝您指出了這一點,但它聲明爲指針得到完全同樣的錯誤。 '從HANDLE到HINSTANCE的無效對話' – Juicy 2015-01-26 20:41:03

+0

錯誤是由於將一個無效參數傳遞給'GetProcAddress()'而引起的,我相信。只需確保,一步調用該函數,然後將其轉換爲另一個「FARPROC *」。請參閱https://msdn.microsoft.com/en-us/library/windows/desktop/ms683199%28v=vs.85%29.aspx上的文檔,該函數返回一個'HMODULE',而不是'HANDLE'。 – 2015-01-26 20:45:45

回答

2

這是你應該怎麼辦它:

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

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

精美的作品!謝謝,我會盡可能給你點。 – Juicy 2015-01-26 20:44:18