VC7.1环境下动态链接库的创建和调用[1]

[入库:2005年8月18日] [更新:2007年3月24日]

本文简介:选择自 youalwayscan 的 blog

一、   动态链接库的创建示例
1.      选择vc.net菜单项,选择文件->新建->项目,在弹出的新建项目的对话框中,选择项目类型为visual c++ 项目,类别为mfc的工程,在右边的模板中,选择mfc dll模板,给项目取名为testdll,选择好项目的位置,按确定健,进入应用程序设置。

2.       在应用程序设置中,我们可以看到,有三种dll类型,它们依次对应着三类dll。
                                            

        选择“使用共享的mfc dll”。
3.     点击完成,得到testdll工程。
4.     在testdll工程中新增mydll类,如下:
/*mydll.h*/
#pragma once
class afx_class_export cmydll
{
public:
 cmydll(void);
 ~cmydll(void);
 int max(int a, int b);
};
/*mydll.cpp */
#include "stdafx.h"
#include ".\mydll.h"
cmydll::cmydll(void)
{
}
cmydll::~cmydll(void)
{
}
int cmydll::max(int a, int b)
{
 if( a>=b )
  return a;
 else
  return b;
}
5.     在testdll工程中新增mydllh.h,如下:
/*mydllh.h*/
#pragma once
#pragma once
#include "mydll.h"
#ifdef _afxdll
#ifdef _debug
#define _md_comment "testdlld.lib"
#define _md_message "automatically linking with testdlld.dll"
#else
#define _md_comment "rtestdll.lib"
#define _md_message "automatically linking with testdlll.dll"
#endif
#endif
#pragma comment(lib, _md_comment)
#pragma message(_md_message)
6.      设置testdll工程的property,如下:
general->output directory                                    ..\..\debug
linker->general->output file                               $(outdir)/testdlld.dll
linker->advance->import library                        $(outdir)/testdlld.lib
7.      修改testdll.def,如下:
; testdll.def : declares the module parameters for the dll.
;library      "testdll"
exports
    ; explicit exports can go here
8.     build testdll。得到testdll.dll和testdll.lib位于..\..\debug目录下。

二、    动态链接库的调用示例
1.     创建mfc application 中的dialog based应用程序,得到testdllapply工程。
2.     在testdllapply工程中的stdafx.h中添加一项:
#include "mydllh.h";
3.     添加对话框中“确定”按钮的消息响应函数,如下:
void cmydllapllydlg::onbnclickedok()
{
 // todo: add your control notification handler code here
 cmydll  mydll;
 int i = mydll.max(2,9);
 onok();
}
4.        设置testdllapply工程的property,如下:
general->output directory                                                            ..\..\debug
c++->general->additional include directories                              testdll工程中mydll.cpp所在目录路径
linker->general->additional library directories                           testdll工程中debug文件夹的目录路径
linker->input->additional dependencies                                      testdll.lib

本文关键:VC7.1环境下动态链接库的创建和调用
  相关方案
Google
 

本站最佳浏览方式为 分辨率 1024x768 IE 6.0(或更高版本的 IE浏览器)

go top