利用文件接口调用Delphi编写的API函数

利用文件接口调用Delphi编写的API函数


2024年5月13日发(作者:)

维普资讯

!墨墨 QQ墨:全 曼 

承德石油高等专科学校学报 

Journal of Chengde Petroleum College 

第4卷第3期2002年9月 

Vo1.4 No.3 Sep.2002 

CN13一l265/TE 

利用文件接口调用Delphi编写的API函数 

马晓晨 

(承德石油高等专科学校计算机与信息工程系,河北承德067000) 

摘要:在研究Windows操作系统API函数的基础上,深入分析了不同开发工具所编写的API函数的调 

用过程,通过事例提出了在VB环境中开发的应用程序利用文件接口调用Delphi编写的API函数的方 

法。 

关键词:接口l APIl函数调用 

中图分类号:TP31 文献标识码:B 文章编号:1008—9446(2002)03—0018—04 

动态链接库(Dynamic Link Library,简称DLL)是一些编译过的可执行代码模块,后缀名为DLL, 

可以在应用程序中或其他DLL中被调用。在Windows环境中,DLL可以实现多个应用程序共享代码 

和资源。Microsoft Windows本身就是由若干个DLL组成的,其它应用程序可以调用这些库中的过程, 

完成窗口与图形的显示、内存管理或其他任务。这些过程有时被称为Windows API,或应用程序接口。 

除了Windows提供的API之外,用户还可以开发自己的API,这种开发给程序开发带来了很多灵 

活性,使系统增加了许多新的功能和方法。而且DLL还独立于编程语言,利用某种开发工具编写的 

DLL可以在其他环境中使用,例如,在Delphi环境中开发的DLL程序可以在Visual C++环境中方便 

地使用;同时,在Delphi环境中也可以方便地使用在Visual C++开发的DLL。但是,在Delphi环境中 

编写的API函数并不是在所有的开发环境中都可以方便顺利的使用。本文以我在实际应用开发过程中 

调用Delphi环境中编写的API函数为例进行分析和说明。 

为了二次开发和扩展光谱采集与分析系统,购买了新的分光计设备,新设备功能强大,并提供了用 

Delphi编写的API函数,通过调用这些函数进行二次开发可使原系统的功能大大加强,原系统是用VB 

开发的,因此在扩展系统功能时需要在VB环境调用Delphi编写的API函数。下面谈一下调用时存在 

的问题及解决办法。 

l 在Visual Basic调用Delphi编写的API函数时出现的问题 

分光计提供的API函数主要有:初始化设备SWDinit、关闭设备SWDclose、设置接收频率 

SWDrate、接收数据SWDscan、关闭设备SWDclose,在程序设计过程中,需要对这5个函数进行调用。 

由于DLL过程存在于Visual Basic应用程序之外的文件中,在使用时必须指定过程的位置和调用参 

数。可以用Declare语句提供这类信息。在声明了DLL过程之后,可以把它当做自己的过程使用。程序 

如下: 

Option Explicit 

Declare Function SWDinit Lib"swdll 初始化设备 

eclDare Function SWDclose Lib"swdll 关闭设备 

Declare Function SWDrate Lib 'swdll”(ByVal chan As Integer) 设置接收频率 

Declare Function SWDscan Lib"swdll”(ByVal chan As Integer,buffer As Integer) 

收稿日期:2002-03—19 

作者简介:马晓晨(1968一),男,承德石油高等专科学校计算机与信息工程系副主任,讲师,主要从事教学管理与计 

算机教学和研究工作。 

维普资讯

马晓晨:利用文件接口调用Delphi编写的API函数 ・19・ 

As Long'接收数据,接收成功返回接收数据的个数,接受不成功返回O,chan为传送数据的通道 

号。 

dim arrayl(O to2050)As single; 

Private Sub Form

...——

Load() 

SWDinit,初始化设备 

SWDrate(5),设置接收频率 

End sub 

Private Sub Form Unload(Cancel As Integer) 

SWDclose 关闭设备 

End Sub 

Private Sub Command

...——

Click() 

SWDscan(1,array1()) 接受通道1的数据 

End Sub 

在程序执行过程中,不能正常接受数据,在调用SDWscan函数时采用其他传送参数地址的办法也 

没有成功。 

2 采用文件做接Iz/,实现VB对Delphi编写的API函数的调用 

通过研究,利用文件做为VB和Delphi应用程序之间传递参数的接口,取得了较好的运行效果。利 

用Delphi编写调用API函数的程序,并把所采集的数据实时写入文件。利用VB编写的分析系统通过 

读取文件中的数据实现了对外部数据的采集。程序如下: 

2.1利用Delphi编写调用API函数的程序 

unit collect 

此处省略一些Delphi的程序说明部分 

type 

atype=array[0.2050]of single;//定义接收数据的数组 

aptype= atype; 

TF0rml=class(TForm) 

∥定义数组指针变量 

//定义窗体 

Timerl:TTimer;//定义一个计时器控件定时采集数据,时间间隔属性初始化为1000。 

procedure Form Create(Sender:TObject); 

procedure Form Close(Sender:TObject;var Action;TClose Action); 

procedure Timerl Timer(Sender:TObjcet); 

end; 

var //定义变量 

Forml:TForml; 

pl:aptype;f:texfile;i:integer;s:string; 

i mplementation 

procedure SWDinit;stdcall; 

∥声明初始化函数 

external'swdll'indexl 

function SWDscan(chan:integer;var buffers:aptype):longint;stdcall;//声明数据接收函数 

external swell index2; 

procedure SWDrate(dsf:integer);stdcall; //声明采集频率设置函数 

external swdll index3; 

procedure SWDclose;stdcall; //声明关闭设备函数 

维普资讯

承德石油高等专科学校学报 

external'swdll index4; 

2002年第4卷第3期 

procedure TForm 1.Form Create(Sender:TObject); 

begin 

SWDinit;SWDrate(5); 

end; 

procedure Form1.TFormClose(Sender:TObject;var Action:TClose Action); 

begin 

SWDclose; 

end; 

procedure TForm1.Timer1 Timer(Sender:TObject); 

begin 

Var 

i:integer; 

fl:textfile; 

S:string; 

begin 

SWDscan(1,p1);//在通道1中采集数据 

AssignFile(fl,,c:\swd\collect.txt ); 

Rewrite(f1); 

for i:一1 to 2050 do 

begin 

//定义文件 

S:=floattostr(pl[i-I); 

Writeln(fl,s); 

end; 

CloseFile(f1); 

end 

2.2利用VB编从文件中读取数据的程序 

首先定义一个计时器控件定时读取文件数据,时间间隔属性Interval初始化为1000。 

Private Sub Timer1 Timer() 

Dim ci(2050)As Integer 

Dim counter As Integer 

Dim strl As String 

Open"c:\Swd\collect.txt”For Input As#1 

For Counter=1 To 2050 

Line Input#1.strl 

If EOF(1)Then GoTo exitl 

ci(Counter)一Val(str1) 

Next 

Exilt:Close#1 

End Sub 

由于篇幅所限本文只介绍了数据的接收,采用同样的方法还可以实现数据的发送,实现设备的控制 

与数据的采集。通过应用,我们不难发现利用文件做接口调用Delphi编写的API函数是一种简捷实用 

的方法。 

维普资讯

马晓晨:利用文件接口调用Delphi编写的API函数 

参考文献: 

[1]丁爱萍.Visual Basic程序设计[M].西安:西安电子科技大学出版社,2001 

[2]Reselman,B.实用Visual Basic 6教程[M].北京:清华大学出版社,2001 

・21・ 

Using File Interface to Call the API Function Programmed in Delphi 

MA Xiao—then 

(Computer and Information Engineering Department,Chengde Petroleum College, 

Chengde,Hebei,067000) 

Abstract:The calling process of windows API function in different programming languages is analysed 

here.And a method of calling API function in delphi with a VB application is presented with exam.. 

ples,in which file interface is available. 

Key words:inerface;API;function calling 

(上接第7页) 

参考文献: 

[1]王允城.油层物理学[M],北京;石油工业出版社,1993 

[2]F.A.L.Dullien.杨富民,黎用启译.多孔介质一流体渗移与孔隙结构[M],北京:石油工业出版社,1990 

Experimental Research on Fingering and Residual Oil Distribution 

TIAN Nai—lin ZHU Jiu—cheng 

(1.Chengde Petroleum College,Chengde,Hebei,067000: 

2.China International Enterprise Cooperation Company,100088) 

Abstract:A set of equipment are designed to study the fingering and residual oil distribution

。whose 

law has been obtained as a result and under different viscosity ration,it is meaningful to the actual 

production. 

Key words:crude oil;fingering;residual oil;experiment 


发布者:admin,转转请注明出处:http://www.yc00.com/news/1715530099a2632091.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信