Happy Codings - Programming Code Examples
Html Css Web Design Sample Codes CPlusPlus Programming Sample Codes JavaScript Programming Sample Codes C Programming Sample Codes CSharp Programming Sample Codes Java Programming Sample Codes Php Programming Sample Codes Visual Basic Programming Sample Codes


Visual Basic Programming Code Examples

Visual Basic > Windows and Controls Code Examples

Writing to the NT event log

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
Writing to the NT event log Below is the basic outline of a class used to write data to the NT event log. An example is included at the bottom of this post. ----Add this to a class called "clsEventLog" Option Explicit Private zsSource As String Private Declare Function RegisterEventSource Lib "advapi32.dll" Alias "RegisterEventSourceA" (ByVal lpUNCServerName As String, ByVal lpSourceName As String) As Long Private Declare Function DeregisterEventSource Lib "advapi32.dll" (ByVal hEventLog As Long) As Long Private Declare Function ReportEvent Lib "advapi32.dll" Alias "ReportEventA" (ByVal hEventLog As Long, ByVal wType As Long, ByVal wCategory As Long, ByVal dwEventID As Long, lpUserSid As Any, ByVal wNumStrings As Long, ByVal dwDataSize As Long, lpStrings As String, lpRawData As Any) As Long Private Declare Function GetLastError Lib "kernel32" () As Long Private Declare Function OpenEventLog Lib "advapi32.dll" Alias "OpenEventLogA" (ByVal lpUNCServerName As String, ByVal lpSourceName As String) As Long Private Declare Function CloseEventLog Lib "advapi32.dll" (ByVal hEventLog As Long) As Long Private Declare Function BackupEventLog Lib "advapi32.dll" Alias "BackupEventLogA" (ByVal hEventLog As Long, ByVal lpBackupFileName As String) As Long Private Declare Function ClearEventLog Lib "advapi32.dll" Alias "ClearEventLogA" (ByVal hEventLog As Long, ByVal lpBackupFileName As String) As Long Private Declare Function GetNumberOfEventLogRecords Lib "advapi32.dll" (ByVal hEventLog As Long, NumberOfRecords As Long) As Long Private Declare Function GetOldestEventLogRecord Lib "advapi32.dll" (ByVal hEventLog As Long, OldestRecord As Long) As Long Private Type EVENTLOGRECORD Length As Long ' Length of full record Reserved As Long ' Used by the service RecordNumber As Long ' Absolute record number TimeGenerated As Long ' Seconds since 1-1-1970 TimeWritten As Long 'Seconds since 1-1-1970 EventID As Long EventType As Integer NumStrings As Integer EventCategory As Integer ReservedFlags As Integer ' For use with paired events (auditing) ClosingRecordNumber As Long 'For use with paired events (auditing) StringOffset As Long ' Offset from beginning of record UserSidLength As Long UserSidOffset As Long DataLength As Long DataOffset As Long ' Offset from beginning of record End Type Public Enum eevLogType EVENTLOG_SUCCESS = 0 EVENTLOG_ERROR_TYPE = 1 EVENTLOG_WARNING_TYPE = 2 EVENTLOG_INFORMATION_TYPE = 4 EVENTLOG_AUDIT_SUCCESS = 8 EVENTLOG_AUDIT_FAILURE = 10 End Enum Property Get Source() As String Source = zsSource End Property Property Let Source(Value As String) zsSource = Value End Property Function Log(sTitle As String, sMessage As String, eLogType As eevLogType) As Boolean Dim lLenMessage As Long Dim lhwndEventLog As Long lhwndEventLog = RegisterEventSource("", zsSource) lLenMessage = Len(sTitle) + 1 'Write the event in the application event log If ReportEvent(lhwndEventLog, eLogType, 0, 1, ByVal 0&, 1, 0, sMessage, ByVal 0&) = 0 Then 'Failed Log = False Else 'Success Log = True End If If lhwndEventLog Then DeregisterEventSource lhwndEventLog End If End Function Private Sub Class_Initialize() zsSource = "VB Event Log" End Sub Sub ViewLog() Shell "eventvwr.exe", vbNormalFocus End Sub ----Add this to a standard module Option Explicit 'Example code Sub TestLog() Dim oEventLog As clsEventLog Set oEventLog = New clsEventLog oEventLog.Source = "Andrew Test" oEventLog.Log "TEST", "TEST MESSAGE", EVENTLOG_ERROR_TYPE End Sub