by Anonymous (267 Submissions)
Category: Windows API Call/Explanation
Compatability: VB 6.0
Difficulty: Unknown Difficulty
Originally Published: Thu 5th November 2009
Date Added: Mon 8th February 2021
Rating: (1 Votes)
Logs event to Windows Application Event Log via ReportEvent function of advapi32 API. Refactored version of code originally submitted by
API Declarations
"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
Public Enum LogTypes
EVENTLOG_SUCCESS = &H0 'Success event
EVENTLOG_ERROR_TYPE = &H1 'Error event
EVENTLOG_WARNING_TYPE = &H2 'Warning event
EVENTLOG_INFORMATION_TYPE = &H4 'Information event
EVENTLOG_AUDIT_SUCCESS = &H8 'Success audit event
EVENTLOG_AUDIT_FAILURE = &H10 'Failure audit event
End Enum
, EventType As LogTypes _
, strEventMsg As String _
, Optional iEventID As Integer = 1 _
, Optional iCategory As Integer = 0) As Long
'******************************************************************
'Returns nonzero for success otherwize zero for fail
'Usage:
'LogEvent([Applicaiton Name], [EventLog Type], [Msg to be logged], [Optional Event ID], [Optional Category Number])
'
'Dim ret As Long
'ret = LogEvent(App.Title, EVENTLOG_SUCCESS, "Success event!!!!")
'******************************************************************
Dim hEvt As Long
Dim ret1 As Long
hEvt = RegisterEventSource(vbNullChar, strSource)
If (hEvt <> 0) Then
ret1 = ReportEvent(hEvt, EventType, iCategory, iEventID, 0, 1, 0, strEventMsg, vbNullChar)
If (ret1 = 0) Then
LogEvent = 0
Exit Function
End If
End If
LogEvent = DeregisterEventSource(hEvt)
End Function
No comments have been posted about Logs event to Windows Application Event Log via ReportEvent function of advapi32 API. Refactored ve. Why not be the first to post a comment about Logs event to Windows Application Event Log via ReportEvent function of advapi32 API. Refactored ve.