Changing the share option to fmShareDenyNone doesn't help! Very odd.
Is this 'as it should be' ("feature" of Notepad) or is there an error in
how Delphi handles fmShareDenyWrite?
BTW I'm using D6.1 Pro on WinNT4.0 SP6.
Any ideas?
Kristofer
worse yet, trying to read the file from another Delphi app, opening the
file using fmOpenRead also fails.
(now I'm *really* confused...)
The VCL ignores the sharing flags if the open mode is fmCreate.
I am not sure if the Windows CreateFile API does support creating a new file
with sharing - if so, it could be considered a VCL bug (unless it is
documented somewhere, of course).
> The file stores text data which I'd like to be able to inspect whlie the
> file is still open for writing.
> I was hoping to be able to open the file using NotePad for for example,
> but NotePad cannot open this file while it is being written to, not even
> just for reading.
Notepad just opens the file exclusively, which is the case for most simple
applications.
--
Eric Jansen
Find your answers at:
Newsgroups archives: http://groups.google.com
Torry's Delphi Pages: http://www.torry.net
Delphi Super Page: http://delphi.icm.edu.pl
> Changing the share option to fmShareDenyNone doesn't help! Very odd.
The problem is the fmCreate. If the file does not exist, create it first and
then reopen the file with fmOpenWrite + fmDenyNone (or something like that).
He, thanks a million! This 'feature' of the fmCreate option is not
documented anywhere that I can see.
I have seen C++ code which creates shareable files, so it must be
possible.
So it certainly looks like a VCL bug then.
> Notepad just opens the file exclusively, which is the case for most
simple
> applications.
AFAICS - it is able to open log files as per above, which are being
written to by another application.
Kristofer
I just tried it with CreateFile API, and it is possible.
f := CreateFile('c:\test.txt', GENERIC_WRITE, FILE_SHARE_READ or
FILE_SHARE_WRITE,
nil, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
>> Notepad just opens the file exclusively, which is the case for most
simple
>> applications.
> AFAICS - it is able to open log files as per above, which are being
> written to by another application.
You are right.
Thanks. But, in the interest of having Kylix-portable code (i.e. sticking
with TFileStream), I will go with your original suggestion (i.e. to
'fmCreate' the file first, then close and re-open with fmOpenReadWrite.
I've implemented that now and it works fine!
Kristofer
"Eric Jansen" <er...@do.not.spam.keyword.nl> wrote in message
news:3c061695$1_1@dnews...
> The VCL ignores the sharing flags if the open mode is fmCreate.
Not true; the sharing flags are indeed taken into account, as
TFileStream uses FileCreate if only fmCreate is passed as the Mode
parameter, otherwise it uses FileOpen with whatever Mode you passed.
This is from D4, so if things have changed in 5 or 6 then pardon me, but
I doubt they would ignore sharing modes.
Damien
Yes, it *looks* like the source does it that way, but the 'problem' lies in
the fact that fmCreate is defined as $ffff, so or-ing it with any share mode
will have no effect.
> > The VCL ignores the sharing flags if the open mode is fmCreate.
>
> Not true; the sharing flags are indeed taken into account, as
> TFileStream uses FileCreate if only fmCreate is passed as the Mode
> parameter
And FileCreate doesn't care about the file mode:
function FileCreate(const FileName: string): Integer;
begin
Result := Integer(CreateFile(PChar(FileName),
GENERIC_READ or GENERIC_WRITE,
0, nil, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0));
end;
OTOH, with fmCreate you simply _can't_ combine any file modes:
fmCreate = $FFFF;
...
fmShareExclusive = $0010;
fmShareDenyWrite = $0020;
fmShareDenyRead = $0030;
fmShareDenyNone = $0040;
But ist should be easy to implement a TFileStream descendant with a
changed constructor only.
-Michael