example to load function in dll
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.
ref
C/C++ | ShareTechnote: https://www.sharetechnote.com/html/C_LoadLibrary.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具