[MFC]MFC下的DLL编程——扩展DLL
来源:互联网 发布:mac dock栏美化 编辑:程序博客网 时间:2024/04/29 03:40
扩展DLL实例
ExtDll项目
1.新建项目ExtDll:MFC DLL-->选择:MFC 扩展DLL(E)-->完成。
2.添加导出类:右击项目,添加类-->MFC类-->类名:CCompute,基类:CObject。
Compute.h文件
//Compute.h#pragma once// CCompute 命令目标class AFX_EXT_CLASS CCompute : public CObject{public:CCompute();virtual ~CCompute();public:int m_data1,m_data2;CCompute(int d1,int d2);int Add();int Sub();int Mul();double Div(); };AFX_EXT_CLASS int Mod(int d1,int d2);/*导出函数的方法2.关键字或宏 除了使用DEF文件来导出函数外,还可以在源程序中使用__declspec(dllexport)关键字或其替代宏AFX_EXT_CLASS: #define AFX_EXT_CLASS AFX_CLASS_EXPORT (定义在头文件afxv_dll.h中) #define AFX_CLASS_EXPORT __declspec(dllexport) (定义在头文件afxver_.h中) 来导出函数和整个C++类。 具体的格式为:导出整个类:class AFX_EXT_CLASS 类名[ : public基类]{……}导出类的成员函数:class 类名[ : public基类]{AFX_EXT_CLASS 返回类型 函数名1(……) ;AFX_EXT_CLASS 返回类型 函数名2(……) ;……}导出外部C格式的(全局)函数:extern "C" __declspec(dllexport) 返回类型 函数名(……) {……} 如果希望用MFC(C++)编写的规则DLL中的函数,也能够被非MFC程序来调用,需要为函数声明指定extern "C"。不然,C++编译器会使用C++类型安全命名约定(也称作名称修饰)和C++调用约定(使用此调用约定从C调用会很困难)。 为了使用方便,可以定义宏:#define DllExport extern "C" __declspec(dllexport)然后再使用它,例如:DllExport int Add(int d1, int d2) {……}*/Compute.cpp文件
// Compute.cpp : 实现文件#include "stdafx.h"#include "Compute.h"// CComputeCCompute::CCompute(){}CCompute::~CCompute(){}CCompute::CCompute(int d1,int d2){m_data1=d1;m_data2=d2;}int CCompute::Add(){return m_data1+m_data2;}int CCompute::Sub(){return m_data1-m_data2;}int CCompute::Mul(){return m_data1*m_data2;}double CCompute::Div(){if (m_data2 == 0){AfxMessageBox(L"Divided by zero!");return 0;}return (double)m_data1/m_data2;}int Mod(int d1,int d2){if (d2 == 0){AfxMessageBox(L"Divided by zero!");return 0;}return d1%d2;}// CCompute 成员函数
ExtClient项目
1.添加客户端项目ExtClient
2.设置依赖项:右击项目,引用-->添加引用 ExtDll.
3.关键代码 ExtClientDlg.h文件
ExtClientDlg.h文件
#include "../ExtDll/Compute.h"CCompute* m_pComp;//在类中添加
ExtClientDlg.cpp文件
void CExtClientDlg::OnBnClickedBntAdd(){ Comp(IDC_BNT_ADD);}void CExtClientDlg::OnBnClickedBntSub(){ Comp(IDC_BNT_SUB);}void CExtClientDlg::OnBnClickedBntMul(){ Comp(IDC_BNT_MUL);} void CExtClientDlg::OnBnClickedBntDiv(){ Comp(IDC_BNT_DIV);} void CExtClientDlg::OnBnClickedBntMod(){ Comp(IDC_BNT_MOD);}void CExtClientDlg::Comp(UINT nID){UpdateData();m_pComp =new CCompute(m_Data1,m_Data2);int r;double dr;switch (nID){case IDC_BNT_ADD:r=m_pComp->Add();break; case IDC_BNT_SUB:r=m_pComp->Sub();break; case IDC_BNT_MUL:r=m_pComp->Mul();break; case IDC_BNT_MIV:dr=m_pComp->Div();break; case IDC_BNT_MOD:r=Mod(m_Data1,m_Data2);break; }delete m_pComp;if (nID != IDC_BNT_DIV){SetDlgItemInt(IDC_EDIT_Result,r);}else{TCHAR buf[20];swprintf_s(buf,20,L"%g",dr);SetDlgItemText(IDC_EDIT_Result,buf);}}
4.效果
- [MFC]MFC下的DLL编程——扩展DLL
- [MFC]MFC下的DLL编程——规则DLL
- MFC扩展DLL编程
- DLL的MFC编程
- MFC--Extension DLL(扩展DLL)
- MFC--Extension DLL(扩展DLL)
- 静态.共享的规则MFC DLL/MFC扩展DLL详解
- 静态.共享的规则MFC DLL/MFC扩展DLL详解
- 静态、共享的规则MFC DLL,MFC扩展DLL详解
- MFC扩展dll
- 深入MFC扩展DLL
- MFC扩展DLL
- MFC扩展DLL 小结
- 深入MFC扩展DLL
- MFC扩展DLL
- MFC "扩展DLL" 制作
- MFC静态DLL,动态DLL,扩展DLL的区别
- MFC的DLL编程基础
- 后缀树与后缀数组
- 周流水0703
- myeclipse安装插件svn的步骤
- 在java的switch支持的类型
- Source Insight使用小技巧小结
- [MFC]MFC下的DLL编程——扩展DLL
- C#连接数据库sqlserver2005,并执行sql语句实例
- linux下用户管理(几个简单的命令)
- c++ 双向链表
- opencv无法解析外部文件
- Git 常用命令(说明)
- response和request的setCharacterEncoding
- Oracle sqlnet.ora, tnsnames.ora, listener.ora
- IOS开发~两个App互相调用