Results 1 to 1 of 1
  1. #1
    nullptr_t's Avatar
    Join Date
    Mar 2016
    Gender
    male
    Posts
    124
    Reputation
    10
    Thanks
    256

    C++ Call a function dynamically from a dll

    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;
    }

  2. The Following User Says Thank You to nullptr_t For This Useful Post:

    Anton Troskie (10-28-2016)

Similar Threads

  1. Call game function by it's address from specified class.
    By down.by.the.river in forum C++/C Programming
    Replies: 0
    Last Post: 04-22-2013, 03:19 AM
  2. Replies: 0
    Last Post: 04-23-2010, 03:39 PM
  3. [Help] New Update from WH.dll pls the LINK!!
    By SacredGold in forum CrossFire Hacks & Cheats
    Replies: 19
    Last Post: 02-26-2010, 11:17 AM
  4. [Help] Dealing with pointers from a dll
    By ctpsolo in forum C++/C Programming
    Replies: 11
    Last Post: 01-26-2010, 11:19 PM
  5. Help with hooking from a dll
    By Anddos in forum C++/C Programming
    Replies: 5
    Last Post: 12-21-2009, 08:11 AM