Dll中封装类

 

在DLL中封装类,如果要在外面调用其对象的话,要通过一个函数来返回该对象的值,创建过程和平时一样!

 

一、实验环境

  本实例的编程工具及运行环境为:Windowsxp,visual studio 2008,Delphi 7

二、实验内容

1.用visual studio 2008建立一个动态链接库classdll.DLL,该库中封装了类Cclassdll。

2.用Delphi编写测试程序调用动态链接库classdll.DLL中的函数。

 

三、实验步骤

 1、参照“Visual studio2008编写dll”一文中“二、编写Non-MFC dll”的过程创建dll程序,

我的程序中包括以下文件:

Classdll.h  stdafx.h  classdll.cpp  dllmain.cpp  stdafx.cpp

 

2、Classdll.h中的内容如下:

// The following ifdef block is the standard way of creating macros which make exporting

// from a DLL simpler. All files within this DLL are compiled with the CLASSDLL_EXPORTS

// symbol defined on the command line. this symbol should not be defined on any project

// that uses this DLL. This way any other project whose source files include this file see

// CLASSDLL_API functions as being imported from a DLL, whereas this DLL sees symbols

// defined with this macro as being exported.

 

#define CLASSDLL_API __declspec(dllexport)

 

     // This class is exported from the classdll.dll

class   Cclassdll {

private:

     int num;

public:

     int  grow(int a);

     int getnum();

     Cclassdll();

     ~Cclassdll();

     // TODO: add your methods here.

};

 

extern "C"

{

 

CLASSDLL_API int createclassdll();

CLASSDLL_API void destroyclassdll(int classid);

CLASSDLL_API int growc(int classid,int a);

CLASSDLL_API int getnumc(int classid);

CLASSDLL_API int fnclassdll(int a,int b);

}

 

3、Class.cpp中的内容如下

// classdll.cpp : Defines the exported functions for the DLL application.

//

#include "stdafx.h"

#include <stdio.h>

#include "classdll.h"

 

// This is the constructor of a class that has been exported.

// see classdll.h for the class definition

int Cclassdll:: grow(int a)

{

     num=num+a;

     return num;

 

}

int Cclassdll:: getnum()

{

     return num;

 

}

 

Cclassdll::Cclassdll()

{

     num=1;

}

 

Cclassdll::~Cclassdll()

{

          

 

}

 

 

 

 

int createclassdll()

{

     Cclassdll* vcdlltry=new Cclassdll  ;

     return (int)vcdlltry;

 

}

 

void destroyclassdll(int classid)

{

Cclassdll* vcobj = (Cclassdll*)classid;

//vcobj->~Cclassdll();

delete vcobj;

vcobj=0;

 

}

int growc(int classid,int a)

{

     Cclassdll* vcobj = (Cclassdll*)classid;

     int c=vcobj->grow(a);

      return c;

    

}

int getnumc(int classid)

{

     Cclassdll* vcobj = (Cclassdll*)classid;

     int c=vcobj->getnum();

     return  c;

 }

// This is an example of an exported function.

int fnclassdll(int a , int b)

{

     return a+b;

}

 

编译即可,调用方式请参照“Visual studio2008编写dll”一文

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: C++的类是面向对象编程的基本单元,其封装是其最重要的特征之一。封装可确保数据和函数都能被保护起来,不会被外部访问和修改,而只能通过类定义的公共接口来操作这些数据。 Dll是动态链接库(Dynamic Link Library)的缩写,它是一种库文件,可以在编译时链接到可执行文件,也可以在运行时加载。封装在动态链接库使用可以更好地保障其安全性和灵活性。 在C++,类的封装可以被编译成动态链接库,也称为类库(Class Library)。这种方式可以使开发者将代码分成多个库文件,并且仅将需要的库文件链接到可执行文件。这种方式可以在多个程序之间共享类定义和函数实现,减少代码重复并提高代码的可维护性。 同时,如果只需修改类库的某个类的实现,而其他类的实现没有改变,那么只需重新编译该库就可以了,而不用重新编译所有代码。 在使用动态链接库时,需要使用C++的导出和导入关键字将类和函数导出到DLL,并在可执行程序使用它们。这些关键字包括__declspec(dllexport)和__declspec(dllimport)等。 总之,在C++使用类的封装dll可以使代码变得更加安全,可维护性更高,并有效地减少代码重复。 ### 回答2: C语言,类的封装需要使用结构体和函数来实现,其结构体表示对象的属性,而函数则表示对象的方法。为了更好地维护代码,实现代码的复用,我们可以将类的封装通过dll动态链接库的方式实现。 首先,我们需要定义一个包含结构体和函数声明的header文件,用来描述类的结构和功能,然后将这个header文件编译成动态链接库,供其他程序调用。这样做的好处在于,只要我们在其他程序引用了这个dll,就可以直接使用这个类,无需重新编写代码,提高了代码的复用性。 同时,在dll封装,我们也需要注意一些问题,比如动态链接库的版本问题,如果不同的程序引用了不同版本的dll,可能会出现运行错误,所以需要在编写dll时考虑这些问题。在使用dll时,也需要注意动态链接库的加载和释放问题,防止出现内存泄漏等问题。 总之,通过dll封装,可以更加方便地实现C语言的类的封装,提高代码的复用性和可维护性,但需要在实现过程注意一些问题,提高代码的质量。 ### 回答3: C++类的封装 DLL是一种将类库打包为可重用代码的方式。DLL全称为Dynamic Link Library,动态链接库。它是一种Windows系统下的动态链接库文件,提供了一种动态加载可以在运行时执行的代码和数据的方式。使用DLL的好处在于,它可以节省资源并提高程序的灵活性。 C++类的封装 DLL可以将类封装为可直接调用的动态链接库。这样,其他程序就可以直接使用这个类库,而无需自行编写相应的代码。使用DLL可以使程序员们更加专注于自己的工作而不必担心代码的实现细节。 封装DLL还有另外一些优点,例如:DLL文件可以被多个应用程序所共享,因此可以减少内存的消耗,提高程序的运行效率。同时,类库也可以在项目开发过程进行独立开发和单元测试,这样可以更好地提高程序的质量和可维护性。 总之,C++类的封装 DLL是一种非常有用的开发技术,它能够有效地提高程序的复用性、可移植性和可维护性。因此,对于需要维护大型项目的开发团队来说,封装DLL是一种非常具有实际意义的开发策略。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值