Recent

Author Topic: File paths on Windows  (Read 6299 times)

Ñuño_Martínez

  • Hero Member
  • *****
  • Posts: 1186
    • Burdjia
File paths on Windows
« on: November 24, 2021, 01:30:05 pm »
This is the thing:  I made a software (a game) that saves the configuration in a file whose path and name is returned by the GetAppConfigFile function.  It works perfect on Linux but some Windows (specifically Windows 10) complains that they can't create the file.  The weird thing is that it creates the configuration directory without problems :o.

I suspect it is related with spaces and/or character enconding (String uses UTF8 but Windows disk stuff uses ASCII ISO-something-1) but I couldn't find anything about how to manage that; I know there's something because I remember reading it shomewhere.

Somebody help me!

Are you interested in game programming? Join the Pascal Game Development community!
Also visit the Game Development Portal

Bart

  • Hero Member
  • *****
  • Posts: 5275
    • Bart en Mariska's Webstek
Re: File paths on Windows
« Reply #1 on: November 24, 2021, 01:53:26 pm »
Is you application using LCL or LazUtils (if it is a Windows GUI app built with Lazarus, then it is).
If so, then UTF8 should not be a problem at all.

Can you show us the relevant part of the code and exactly at what line it fails?
I suppose you use False for the "Global" paramater of GetAppConfigDir?

All my apps use a some variant of GetAppConfigDir to store app config files, and this works flawless under all Windows I tested.
However if run under Citrix system, you should use the roaming profile to store config files.

Bart

loaded

  • Hero Member
  • *****
  • Posts: 824
Re: File paths on Windows
« Reply #2 on: November 24, 2021, 02:21:35 pm »
I had a similar problem before. I think it was solved with the following.
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.   fname:UTF8String='C:\Users\mali.aydin\Desktop\Türkçe\ĞÜŞİÖÇ.txt';
  4. begin
  5.   // UTF8ToISO_8859_1 uses add LConvEncoding.;
  6.   ExecuteProcess('explorer.exe', '/select,'+PChar(UTF8ToISO_8859_1(fname)), []);
  7. end;  
Check out  loaded on Strava
https://www.strava.com/athletes/109391137

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11383
  • FPC developer.
Re: File paths on Windows
« Reply #3 on: November 24, 2021, 02:46:41 pm »
Another possible problem:  some of the directories are not normal directories , but links.

Thaddy

  • Hero Member
  • *****
  • Posts: 14209
  • Probably until I exterminate Putin.
Re: File paths on Windows
« Reply #4 on: November 24, 2021, 02:57:10 pm »
I suspect it is related with spaces and/or character enconding (String uses UTF8 but Windows disk stuff uses ASCII ISO-something-1)
No it does not. It uses UTF-16 (and older UCS2, but that is now a subset ) since Windows NT4, which is a generation ago ( 30 +).
It may work if you define UNICODESTRING instead of the UTF-8 Kludge. ("Kludge" is only for Windows since it was way ahead of its time with the UCS2 encoding)
So try {$mode delphiunicode} for the unit that gives you problems.
And note: everything else you have seen about ascii on windows including and after XP is due to a wrapper layer that kind of makes it work. Since WIN NT4 the core API is Unicode16  and (very old versions since it predates many imrovements and standards) UCS2. Period.
« Last Edit: November 24, 2021, 03:09:22 pm by Thaddy »
Specialize a type, not a var.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11383
  • FPC developer.
Re: File paths on Windows
« Reply #5 on: November 24, 2021, 03:06:40 pm »
Thaddy: Apparently you don't understand the current unicode possibilities (of FPC 3+)

If you use system,sysutils units, and no direct windows calls, a lazarus program that is set to utf8 should work.

The easiest way to check this is probably to output inttostr(defaultsystemcodepage) to a memo and check it is 65001

« Last Edit: November 24, 2021, 03:11:15 pm by marcov »

Thaddy

  • Hero Member
  • *****
  • Posts: 14209
  • Probably until I exterminate Putin.
Re: File paths on Windows
« Reply #6 on: November 24, 2021, 03:13:01 pm »
Thaddy: Apparently you don't understand the current unicode possibilities (of FPC 3+)

If you use system,sysutils units, and no direct windows calls, a lazarus program that is set to utf8 should work.
No I do understand. But regarding the file system API on Windows this is not enough. You will need to specify unicodestring. Also goes for the registry if there occur very long paths.
You should have written in most (99%) but not all cases.
Especially very low level system access can still bite you and my guess is that is what happened here.
Specialize a type, not a var.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11383
  • FPC developer.
Re: File paths on Windows
« Reply #7 on: November 24, 2021, 03:14:49 pm »
But regarding the file system API on Windows this is not enough. You will need to specify unicodestring. Also goes for the registry if there occur very long paths.
You should have written in most (99%) but not all cases.

No just tick the utf8 box in the application/manifest/resource tab. The long file name one is there too.

Bart

  • Hero Member
  • *****
  • Posts: 5275
    • Bart en Mariska's Webstek
Re: File paths on Windows
« Reply #8 on: November 24, 2021, 06:48:57 pm »
Maybe wait until Ñuño_Martínez replies, otherwise it's just guess work.

Bart

Ñuño_Martínez

  • Hero Member
  • *****
  • Posts: 1186
    • Burdjia
Re: File paths on Windows
« Reply #9 on: December 06, 2021, 02:46:41 pm »
Sorry, there were busy weeks.

Is you application using LCL or LazUtil

No, it isn't.  I'm not using GUI.

Can you show us the relevant part of the code and exactly at what line it fails?
I suppose you use False for the "Global" paramater of GetAppConfigDir?
I use GetAppConfigFile instead:
Code: Pascal  [Select][+][-]
  1.     fFileName := GetAppConfigFile (FALSE);
  2.  

Then, I use it to create the directory and the file:
Code: Pascal  [Select][+][-]
  1. (* Saves changes. *)
  2.   PROCEDURE TmngConfiguration.Save;
  3.   VAR
  4.     DirPath: STRING;
  5.   BEGIN
  6.   { Be sure the directory exists. }
  7.     DirPath := ExtractFileDir (fFileName);
  8.     IF NOT DirectoryExists (DirPath) THEN
  9.       IF NOT CreateDir (DirPath) THEN
  10.       { Changed this to avoid problems with bad encoded file names
  11.         (i.e. Windows).  Now just put it down in to the log file.
  12.         RAISE mngConfigException.CreateFmt (COULDNT_SAVE_CFG_FILE, [fFileName]);
  13.       }
  14.         mngApplication.LogFmt (etWarning, COULDNT_SAVE_CFG_FILE, [fFileName]);
  15.  
  16.   { Save the file. }
  17.     IF NOT al_save_config_file (fFileName, fConfig) THEN
  18.     { See previous long comment.
  19.       RAISE mngConfigException.CreateFmt (COULDNT_SAVE_CFG_FILE, [fFileName]);
  20.     }
  21.       mngApplication.LogFmt (etWarning, COULDNT_SAVE_CFG_FILE, [fFileName]);
  22.   { Event. }
  23.     fOnSave.Notify
  24.   END;
  25.  
As I've said, it creates the directory but fails creating the file.

al_save_config_file is from Allegro.



I had a similar problem before. I think it was solved with the following.
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.   fname:UTF8String='C:\Users\mali.aydin\Desktop\Türkçe\ĞÜŞİÖÇ.txt';
  4. begin
  5.   // UTF8ToISO_8859_1 uses add LConvEncoding.;
  6.   ExecuteProcess('explorer.exe', '/select,'+PChar(UTF8ToISO_8859_1(fname)), []);
  7. end;  
I'll see how I can use that UTF8ToISO_8859_1.

Anyway what if it is installed in a non-western computer (for example Japan)?

Maybe wait until Ñuño_Martínez replies, otherwise it's just guess work.

Bart
My bad.  As I've said it were busy weeks.

I must say that I don't care to be compatible with older than Windows 7 (at themoment).

Also, the program creates the file in my own Win7 computer and in two Win10 computer I have access to:  I have limited access to one that fails.
Are you interested in game programming? Join the Pascal Game Development community!
Also visit the Game Development Portal

balazsszekely

  • Guest
Re: File paths on Windows
« Reply #10 on: December 06, 2021, 03:06:42 pm »
@ Ñuño_Martínez

Quote
As I've said, it creates the directory but fails creating the file.
After your applications fails to create the file what is the exact error message?
Code: Pascal  [Select][+][-]
  1. uses sysutils;
  2.  
  3. //...
  4. WriteLn(SysErrorMessage(GetLastOSError));

 

TinyPortal © 2005-2018