Board index » delphi » udf that returns cstring doesn't works

udf that returns cstring doesn't works

Hi,

I have this function, (for test purpose)

function Left(var sz : pchar; cnt : integer):pchar;
var i : integer;
begin
    if sz = nil then
        result := nil
    else
    begin
        i := 0;
        while ((sz[i] <> #0) and (i < cnt)) do Inc(i);
        result := ib_util_malloc(i+1);
        Move(sz[0],result[0],i);
        result[i] := #0;
    end;
end;

And this interbase declaration

declare external function f_left
 cstring(64), integer
 returns cstring(64) free_it
 entry_point 'Left' module_name 'IBfunciones';

Everything seem good, but when I run the function in an select statement I
get a date set very strange, first record id blank, next records have null
value, then the ibserver shuts down.

I have set the dll in de udfdirecotry, use the ib_util and sharemem units in
my proyect, even I have functions that returns integers ans works fine, the
trouble is with cstring and pchar.

Any Idea ?

 

Re:udf that returns cstring doesn't works


Quote
"Miguel Guzman" <mguz...@flecha-amarilla.com> wrote in message

news:3cb3b9f8_2@dnews...

Quote

> declare external function f_left
>  cstring(64), integer
>  returns cstring(64) free_it
>  entry_point 'Left' module_name 'IBfunciones';

Either change it to
cstring(64), integer BY VALUE
XOR
change your UDF to receive CNT as a pointer to int.
(Choose one, not both ways.)

C.
--
Claudio Valderrama C. - http://www.cvalde.com - http://www.firebirdSql.org
Independent developer
Owner of the Interbase? WebRing

Re:udf that returns cstring doesn't works


Claudio,

Thanks for help, second option was the solution also I  removed the var
sentence before the parameter pchar.

Regards.

"Claudio Valderrama C." <cva...@willbounce.org> escribi en el mensaje
news:3cb40185_2@dnews...

Quote
> "Miguel Guzman" <mguz...@flecha-amarilla.com> wrote in message
> news:3cb3b9f8_2@dnews...

> > declare external function f_left
> >  cstring(64), integer
> >  returns cstring(64) free_it
> >  entry_point 'Left' module_name 'IBfunciones';

> Either change it to
> cstring(64), integer BY VALUE
> XOR
> change your UDF to receive CNT as a pointer to int.
> (Choose one, not both ways.)

> C.
> --
> Claudio Valderrama C. - http://www.cvalde.com - http://www.firebirdSql.org
> Independent developer
> Owner of the Interbase? WebRing

Other Threads