Search Tools Links Login

Easily write to the Windows Event Log


This module allows you to write text to the Windows Event Log with with a single method call. Includes options for Type, Source, Category, and Event.

Usage

LogEvent([Applicaiton Name], [EventLog Type], [Msg to be logged], [Optional Event ID], [Optional Category Number])

Example

Dim ret as Long
ret = LogEvent("TestApp1", EVENTLOG_SUCCESS, "Success event!!!!")

Code

Copy the entire code below into a VB Module. You can then add the module to a project and use a single function to write to the Event Viewer.

'******************************************************************
'Information compiled from MSDN Articles on Platform SDK: Debugging and Error Handling
'MSDN Home > MSDN Library > SDK Documentation > Event Logging > _
' Event Logging Reference > Event Logging Functions
'
'FUNCTIONS:
'Public Function LogEvent(strSource As String, dEventType As Long, _
'    strError As String, Optional iEventID As Integer = 26, _
'    Optional iCategory As Integer = 26) As Long
'
' returns nonzero for success otherwize zero for fail
'
'Module by Antonio Zavala, antonio.zavala@usaa.com
'United Services Automobile Association, 10/2002
'******************************************************************
Private Declare Function RegisterEventSource Lib "advapi32" Alias _
    "RegisterEventSourceA" (ByVal lpUNCServerName As String, ByVal lpSourceName As String) As Long

Private Declare Function DeregisterEventSource Lib "advapi32" _
    (ByVal hEventLog As Long) As Boolean

Private Declare Function ReportEvent _
    Lib "advapi32" Alias "ReportEventA" _
    (ByVal hEventLog As Long, _
    ByVal wType As Long, _
    ByVal wCategory As Long, _
    ByVal dwEventID As Long, _
    ByVal lpUserSid As Long, _
    ByVal wNumStrings As Long, _
    ByVal dwDataSize As Long, _
    lpStrings As Any, _
    lpRawData As Any) As Long

' Event Type Constants
Public Const EVENTLOG_SUCCESS = &H0            'Success event
Public Const EVENTLOG_ERROR_TYPE = &H1         'Error event
Public Const EVENTLOG_WARNING_TYPE = &H2     'Warning event
Public Const EVENTLOG_INFORMATION_TYPE = &H4 'Information event
Public Const EVENTLOG_AUDIT_SUCCESS = &H8     'Success audit event
Public Const EVENTLOG_AUDIT_FAILURE = &H10     'Failure audit event

Public Function LogEvent(strSource As String, dEventType As Long, strError As String, Optional iEventID As Integer = 26, Optional iCategory As Integer = 26) As Long
' returns nonzero for success otherwize zero for fail
Dim ret1, ret2, hEvt As Long
hEvt = RegisterEventSource(vbNullChar, strSource)
If hEvt Then
    ret1 = ReportEvent(hEvt, dEventType, iCategory, iEventID, 0, 1, 0, strError, vbNullChar)
    If Not ret1 Then
        LogEvent = 0
        Exit Function
    End If
End If
LogEvent = DeregisterEventSource(hEvt)
End Function

About this post

Posted: 2018-01-13
By: AntonioZavala
Viewed: 616 times

Categories

Windows

Visual Basic 6

Attachments

No attachments for this post


Loading Comments ...

Comments

No comments have been added for this post.

You must be logged in to make a comment.