类似地, msgbox 被 typedef 转换为函数指针类型,所以 me 是 msgbox ,不是 msgbox * . GetProcAddress 失败的原因是因为user32.dll导出了两个函数 MessageBoxA 和 MessageBoxW .当您简单地在代码中调用 MessageBox 时,Windows.h中定义的宏会根据是否为 UNICODE 进行编译将其替换为2个实际函数名称之一..但是,当您尝试直接访问导出的函数时,需要显式指定要获取指针的函数. #include< iostream>#include< windows.h>typedef int(__ stdcall * msgbox)(HWND,LPCSTR,LPCSTR,UINT);int main(无效){HMODULE hModule = :: LoadLibrary(L"User32.dll");msgbox me = NULL;if(hModule!= NULL){我= reinterpret_cast< msgbox>(:: GetProcAddress(hModule,"MessageBoxA"));}if(我!= NULL){(* me)(NULL,我是一个消息框",你好",MB_OK);}if(hModule!= NULL){:: FreeLibrary(hModule);}} I want to call MessageBox() function in such way:1). load needed library2). get the function address3). call itSo, for such aim as I understand, I should define new type with all types of arguments in MessageBox function.It returnes INT and accepts: HWND, LPCSTR, LPCSTR, UNIT.So I registred new type:typedef int(__stdcall *msgbox)(HWND, LPCSTR, LPCSTR, UINT);I have problems with calling such function. Does such way work for all functions or only for exported?How can I call MessageBox exactly in such way?Full code:#include <iostream>#include <windows.h>using namespace std;typedef int(__stdcall *msgbox)(HWND, LPCSTR, LPCSTR, UINT);int main(void){ HINSTANCE__ *hModule = LoadLibrary(L"\\Windows\\System32\\User32.dll"); msgbox *me = 0; if(hModule != 0) { me = (msgbox*)GetProcAddress(hModule, "MessageBox"); } return 0;} 解决方案 Why are you declaring everything as a pointer?LoadLibrary returns an HMODULE, not an HINSTANCE__ * (it will work with the latter but it's always better to adhere to the documentation).Similarly, msgbox is typedef'd to a function pointer type, so me is a msgbox, not a msgbox *.The reason why GetProcAddress fails is because user32.dll exports 2 functions, MessageBoxA and MessageBoxW. When you simply call MessageBox in your code, macros defined in Windows.h replace it with one of the 2 actual function names depending on whether you're compiling for UNICODE or not. But when you're trying to directly access the exported function as you are doing, you need to explicitly specify which one you're trying to get a pointer to. #include <iostream>#include <windows.h>typedef int(__stdcall *msgbox)(HWND, LPCSTR, LPCSTR, UINT);int main(void){ HMODULE hModule = ::LoadLibrary(L"User32.dll"); msgbox me = NULL; if( hModule != NULL ) { me = reinterpret_cast<msgbox>( ::GetProcAddress(hModule, "MessageBoxA") ); } if( me != NULL ) { (*me)( NULL, "I'm a MessageBox", "Hello", MB_OK ); } if( hModule != NULL ) { ::FreeLibrary( hModule ); }} 这篇关于如何使用GetProcAddress函数调用MessageBox?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
07-25 01:17