• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1054
  • Last Modified:

How can I write an event to the Security Log?

I am developing a VB appilcation (for Windows XP ) which has a feature to generate event logs for troubleshooting purposes.  However, I want the flexibility to decide which event log (app, sys, sec) to add an event to.
Is there a tool, script, or any available method to write to the Security event log?  I do not want or need to modify/delete existing entries; just add new ones (oppose to adding to the application log).
0
free4u
Asked:
free4u
  • 2
1 Solution
 
Shanmuga SundaramCommented:
Please check whether this helps or gives any idea

Private Const EVENTLOG_SUCCESS = &H0
Private Const EVENTLOG_ERROR_TYPE = &H1
Private Const EVENTLOG_WARNING_TYPE = &H2
Private Const EVENTLOG_INFORMATION_TYPE = &H4
Private Const EVENTLOG_AUDIT_SUCCESS = &H8
Private Const EVENTLOG_AUDIT_FAILURE = &H10
Private Const EVENTLOG_SEQUENTIAL_READ = &H1
Private Const EVENTLOG_SEEK_READ = &H2
Private Const EVENTLOG_FORWARDS_READ = &H4
Private Const EVENTLOG_BACKWARDS_READ = &H8
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
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 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 Sub Form_Load()
    
    Dim hEventLog As Long, LogString As String, Ret As Long, ELR As EVENTLOGRECORD
    Dim bBytes(1 To 1024) As Byte
    hEventLog = OpenEventLog(vbNullString, "c:\testlog.bak")
    
    ClearEventLog hEventLog, vbNullString
    
    ReportEvent hEventLog, EVENTLOG_INFORMATION_TYPE, 0, 0, ByVal 0&, 1, 0, "Hello World!", ByVal 0&
    
    GetNumberOfEventLogRecords hEventLog, Ret
    MsgBox "Events reported: " + CStr(Ret)
    
    GetOldestEventLogRecord hEventLog, Ret
    MsgBox "Oldest event record: " + CStr(Ret)
    
    BackupEventLog hEventLog, "c:\testlog.bak"
    
    CloseEventLog hEventLog
End Sub

Open in new window

0
 
jahboiteCommented:
http://msdn2.microsoft.com/en-us/library/e6t4tk09(VS.80).aspx

I believe that the security event log is for system use only, meaning you can't write to it - the documentation doesn't state this explicitly, so it's worth trying anyway!.  You can register your application as an event source and then write to event logs as well as creating your own event logs.
0
 
jahboiteCommented:
Thanks free4u!  How did it go?  I assume you weren't, in the end, able to write to the security log, but did you manage to write to some log?
0

Featured Post

Put Machine Learning to Work--Protect Your Clients

Machine learning means Smarter Cybersecurity™ Solutions.
As technology continues to advance, managing and analyzing massive data sets just can’t be accomplished by humans alone. It requires huge amounts of memory and storage, as well as high-speed processing of the cloud.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now