LoadLibrary
The LoadLibrary function maps the specified executable module into the address space of the calling process.
For additional load options, use the LoadLibraryEx function.
HMODULE LoadLibrary(
LPCTSTR lpFileName // file name of module
);
GetProcAddress
The GetProcAddress function retrieves the address of the specified exported dynamic-link library (DLL) function.
FARPROC GetProcAddress(
HMODULE hModule, // handle to DLL module
LPCSTR lpProcName // function name
);
FreeLibrary
The FreeLibrary function 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(
HMODULE hModule // 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代码如下
void CMfc01Dlg::OnButton1() { HMODULE hModule=LoadLibrary("project01.dll"); typedef int (*Function)(int x,int y); if (hModule) { Function Fuc=(Function)GetProcAddress(hModule,"add"); if (Fuc) { CString s; s.Format("3+1=%d",Fuc(3,1)); MessageBox(s); } FreeLibrary(hModule); } } |
图
备注
extern "C" __declspec(dllexport) int add(int x,int y) { return x+y; } extern "C" __declspec(dllexport) int sub(int x,int y) { return x-y; } |
相关链接