c++常规dll书写
DLL的建立与使用
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
()导出函数
1:导出函数
__declspec(dllexport) void ShowDlg(void)
{
CDllDialog dllDialog;
dllDialog.DoModal ();
}
2:修改.def文件,加上函数名称
; DrawTestDll.def : Declares the module parameters for the DLL.
LIBRARY "DrawTestDll"
DESCRIPTION 'DrawTestDll Windows Dynamic Link Library'
EXPORTS
; Explicit exports can go here
ShowDlg@1
3:将DLL文件复制到程序所在目录
4:在程序里调用DLL
void CDllTestView::OnLButtonDown(UINT nFlags, CPoint point)
{
typedef void (*lpFun)(void);
HINSTANCE hDll; //DLL句柄
hDll = LoadLibrary("DrawTestDll.dll"); //加载DLL资源
if (NULL==hDll) //寻找顺序,EXE所在目录,工程所在目录,System、windows目录
{
MessageBox("DLL加载失败");
}
lpFun addFun; //函数指针
addFun = (lpFun)GetProcAddress(hDll,"ShowDlg"); //得到函数ShowDlg的地址
if (NULL==addFun)
{
MessageBox("DLL中函数寻找失败");
}
HINSTANCE exe_hinstance=AfxGetResourceHandle(); //得到主程序的实例句柄
AfxSetResourceHandle(hDll); //设置主程序的句柄为DLL实例(利用DLL里的资源)
addFun(); //调用函数
AfxSetResourceHandle(exe_hinstance);//重新设置主程序的实例句柄
FreeLibrary(hDll); //赦放DLL资源
}
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
DLL中类的使用
1:DLL中添加新类
类的.h文件:
//导出一个类(包括其方法、属性)
class _declspec(dllexport) CDllClass {
public:
CDllClass(void);
void MSG(const char * const str);
};
类的.cpp文件:
#include "stdafx.h"
#include "DllClass.h"
CDllClass::CDllClass()
{
return;
}
void CDllClass::MSG(const char * const str)
{
MessageBox(NULL,str,"",MB_OK);
}
2:将编译好的DLL文件、Lib文件和类的头文件复制到工程目录
3:修改类的头文件(将dll的头文件中为 dllexport,在应用文件中为dllimport)
//导入一个类(包括其方法、属性)
class _declspec(dllimport) CDllClass {
public:
CDllClass(void);
void MSG(const char * const str);
};
4:在调用类的实现函数的文件里加上类的头文件和Lib文件的引用
#include "DllClass.h" //头文件的引入,是程序可以定义类的对象
#pragma comment(lib,"DrawDll.lib") //具体函数的定义
正常使用即可
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
()导出函数
1:导出函数
__declspec(dllexport) void ShowDlg(void)
{
CDllDialog dllDialog;
dllDialog.DoModal ();
}
2:修改.def文件,加上函数名称
; DrawTestDll.def : Declares the module parameters for the DLL.
LIBRARY "DrawTestDll"
DESCRIPTION 'DrawTestDll Windows Dynamic Link Library'
EXPORTS
; Explicit exports can go here
ShowDlg@1
3:将DLL文件复制到程序所在目录
4:在程序里调用DLL
void CDllTestView::OnLButtonDown(UINT nFlags, CPoint point)
{
typedef void (*lpFun)(void);
HINSTANCE hDll; //DLL句柄
hDll = LoadLibrary("DrawTestDll.dll"); //加载DLL资源
if (NULL==hDll) //寻找顺序,EXE所在目录,工程所在目录,System、windows目录
{
MessageBox("DLL加载失败");
}
lpFun addFun; //函数指针
addFun = (lpFun)GetProcAddress(hDll,"ShowDlg"); //得到函数ShowDlg的地址
if (NULL==addFun)
{
MessageBox("DLL中函数寻找失败");
}
HINSTANCE exe_hinstance=AfxGetResourceHandle(); //得到主程序的实例句柄
AfxSetResourceHandle(hDll); //设置主程序的句柄为DLL实例(利用DLL里的资源)
addFun(); //调用函数
AfxSetResourceHandle(exe_hinstance);//重新设置主程序的实例句柄
FreeLibrary(hDll); //赦放DLL资源
}
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
DLL中类的使用
1:DLL中添加新类
类的.h文件:
//导出一个类(包括其方法、属性)
class _declspec(dllexport) CDllClass {
public:
CDllClass(void);
void MSG(const char * const str);
};
类的.cpp文件:
#include "stdafx.h"
#include "DllClass.h"
CDllClass::CDllClass()
{
return;
}
void CDllClass::MSG(const char * const str)
{
MessageBox(NULL,str,"",MB_OK);
}
2:将编译好的DLL文件、Lib文件和类的头文件复制到工程目录
3:修改类的头文件(将dll的头文件中为 dllexport,在应用文件中为dllimport)
//导入一个类(包括其方法、属性)
class _declspec(dllimport) CDllClass {
public:
CDllClass(void);
void MSG(const char * const str);
};
4:在调用类的实现函数的文件里加上类的头文件和Lib文件的引用
#include "DllClass.h" //头文件的引入,是程序可以定义类的对象
#pragma comment(lib,"DrawDll.lib") //具体函数的定义
正常使用即可