🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.cppbuilder.nativeapi
Article #198

Re: CreateFile to open a file already opened

#198
From: Colin B Maharaj
Date: Wed, 02 Jul 2008 20:25
65 lines
3347 bytes
Remy, my first (or was it 2nd) instinct was to look at the
VCL code implementation. But I was just zogged out ...

Thanks again Remy.




Remy Lebeau (TeamB) wrote:
> "Colin B Maharaj" <noreply@myhost.com> wrote in message 
> news:486bb9bd$1@newsgroups.borland.com...
> 
>>   if (hf == (HANDLE)0xffffffff)
> 
> Use the INVALID_HANDLE_VALUE macro instead:
> 
>     if (hf == INVALID_HANDLE_VALUE)
> 
>>      char *lpMsgBuf;
>>      FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | 
>> FORMAT_MESSAGE_FROM_SYSTEM,NULL,
>>      GetLastError(),MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),(LPTSTR) 
>> &lpMsgBuf,0,NULL);
> 
> The VCL has a SysErrorMessage() function that does that work for you:
> 
>     ShowMessage(SysErrorMessage(GetLastError()));
> 
> In any case, GetLastError() is returning ERROR_SHARING_VIOLATION (32, "The 
> process cannot access the file because it is being used by another 
> process.").  Which means your call to CreateFile() is not asking for enough 
> sharing rights to allow it to succeed.
> 
> If you take your second call to FileOpen():
> 
>     int FileHandle2 = FileOpen(A, fmOpenRead | fmShareDenyNone);
> 
> And look at how FileOpen() is implemented in SysUtils.pas (if you have the 
> VCL source code installed), you will see that it expands to the following 
> CreateFile() call:
> 
>     ... = CreateFile(FileName, GENERIC_READ, FILE_SHARE_READ | 
> FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
> 
> Notice the extra FILE_SHARE_WRITE flag?  That is the key to making your code 
> work.
> 
> Your first call to FileOpen() specifies the fmOpenReadWrite flag:
> 
>     int FileHandle = FileOpen(A, fmOpenReadWrite|fmShareDenyNone);
> 
> That expands to this:
> 
>     ... = CreateFile(FileName, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ 
> | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
> 
> Since the file is writable, any subsequent opening of the file while the 
> first handle is still active must specify the FILE_SHARE_WRITE flag or the 
> opening fails.  That is why your second call to FileOpen() succeeds but your 
> call to CreateFile() fails.
> 
> 
> Gambit 
> 
> 
Technical Details
Message-ID: <486be428$1@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!proxad.net!feeder1-2.proxad.net!newsfeed.straub-nv.de!feeder.erje.net!news.isoc.lu!newsgroups.borland.com!not-for-mail
References: <486b71d7$1@newsgroups.borland.com> <486bad42$1@newsgroups.borland.com> <486bb9bd$1@newsgroups.borland.com> <486bd4ae$1@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,d59c8fd30fdb585e
X-Google-Attributes: gid10cc67,domainid0,public,usenet
X-Google-Language: ENGLISH,ASCII-7-bit
Date: Wed, 02 Jul 2008 16:25:28 -0400
From: Colin B Maharaj <noreply@myhost.com>
User-Agent: Thunderbird 2.0.0.14 (Windows/20080421)
MIME-Version: 1.0
Newsgroups: borland.public.cppbuilder.nativeapi
Subject: Re: CreateFile to open a file already opened
References: <486b71d7$1@newsgroups.borland.com> <486bad42$1@newsgroups.borland.com> <486bb9bd$1@newsgroups.borland.com> <486bd4ae$1@newsgroups.borland.com>
In-Reply-To: <486bd4ae$1@newsgroups.borland.com>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit
X-Antivirus: avast! (VPS 080701-0, 07/01/2008), Outbound message
X-Antivirus-Status: Clean
NNTP-Posting-Host: 190.58.98.8
Message-ID: <486be428$1@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1215030312 190.58.98.8 (2 Jul 2008 13:25:12 -0700)
Lines: 65
Path: arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!proxad.net!feeder1-2.proxad.net!newsfeed.straub-nv.de!feeder.erje.net!news.isoc.lu!newsgroups.borland.com!not-for-mail