1.MFC扩展的DLL的编写与使用
1.1编写MFC扩展的DLL
File->New->Project 选择MFC DLL
Next 保持默认设置即可
在MFCDLL.def文件中添加导出函数名称HintBox,程序代码如下所示
; MFCDLL.def : 声明 DLL 的模块参数。
LIBRARY
EXPORTS
; 此处可以是显式导出
HintBox
LIBRARY
EXPORTS
; 此处可以是显式导出
HintBox
在头文件MFCDLL.h文件中声明导出函数,程序代码如下所示
// CMFCDLLApp
// 有关此类实现的信息,请参阅 MFCDLL.cpp
//
extern "C" void PASCAL EXPORT HintBox();
class CMFCDLLApp : public CWinApp
{
public:
CMFCDLLApp();
// 重写
public:
virtual BOOL InitInstance();
DECLARE_MESSAGE_MAP()
};
在文件MFCDLL.cpp中定义函数的实现部分,棋程序代码如下所示
extern "C" void PASCAL EXPORT HintBox()
{
AfxMessageBox("您工作的时间太长了,请休息片刻!");
}
{
AfxMessageBox("您工作的时间太长了,请休息片刻!");
}
最后 选择Build->"Build MFC DLL"命令。
1.2使用MFC扩展的DLL
1.2.1 静态链接
#include "mfcdll.h"
#pragma comment(lib,"mfcdll.lib")
void CusedllmfcDlg::OnBnClickedButton1()
{
HintBox();
}
#pragma comment(lib,"mfcdll.lib")
void CusedllmfcDlg::OnBnClickedButton1()
{
HintBox();
}
1.2.2动态链接
不需要加载头文件
typedef void(*fun)();
void CusedllmfcDlg::OnBnClickedButton1()
{
// TODO: 在此添加控件通知处理程序代码
fun f;
HINSTANCE hInstLibrary=LoadLibrary(_T("MFCDLL"));
if(hInstLibrary==NULL)
{
FreeLibrary(hInstLibrary);
MessageBox("DLL加载失败");
}
f=(fun)GetProcAddress(hInstLibrary,"HintBox");
if(f==NULL)
{
FreeLibrary(hInstLibrary);
MessageBox("HintBox函数加载失败");
}
f();
}
2Win32 DLL的创建和使用
2.1Win32DLL的创建
新建一个factorial.h文件,添加如下代码
#ifndef FACTORIAL_H
#define FACTORIAL_H
extern "C" _declspec(dllexport) int factorial(int n);
#endif
#define FACTORIAL_H
extern "C" _declspec(dllexport) int factorial(int n);
#endif
在factorial.cpp文件中,添加如下代码
// factorial.cpp : 定义 DLL 应用程序的导出函数。
//
#include "stdafx.h"
int factorial(int n)
{
n++;
return n;
}
//
#include "stdafx.h"
int factorial(int n)
{
n++;
return n;
}
选择"Build"->"Build factorial"命令 即可。
2.2Win32DLL的使用
使用方式同MFC的DLL一样,也具备两种方式。
转载于:https://blog.51cto.com/andydeng/838494