Subject [firebird-support] UDF Assistance
Author Martin Dew
I basically have some VARCHAR(8192) fields which I only want to return a
maximum of 100 characters from them (large Data to fetch on some
queries) ..

I have written a UDF in Delphi ( library and Unit file below ) to do
this through a function called OWL_STRLEFT100.

I have declared it using;

declare external function OWL_STRLEFT100
CSTRING(8192) CHARACTER SET NONE,
integer
returns
CSTRING(100) CHARACTER SET NONE
entry_point 'OWL_STRLEFT100' module_name 'OwlUDFs.dll';


however, when I try to run it say for example;

select OWL_STRLEFT100(symptoms,1) from sp_patlogview

It crashes the connection (error message "Connection lost to database").

I am assuming I have done something wrong here, could anyone help me out
and point out the errors of my ways ?

Thanks in advance.
Martin

-----

library OwlUDFs;

{ Important note about DLL memory management: ShareMem must be the
first unit in your library's USES clause AND your project's (select
Project-View Source) USES clause if your DLL exports any procedures or
functions that pass strings as parameters or function results. This
applies to all strings passed to and from your DLL--even those that
are nested in records and classes. ShareMem is the interface unit to
the BORLNDMM.DLL shared memory manager, which must be deployed along
with your DLL. To avoid using BORLNDMM.DLL, pass string information
using PChar or ShortString parameters. }

uses
SysUtils,
Classes,
OwlUDF in 'OwlUDF.pas';

{$R *.RES}

exports
OWL_STRLEFT100;

begin
end.


-----


unit OwlUDF;

interface

function OWL_STRLEFT100(var sz: PChar; var cnt: Integer): PChar;
cdecl; export;

implementation

function OWL_STRLEFT100(var sz: PChar; var cnt: Integer): PChar;
Var
i : integer;
s : String;
begin
if (sz = nil) then begin
result := nil
end
else if (cnt = 0) then begin
result := nil
end
else begin
if (cnt >= 100) then
i := 99
else
i := cnt;

s := copy(sz,0,i);
result := PChar(s);
end;
end;

end.