Article View: comp.os.ms-windows.programmer.win32
Article #2177Reading Event Logs?
From: Niklas Nihlen
Date: Fri, 12 Jun 1998 00:00
Date: Fri, 12 Jun 1998 00:00
191 lines
10490 bytes
10490 bytes
--------------955E07BD722FC060D991742E
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
I'm writing a little application that needs to read the Windows NT 4.0
Event Logs. I am using the OpenEventLog and ReadEventLog functions (se
code below). But my problem is I don't manage to extract all the
information that you can get if you inspect a event in Windows NT event
wiever. Using code similar to the one below, I esaly get the date, time
and source (who reported the event) but I don't manage to get the
category, User, Computer and description.
I would greatly appreciate any help on this problem!
EVENTLOGRECORD *pevlr;
BYTE bBuffer[BUFFER_SIZE];
DWORD dwRead, dwNeeded, cRecords, dwThisRecord = 0;
/* Open the Application event log. */
h = OpenEventLog(NULL, "Application"); /* source */
if (h == NULL)
ErrorExit("could not open Application event log");
pevlr = (EVENTLOGRECORD *) &bBuffer;
/*
* Opening the event log positions the file pointer
* for this handle at the beginning of the log.
*
* Read records sequentially until there
* are no more.
*/
while (ReadEventLog(h, /* event log handle */
EVENTLOG_FORWARDS_READ | /* reads forward */
EVENTLOG_SEQUENTIAL_READ, /* sequential read */
0, /* ignored for sequential reads */
pevlr, /* address of buffer */
BUFFER_SIZE, /* size of buffer */
&dwRead, /* count of bytes read */
&dwNeeded)) { /* bytes in next record */
while (dwRead > 0) {
/*
* Print the event ID, type, and source name.
* The source name is just past the end of the
* formal structure.
*/
printf("%02d Event ID: 0x%08X ",
dwThisRecord++, pevlr->EventID);
printf("EventType: %d Source: %s\n",
pevlr->EventType, (LPSTR) ((LPBYTE) pevlr +
sizeof(EVENTLOGRECORD)));
dwRead -= pevlr->Length;
pevlr = (EVENTLOGRECORD *)
((LPBYTE) pevlr + pevlr->Length);
}
pevlr = (EVENTLOGRECORD *) &bBuffer;
}
CloseEventLog(h);
Regards,
Niklas Nihlen
niklas@medcom.se
--------------955E07BD722FC060D991742E
Content-Type: text/html; charset=us-ascii
Content-Transfer-Encoding: 7bit
<HTML>
<P>I'm writing a little application that needs to read the Windows NT 4.0
Event Logs. I am using the OpenEventLog and ReadEventLog functions (se
code below). But my problem is I don't manage to extract all the information
that you can get if you inspect a event in Windows NT event wiever. Using
code similar to the one below, I esaly get the date, time and source (who
reported the event) but I don't manage to get the category, User, Computer
and description.
<BR>I would greatly appreciate any help on this problem!
<BR>
<DL><FONT FACE="Courier New,Courier"></FONT>
<P><FONT FACE="Courier New,Courier">EVENTLOGRECORD *pevlr;</FONT>
<BR><FONT FACE="Courier New,Courier">BYTE bBuffer[BUFFER_SIZE];</FONT>
<BR><FONT FACE="Courier New,Courier">DWORD dwRead, dwNeeded, cRecords,
dwThisRecord = 0;</FONT>
<BR><FONT FACE="Courier New,Courier"> </FONT>
<BR><FONT FACE="Courier New,Courier"> /* Open the Application
event log. */</FONT>
<BR><FONT FACE="Courier New,Courier"> </FONT>
<BR><FONT FACE="Courier New,Courier"> h = OpenEventLog(NULL,
"Application"); /* source */</FONT>
<BR><FONT FACE="Courier New,Courier">
if (h == NULL)</FONT>
<BR><FONT FACE="Courier New,Courier">
ErrorExit("could not open Application event log");</FONT>
<BR><FONT FACE="Courier New,Courier"> </FONT>
<BR><FONT FACE="Courier New,Courier"> pevlr = (EVENTLOGRECORD
*) &bBuffer;</FONT>
<BR><FONT FACE="Courier New,Courier"> </FONT>
<BR><FONT FACE="Courier New,Courier"> /*</FONT>
<BR><FONT FACE="Courier New,Courier"> * Opening
the event log positions the file pointer</FONT>
<BR><FONT FACE="Courier New,Courier"> * for this
handle at the beginning of the log.</FONT>
<BR><FONT FACE="Courier New,Courier"> *</FONT>
<BR><FONT FACE="Courier New,Courier"> * Read records
sequentially until there</FONT>
<BR><FONT FACE="Courier New,Courier"> * are no
more.</FONT>
<BR><FONT FACE="Courier New,Courier"> */</FONT>
<BR><FONT FACE="Courier New,Courier"> </FONT>
<BR><FONT FACE="Courier New,Courier"> while (ReadEventLog(h,
/* event log handle */</FONT>
<BR><FONT FACE="Courier New,Courier">
EVENTLOG_FORWARDS_READ | /* reads forward */</FONT>
<BR><FONT FACE="Courier New,Courier">
EVENTLOG_SEQUENTIAL_READ, /* sequential read */</FONT>
<BR><FONT FACE="Courier New,Courier">
0, /*
ignored for sequential reads */</FONT>
<BR><FONT FACE="Courier New,Courier">
pevlr, /* address of buffer
*/</FONT>
<BR><FONT FACE="Courier New,Courier">
BUFFER_SIZE, /* size of buffer
*/</FONT>
<BR><FONT FACE="Courier New,Courier">
&dwRead, /* count of bytes read
*/</FONT>
<BR><FONT FACE="Courier New,Courier">
&dwNeeded)) { /* bytes in next record
*/</FONT>
<BR><FONT FACE="Courier New,Courier"> </FONT>
<BR><FONT FACE="Courier New,Courier">
while (dwRead > 0) {</FONT>
<BR><FONT FACE="Courier New,Courier"> </FONT>
<BR><FONT FACE="Courier New,Courier">
/*</FONT>
<BR><FONT FACE="Courier New,Courier">
* Print the event ID, type, and source name.</FONT>
<BR><FONT FACE="Courier New,Courier">
* The source name is just past the end of the</FONT>
<BR><FONT FACE="Courier New,Courier">
* formal structure.</FONT>
<BR><FONT FACE="Courier New,Courier">
*/</FONT>
<BR><FONT FACE="Courier New,Courier"> </FONT>
<BR><FONT FACE="Courier New,Courier">
printf("%02d Event ID: 0x%08X ",</FONT>
<BR><FONT FACE="Courier New,Courier">
dwThisRecord++, pevlr->EventID);</FONT>
<BR><FONT FACE="Courier New,Courier">
printf("EventType: %d Source: %s\n",</FONT>
<BR><FONT FACE="Courier New,Courier">
pevlr->EventType, (LPSTR) ((LPBYTE) pevlr +</FONT>
<BR><FONT FACE="Courier New,Courier">
sizeof(EVENTLOGRECORD)));</FONT>
<BR><FONT FACE="Courier New,Courier"> </FONT>
<BR><FONT FACE="Courier New,Courier">
dwRead -= pevlr->Length;</FONT>
<BR><FONT FACE="Courier New,Courier">
pevlr = (EVENTLOGRECORD *)</FONT>
<BR><FONT FACE="Courier New,Courier">
((LPBYTE) pevlr + pevlr->Length);</FONT>
<BR><FONT FACE="Courier New,Courier">
}</FONT>
<BR><FONT FACE="Courier New,Courier"> </FONT>
<BR><FONT FACE="Courier New,Courier">
pevlr = (EVENTLOGRECORD *) &bBuffer;</FONT>
<BR><FONT FACE="Courier New,Courier"> }</FONT>
<BR><FONT FACE="Courier New,Courier"> </FONT>
<BR><FONT FACE="Courier New,Courier"> CloseEventLog(h);</FONT><FONT FACE="Courier New,Courier"></FONT>
<P>Regards,
<P>Niklas Nihlen
<BR>niklas@medcom.se</DL>
</HTML>
--------------955E07BD722FC060D991742E--
Technical Details
Message-ID:
<3580F5F7.33A7B8E9@medcom.se>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailShow Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 113dfb,b34767644b8788b7,start X-Google-Attributes: gid113dfb,public X-Google-Thread: 105df1,b34767644b8788b7,start X-Google-Attributes: gid105df1,public X-Google-Thread: f5ca7,b34767644b8788b7,start X-Google-Attributes: gidf5ca7,public From: Niklas Nihlen <niklas@medcom.se> Subject: Reading Event Logs? Date: 1998/06/12 Message-ID: <3580F5F7.33A7B8E9@medcom.se> X-Deja-AN: 362012879 Content-Type: multipart/alternative; boundary="------------955E07BD722FC060D991742E" Organization: Media Communications Eur Mime-Version: 1.0 Reply-To: niklas@medcom.se Newsgroups: comp.os.ms-windows.programmer.win32,comp.os.ms-windows.programmer.misc,borland.public.cppbuilder.winapi