2013-03-28 707 views
3
BOOL (WINAPI *gmse)(LPMEMORYSTATUSEX) = GetProcAddress(
       kernel32, "GlobalMemoryStatusEx"); 

這是在.cpp文件中。編譯上面的代碼時,我得到了下面的錯誤。無法從'FARPROC'轉換爲'BOOL(__cdecl *)(LPMEMORYSTATUSEX)'

error C2440: 'initializing' : cannot convert from 'FARPROC' to 'BOOL (__cdecl *)(LPMEMORYSTATUSEX)' 
    This conversion requires a reinterpret_cast, a C-style cast or function-style cast 

我似乎無法找出什麼我應該投的GetProcAddress功能。 有人可以請指出我在正確的方向嗎?

謝謝

回答

6

您需要將其轉換爲函數指針類型。到simplfy,使用typedef爲函數指針類型:

typedef BOOL (WINAPI *gmse_t)(LPMEMORYSTATUSEX); 

gmse_t gmse = (gmse_t)GetProcAddress(kernel32, "GlobalMemoryStatusEx"); 

MSDN上GetProcAddress()參考頁提供了示例代碼。

+0

爲宏保留ALL UPPERCASE標識符是一個好主意,以減少無意識文本替換的可能性。 – 2013-03-28 11:13:31

+0

@ Cheersandhth.-Alf,關於替代點。更新。 – hmjd 2013-03-28 11:16:35

0

您需要投射從GetProcAddress獲得的通用指針。

所以,而不是目前的

BOOL (WINAPI *gmse)(LPMEMORYSTATUSEX) = GetProcAddress(
      kernel32, "GlobalMemoryStatusEx"); 

auto const gmse = reinterpret_cast<BOOL (WINAPI*)(LPMEMORYSTATUSEX)>(
    GetProcAddress(kernel32, "GlobalMemoryStatusEx") 
    ); 

除了補充說const我會爲函數指針使用更多的自我記錄的名字一樣,以及怎麼樣稱它爲GLobalMemoryStatusEx

相關問題