首页 > 代码库 > Dll中封装类
Dll中封装类
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”一文
Dll中封装类