Article View: borland.public.delphi.nativeapi
Article #108Re: Lock a File
From: Andreas Schmidt
Date: Fri, 23 Jan 2004 16:16
Date: Fri, 23 Jan 2004 16:16
67 lines
2971 bytes
2971 bytes
"DP" <ramneek_devinder@comcast.net> schrieb im Newsbeitrag
news:Xns94795E615C598ramneekdevindercomca@207.105.83.66...
> Hi,
> How do I Lock a file so that second user trying to open the file is just
> able to read the contents. The first user needs to be able to read and
> write both.
> I have been trying to play with CreateFile API and its flags and
somehow
> it seems to fail when an attempt is made to open a same file across the
> network by two users.
>
I prefer to use TFileStream.
On first machine/process:
// the file must already exists
// if not, it must be created
// be carefull with fmCreate!
// if you use fmCreate then share-attributes will ingored [1]
// others may read the file, but they are not allowed to write
fs := TFileStream('\\fileserver\directory\test.txt', fmOpenReadWrite or
fmShareDenyWrite);
On the other machines:
// open the file for reading, others are allowed to read and write
fs := TFileStream('\\fileserver\directory\test.txt', fmOpenRead or
fmShareDenyNone);
-----------------------------------------------------
[1]
internally, a call to FileCreate will be made:
The FileCreate function doesn't have a "mode" parameter,
so it isn't possible to pass Shared-Flags in.
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;
// revised code (this is how Borland should have made it)
function FileCreate(const FileName: string; Mode: LongWord): Integer;
const
ShareMode: array[0..4] of LongWord = (
0,
0,
FILE_SHARE_READ,
FILE_SHARE_WRITE,
FILE_SHARE_READ or FILE_SHARE_WRITE);
begin
Result := Integer(CreateFile(PChar(FileName), GENERIC_READ or
GENERIC_WRITE,
ShareMode[(Mode and $F0) shr 4], nil, CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL, 0));
end;
hth
Andreas
Technical Details
Message-ID:
<401148d1$2@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-mailReferences:
<Xns94795E615C598ramneekdevindercomca@207.105.83.66>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: f94dd,31daf11ccc049e8b X-Google-Attributes: gidf94dd,public X-Google-ArrivalTime: 2004-01-23 08:40:29 PST From: "Andreas Schmidt" <a_j_schmidt@rocketmail.com> Newsgroups: borland.public.delphi.nativeapi References: <Xns94795E615C598ramneekdevindercomca@207.105.83.66> Subject: Re: Lock a File Date: Fri, 23 Jan 2004 17:16:14 +0100 Lines: 75 MIME-Version: 1.0 Content-Type: text/plain; charset="Windows-1252" Content-Transfer-Encoding: 7bit X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2600.0000 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2600.0000 NNTP-Posting-Host: 212.185.107.65 Message-ID: <401148d1$2@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1074874577 212.185.107.65 (23 Jan 2004 08:16:17 -0700) 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