Dll中封装类

本文详细介绍了如何使用Visual Studio 2008和Delphi创建动态链接库(DLL),并封装了一个名为Cclassdll的类。包括创建DLL的过程、类的定义与实现,以及DLL的调用方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

在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”一文

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值