用VC++ 6.0编写和使用动态链接库



一、编写动态链接库

首先利用VC++新建一个Win32 Dynamic-Link Library类型的工程,工程取名为:Dll1,并在AppWizard的第一步选择“An empty Dll project”选项,即创建一个空的动态链接库工程。然后为工程添加一个C++源文件:Dll1.cpp,一个C++头文件:Dll1.h。在Dll1.h中添加函数定义,其代码如下:

  1. #ifdef DLL_API  
  2.   
  3. #else  
  4. #define DLL_API _declspec(dllimport)  
  5. #endif  
  6.   
  7. DLL_API int add(int a,int b);  
  8. 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中编写函数的实现代码,其代码如下:
  1. #define DLL_API _declspec(dllimport)  
  2. #include "Dll1.h"  
  3.   
  4. int add(int a,int b)  
  5. {  
  6.     return a+b;  
  7. }  
  8.   
  9. int sub(int a,int b)  
  10. {  
  11.     return a-b;  
  12. }  
#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的调用代码,其代码如下:

  1. #include "..\Dll1\Dll1.h"  
  2.   
  3. void CDllTestDlg::OnBtnAdd()   
  4. {  
  5.     // TODO: Add your control notification handler code here  
  6.     CString str;  
  7.     str.Format("5+3=%d",add(5,3));  
  8.     MessageBox(str);  
  9. }  
  10.   
  11. void CDllTestDlg::OnBtnSub()   
  12. {  
  13.     // TODO: Add your control notification handler code here  
  14.     CString str;  
  15.     str.Format("5-3=%d",sub(5,3));  
  16.     MessageBox(str);  
  17. }  
#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函数的调用,这个方法不难只是有点麻烦。

  1. HMOUDLE LoadLibrary(LPCTSTR lpFileName)  
  2. FARPROC  GetProcAddress(HMOUDLE hMoudle, LPCSTR lpProcName)  
HMOUDLE LoadLibrary(LPCTSTR lpFileName)
FARPROC  GetProcAddress(HMOUDLE hMoudle, LPCSTR lpProcName)
使用动态链接的方法使用Dll只需将代码作如下修改即可,代码如下:
  1. #include "..\Dll1\Dll1.h"  
  2.   
  3. void CDllTestDlg::OnBtnAdd()   
  4. {  
  5.     // TODO: Add your control notification handler code here  
  6.     /*CString str; 
  7.     str.Format("5+3=%d",add(5,3)); 
  8.     MessageBox(str);*/  
  9.     HINSTANCE hInst;  
  10.     hInst = LoadLibrary("Dll1.dll");  
  11.     typedef int (*ADDPROC) (int a,int b);  
  12.     ADDPROC Add = (ADDPROC)GetProcAddress(hInst,"add");  
  13.     if(!Add)  
  14.     {  
  15.         MessageBox("获取函数地址失败");  
  16.         return;  
  17.     }  
  18.     CString str;  
  19.     str.Format("5+3=%d",add(5,3));  
  20.     MessageBox(str);  
  21. }  
#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函数

  1. BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)  
BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)

就跟Win32程序的入口是WinMain一样,Dll的入口就是DllMain。但是貌似这个函数不是必须的。并且书上不建议在这个函数中进行太复杂的调用,因为当你的这个动态链接库被载入时,系统模块(比如user32.dll或者GDI32.dll)可能还没有被载入。如果自己编写的DllMain函数需要调用这两个核心DLL中的某些函数的话,这时就会失败,从而导致程序终止。


自己的话:其实还有一些问题没有解决,怎么创建并使用MFC的DLL,普通动态链接库工程如何使用.def文件,以后有机会的话一定会弄明白再写上来的。

最后感谢孙鑫老师的《VC++深入详解》,这里的内容大部分都是出自他的书上的,我只是改了下顺序而已。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值