Microsoft KB Archive/216146

= How To Get the Number of Windows NT Event Log Records in Visual Basic =

Article ID: 216146

Article Last Modified on 7/15/2004

-

APPLIES TO


 * Microsoft Visual Basic 4.0 Standard Edition
 * Microsoft Visual Basic 4.0 Professional Edition
 * Microsoft Visual Basic 4.0 32-Bit Enterprise Edition
 * Microsoft Visual Basic 5.0 Learning Edition
 * Microsoft Visual Basic 6.0 Learning Edition
 * Microsoft Visual Basic 5.0 Professional Edition
 * Microsoft Visual Basic 6.0 Professional Edition
 * Microsoft Visual Basic 5.0 Enterprise Edition
 * Microsoft Visual Basic 6.0 Enterprise Edition

-



This article was previously published under Q216146



SUMMARY
Event logging in Windows provides a standard, centralized way for Windows and other applications to record important software and hardware events. Event logging also supplies a standard user interface for viewing the logs and a programming interface for examining the logs.

This article illustrates how to get the number of records contained within a specified Microsoft Windows 2000 or Windows NT Event Log with Visual Basic version 4.0 and later. This can be done using the GetNumberOfEventLogRecords API call. While Visual Basic 5.0 and later versions have built in functionality for event logging, they do not provide the functionality that the Win32 API does.



MORE INFORMATION
The following example shows how to get the number of event log records in the local Application Event Log.

Step by Step Example
 Create a new Visual Basic project. Form1 is created by default. Add a command button to Form1. Change the name of the command button to cmdGetNumberOfEventLogRecords.  Add the following code to the Form1 code window: Private Declare Function GetNumberOfEventLogRecords Lib "advapi32.dll" ( _   ByVal hEventLog As Long, _    NumberOfRecords As Long) _ As Long

Private Declare Function CloseEventLog Lib "advapi32.dll" ( _   ByVal hEventLog As Long) _ As Long

Private Declare Function OpenEventLog Lib "advapi32.dll" Alias "OpenEventLogA" ( _   ByVal lpUNCServerName As String, _    ByVal lpSourceName As String) _ As Long

Private Sub cmdGetNumberOfEventLogRecords_Click Dim hEventLog As Long Dim lEventLogRecordCount As Long Dim lretv As Long hEventLog = OpenEventLog(vbNullString, "Application") If hEventLog = 0 Then Debug.Print "OpenEventLog Failed" Exit Sub End If   lretv = GetNumberOfEventLogRecords(hEventLog, lEventLogRecordCount) If lretv = 0 Then Debug.Print GetNumberOfEventLogRecords Failed"        Exit Sub    Else        Debug.Print "Number of records: " & CStr(lEventLogRecordCount)    End If    lretv = CloseEventLog(hEventLog)    If lretv = 0 Then         Debug.Print "CloseEventLog Failed"         Exit Sub    End If

End Sub  Press F5 to run the project, and then and click the cmdGetNumberOfEventLogRecords command button. A message box should appear that displays the number of records.

