Dll中封装类

Dll中封装类

分类: C#功能实现2012-01-04 17:40 1211人阅读 评论(0) 收藏 举报

dlldelphiapiconstructorclassmacros

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

时间: 2024-08-01 05:11:24

Dll中封装类的相关文章

关于WPF 中 “System.Windows.Markup.XamlParseException”类型的未经处理的异常在 PresentationFramework.dll 中发生 异常的处理。

前几天做一个项目的时候出现了一个异常: “System.Windows.Markup.XamlParseException”类型的未经处理的异常在 PresentationFramework.dll 中发生 其他信息: “在“System.Windows.Markup.StaticResourceHolder”上提供值时引发了异常.”,行号为“8”,行位置为“33”. 现象描述: 在处理界面时,界面上增加了一个LISTBOX来处理接收来的数据,单击列表中的其中一项时会弹出详细信息.所以我就在样式

“System.InvalidOperationException”类型的未经处理的异常在 ESRI.ArcGIS.AxControls.dll 中发生

问题描述: 新手们进行ArcGIS ArcObject开发时经常会遇到各种十分古怪的问题,比如下面的这个问题: “System.InvalidOperationException”类型的未经处理的异常在 ESRI.ArcGIS.AxControls.dll 中发生 其他信息: ArcGIS version not specified. You must call RuntimeManager.Bind before creating any ArcGIS components. 此时虽然程序能正

MFC的DLL中实现定时器功能

方法一:创建一个线程, 反复读系统时间不就可以了? 如果定时要求不严,用Sleep就可以了.DWORD WINAPI TimerThread(LPVOID pamaram) { UINT oldTickCount, newTickCount; oldTickCount = GetTickCount(); //获取的是毫秒数 while(TRUE) { while(TRUE) { newTickCount = GetTickCount(); // 获取的是毫秒数 if(newTickCount -

[转]从普通DLL中导出C++类 – dllexport和dllimport的使用方法(中英对照、附注解)

这几天写几个小程序练手,在准备将一个类导出时,发现还真不知道如果不用MFC的扩展DLL,是怎么导出的.但我知道dllexport可以导出函数和变量,而且MFC扩展DLL就算是使用了MFC的功能,但能否导出类应该也不是必须用MFC才能够做到,一定是有相应的机制可以实现.于是查了一下MSDN,发现这个机制简单的可怕,原来就和导出函数一样,把dllexport关键字加到类名前就可以了.估计和我一样的同学大有人在,把MSDN的相关文档翻译出来,附上我的注解,希望对大家有用. 评注程序均在Visual S

如何隐藏DLL中,导出函数的名称?

一.引言 很多时候,我们写了一个Dll,不希望别人通过DLL查看工具,看到我们的导出函数名称.可以通过以下步骤实现: 1. 在def函数中做如下定义: LIBRARY EXPORTS HideFuncName @1 NONAME 通过添加NONAME关键字,隐藏函数名,这样的话,用Dependency Walker来查看该Dll,只能看到如下结果: 2. 如何调用该Dll中的函数,通过ID的方式调用,代码如下: 1 typedef void (WINAPI *FPHideFuncName)();

[转帖] 关于形如--error LNK2005: xxx 已经在 msvcrtd.lib ( MSVCR90D.dll ) 中定义--的问题分析解决

原文:http://www.cnblogs.com/qinfengxiaoyue/archive/2013/02/01/2889668.html 转自:http://hi.baidu.com/qinfengxiaoyue/item/ff262ccfb53b4c2ba0b50a89 引自:http://blog.csdn.net/sptoor/archive/2011/02/23/6203376.aspx 对全文有改动. 提示:阅读前需要对静态库(.lib).动态库(.dll).导入库(这个还是.

未经处理的异常在 System.Data.dll 中发生。其他信息:在应使用条件的上下文(在 &#39;***&#39; 附近)中指定了非布尔类型的表达式。

机房收费系统中,有些人在联合查询这个模块用的是存储过程,我先尝试着在数据库中建立了一个视图,然后在UI层做个判断并生成查询条件strCondition. 在机房收费系统的"联合查询"模块中出现的问题:"System.Data.SqlClient.SqlException"类型的未经处理的异常在 System.Data.dll 中发生.其他信息: 在应使用条件的上下文(在 '@strCondition' 附近)中指定了非布尔类型的表达式. 出错的DAL层代码为: Pu

WPF - 如何引用external dll中图片

前几天一直想引用别的DLL里面的一个图片.总是出现各种各样问题,一气之下将图片拷贝到当前Project中,运行良好.虽然知道引用图片,就1.2.列出来的2点就够了. 1. The Build Action should be Resource instead of Embedded Resource. 2. In the WPF application with the following XAML: <Image Source="/ClassLibraryName;Component/i

.Net中把图片等文件放入DLL中,并在程序中引用

[摘要] 有时我们需要隐藏程序中的一些资源,比如游戏,过关后才能看到图片,那么图片就必须隐藏起来,否则不用玩这个游戏就可以看到你的图片了,呵呵. 本文就讲述了如何把文件(比如图片,WORD文档等等) 隐藏到DLL中,然后在程序中可以自己根据需要导出图片进行处理. 注:本站原创,转载请注明本站网址:http://www.beinet.cn/blog/ [全文] 第1步:我们要生成一个资源文件,先把要隐藏的文件放入到这个资源文件中 (资源文件大致可以存放三种数据资源:字节数组.各种对象和字符串) 首