Subject Re: [ib-support] UDF + Delphi
Author Andreas Prucha
On 25 May 2002 at 22:44, semprolbat wrote:

> I wrote a function in delphi:
>
> function udftest(i:integer):pChar;
> begin
> result:=pChar(intToStr(i));
> end;
>
> with a corresponding
>
> declare external function udftest
> integer
> returns cstring(80) free_it
> entry_point 'udftest'
> module_name 'my_udf.dll';
>
> select udftest(1) from rdb$database; never(?) returns 1.
> I guess this is due to a memory-handling issue.

Yep. IntToStr creates a AnsiString which InterBase can't handle. And the parameters
are usually passed by reference by default.

function ib_util_malloc(s: integer): pointer; cdecl; external 'ib_util.dll';

function udftest(i: PInteger): PChar; cdecl;
var
s: String;
begin
Result := NIL;
If i <> NIL then
begin
s := IntToStr(i^);
Result := ib_util_malloc(Length(s)+1);
StrPCopy(Result, s);
end;
end;

This should do the job.

--
Best Regards - Mit freundlichen Gruessen,

Andreas Prucha
helicon software development