Article View: borland.public.cppbuilder.nativeapi
Article #197Re: CreateFile to open a file already opened
From: Remy Lebeau (TeamB)
Date: Wed, 02 Jul 2008 19:17
Date: Wed, 02 Jul 2008 19:17
55 lines
3034 bytes
3034 bytes
"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:
<486bd4ae$1@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!news2.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!feeder.erje.net!news.isoc.lu!newsgroups.borland.com!not-for-mailReferences:
<486b71d7$1@newsgroups.borland.com> <486bad42$1@newsgroups.borland.com> <486bb9bd$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 From: "Remy Lebeau \(TeamB\)" <no.spam@no.spam.com> Newsgroups: borland.public.cppbuilder.nativeapi References: <486b71d7$1@newsgroups.borland.com> <486bad42$1@newsgroups.borland.com> <486bb9bd$1@newsgroups.borland.com> Subject: Re: CreateFile to open a file already opened Date: Wed, 2 Jul 2008 12:17:51 -0700 Lines: 57 Organization: Lebeau Software X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2900.3138 X-RFC2646: Format=Flowed; Response X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.3198 NNTP-Posting-Host: 207.178.157.90 Message-ID: <486bd4ae$1@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1215026350 207.178.157.90 (2 Jul 2008 12:19:10 -0700) Path: arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!news2.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!feeder.erje.net!news.isoc.lu!newsgroups.borland.com!not-for-mail