Environments: Lazarus 1.4.4 with FPC 3.0.1 (old fpcup one), Lazarus 1.6 with FPC 3.0.0 (public release version)
Situation: file system with international filenames, trying to access a filename with special polish characters on English or German computers.
Experienced: filename garbled when creating the file.
Expected: proper handling.
Here's my code:
program UnicodeFilenameAccessTest;
{$mode objfpc}{$H+}
//{$ModeSwitch UnicodeStrings}
uses
Windows,
SysUtils,
LazFileUtils,
LazUTF8;
function xCreateFileW(lpFileName: LPCWSTR; dwDesiredAccess: DWORD; dwShareMode: DWORD; lpSecurityAttributes: LPSECURITY_ATTRIBUTES; dwCreationDisposition: DWORD;
dwFlagsAndAttributes: DWORD; hTemplateFile: HANDLE): HANDLE; stdcall; external 'kernel32' Name 'CreateFileW';
procedure CreateTestFile;
const
TestFileNameUTF8: UTF8String = 'C:\Temp\Przepraszam Cię UTF-8.txt';
TestFileNameUnicode: UnicodeString = 'C:\Temp\Przepraszam Cię Unicode.txt';
TestFileNameWideChar: PWideChar = 'C:\Temp\Przepraszam Cię PWideChar.txt';
var
h: THandle;
begin
// Using LazFileUtils
h := FileCreateUTF8(TestFileNameUTF8);
if h > 0 then begin
WriteLn('Opened file');
CloseHandle(h);
end else begin
WriteLn(SysErrorMessage(GetLastError));
end;
// Using fileutilh.inc unicode variant
h := FileCreate(TestFileNameUnicode);
if h > 0 then begin
WriteLn('Opened file');
CloseHandle(h);
end else begin
WriteLn(SysErrorMessage(GetLastError));
end;
// Direct API access
h := xCreateFileW(TestFileNameWideChar, GENERIC_ALL, 7, nil, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
if h > 0 then begin
WriteLn('Opened file');
CloseHandle(h);
end else begin
WriteLn(SysErrorMessage(GetLastError));
end;
end;
begin
CreateTestFile;
end.
In the filesystem, I get:
- Przepraszam CiÄ PWideChar.txt
- Przepraszam CiÄ Unicode.txt
- Przepraszam CiÄ UTF-8.txt
It looks like there is some silly UTF-8 conversion happening that converts the filename to UTF-8, regardless which method I choose to create the file (and I've been trying even more, including TFileStream and others), even when calling the MSDN API with a PWideChar.
What am I doing wrong here?
Is there a chance FPC 3.1.1 would behave better? fpcup fails for me currently due to BGRA issues, so I can't try right now.