COM组件DLL编程

来源:互联网 发布:情侣礼物一对知乎 编辑:程序博客网 时间:2024/05/18 10:21

COM组件的DLL编程首先需准备COM组件的DLL资源。

创建一个 “Win32 Dynamic-Link Library” 动态链接库,命名为MathDLL,编译产生的DLL文件为 MathDLL.dll.

在工程文件中添加 ISimpleMath.h头文件,表示创建一个抽象基类ISimpleMath

代码:

#ifndef _ISimpleMath_Include
#define _ISimpleMath_Include

/************ ISimpleMath.h ******/

//定义抽象基类
class ISimpleMath{
public:
virtual int sum100(int m, int n)=0;
virtual int sum200(int m,int n)=0;
};
#endif

再添加 SimpleMath.h 和 SimpleMath.cpp ,创建一个继承于抽象基类(接口)ISimpleMath 的 SimpleMath类

SimpleMath 的头文件的代码:

#ifndef _SimpleMath_Include
#define _SimpleMath_Include

/******** SimpleMath.h ********/

#include "ISimpleMath.h" //接口的头文件

//创建一个继承于抽象基类的实现类
class SimpleMath:public ISimpleMath{
public:
int _declspec(dllexport) sum100(int m, int n);
int _declspec(dllexport) sum200(int m,int n);
};
#endif

SimpleMath.cpp 实现文件的代码:

/******** SimpleMath.cpp ********/

#include "SimpleMath.h"
int SimpleMath::sum100(int m,int n){
return m+n+100;
}
int SimpleMath::sum200(int m,int n){
return m+n+200;
}

编译以上工程即可获得MathDLL。

原文地址:http://blog.163.com/c_mulder/blog/static/25545465200722101746117/

原创粉丝点击