Skip to content

Latest commit

 

History

History

resolving-functions-runtime

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 

Resolving Functions at runtime

The C code demonstrates how to dynamically resolve the address of the MessageBoxA function from the user32.dll library at runtime using GetProcAddress and LoadLibrary. This method allows the function to be used without statically linking it in the program, which is often visible in the Import Address Table (IAT) of the executable.

Here’s the explanation of how it works:

  1. LoadLibrary Function: The LoadLibrary function is used to load a DLL into the process's memory space. In this case, LoadLibrary("User32.dll") loads the user32.dll library, which contains the MessageBoxA function among others. If the DLL is already loaded, this function just increments the reference count.

  2. GetProcAddress Function: This function retrieves the address of an exported function from the DLL loaded into memory. GetProcAddress(LoadLibrary("User32.dll"), "MessageBoxA") is used to get the address of MessageBoxA. This function pointer is stored in the variable address.

  3. Function Pointer: After obtaining the address, the code treats it as a function pointer MyMessageBox that can be used to call MessageBoxA. This is defined by casting address to the appropriate function type, which in this case includes parameters compatible with MessageBoxA.

By using LoadLibrary and GetProcAddress, the code doesn't depend on the linker to resolve the DLL function during compile time. This approach has a couple of implications:

  • Flexibility: The program can decide at runtime whether to use the function or not, which is particularly useful for optional functionality.
  • Evading Static Analysis: Since MessageBoxA is not statically linked, tools like IDA Pro won't show MessageBoxA as an imported function from user32.dll in the static analysis. This can be useful in scenarios where avoiding detection by static analysis tools is desired, such as in software protection schemes or certain types of malware.

This method is a form of "manual" function resolution that provides more control over which libraries and functions are loaded and used during the execution of a program.