I'm using Firebird 2.5.
I'm trying to write a Delphi XE2 UDF to return some string from some input params.
Here is the function in Delphi:

function FormatRasaSingle(var formatRasa : PChar; var aDenumire : PChar; var aCod : PChar; var aSimbol : PChar; var aTulpina : PChar) : PChar;
var tmpString : String;
tmpInteger : Integer;
begin
tmpString := formatRasa;
tmpString := StringReplace(tmpString, '#DENUMIRE', aDenumire, [rfReplaceAll, rfIgnoreCase]);
tmpString := StringReplace(tmpString, '#COD', aCod, [rfReplaceAll, rfIgnoreCase]);
tmpString := StringReplace(tmpString, '#SIMBOL', aSimbol, [rfReplaceAll, rfIgnoreCase]);
tmpString := StringReplace(tmpString, '#TULPINA', aTulpina, [rfReplaceAll, rfIgnoreCase]);
tmpString := 'MAMA'; <<<--- only for testing purpose not a real line
tmpInteger := Length(tmpString) + 1;
Result := ib_util_malloc(tmpInteger);
StrPCopy(Result, tmpString);
end;

the udf declaration is:

DECLARE EXTERNAL FUNCTION FORMATRASASINGLE
CSTRING(255),
CSTRING(255),
CSTRING(255),
CSTRING(255),
CSTRING(255)
RETURNS CSTRING(255) FREE_IT
ENTRY_POINT 'FormatRasaSingle' MODULE_NAME 'eliteSoftFirebird';

when i'm trying to test the function result only the first letter "M".
the lines
tmpString := 'MAMA';
is for test only. The real code is before this line.
but is not working.
can someone advice me?
where is the mistake?
Also when I try to input one parameter I get a big error like "error reading data from the connection".

Tks
Razvan