一、创建MFC动态链接库
创建一个MFC规则动态链接库(MFCDLL)
在MFCDLL.CPP末尾处加入实现代码:
 int sum(int a,int b)
int sum(int a,int b)


 {
{
 return a+b;
    return a+b;
 }
}
 int sub(int a,int b)
int sub(int a,int b)


 {
{
 return a-b;
    return a-b;
 }
}
 int mul(int a,int b)
int mul(int a,int b)


 {
{
 return a*b;
    return a*b;
 }
}
 int pdiv(int a,int b)
int pdiv(int a,int b)


 {
{
 return a/b;
    return a/b;
 }
}
 void Message()
void Message()


 {
{
 AfxMessageBox("message from dll");
    AfxMessageBox("message from dll");
 }
}
把函数声明写在.def模块定义文件中,编译器根据这个文件的函数声明来生成LIB文件,如果将函数声明写入头文件中,编译器编译程序时还是以.def中的声明为主。
把函数声明放在EXPORTS语句下面:

 MFCDLL.def
MFCDLL.def
 ; MFCDLL.def : Declares the module parameters for the DLL.
; MFCDLL.def : Declares the module parameters for the DLL.

 LIBRARY      "MFCDLL"
LIBRARY      "MFCDLL"
 DESCRIPTION  'MFCDLL Windows Dynamic Link Library'
DESCRIPTION  'MFCDLL Windows Dynamic Link Library'

 EXPORTS
EXPORTS
 ; Explicit exports can go here
    ; Explicit exports can go here
 sum @1;
    sum @1;
 sub @2;
    sub @2;
 mul @3;
    mul @3;
 pdiv @4;
    pdiv @4;
 Message @5;
    Message @5;表示函数的序号
完成上述两步,通过编译就生成DLL了。生成了MFCDLL.DLL和MFCDLL.LIB两个文件。
最后,写个简单的测试程序:
新建一个MFC对话框工程,命名为test,复制MFCDLL.DLL到测试程序所在目录,用隐式连接来调用DLL中的函数,在实现文件testDlg.cpp的预处理中添加如下代码:
 #pragma comment(lib,"MFCDLL")
#pragma comment(lib,"MFCDLL")
 int sum(int a,int b);
int sum(int a,int b);
 int sub(int a,int b);
int sub(int a,int b);
 int mul(int a,int b);
int mul(int a,int b);
 int pdiv(int a,int b);
int pdiv(int a,int b);
 void Message();
void Message();
最后将MFCDLL.lib文件也复制进测试程序所在目录,或者在测试程序Project/Setting,Link中将.lib模块的地址写进去,否则链接的时候会提示无法打开MFCDLL.lib文件。
二、创建Win32静态链接库(Win32Static)
向工程中添加实现文件 Win32DLL.cpp:

 Win32DLL.cpp
Win32DLL.cpp
 #include <windows.h>
#include <windows.h>
 #include "Win32DLL.h"
#include "Win32DLL.h"

 int sum(int a,int b)
int sum(int a,int b)


 {
{
 return a+b;
    return a+b;
 }
}
 int sub(int a,int b)
int sub(int a,int b)


 {
{
 return a-b;
    return a-b;
 }
}
 int mul(int a,int b)
int mul(int a,int b)


 {
{
 return a*b;
    return a*b;
 }
}
 int pdiv(int a,int b)
int pdiv(int a,int b)


 {
{
 return a/b;
    return a/b;
 }
}
 void Message()
void Message()


 {
{
 MessageBox(NULL,"message from static dll","DLL",MB_OK);
    MessageBox(NULL,"message from static dll","DLL",MB_OK);
 }
}用Win32 Static Library 方式创建静态链接库不用.def文件,但是需要头文件,向工程添加Win32Dll.h:

 Win32Dll.h
Win32Dll.h
 extern "C" __declspec(dllexport) int sum(int a,int b);
extern "C" __declspec(dllexport) int sum(int a,int b);
 extern "C" __declspec(dllexport) int sub(int a,int b);
extern "C" __declspec(dllexport) int sub(int a,int b);
 extern "C" __declspec(dllexport) int mul(int a,int b);
extern "C" __declspec(dllexport) int mul(int a,int b);
 extern "C" __declspec(dllexport) int pdiv(int a,int b);
extern "C" __declspec(dllexport) int pdiv(int a,int b);
 extern "C" __declspec(dllexport) void Message();
extern "C" __declspec(dllexport) void Message();用Win32创建的静态链接库只生成一个.lib文件,所以在链接时只能用隐式链接来调用,调用的时候在测试程序实现文件testDlg.cpp中加入下面代码:
 #include "Win32Dll.h"    //包含静态链接库的头文件
#include "Win32Dll.h"    //包含静态链接库的头文件
 #pragma comment(lib,"Win32Static") //隐式调用库文件
#pragma comment(lib,"Win32Static") //隐式调用库文件三、创建Win32动态链接库(Win32Dynic)
在Win32Dynic.cpp中添加实现函数
在Win32Dynic.h中添加函数声明
经过编译生成DLL文件和LIB文件。
	
posted on 2010-06-01 11:22 
CrazyNerd 阅读(997) 
评论(2)  编辑 收藏 引用  所属分类: 
Windows编程