一、编写动态链接库
首先利用VC++新建一个Win32 Dynamic-Link Library类型的工程,工程取名为:Dll1,并在AppWizard的第一步选择“An empty Dll project”选项,即创建一个空的动态链接库工程。然后为工程添加一个C++源文件:Dll1.cpp,一个C++头文件:Dll1.h。在Dll1.h中添加函数定义,其代码如下:
- #ifdef DLL_API
- #else
- #define DLL_API _declspec(dllimport)
- #endif
- DLL_API int add(int a,int b);
- DLL_API int sub(int a,int b);
#ifdef DLL_API
#else
#define DLL_API _declspec(dllimport)
#endif
DLL_API int add(int a,int b);
DLL_API int sub(int a,int b);
接下来在Dll.cpp中编写函数的实现代码,其代码如下:
- #define DLL_API _declspec(dllimport)
- #include "Dll1.h"
- int add(int a,int b)
- {
- return a+b;
- }
- int sub(int a,int b)
- {
- return a-b;
- }
#define DLL_API _declspec(dllimport)
#include "Dll1.h"
int add(int a,int b)
{
return a+b;
}
int sub(int a,int b)
{
return a-b;
}
编译连接后会生成Dll1.dll 和 Dll1.lib。
二、通过隐式链接使用动态链接库
建立一个基于对话框的MFC应用程序,工程取名为“DllTest”,在该工程的主对话框中放入两个按钮,按钮名称为Add 和Sub,按钮ID为IDC_BTN_ADD 和 IDC_BTN_SUBTRACT。然后在DllTest程序中,选择【Project/Settings】菜单命令,打开工程设置对话框,选择Link选项卡,在“Object/library modules"选项编辑框中输入dll1.lib。将刚刚编译动态链接库生成的两个文件Dll1.dll和Dll1.lib,还有其头文件都放入DllTest的工程目录下。然后在两个按钮的实现代码中添加dll的调用代码,其代码如下:
- #include "..\Dll1\Dll1.h"
- void CDllTestDlg::OnBtnAdd()
- {
- // TODO: Add your control notification handler code here
- CString str;
- str.Format("5+3=%d",add(5,3));
- MessageBox(str);
- }
- void CDllTestDlg::OnBtnSub()
- {
- // TODO: Add your control notification handler code here
- CString str;
- str.Format("5-3=%d",sub(5,3));
- MessageBox(str);
- }
#include "..\Dll1\Dll1.h"
void CDllTestDlg::OnBtnAdd()
{
// TODO: Add your control notification handler code here
CString str;
str.Format("5+3=%d",add(5,3));
MessageBox(str);
}
void CDllTestDlg::OnBtnSub()
{
// TODO: Add your control notification handler code here
CString str;
str.Format("5-3=%d",sub(5,3));
MessageBox(str);
}
编译链接运行,即可看到测试结果。
使用隐式加载方法比较简单,但是它会在程序运行中把所有使用到的dll都装入内存。所以如果动态链接库过大或者过多或者只在某一种情况下才使用,则适合使用动态链接方法。
三、通过动态链接使用动态链接库
动态链接方法主要是通过这两个API函数进行Dll函数的调用,这个方法不难只是有点麻烦。
HMOUDLE LoadLibrary(LPCTSTR lpFileName)
FARPROC GetProcAddress(HMOUDLE hMoudle, LPCSTR lpProcName)
使用动态链接的方法使用Dll只需将代码作如下修改即可,代码如下:
- #include "..\Dll1\Dll1.h"
- void CDllTestDlg::OnBtnAdd()
- {
- // TODO: Add your control notification handler code here
- /*CString str;
- str.Format("5+3=%d",add(5,3));
- MessageBox(str);*/
- HINSTANCE hInst;
- hInst = LoadLibrary("Dll1.dll");
- typedef int (*ADDPROC) (int a,int b);
- ADDPROC Add = (ADDPROC)GetProcAddress(hInst,"add");
- if(!Add)
- {
- MessageBox("获取函数地址失败");
- return;
- }
- CString str;
- str.Format("5+3=%d",add(5,3));
- MessageBox(str);
- }
#include "..\Dll1\Dll1.h"
void CDllTestDlg::OnBtnAdd()
{
// TODO: Add your control notification handler code here
/*CString str;
str.Format("5+3=%d",add(5,3));
MessageBox(str);*/
HINSTANCE hInst;
hInst = LoadLibrary("Dll1.dll");
typedef int (*ADDPROC) (int a,int b);
ADDPROC Add = (ADDPROC)GetProcAddress(hInst,"add");
if(!Add)
{
MessageBox("获取函数地址失败");
return;
}
CString str;
str.Format("5+3=%d",add(5,3));
MessageBox(str);
}
四、DllMain函数
BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
就跟Win32程序的入口是WinMain一样,Dll的入口就是DllMain。但是貌似这个函数不是必须的。并且书上不建议在这个函数中进行太复杂的调用,因为当你的这个动态链接库被载入时,系统模块(比如user32.dll或者GDI32.dll)可能还没有被载入。如果自己编写的DllMain函数需要调用这两个核心DLL中的某些函数的话,这时就会失败,从而导致程序终止。
自己的话:其实还有一些问题没有解决,怎么创建并使用MFC的DLL,普通动态链接库工程如何使用.def文件,以后有机会的话一定会弄明白再写上来的。
最后感谢孙鑫老师的《VC++深入详解》,这里的内容大部分都是出自他的书上的,我只是改了下顺序而已。