🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.delphi.nativeapi
Article #118

Re: TFileStream save without freeing stream

#118
From: Peter Haas
Date: Tue, 20 Jan 2004 16:06
55 lines
2493 bytes
Hi Mark,

Mark Williams wrote in <4006da70$2@newsgroups.borland.com>:
> Having used
> 
> fs := TFileStream.Create(fn, fmCreate or fmShareDenyWrite);

This don't make sense.

fmCreate or fmShareDenyWrite = fmCreate

TFileStream.Create differentiate between fmCreate and other flags.
fmCreate -> FileCreate, other -> FileOpen.

FileCreate use CREATE_ALWAYS and don't use any share flags.

> where fn is the name of a file to open and lock the file.  How do I then
> save to the file without freeing the stream and losing the protection?

You need to write a own TFileStream class (untested):

type
  TFileStreamEx = class(THandleStream)
  public
    constructor Create(const FileName: string; DesiredAccess, ShareMode,
      CreationDisposition: DWORD);
    destructor Destroy; override;
  end;

constructor TFileStreamEx.Create(const FileName: string; DesiredAccess,
  ShareMode, CreationDisposition: DWORD);
begin
  FHandle := CreateFile(PChar(FileName), DesiredAccess,
    ShareMode, nil, CreationDisposition, FILE_ATTRIBUTE_NORMAL, 0);
  if FHandle = INVALID_HANDLE_VALUE then
  begin
    FHandle := 0;
    raise EFOpenError.CreateResFmt(@SFOpenError, [FileName]);
  end;
end;

destructor TFileStreamEx.Destroy;
begin
  if FHandle <> 0 then
    FileClose(FHandle);
end;


  fs := TFileStreamEx.Create(fn, GENERIC_READ or GENERIC_WRITE,
    FILE_SHARE_READ, CREATE_ALWAYS);

Bye Peter.
-- 
JEDI+ API, the active JEDI Header Conversions Project:
http://jediplus.pjh2.de
Technical Details
Message-ID: <400d54ce$1@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!news.maxwell.syr.edu!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail
References: <4006da70$2@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: f94dd,ec1d7a80411df19c
X-Google-Attributes: gidf94dd,public
X-Google-ArrivalTime: 2004-01-20 08:40:50 PST
From: Peter Haas <newsgroupfeedback@pjh2.de>
Newsgroups: borland.public.delphi.nativeapi
Subject: Re: TFileStream save without freeing stream
Date: Tue, 20 Jan 2004 17:06:33 +0100
Reply-To: newsgroupfeedback@pjh2.de
References: <4006da70$2@newsgroups.borland.com>
X-Newsreader: Forte Agent 1.93/32.576 Deutsch (Standard)
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
User-Agent: Hamster/2.0.0.0
NNTP-Posting-Host: 62.157.76.64
Message-ID: <400d54ce$1@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1074615502 62.157.76.64 (20 Jan 2004 08:18:22 -0700)
Lines: 55
Path: arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!news.maxwell.syr.edu!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail