ReportEvent fails
-
I was trying to write data in the event log. I found these lines of code somewhere: Dim bRC As Boolean Dim iNumStrings As Integer Dim hEventLog As Long Dim hMsgs As Long Dim cbStringSize As Long Dim iEventID As Integer Dim sMessage As String Dim sSource As String sSource = "ErrLog" sMessage = "Success Message" hEventLog = RegisterEventSource("", sSource) cbStringSize = Len(sMessage) + 1 hMsgs = GlobalAlloc(&H40, cbStringSize) CopyMemory ByVal hMsgs, ByVal sMessage, cbStringSize iNumStrings = 1 If ReportEvent(hEventLog, _ EVENTLOG_SUCCESS, 0, _ EBALOG_SUCCESS, 0&, _ iNumStrings, cbStringSize, _ hMsgs, hMsgs) = 0 Then MsgBox "FAILED" Else '-- Sucessful MsgBox "Success" End If Call GlobalFree(hMsgs) DeregisterEventSource (hEventLog) But it doesn't work, The function always returns 0. And it doesn't write anything on the log. please help. SDE
-
I was trying to write data in the event log. I found these lines of code somewhere: Dim bRC As Boolean Dim iNumStrings As Integer Dim hEventLog As Long Dim hMsgs As Long Dim cbStringSize As Long Dim iEventID As Integer Dim sMessage As String Dim sSource As String sSource = "ErrLog" sMessage = "Success Message" hEventLog = RegisterEventSource("", sSource) cbStringSize = Len(sMessage) + 1 hMsgs = GlobalAlloc(&H40, cbStringSize) CopyMemory ByVal hMsgs, ByVal sMessage, cbStringSize iNumStrings = 1 If ReportEvent(hEventLog, _ EVENTLOG_SUCCESS, 0, _ EBALOG_SUCCESS, 0&, _ iNumStrings, cbStringSize, _ hMsgs, hMsgs) = 0 Then MsgBox "FAILED" Else '-- Sucessful MsgBox "Success" End If Call GlobalFree(hMsgs) DeregisterEventSource (hEventLog) But it doesn't work, The function always returns 0. And it doesn't write anything on the log. please help. SDE
The return value is zero because there was a failure. Use GetLastError to get the error number, then look that up here[^]. This is the System Error Codes list on MSDN. You might want to check the value returned by RegisterEventSource first. It looks like your not getting a good EventLog handle returned here. Your passing in an empty string instead of a 0 (which means NULL). This is where I think your problem is. If you change the call to this, it might work:
hEventLog = RegisterEventSource("." & chr$(0), sSource)
Again, if you get back a zero for either RegisterEventSource or ReportEvent, then use GetLastError to find out what the error was. RageInTheMachine9532