I want to call MessageBox() function in such way:
1). load needed library
2). get the function address
3). call it
So, 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?
LoadLibraryreturns anHMODULE, not anHINSTANCE__ *(it will work with the latter but it’s always better to adhere to the documentation).Similarly,
msgboxistypedef‘d to a function pointer type, someis amsgbox, not amsgbox *.The reason why
GetProcAddressfails is because user32.dll exports 2 functions,MessageBoxAandMessageBoxW. When you simply callMessageBoxin your code, macros defined in Windows.h replace it with one of the 2 actual function names depending on whether you’re compiling forUNICODEor 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.