writing to event viewer to a new event 
Author Message
 writing to event viewer to a new event

i want to write to event viewer ,but to a diffrent place then to the default
which it writes for me now that is to "Application"
can i for example create my owen event called "MyEvent" and to write the
events to there? (i know it's possible in .NET) but how can i do the same in
vb6?

this is the code i use :

Code:
Declare Function RegisterEventSource Lib "advapi32.dll" Alias
"RegisterEventSourceA" (ByVal lpUNCServerName As String, ByVal lpSourceName
As String) As Long
Declare Function DeregisterEventSource Lib "advapi32.dll" (ByVal hEventLog
As Long) As Long
Declare Function ReportEvent Lib "advapi32.dll" Alias "ReportEventA" (ByVal
hEventLog As Long, ByVal wType As Integer, ByVal wCategory As Integer, ByVal
dwEventID As Long, ByVal lpUserSid As Any, ByVal wNumStrings As Integer,
ByVal dwDataSize As Long, plpStrings As Long, lpRawData As Any) As Boolean
Declare Function GetLastError Lib "kernel32" () As Long
Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (hpvDest As Any,
hpvSource As Any, ByVal cbCopy As Long)
Declare Function GlobalAlloc Lib "kernel32" (ByVal wFlags As Long, ByVal
dwBytes As Long) As Long
Declare Function GlobalFree Lib "kernel32" (ByVal hMem As Long) As Long

Public Const c_EVENTLOG_SUCCESS = 0
Public Const c_EVENTLOG_ERROR_TYPE = 1
Public Const c_EVENTLOG_WARNING_TYPE = 2
Public Const c_EVENTLOG_INFORMATION_TYPE = 4
Public Const c_EVENTLOG_AUDIT_SUCCESS = 8
Public Const c_EVENTLOG_AUDIT_FAILURE = 10
and the function :
hEventLog = RegisterEventSource("", psSource)
  llStringSize = Len(psMessage) + 1
  hMsgs = GlobalAlloc(&H40, llStringSize)
  Call CopyMemory(ByVal hMsgs, ByVal psMessage, llStringSize)
  llNumStrings = 1

  Call ReportEvent(hEventLog, plEventType, plEventCategory, plEventID, 0&,
llNumStrings, llStringSize, hMsgs, hMsgs)

  Call GlobalFree(hMsgs)

  Call DeregisterEventSource(hEventLog)

Thanks in advance
peleg



Sun, 10 Jul 2011 00:38:56 GMT  
 writing to event viewer to a new event
Hi pelegk1,


Quote:
>i want to write to event viewer ,but to a diffrent place then to the
>default
> which it writes for me now that is to "Application"
> can i for example create my owen event called "MyEvent" and to write the
> events to there? (i know it's possible in .NET) but how can i do the same
> in
> vb6?

You could write a COM component in .NET and use that from your VB6 app.


Sun, 10 Jul 2011 10:46:31 GMT  
 writing to event viewer to a new event



Quote:
> can i for example create my owen event called "MyEvent"
> and to write the events to there? (i know it's possible in .NET)
> but how can i do the same in vb6?

The following code demonstrates that - it logs the Events
to your App.Titles-Name (in an equally named, separate
branch, beside the 'Application'-branch in the Event-
Viewer).

You can check the Event-Logging then, calling it this way:
Sub Main()
  LogEvent "My EventTestApp Info-String", EVENTLOG_INFORMATION
  LogEvent "My EventTestApp Warning-String", EVENTLOG_WARNING
  LogEvent "My EventTestApp Error-String", EVENTLOG_ERROR
End Sub

Ok, here comes the appropriate module:

'***into a LogEvents.bas Module for example
Option Explicit

Private Const HKLM& = &H80000002
Private Const REG_SZ& = 1
Private Const REG_DWORD& = 4

Public Enum LogTypes
  EVENTLOG_ERROR = 1
  EVENTLOG_WARNING = 2
  EVENTLOG_INFORMATION = 4
End Enum
Private Declare Function RegisterEventSourceW Lib "advapi32" _
  (ByVal lpUNCServerName As Long, ByVal lpSourceName As Long) As Long
Private Declare Function DeregisterEventSource Lib "advapi32" _
  (ByVal hEventLog As Long) As Long
Private Declare Function ReportEventW Lib "advapi32" _
  (ByVal hEventLog As Long, ByVal wType As Integer, _
   ByVal wCategory As Integer, ByVal dwEventID As Long, _
   ByVal lpUserSid As Any, ByVal wNumStrings As Integer, _
   ByVal dwDataSize As Long, plpStrings As Any, lpRawData As Any) As Long
Private Declare Function RegOpenKeyW Lib "advapi32" _
  (ByVal hKey As Long, ByVal lpSubKey As Long, phkResult As Long) As Long
Private Declare Function RegCreateKeyW Lib "advapi32" _
  (ByVal hKey As Long, ByVal lpSubKey As Long, phkResult As Long) As Long
Private Declare Function RegSetValueExW Lib "advapi32" _
  (ByVal hKey As Long, ByVal lpValueName As Long, ByVal Reserved As Long, _
   ByVal dwType As Long, lpData As Any, ByVal cbData As Long) As Long
Private Declare Function RegCloseKey Lib "advapi32" _
  (ByVal hKey As Long) As Long

Public Sub LogEvent(ByVal EvtString As String, _
           Optional ByVal LogType As LogTypes = EVENTLOG_INFORMATION)
Dim hEventLog As Long, RegKey As String, EvtDll As String, hK&
  hEventLog = RegisterEventSourceW(0, StrPtr(App.Title))
  If hEventLog = 0 Then Exit Sub
  RegKey = "System\CurrentControlSet\Services\Eventlog\" & _
           App.Title & "\" & App.Title
  EvtDll = "%SystemRoot%\System32\msvbvm60.dll"
  If RegOpenKeyW(HKLM, StrPtr(RegKey), hK) Then   'if not yet existent
    If RegCreateKeyW(HKLM, StrPtr(RegKey), hK) Then Exit Sub
    RegSetValueExW hK, StrPtr("EventMessageFile"), 0, REG_SZ, _
                ByVal StrPtr(EvtDll), Len(EvtDll)
    RegSetValueExW hK, StrPtr("TypesSupported"), 0, REG_DWORD, 4&, 4
    RegCloseKey hK
  Else
    RegCloseKey hK
  End If
  EvtString = Split(",Error:,Warning:,,Information:", ",")(LogType) _
              & vbCrLf & vbCrLf & EvtString
  ReportEventW hEventLog, LogType, 0, 1, 0&, 1, 0, StrPtr(EvtString), 0&
  DeregisterEventSource hEventLog
End Sub

Olaf



Mon, 11 Jul 2011 00:57:21 GMT  
 writing to event viewer to a new event



Sorry, just ported my ANSI-Version to the W-version
and one small mistake was slipping through...

Please change:

Quote:
>     RegSetValueExW hK, StrPtr("EventMessageFile"), 0, REG_SZ, _
>                 ByVal StrPtr(EvtDll), Len(EvtDll)

To:

RegSetValueExW hK, StrPtr("EventMessageFile"), 0, REG_SZ, _
                 ByVal StrPtr(EvtDll), LenB(EvtDll)

....change Len(EvtDll) to LenB(EvtDll)

Then it should work.

Olaf



Mon, 11 Jul 2011 01:10:26 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Write to Windows NT Event viewer

2. Write to Windows NT Event viewer

3. Write to Windows NT Event viewer

4. Writing to Windows NT Event Viewer

5. Send events to Event Viewer

6. How do you write a events thats is call before the item_remove events from Outlook

7. Write events to the event log ?

8. Does anyone know how to write an event into NT Event Log

9. Making an Event Log Message File in VB (NOT WRITING TO THE EVENT LOG)

10. Writing events to the NT Event Log

11. Event Log: Writing to the NT Event Log

12. Do I Find or Write the new Form Events (Activated,DeActivated,Closing,Closed)

 

 
Powered by phpBB® Forum Software