LoadLibrary

Win32 Dyanmic-Link Library的调用LoadLibrary关键点
LoadLibrary

TheLoadLibraryfunction maps the specified executable module into the address space of the calling process.

For additional load options, use theLoadLibraryExfunction.

HMODULE LoadLibrary(

LPCTSTRlpFileName// file name of module

);

GetProcAddress

TheGetProcAddressfunction retrieves the address of the specified exported dynamic-link library (DLL) function.

FARPROC GetProcAddress(

HMODULEhModule,// handle to DLL module

LPCSTRlpProcName// function name

);

FreeLibrary

TheFreeLibraryfunction decrements the reference count of the loaded dynamic-link library (DLL). When the reference count reaches zero,

the module is unmapped from the address space of the calling process and the handle is no longer valid.

BOOL FreeLibrary(

HMODULEhModule// handle to DLL module

);

实现过程

1.新建1个 MFC AppWizard(exe)项目,Project name:MFC01

2.选中Dialog Base对话框类型的程序。

3.删除多余的控件,添加1个按钮,编译一下。

4.将project01 Debug里面的project01.dll复制到MFC01的Debug目录下

5.调用dll代码如下


voidCMfc01Dlg::OnButton1(){HMODULEhModule=LoadLibrary("project01.dll");typedef int (*Function)(intx,inty);if (hModule){FunctionFuc=(Function)GetProcAddress(hModule,"add");if (Fuc){CStrings;s.Format("3+1=%d",Fuc(3,1));MessageBox(s);}FreeLibrary(hModule);}}

备注
注:LoadLibrary与只能调用 DLL 里面带 extern "C"的ADDPROC是一个变量ADDPROC SUB= (ADDPROC)GetProcAddress(hModule,"sub");如果调用Windows的Dlltypedef int (Function)(intx,inty);修改为typedef int (WINAPIFunction)(intx,inty);本例的DLL代码1.创建1个 Win32 Dyanmic-Link Library ,Project name:project012.选中 An empty DLL project.3.Ctrl+N 新建一个 C++ Source File ,File:project01Win32 Dyanmic-Link Library的创建


extern"C"__declspec(dllexport) intadd(intx,inty){returnx+y;}extern"C"__declspec(dllexport) int sub(intx,inty){returnx-y;}

相关链接

部分API无法直接使用需要调用系统DLL里面的API




通过 为知笔记 发布



附件列表

原文链接: https://www.cnblogs.com/xe2011/archive/2013/02/23/2923671.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/78704

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月9日 下午6:39
下一篇 2023年2月9日 下午6:39

相关推荐