🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

Re: problem with WriteFile

#676
From: Remy Lebeau (TeamB)
Date: Fri, 22 Feb 2008 01:34
62 lines
3121 bytes
"Mike Collins" <its@TheBottomOfThePost> wrote in message 
news:47be215a@newsgroups.borland.com...

> 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.

You are using OPEN_ALWAYS when calling CreateFile(), which allows it to open 
an existing file without clearing its contents.  However, after opening the 
file, you are not seeking to the end before then writing new data to it.  So 
when opening an existing file, you end up overwritting the existing data at 
the beginning of the file.

> My code looks like this:

Try this instead:

    void TEventLogThread::WriteEventToLog(TSTR_LOG_ONLY_EVENT 
&p_strEvtLogData)
    {
        if( hEventLog == INVALID_HANDLE_VALUE )
        {
            hEventLog = CreateFile(szEventLogPath.c_str(), GENERIC_WRITE, 
FILE_SHARE_READ, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_HIDDEN | 
FILE_ATTRIBUTE_SYSTEM, NULL);

            if( hEventLog == INVALID_HANDLE_VALUE )
                return;

            ULARGE_INTEGER li;
            li.QuadPart = 0;

            li.LowPart = SetFilePointer(hEventLog, (LONG)li.LowPart, 
(PLONG)&li.HighPart, FILE_END);
            if( (li.LowPart == 0xFFFFFFFF) && (GetLastError() != 0) )
            {
                CloseHandle(hEventLog);
                hEventLog = INVALID_HANDLE_VALUE:
                return;
            }
        }

        TDateTime dtEvntWhen = SystemTimeToDateTime(p_strEvtLogData.stWhen);

        AnsiString szBuff;
        szBuff.sprintf("%s,%s,%d,%d,%d,%s,%s\r\n",
            FormatDateTime(SHORT_DATE_FORMAT, dtEvntWhen).c_str(),
            FormatDateTime(SHORT_TIME_FORMAT, dtEvntWhen).c_str(),
            p_strEvtLogData.iEventId,
            p_strEvtLogData.iStatusId,
            p_strEvtLogData.iReasonId,
            p_strEvtLogData.cUserSID.c_str(),
            p_strEvtLogData.cObject.c_str());

        DWORD dwWritten;
        WriteFile(hEventLog, szBuff.c_str(), szBuff.Length(), &dwWritten, 
NULL);
    }


Gambit 
Technical Details
Message-ID: <47be267d$1@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!feeder3.cambrium.nl!feeder2.cambrium.nl!feed.tweaknews.nl!193.201.147.68.MISMATCH!feeder.news-service.com!newsfeed.freenet.de!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail
References: <47be215a@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,7943bd71a4bf122a
X-Google-Attributes: gid10cc67,public,usenet
X-Google-Language: ENGLISH,ASCII-7-bit
From: "Remy Lebeau \(TeamB\)" <no.spam@no.spam.com>
Newsgroups: borland.public.cppbuilder.nativeapi
References: <47be215a@newsgroups.borland.com>
Subject: Re: problem with WriteFile
Date: Thu, 21 Feb 2008 17:34:14 -0800
Lines: 64
Organization: Lebeau Software
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2900.3138
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.3198
X-RFC2646: Format=Flowed; Response
NNTP-Posting-Host: 207.178.157.90
Message-ID: <47be267d$1@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1203644029 207.178.157.90 (21 Feb 2008 17:33:49 -0700)
Path: arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!feeder3.cambrium.nl!feeder2.cambrium.nl!feed.tweaknews.nl!193.201.147.68.MISMATCH!feeder.news-service.com!newsfeed.freenet.de!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail