[fpc-pascal] TFileStream.Create and hidden files on Windows

Bart bartjunk64 at gmail.com
Thu Jan 15 17:28:28 CET 2015


I have some old tools lying around that are still in use, and I
modernized them a bit.
One part of that is to replace file handling with FileStreams whenever

This all went without major hickups, until one of my tools suddenly crashed.

Where in the past it did a Rewrite(Filevar,1), it now uses
TFileStream.Create with appropriate parameters.
The file in question turned out to have the hidden attribute set.
While this poses no problem to rewrite/reset, TFileStream.Create does
not like it and fails.
MS explains why:

Of course it was a simple fix.
But I found it a little bit strange and asked myself why we did not
handle this inside FileCreate?

An obvious reason might be that Delphi (7) behaves the same.
But from a cross-platform standpoint the same code runs without
problems on hidden file in linux (and yes, I know hidden files on
linus is a different concept).

But now I have to fiddle with the attribute (twice) and ifdef it for
windows in all of my code using TFileStream, which looks ugly.

So why not have something like:

function FileCreate(Const FileName : string;  ShareMode: Integer;
Rights: Integer) : THandle;
  dwFlagAndAttributes: DWORD;
  Attr: LongInt;
  dwFlagAndAttributes := FILE_ATTRIBUTE_NORMAL;
  Attr := FileGetAttrUtf8(FileName);
  if (Attr <> LongInt(feInvalidHandle)) and ((Attr and faHidden) > 0) then
    //If a file is hidden than CreateFile fails if it does not have
    dwFlagAndAttributes := dwFlagAndAttributes or FILE_ATTRIBUTE_HIDDEN;
  Result := CreateFileW(PWideChar(UTF8Decode(FileName)), GENERIC_READ
                       dword(ShareModes[(ShareMode and $F0) shr 4]),
nil, CREATE_ALWAYS, dwFlagAndAttributes, 0);

Or, alternatively handle it in TFileStream.Create?


More information about the fpc-pascal mailing list