|
本帖最后由 cpp2019 于 2021-3-7 12:37 编辑
今天来分享下DLL“动态调用”的两种“姿势”,注释不多写,假如看不懂就仔细看看方式一和方式二的区别,多上机试试。
调用姿势一代码
- // 调用方式一
- int Message(HWND hwnd)
- {
- HINSTANCE hInst = LoadLibrary(_T("test.dll"));
- if (!hInst)
- {
- MessageBox(hwnd, _T("载入DLL出错!"), _T("错误"), MB_ICONERROR);
- return 1;
- }
- typedef void(*PROC)(LPCTSTR Text, LPCTSTR Caption);
- PROC ShowMsg = (PROC)GetProcAddress(hInst, "Msg");
- if (ShowMsg)
- {
- ShowMsg(_T("中华人民共和国万岁"), _T("提示"));
- }
- FreeLibrary(hInst);
- return 0;
- }
复制代码
调用方式一截图
调用姿势二代码
- // 调用方式二
- int Message()
- {
- HINSTANCE hInst = LoadLibrary(_T("test.dll"));
- if (!hInst)
- {
- MessageBox(NULL, _T("载入DLL出错!"), _T("错误"), MB_ICONERROR);
- return 1;
- }
- typedef void(*PROC)(LPCTSTR Text, LPCTSTR Caption);
- PROC ShowMsg = (PROC)GetProcAddress(hInst, (LPSTR)1);
- if (ShowMsg)
- {
- ShowMsg(_T("伟大领袖毛主席万岁"), _T("提示"));
- }
- FreeLibrary(hInst);
- return 0;
- }
复制代码
调用方式二截图
Dll 部分代码
- extern "C" void _declspec(dllexport) Msg(TCHAR* lpText, TCHAR* lpCaption)
- {
- MessageBox(NULL, lpText, lpCaption, MB_OK);
- }
复制代码
完整项目打包(8.04KB)
|
评分
-
查看全部评分
上一篇: 求windows下mozjs-45.0.2编译方法下一篇: 解析命令行参数
|