Article View: borland.public.cppbuilder.nativeapi
Article #675problem with WriteFile
From: Mike Collins
Date: Fri, 22 Feb 2008 01:21
Date: Fri, 22 Feb 2008 01:21
52 lines
2920 bytes
2920 bytes
Hi all, posted a few weeks ago about an event log system I was developing.
I have a basic system where events are created and stuck on a queue. I then
have a thread that takes events of the queue and writes them to an event log
using CreateFile / WriteFile.
However, I have found that each time the thread starts, and opens / writes
data for the first time, it deletes some of the previous data. This only
happens when the thread first starts, thereafter, the data is written
correctly. I assume there must be some issue with the file pointer but i'm
not sure how to initialise it.
My code looks like this:
void TEventLogThread::WriteEventToLog(TSTR_LOG_ONLY_EVENT &p_strEvtLogData)
{
if ((!hEventLog)||(hEventLog == INVALID_HANDLE_VALUE))
{
hEventLog = CreateFile(szEventLogPath.c_str(), GENERIC_WRITE,
FILE_SHARE_READ, NULL, OPEN_ALWAYS,
FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM,
0);
}
if (hEventLog != INVALID_HANDLE_VALUE)
{
DWORD dwWritten;
AnsiString szBuff;
TDateTime dtEvntWhen;
dtEvntWhen = SystemTimeToDateTime(p_strEvtLogData.stWhen);
szBuff = AnsiString(FormatDateTime(SHORT_DATE_FORMAT, dtEvntWhen) +
"," +
FormatDateTime(SHORT_TIME_FORMAT, dtEvntWhen) +
"," +
IntToStr(p_strEvtLogData.iEventId) + "," +
IntToStr(p_strEvtLogData.iStatusId) + "," +
IntToStr(p_strEvtLogData.iReasonId) + "," +
p_strEvtLogData.cUserSID + "," +
p_strEvtLogData.cObject + "\r\n");
dwWritten = szBuff.Length();
WriteFile(hEventLog, szBuff.c_str(), szBuff.Length(), &dwWritten,
NULL);
//FlushFileBuffers(hEventLog); // <- wasn't sure about this...
}
}
I keep the file handle open whilst the thread is alive, then it is closed.
Many thanks in advance,
Mike C
Technical Details
Message-ID:
<47be215a@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!proxad.net!feeder1-2.proxad.net!213.200.89.82.MISMATCH!tiscali!newsfeed1.ip.tiscali.net!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mailShow Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,7943bd71a4bf122a,start X-Google-Attributes: gid10cc67,public,usenet X-Google-Language: ENGLISH,ASCII-7-bit From: "Mike Collins" <its@TheBottomOfThePost> Newsgroups: borland.public.cppbuilder.nativeapi Subject: problem with WriteFile Date: Fri, 22 Feb 2008 01:21:40 -0000 Lines: 54 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.3790.3959 X-RFC2646: Format=Flowed; Original X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.4133 NNTP-Posting-Host: 78.144.175.160 Message-ID: <47be215a@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1203642714 78.144.175.160 (21 Feb 2008 17:11:54 -0700) Path: arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!proxad.net!feeder1-2.proxad.net!213.200.89.82.MISMATCH!tiscali!newsfeed1.ip.tiscali.net!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail