传递和返回数据到一个Delphi编写的UDF

Delphi教程 2025-08-31

菜鸟学堂:

[udf系列之四]:传递和返回数据到一个delphi编写的udf

warton译

作者: chris levesque, tina grubbe, brett bandy

--------------------------------------------------------------------------------

[译者叙]:

前面我已经翻译了几篇关于编写udf的文章,虽然一些朋友可能也从中得到了一点帮助,但是可能对udf的

认识还存在一些问题。今天,我再翻译两文章,这两篇文章都是来自mer system (http://www.m**e*rs.com)

的,有兴趣的朋友可以查看原文。

[论点]:

当动态链接库没有为受保护的数据值做特殊的预防时,我们的udf带有参数值或返回值的数据结果

可能处在一个受保护的异常或错误结果之中。

[解决方案]:

每一个日期值被保存在两个32位的整数类型之中:一个表示日期的signed integer,和一个表示

时间的unsigned integer。使用delphi代码来定义这个结构(isc_quad)和结构的指针(pisc_quad):

type

{interbase date/time record}

isc_quad = record

isc_quad_high : integer ; // date

isc_quad_low : cardinal ; // time

end;

pisc_quad = ^isc_quad;

为了保护返回值,在函数定义的外部申明一个线程安全的isc_quad变量,使它保存返回值(如果返回值

是一个日期型的数据)。

threadvar

tempquad : isc_quad;

然后编写你的函数以便结果指向线程变量。

// 定义函数

// this function adds a number of days to an existing date.

function dayadd( var days: integer; ibdate pisc_quad) : pisc_quad; cdecl; export;

begin

tempquad.isc_quad_high := ibdate^.isc_quad_high + days;

tempquad.isc_quad_low := ibdate^.isc_quad_low;

result := @tempquad;

end;

本主来自:mer systems inc.. http://www.me*r**s.com