Recent

Author Topic: FPC 3.2.2 Call Through don't work  (Read 524 times)

paule32

  • Hero Member
  • *****
  • Posts: 645
  • One in all. But, not all in one.
FPC 3.2.2 Call Through don't work
« on: April 17, 2025, 06:02:15 pm »
Hello,
My Goal is to call through a wrapper Function to Append Strings as PChar.
But I fail.

I have the following Code:

Code: Pascal  [Select][+][-]
  1. function StrCat_(Dest: PChar; Source: PChar): PChar; stdcall; export;
  2. var
  3.   D: PChar;
  4. begin
  5.   D := Dest;
  6.   while D^ <> #0 do
  7.     Inc(D);
  8.  
  9.   // Hänge Source an
  10.   while Source^ <> #0 do
  11.   begin
  12.     D^ := Source^;
  13.     Inc(D);
  14.     Inc(Source);
  15.   end;
  16.  
  17.   D^ := #0; // Nullterminator
  18.   Result := Dest;
  19. end;
  20. function StrCat(Dest: PChar; Source: PChar): PChar; stdcall;
  21. var
  22.   R: PChar;
  23. begin
  24.   result := StrCat_(R, Source);
  25. end;
  26.  

Then in Code, the Call:

Code: Pascal  [Select][+][-]
  1. function foo;
  2. var
  3.   cmdline   : PAnsiChar;
  4.   Args      : PPAnsiChar;
  5.   ArgsCount : Integer;
  6.   R         : PChar;
  7.   TotalLen  : Integer;
  8. begin
  9.   result := nil;
  10.   CmdLine := GetCommandLineA;
  11.   Args    := CommandLineToArgvA(CmdLine, ArgsCount);
  12.  
  13.   if Args = nil then
  14.   begin
  15.     MessageBoxA(0,'Error: can not parse command line.', 'Error', MB_OK);
  16.     ExitProcess(1);
  17.   end;
  18.      
  19.   TotalLen := 128;
  20.  
  21.   R := StrAlloc(TotalLen);
  22.  
  23.   StrCopy(R, 'Count of Parameters: ');
  24.   StrCat(R, IntToStr(ArgsCount));
  25.  
  26.   MessageBoxA(0, R, '222  11  222', 0);
  27.  
  28.   StrDispose(R);
  29.   ExitProcess(0);
  30. end;

the StrCat don't append the ArgsCount.
When I use StrCat_ then the ArgsCount will be append to R

I don't know how to fix this.
Is the reserved TotalLen Space to small ?
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.

cdbc

  • Hero Member
  • *****
  • Posts: 2600
    • http://www.cdbc.dk
Re: FPC 3.2.2 Call Through don't work
« Reply #1 on: April 17, 2025, 06:42:16 pm »
Hi
This:
Code: Pascal  [Select][+][-]
  1. function StrCat(Dest: PChar; Source: PChar): PChar; stdcall;
  2. var
  3.   R: PChar;
  4. begin
  5.   result := StrCat_(R, Source);
  6. end;
tries to concatenate source onto 'R', which is totally unallocated!!!
Sorry to have to say that it's useless!

The first algorithm seems ok though...
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE6/QT6 -> FPC Release -> Lazarus Release &  FPC Main -> Lazarus Main

Thaddy

  • Hero Member
  • *****
  • Posts: 18729
  • To Europe: simply sell USA bonds: dollar collapses
Re: FPC 3.2.2 Call Through don't work
« Reply #2 on: April 17, 2025, 06:52:23 pm »
Use strlen on source and getmem/allocmem with that length for dest.
Horrible code, though. C style string handling is way inferior to Pascal strings.
It is also slower because strlen is slow (linear) and Pascal strings store length(one read).
« Last Edit: April 17, 2025, 06:56:08 pm by Thaddy »
If Europe sells their USA bonds the USD will collapse. Europe can affort that given average state debts. The USA can't affort that. Just an advice...

paule32

  • Hero Member
  • *****
  • Posts: 645
  • One in all. But, not all in one.
Re: FPC 3.2.2 Call Through don't work
« Reply #3 on: April 17, 2025, 08:35:43 pm »
yes, FPC is extremly fast and it extremly optimized the Code.
I thought that C is a fast high level DSL ?

Okay.
Thanks for the reply.
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.

 

TinyPortal © 2005-2018