Board index » delphi » UDF's Written in Delphi and Interbase

UDF's Written in Delphi and Interbase

I have tried to create a UDF for an Interbase DB with Delphi.  The udf is a
function that calculates the distance between a pair of Latitudes and
Longitudes.  The inputs are all Double and the return value is a double.  I
wrote a test application in Delphi that tests the .dll and it works
perfectly.

Then, I set up the declaration in Interbase after copying the .dll to the
same directory as the ib_udf.dll (which contains the trig functions used in
my .dll and that have already been declared in the Interbase DB.  No matter
what I specify as the return type I get nonsensical data, sometimes the same
values no matter what the input values.  My guess is that there is some kind
of conversion problem happening between Delphi data types and Interbase
data.  My function has no 'includes' since there are no string variables:

Here is the function code in Delphi:
library IB_MAMSudf;

uses
  SysUtils,
  Classes;

{$R *.RES}

function GetDistance(ClientLatitude, ClientLongitude, ClinicLatitude,
ClinicLongitude: Double): Double; StdCall;
begin
If ((ClientLatitude <> ClinicLatitude) And (ClientLongitude <>
ClinicLongitude)) Then
    Result := 3959 * (Arctan(-(Sin(ClientLatitude / 57.3) *
Sin(ClinicLatitude / 57.3) +
     Cos(ClientLatitude / 57.3) * Cos(ClinicLatitude / 57.3) *
Cos(ClinicLongitude / 57.3 -
      ClientLongitude / 57.3)) / (Sqrt(-(Sin(ClientLatitude / 57.3) *
Sin(ClinicLatitude / 57.3)
       + Cos(ClientLatitude / 57.3) * Cos(ClinicLatitude / 57.3) *
Cos(ClinicLongitude / 57.3 -
        ClientLongitude / 57.3)) * (Sin(ClientLatitude / 57.3) *
Sin(ClinicLatitude / 57.3) +
        Cos(ClientLatitude / 57.3) * Cos(ClinicLatitude / 57.3) *
Cos(ClinicLongitude / 57.3 -
         ClientLongitude / 57.3)) + 1))) + 2 * Arctan(1))
Else
    Result := 30;
end;
exports
  GetDistance;
end.

Hers is my Declaration:

DECLARE EXTERNAL FUNCTION GETDISTANCE
 DOUBLE PRECISION,DOUBLE PRECISION,DOUBLE PRECISION,DOUBLE PRECISION
  RETURNS DOUBLE PRECISION BY VALUE
  ENTRY_POINT 'GetDistance'
  MODULE_NAME 'IB_MAMSudf.dll'

Any help would be greatly appreciated as I can not find any examples of how
to create a udf in Delphi.  All the examples in the Interbase docs are for C
and I don't know C.

Thanks in advance

John Griffith  jo...@barjohn.com

 

Re:UDF's Written in Delphi and Interbase


Quote
John Griffith wrote:

> function GetDistance(ClientLatitude, ClientLongitude, ClinicLatitude,
> ClinicLongitude: Double): Double; StdCall;

        Try declaring this function cdecl instead of stdcall.  Also, I always
pass arguments as var, although it shouldn't make a difference.

Quote
> Any help would be greatly appreciated as I can not find any examples of how
> to create a udf in Delphi.  All the examples in the Interbase docs are for C
> and I don't know C.

        This is useful:

http://www.interbase2000.com/doc_deatz_udf.htm

        HTH,

        -Craig

--
Craig Stuntz (TeamB)       Senior Developer, Vertex Systems Corp.
Delphi/InterBase weblog:   http://delphi.weblogs.com
Use Borland servers; posts via others are not seen by TeamB.
For more info, see http://www.borland.com/newsgroups/genl_faqs.html

Re:UDF's Written in Delphi and Interbase


I tried your suggestion and it didn't work.  I still get -2.5.....exx for
every calculation.
When you say you pass arguments as var, do you mean you make them variant
instead of double?

"Craig Stuntz (TeamB)" <cstuntz@no_spam.vertexsoftware.com> wrote in message
news:3B000849.E773AF6F@no_spam.vertexsoftware.com...

Quote

> John Griffith wrote:

> > function GetDistance(ClientLatitude, ClientLongitude, ClinicLatitude,
> > ClinicLongitude: Double): Double; StdCall;

> Try declaring this function cdecl instead of stdcall.  Also, I always
> pass arguments as var, although it shouldn't make a difference.

> > Any help would be greatly appreciated as I can not find any examples of
how
> > to create a udf in Delphi.  All the examples in the Interbase docs are
for C
> > and I don't know C.

> This is useful:

> http://www.interbase2000.com/doc_deatz_udf.htm

> HTH,

> -Craig

> --
> Craig Stuntz (TeamB)       Senior Developer, Vertex Systems Corp.
> Delphi/InterBase weblog:   http://delphi.weblogs.com
> Use Borland servers; posts via others are not seen by TeamB.
> For more info, see http://www.borland.com/newsgroups/genl_faqs.html

Re:UDF's Written in Delphi and Interbase


Quote
John Griffith wrote:

> I tried your suggestion and it didn't work.  I still get -2.5.....exx for
> every calculation.
> When you say you pass arguments as var, do you mean you make them variant
> instead of double?

        No, I mean var:

function GetDistance(var ClientLatitude, ClientLongitude,
ClinicLatitude,
ClinicLongitude: Double): Double; cdecl;

        Come to think of it, this should make a difference.

        -Craig

--
Craig Stuntz (TeamB)       Senior Developer, Vertex Systems Corp.
Delphi/InterBase weblog:   http://delphi.weblogs.com
Use Borland servers; posts via others are not seen by TeamB.
For more info, see http://www.borland.com/newsgroups/genl_faqs.html

Re:UDF's Written in Delphi and Interbase


Your solution WORKED!!!  THANK YOU VERY MUCH!! I have spent days beating my
brains out on why such a simple program would work in Delphi and not in
Interbase.  I really appreciate your help

John Griffith
"Craig Stuntz (TeamB)" <cstuntz@no_spam.vertexsoftware.com> wrote in message
news:3B00310A.1D0A48DF@no_spam.vertexsoftware.com...

Quote

> John Griffith wrote:

> > I tried your suggestion and it didn't work.  I still get -2.5.....exx
for
> > every calculation.
> > When you say you pass arguments as var, do you mean you make them
variant
> > instead of double?

> No, I mean var:

> function GetDistance(var ClientLatitude, ClientLongitude,
> ClinicLatitude,
> ClinicLongitude: Double): Double; cdecl;

> Come to think of it, this should make a difference.

> -Craig

> --
> Craig Stuntz (TeamB)       Senior Developer, Vertex Systems Corp.
> Delphi/InterBase weblog:   http://delphi.weblogs.com
> Use Borland servers; posts via others are not seen by TeamB.
> For more info, see http://www.borland.com/newsgroups/genl_faqs.html

Other Threads