Free Trial Issue of Visual Studio Magazine

VB5, VB6 Call the WriteEventLog Method
Private Sub cmdWriteEventLog_Click()
Dim enmLogType As enmLogType
Dim abytDataBuffer() As Byte
Dim astrEventText() As String
' Other DIMs not shown
If optLocalRemote(0).Value = True Then
        strServerName$ = ""
Else
        strServerName$ = _
                Trim$(txtRemoteMachine.Text)
End If
strAppName = APP_NAME
strEventLogName = cboLogName.List _
        (Me.cboLogName.ListIndex)
abytDataBuffer = _ 
        Me.txtEventData.Text
        ReDim astrEventText (0 To 0) As String
        astrEventText(0) = Me.txtEventString.Text
strAppNameMunged = GetAppNameMunged _
        (strEventLogName, strAppName)
If optErrorLevel(0).Value = True Then
        enmLogType = LogInfo
ElseIf optErrorLevel(1). Value = True Then
        enmLogType = LogWarning
ElseIf optErrorLevel(2). Value = True Then
        enmLogType = LogError
End If
lngEventID = CLng(cboEventID.ItemData _
        (Me.cboEventID.ListIndex))
lngCategory = CLng(Me.cboCategory.List _
        (Me.cboCategory.ListIndex))
m_objEvntLog.EventDataBuffer = abytDataBuffer
m_objEvntLog.EventCategory = lngCategory
If UCase$(Trim$ (strEventLogName)) = _
        "SECURITY" Then
        MsgBox "Only the LocalSystem acct can " & _
                "write to the security log"
Else
        On Error GoTo WriteLog_Err
m_objEvntLog.WriteEventLog strServerName, _
        strAppNameMunged, enmLogType, _
        lngEventID, strEventLogName, astrEventText
End If
Exit Sub
WriteLog_Err:
' MsgBox with error info
End Sub
Listing 1 | Calling the WriteEventLog method in the WriteEvents.dll is straightforward. Most of the code deals with capturing information from the sample UI, something you probably don't need in a real app. The Category and binary data (EventDataBuffer) are optional properties you can set.