C/C++

 

 

 

 

Load Dll - LoadLibrary()

 

 

< Example 1 >

 

In this example, I will create a simple command line program that pops up a message box shown below using the API that is implemented in a dll called user32.dll (Windows system dll).

 

 

Following the full source code for this example.

 

// LoadLibrary_Ex01.cpp

#include "stdafx.h"

#include "windows.h"

#include "stdio.h"

 

typedef int (WINAPI *pUserMsgBox)(HWND, LPCSTR, LPCSTR, UINT);

 

int _tmain(int argc, _TCHAR* argv[])

{

  

    int ret = 0;

 

    HMODULE hDllUser32 = LoadLibrary(L"user32.dll");

    pUserMsgBox lpUserMsgBox = (pUserMsgBox)GetProcAddress(hDllUser32, "MessageBoxA");

    

    ret = lpUserMsgBox(NULL,"This is a MessageBox wrapping 'MessageBoxA()'", "Message Box Test", MB_OK);

 

    FreeLibrary(hDllUser32);

 

    printf("Return value = %d\n",ret);

 

    printf("Print Any Key to Quit \n");

    getchar();

 

}

 

typedef int (WINAPI *pUserMsgBox)(HWND, LPCSTR, LPCSTR, UINT);

 

Where does this come from ? you may easily understand 'pUserMsgBox' is a pointer (function pointer) that I want to assign a function from the specified dll. But where does 'int WINAPI' and '(HWND, LPCSTR, LPCSTR, UINT)' come from ? This is the information you have to know in advance. If it is windows system dll, you would easily get those information from Googling. If it is the dll provided by third party company, you should have API manual from the company. In this example, I am using a Windows system dll named user32.dll, so I could find API document easily as shown below. Refer to MessageBox function page. Once you have this API document, you may not need any further explanation about this line.

    int WINAPI MessageBox(

      _In_opt_ HWND    hWnd,

      _In_opt_ LPCTSTR lpText,

      _In_opt_ LPCTSTR lpCaption,

      _In_     UINT    uType

    );

 

 

HMODULE hDllUser32 = LoadLibrary(L"user32.dll");

 

This line loads "user32.dll" from Windows system directory (more specifically, from C:\Windows\System32) and assign the handle to the variable hDllUser32. If the dll is not the system dll, you need to specify the full path of the dll.  'L' is to convert the string "user32.dll" into Unicode format. If you remove 'L', you would get compilation error like 'error C2664: 'LoadLibraryW' : cannot convert parameter 1 from 'const char [31]' to 'LPCWSTR''.

 

 

pUserMsgBox lpUserMsgBox = (pUserMsgBox)GetProcAddress(hDllUser32, "MessageBoxA");

 

GetProcAddress searches the dll assigned to hDllUser32 and find the location (pointer) of MessageBoxA function within the dll and returns the function pointer. This returned function point is assigned to another pointer lpUserMsgBox. By this, you can use the original function "MessageBoxA" with a your own function name "lpUserMsgBox. "

 

 

ret = lpUserMsgBox(NULL,"This is a MessageBox wrapping 'MessageBoxA()'", "Message Box Test", MB_OK);

 

With this line, you can use MessageBoxA with your own function name. The result is same as running following function.

 

ret =MessageBoxA(NULL,"This is a MessageBox wrapping 'MessageBoxA()'", "Message Box Test", MB_OK);

 

 

FreeLibrary(hDllUser32);

 

This unloads the dll assigned to hDllUser32 and clears the pointer.