🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: borland.public.cppbuilder.nativeapi
3 messages
3 total messages Started by Mike Collins Fri, 22 Feb 2008 01:21
problem with WriteFile
#675
Author: Mike Collins
Date: Fri, 22 Feb 2008 01:21
52 lines
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
Re: problem with WriteFile
#676
Author: 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 
Re: problem with WriteFile
#677
Author: Mike Collins
Date: Fri, 22 Feb 2008 02:08
8 lines
1188 bytes
lol - beat me to it, I've just finished writing the same thing - forgot to 
set the file position.

All working now, but thanks for you time anyway

Best regards,

Mike 
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads