Subject Re: [firebird-support] UDF:s and strings i Delphi
Author Urs Liska
Pavel Menshchikov schrieb:
> Hello Kjell,
>
> KR> I saw in an UDF intro doc for Delphi that you could cast the string
> KR> input args to Delphi strings using
> KR> string(ThePCharArg)
> Do you mean casting inside an UDF? Something like
> -----
> function myUDF(InStr: PChar): PChar; cdecl; export;
> var
> tmpstr: String;
> begin
> ...
> tmpstr:=String(InStr);
> ...
> end;
> -----
> I think there are no problems with that.
>
> KR> I've also read that UDF:s should use FB's (IB's?) memory allocation
> KR> function(s).
> As I remember, it is not recommended to use borland strings as
> input/output parameters or returning value because they are handled by
> borland memory manager. Instead, it is recommended to use MS or FB/IB
> memory manager. For example:
> -----
> // MS memory allocation function
> function malloc(Size: Integer): Pointer; cdecl; external 'msvcrt.dll';
> function myUDF(InStr: PChar): PChar; cdecl; export;
> var
> tmpstr: String;
> begin
> ...
> tmpstr:='some info';
> ...
> Result:=malloc(Length(tmpstr)+1); // +1 for C str. terminator
> StrPCopy(Result, tmpstr);
> ...
> end;
> -----
> Note that such UDF must be declared with FREE_IT for return value in
> FB database: FREE_IT indicates that the memory was allocated and FB
> has to free it. Example:
In several tutorials it is advised to use the special function
ib_util_malloc that is declared in the unit ib_externals.pas (that
you'll find in some subdirectory of your installation). But I don't
really know the difference this makes to using malloc.

And: I did only see the "returns parameter n" construct for blob
parameters. Usually one writes "returns cstring(nn)"

HTH
Urs