Code:
#include <Windows.h>
//replacement for the MessageBoxApi Function:
typedef int (WINAPI* DynamicMessageBoxFunc)(HWND wnd, LPCSTR message, LPCSTR title, UINT icon);
//this is the actual replacement function:
int __stdcall DynamicMessageBox(HWND wnd, LPCSTR message, LPCSTR title, UINT icon)
{
BOOL FreeResult = 0, RunTimeLinkSuccess = 0; //variables for later use
HMODULE LibraryHandle = 0; //Here the handle to API module/dll will be stored.
DynamicMessageBoxFunc DynMsgBox = 0;
LibraryHandle = LoadLibrary("user32.dll"); //get the handle of our API module
//by loading it to the current process. MessageBoxA function is defined in user32.dll
//so it will now be loaded.
if (LibraryHandle != NULL) //if the library loading was successfull..
{
DynMsgBox = (DynamicMessageBoxFunc)GetProcAddress(LibraryHandle,
"MessageBoxA"); //Get the address of MessageBoxA in user32.dll
if (RunTimeLinkSuccess = (DynMsgBox != NULL)) //if operation was successful...
{
int ReturnValue = DynMsgBox(wnd, message, title, icon); //call messageboxa function
//from user32.dll
}
FreeResult = FreeLibrary(LibraryHandle); //after we called messagebox, unload user32.dll
//from this process...
return FreeResult; //routine was successful
}
return EXIT_FAILURE; //else, it failed
}
//now let's use it:
int main()
{
DynamicMessageBox(0, "Hello, this is a test!", "test!", 0);
return 0;
}