Article View: borland.public.cppbuilder.nativeapi
Article #196Re: CreateFile to open a file already opened
From: Colin B Maharaj
Date: Wed, 02 Jul 2008 17:24
Date: Wed, 02 Jul 2008 17:24
85 lines
3610 bytes
3610 bytes
Thanks for responding Remy.
The fact is my actual Code uses absolute paths.
Here is a sample code that I have put together
that actually give the same problem I am experiencing.....
(just put a button on a form and test it)
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
AnsiString A = ExtractFilePath(ParamStr(0));
A += "MyFile.txt";
ShowMessage(A);
// Create the file myfile.txt in the same place as the EXE.
{
int h = FileCreate(A);
AnsiString SomeText = "The file contents\r\n";
FileWrite(h, SomeText.c_str(), SomeText.Length());
FileClose(h);
}
// open the file...
int FileHandle = FileOpen(A, fmOpenReadWrite|fmShareDenyNone);
// open it again....
int FileHandle2 = FileOpen(A, fmOpenRead |fmShareDenyNone);
FileSeek(FileHandle2, (int)0, 0);
char s[64];
FileRead(FileHandle2, s, 63);
FileClose(FileHandle2);
// close the second instance....
ShowMessage(s);
// open it using the CreateFile Winapi...and handle possible error...
HANDLE hf =
CreateFile(A.c_str(),GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_EXISTING,0,NULL);
if (hf == (HANDLE)0xffffffff)
{
char *lpMsgBuf;
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM,NULL,
GetLastError(),MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),(LPTSTR)
&lpMsgBuf,0,NULL);
MessageBox( NULL, lpMsgBuf, "GetLastError",
MB_OK|MB_ICONINFORMATION );
LocalFree( lpMsgBuf );
}
else
{
CloseHandle(hf);
}
// close off the initial handle....
FileClose(FileHandle);
}
//---------------------------------------------------------------------------
Remy Lebeau (TeamB) wrote:
> "Colin B Maharaj" <noreply@myhost.com> wrote in message
> news:486b71d7$1@newsgroups.borland.com...
>
>> Finally I have this code I am using (not my code)
>> and is WinAPI and try to open the file similar to the
>> read operation above BUT IT FAILS!!
>
> What does GetLastError() return when CreateFile() fails? Since you are
> using relative paths, not absolute paths, I ma guessing that the API code is
> trying to open a file that does not actually exist.
>
>> Or, is an int FileHandle like what you see in 2nd line the
>> same as the HANDLE in the third.
>
> Yes.
>
>
> Gambit
>
>
Technical Details
Message-ID:
<486bb9bd$1@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!feeder3.cambrium.nl!feeder1.cambrium.nl!feed.tweaknews.nl!194.109.133.81.MISMATCH!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!feeder.erje.net!news.isoc.lu!newsgroups.borland.com!not-for-mailReferences:
<486b71d7$1@newsgroups.borland.com> <486bad42$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 13:24:29 -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> In-Reply-To: <486bad42$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: <486bb9bd$1@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1215019453 190.58.98.8 (2 Jul 2008 10:24:13 -0700) Lines: 85 Path: arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!feeder3.cambrium.nl!feeder1.cambrium.nl!feed.tweaknews.nl!194.109.133.81.MISMATCH!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!feeder.erje.net!news.isoc.lu!newsgroups.borland.com!not-for-mail