Atlanta Custom Software Development 

 
   Search        Code/Page
 

User Login
Email

Password

 

Forgot the Password?
Services
» Web Development
» Maintenance
» Data Integration/BI
» Information Management
Programming
  Database
Automation
OS/Networking
Graphics
Links
Tools
» Regular Expr Tester
» Free Tools

How to Write Multiple String to Windows NT EventLog ?
[ All Languages » VB »  WinNT]

Total Hit ( 2503)

Rate this article:     Poor     Excellent 

 Submit Your Question/Comment about this article

Rating


 


This example code will show you how to write to EventLog. If you have defined Message with multiple parameter substitutions then you have to pass pointer to array of string pointers....!!! Sounds little complicate huhhhhh.. Lets see actual code to clear this confusion.

Note : Before you write to EventLog you have to register Event Source. Registring event source is awhole different story. Check MSDN documentation on Using Event Logging.

Step-By-Step Example

- Create a standard exe project
- Add the following code in form1 code window

Click here to copy the following block
Private Const EVENTLOG_SUCCESS = 0
Private Const EVENTLOG_ERROR_TYPE = 1
Private Const EVENTLOG_WARNING_TYPE = 2
Private Const EVENTLOG_INFORMATION_TYPE = 4
Private Const EVENTLOG_AUDIT_SUCCESS = 8
Private Const EVENTLOG_AUDIT_FAILURE = 10

Private Const MAX_NUMBER_SM_PARAMETERS = 5

Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" ( _
    hpvDest As Any, _
    hpvSource As Any, _
    ByVal cbCopy As Long)

Declare Function GetLastError Lib "kernel32" () 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

Declare Function DeregisterEventSource Lib "advapi32.dll" ( _
    ByVal hEventLog As Long) As Long

Private Declare Function RegisterEventSource Lib "advapi32.dll" Alias "RegisterEventSourceA" ( _
    ByVal lpUNCServerName As String, _
    ByVal lpSourceName As String) 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

Private Sub TestWriteLogEvent()
  Dim intCategory As Integer
  Dim intCounter As Integer
  Dim intLogType As Integer
  Dim intNumParams As Integer
  Dim lgEventID As Long
  Dim lgHandle As Long
  Dim lgReturnValue As Long
  Dim lgStringBufferPointer(MAX_NUMBER_SM_PARAMETERS) As Long
  Dim lgStringSize As Long
  Dim strLogEntry As String
  Dim strParams(MAX_NUMBER_SM_PARAMETERS) As String
  Dim strSource As String

  On Error GoTo ErrorLogEvent

  'these are just example values
  intCategory = 0
  intLogType = EVENTLOG_ERROR_TYPE
  lgEventID = 100
  intNumParams = 3
  strParams(0) = "My Program Name"
  strParams(1) = "Some Error String"
  strParams(2) = "Another Error String"

  For intCounter = 0 To intNumParams - 1
    strLogEntry = strParams(intCounter)
    lgStringSize = Len(strLogEntry) + 1
    lgStringBufferPointer(intCounter) = GlobalAlloc(&H40, lgStringSize)
    CopyMemory ByVal lgStringBufferPointer(intCounter), ByVal strLogEntry, lgStringSize
  Next intCounter

  strSource = App.Title
  lgHandle = RegisterEventSource(vbNullString, strSource)

  lgReturnValue = ReportEvent(lgHandle, _
      intLogType, _
      intCategory, _
      lgEventID, _
      0&, _
      intNumParams, _
      0, _
      lgStringBufferPointer(0), _
      vbNullString)

  For intCounter = 0 To intNumParams - 1
    Call GlobalFree(lgStringBufferPointer(intCounter))
  Next intCounter

  lgReturnValue = DeregisterEventSource(lgHandle)

  Exit Sub

ErrorLogEvent:
  'handle error here
  Exit Sub
End Sub

Private Sub Form_Load()
  Call TestWriteLogEvent
End Sub


Submitted By : Nayan Patel  (Member Since : 5/26/2004 12:23:06 PM)

Job Description : He is the moderator of this site and currently working as an independent consultant. He works with VB.net/ASP.net, SQL Server and other MS technologies. He is MCSD.net, MCDBA and MCSE. In his free time he likes to watch funny movies and doing oil painting.
View all (893) submissions by this author  (Birth Date : 7/14/1981 )


Home   |  Comment   |  Contact Us   |  Privacy Policy   |  Terms & Conditions   |  BlogsZappySys

© 2008 BinaryWorld LLC. All rights reserved.