Recent

Author Topic: DelteFile function - UnicodeString expected PChar  (Read 708 times)

Int3g3r

  • New Member
  • *
  • Posts: 27
DelteFile function - UnicodeString expected PChar
« on: May 22, 2025, 02:28:42 pm »
Hello

Can someone explain to me why it is so difficult to delete a file if you have the path?  >:(
I can't, and I don't know why.

First (see image), why does autocomplete first show the DeleteFile() function with LPCSTR.
If i chose it, i get now RawByteString oder UnicodeString?

Second. How do i delete a file, this can't be that hard.

What is wrong here?
Quote
dm_main.pas(102,39) Error: Incompatible type for arg no. 1: Got "UnicodeString", expected "PChar"

Code: Pascal  [Select][+][-]
  1. uses
  2.   Classes, SysUtils, Dialogs, Windows, LazFileUtils, LR_DBSet, LR_Class,
  3.   LR_Desgn, lr_e_pdf, uResource, uini, DB, ZConnection, ZSqlProcessor, ZDataset,
  4.   ZAbstractRODataset, DBCtrls, fileutil, LazUtf8;
  5.  
  6. //.....
  7.  
  8. procedure TdmMain.DataModuleCreate(Sender: TObject);
  9. var
  10.   tempDirectoryPath: String;
  11.   files: TStringList;
  12.   item: String;
  13. begin
  14.   tempDirectoryPath := ExtractFilePath(ParamStr(0))+'temp';
  15.   if not DirectoryExists(tempDirectoryPath) then
  16.   begin
  17.    CreateDir(tempDirectoryPath);
  18.   end
  19.   else
  20.   begin
  21.      try
  22.        files := TStringList.Create;
  23.        files := FindAllFiles(tempDirectoryPath,'*.*',true);
  24.        for item in files do
  25.        begin
  26.           ShowMessage(item);
  27.           DeleteFile(UTF8ToUTF16(item));
  28.        end;
  29.      finally
  30.        files.Free;
  31.      end;
  32.   end;
  33. end;    
  34.  


I have created a new project and it works.  >:D
I do not understand.

Code: Pascal  [Select][+][-]
  1. uses
  2.    FileUtil, LazUtf8
  3.  
  4. //...
  5.  
  6. procedure TformMain.btnGenerateClick(Sender: TObject);
  7. var
  8.    path: String;
  9.    files: TStringList;
  10.    item: String;
  11. begin
  12.    path:= 'C:\Temp';
  13.    try
  14.      files := TStringList.Create;
  15.      files := FindAllFiles(path,'*.*',false);
  16.      for item in files do
  17.      begin
  18.        ShowMessage(item);
  19.        DeleteFile(UTF8ToUTF16(item));
  20.      end;
  21.    finally
  22.      files.Free;
  23.    end;
  24. end;    
  25.  


Regards Int3g3r
« Last Edit: May 22, 2025, 02:32:05 pm by Int3g3r »

Nimbus

  • Jr. Member
  • **
  • Posts: 57
Re: DelteFile function - UnicodeString expected PChar
« Reply #1 on: May 22, 2025, 02:56:56 pm »
One with LPCSTR comes from Windows unit which you have on the uses list. One you probably want is from SysUtils, you can try calling explicitly, e.g.
Code: Pascal  [Select][+][-]
  1. SysUtils.DeleteFile(item);

Int3g3r

  • New Member
  • *
  • Posts: 27
Re: DelteFile function - UnicodeString expected PChar
« Reply #2 on: May 22, 2025, 03:24:26 pm »
One with LPCSTR comes from Windows unit which you have on the uses list. One you probably want is from SysUtils, you can try calling explicitly, e.g.
Code: Pascal  [Select][+][-]
  1. SysUtils.DeleteFile(item);

Thank you very much !!!

Why does Lazarus not recognize that i want the function from SysUtils?
Why do i have to specify it?
Is this not clear if i pass a "UnicodeString" ?

Regards Int3g3r
« Last Edit: May 22, 2025, 03:26:12 pm by Int3g3r »

Zvoni

  • Hero Member
  • *****
  • Posts: 2983
Re: DelteFile function - UnicodeString expected PChar
« Reply #3 on: May 22, 2025, 03:50:55 pm »
Why does Lazarus not recognize that i want the function from SysUtils?
Why do i have to specify it?
Is this not clear if i pass a "UnicodeString" ?

Regards Int3g3r
Because FPC/Lazarus RESPECTS the Order of Units included in the Uses-Clause, the "last" one winning the race.
So if your SysUtils is BEFORE the Windows-unit in Uses, Windows wins
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

Int3g3r

  • New Member
  • *
  • Posts: 27
Re: DelteFile function - UnicodeString expected PChar
« Reply #4 on: May 22, 2025, 03:56:11 pm »
Because FPC/Lazarus RESPECTS the Order of Units included in the Uses-Clause, the "last" one winning the race.
So if your SysUtils is BEFORE the Windows-unit in Uses, Windows wins

Perfect, Thank you  :)

WooBean

  • Sr. Member
  • ****
  • Posts: 290
Re: DelteFile function - UnicodeString expected PChar
« Reply #5 on: May 22, 2025, 06:18:02 pm »
Because FPC/Lazarus RESPECTS the Order of Units included in the Uses-Clause, the "last" one winning the race.
So if your SysUtils is BEFORE the Windows-unit in Uses, Windows wins

It may be surprising but when units (or generally source names) are dotted the rule becomes more complicated.
At this case at first is searched the longest (in dots number) fitting dotted source name (including a program, a library, a package or units).

Nobody had promissed that all will be simple, and then kept his word.
« Last Edit: May 22, 2025, 06:32:02 pm by WooBean »
Platforms: Win7/64, Linux Mint 22.1 Xia

PascalDragon

  • Hero Member
  • *****
  • Posts: 6008
  • Compiler Developer
Re: DelteFile function - UnicodeString expected PChar
« Reply #6 on: May 22, 2025, 08:57:54 pm »
Why does Lazarus not recognize that i want the function from SysUtils?
Why do i have to specify it?
Is this not clear if i pass a "UnicodeString" ?

DeleteFile (no matter if SysUtils or Windows) is not declared as overload, thus the compiler stops searching for possible candidates once it found the first candidate without overload. This is part of the language rules. As others wrote you can enforce this by explicitly providing the unit name.

(And no, we have no desire to add overload to each and everything)

Because FPC/Lazarus RESPECTS the Order of Units included in the Uses-Clause, the "last" one winning the race.
So if your SysUtils is BEFORE the Windows-unit in Uses, Windows wins

It may be surprising but when units (or generally source names) are dotted the rule becomes more complicated.
At this case at first is searched the longest (in dots number) fitting dotted source name (including a program, a library, a package or units).

For an identifier without any unit or namespace identifier there is no difference.

 

TinyPortal © 2005-2018