动态链接库DLL

静态链接库的使用方法

示例代码:http://pan.baidu.com/s/1i303qZV

1.创建工程

image

2.向工程中添加.cpp 和.h

3.头文件代码

#ifdef  MyDLL_API 

#else
#define  MyDLL_API _declspec(dllexport)

#endif
MyDLL_API int Add(int a,int b);
MyDLL_API int Sub(int a,int b);
4.CPP中的代码
#define  MyDLL_API _declspec(dllexport)
#include "DLL.h"
int Add(int a,int b)
{
	return a+b;
}	

int Sub(int a,int b)
{
	return a-b;
} 
静态链接库的调用:
1.讲.DLL .LIB  .H 复制到工程目录下
2.工程--设置--链接  将.lib文件添加到链接中 多个用空格隔开
3.添加头文件
4.直接使用方法
 
动态链接库的创建:http://pan.baidu.com/s/1qWJlVBm

1.创建工程

image

2.向工程中添加.cpp 和.def

3.cpp代码

 int _stdcall Add(int a,int b)
{

	return a+b;
}	

int  _stdcall Sub(int a,int b)
{
	return a-b;
} 

3.  .def代码

LIBRARY DLL

EXPORTS
Add
Sub
动态链接库的使用:
1.将DLL复制到工程目录下
2.添加以下代码
void CTestDllDlg::OnBtAdd() 
{
	// TODO: Add your control notification handler code here
	CString str;
	HINSTANCE hInst;
	hInst=LoadLibrary("DLL.dll");
	typedef int (_stdcall *ADDPROC)(int a,int b);
	ADDPROC Add=(ADDPROC)GetProcAddress(hInst,"Add");
	if(!Add)
	{
		MessageBox("获取函数地址失败!");
		return;
	}
	str.Format("5+3=%d",Add(5,3));
	MessageBox(str);
}

void CTestDllDlg::OnBtSub() 
{
	// TODO: Add your control notification handler code here
	CString str;
	HINSTANCE hInst;
	hInst=LoadLibrary("DLL.dll");
	typedef int (_stdcall *SUBPROC)(int a,int b);
	SUBPROC Sub=(SUBPROC)GetProcAddress(hInst,"Sub");
	if(!Sub)
	{
		MessageBox("获取函数地址失败!");
		return;
	}
	str.Format("5-3=%d",Sub(5,3));
	MessageBox(str);
}
 
posted @ 2015-01-02 23:17  qq921201008  阅读(228)  评论(0编辑  收藏  举报