Recent

Author Topic: FPC 3.2.2 fpc_chararray_to_ansistr to be or not to be ?  (Read 285 times)

paule32

  • Sr. Member
  • ****
  • Posts: 441
FPC 3.2.2 fpc_chararray_to_ansistr to be or not to be ?
« on: April 18, 2025, 11:32:14 am »
I have this Code:

Code: Pascal  [Select][+][-]
  1. function IntToStrCRT32(Value: Integer): PChar; stdcall; export;
  2. var
  3.   Buf: array[0..16] of Char;
  4. begin
  5.   _itoa(Value, Buf, 10);
  6.   Result := string(AnsiString(Buf, true));
  7. end;

And I get the message:

Code: Pascal  [Select][+][-]
  1. Fatal: Syntax error, ")" expected but "," found

when I use this code:

Code: Pascal  [Select][+][-]
  1. function IntToStrCRT32(Value: Integer): PChar; stdcall; export;
  2. var
  3.   Buf: array[0..16] of Char;
  4. begin
  5.   _itoa(Value, Buf, 10);
  6.   Result := string(AnsiString(Buf));
  7. end;

I get the following message:

Code: Pascal  [Select][+][-]
  1. Utils.pas(161,20) Error: Wrong number of parameters specified for call to "$fpc_chararray_to_ansistr"
  2. system.pas(869,10) Error: Found declaration: $fpc_chararray_to_ansistr(const {Open} Array Of Char;Boolean=`TRUE`):AnsiString;
:o :'(

The implemenatation is as follows:

Code: Pascal  [Select][+][-]
  1. Function fpc_chararray_to_ansistr(const arr: array of char; zerobased: boolean = true): ansistring; compilerproc;
  2. var
  3.   i  : SizeInt;
  4. begin
  5.   if (zerobased) then
  6.     begin
  7.       if (arr[0]=#0) Then
  8.         i := 0
  9.       else
  10.       begin  
  11.         i:=IndexChar(arr,high(arr)+1,#0);
  12.         if i = -1 then
  13.           i := high(arr)+1;
  14.       end;    
  15.     end
  16.   else
  17.     i := high(arr)+1;
  18.   SetLength(fpc_CharArray_To_AnsiStr,i);
  19.   if i > 0 then
  20.     Move (arr[0],Pointer(fpc_CharArray_To_AnsiStr)^,i);
  21. end;

So, the Question is: _How to use the function ?
MS-IIS - Internet Information Server, Apache, PHP/HTML/CSS, MinGW-32/64 MSys2 GNU C/C++ 13 (-stdc++20), FPC 3.2.2
A Friend in need, is a Friend indeed.

bytebites

  • Hero Member
  • *****
  • Posts: 710
Re: FPC 3.2.2 fpc_chararray_to_ansistr to be or not to be ?
« Reply #1 on: April 18, 2025, 01:49:41 pm »
Anyway PChar and string are incompatible types.

jamie

  • Hero Member
  • *****
  • Posts: 6888
Re: FPC 3.2.2 fpc_chararray_to_ansistr to be or not to be ?
« Reply #2 on: April 18, 2025, 02:46:54 pm »
@paule32
Holes in yoir code.
" hoes holes everywhere holes blocking up the scenery blowing my mind"
Why not simply return the pointer to local array instead of all that translation .
Jamie
The only true wisdom is knowing you know nothing

PascalDragon

  • Hero Member
  • *****
  • Posts: 5962
  • Compiler Developer
Re: FPC 3.2.2 fpc_chararray_to_ansistr to be or not to be ?
« Reply #3 on: April 18, 2025, 02:47:47 pm »
So, the Question is: _How to use the function ?

Simple: you don't. You are not supposed to use these functions directly, instead FPC instruments them accordingly based on the involved code. In this case the value of the zerobased parameter depends on whether the array in question is one where the RTL needs to look for the terminating NUL or can derive the size from the variable itself.

 

TinyPortal © 2005-2018