Changes between Initial Version and Version 1 of Ticket #3237, comment 2


Ignore:
Timestamp:
05/21/16 21:11:44 (8 years ago)
Author:
Melba23
Comment:

Legend:

Unmodified
Added
Removed
Modified
  • Ticket #3237, comment 2

    initial v1  
    1 #RequireAdmin
    2 
    3 
    4 ;++++++++++++++++++++++++++++++++++START OF INCLUDE
    5 ;;#include <EventLog.au3>
    6 ;#include-once
    7 
    8 #include "Date.au3"
    9 #include "Security.au3"
    10 #include "StructureConstants.au3"
    11 #include "WinAPI.au3"
    12 
    13 ; #INDEX# =======================================================================================================================
    14 ; Title .........: Event_Log
    15 ; AutoIt Version : 3.3.14.2
    16 ; Language ......: English
    17 ; Description ...: Functions that assist Windows System logs.
    18 ; Description ...: When an error occurs, the system administrator or support technicians must determine what  caused  the  error,
    19 ;                  attempt to recover any lost data, and prevent the error from recurring.  It is helpful  if  applications,  the
    20 ;                  operating system, and other system services record important events such as low-memory conditions or excessive
    21 ;                  attempts to access a disk.  Then the system administrator can  use  the  event  log  to  help  determine  what
    22 ;                  conditions caused the error and the context in which it occurred.  By periodically viewing the event log,  the
    23 ;                  system administrator may be able to identify problems (such as a failing hard drive) before they cause damage.
    24 ; Author(s) .....: Paul Campbell (PaulIA), Gary Frost
    25 ; Dll ...........: advapi32.dll
    26 ; ===============================================================================================================================
    27 
    28 ; #VARIABLES# ===================================================================================================================
    29 Global $__g_sSourceName_Event
    30 ; ===============================================================================================================================
    31 
    32 ; #CONSTANTS# ===================================================================================================================
    33 Global Const $EVENTLOG_SUCCESS = 0x00000000
    34 Global Const $EVENTLOG_ERROR_TYPE = 0x00000001
    35 Global Const $EVENTLOG_WARNING_TYPE = 0x00000002
    36 Global Const $EVENTLOG_INFORMATION_TYPE = 0x00000004
    37 Global Const $EVENTLOG_AUDIT_SUCCESS = 0x00000008
    38 Global Const $EVENTLOG_AUDIT_FAILURE = 0x00000010
    39 Global Const $EVENTLOG_SEQUENTIAL_READ = 0x00000001
    40 Global Const $EVENTLOG_SEEK_READ = 0x00000002
    41 Global Const $EVENTLOG_FORWARDS_READ = 0x00000004
    42 Global Const $EVENTLOG_BACKWARDS_READ = 0x00000008
    43 
    44 Global Const $__EVENTLOG_LOAD_LIBRARY_AS_DATAFILE = 0x00000002
    45 Global Const $__EVENTLOG_FORMAT_MESSAGE_FROM_HMODULE = 0x00000800
    46 Global Const $__EVENTLOG_FORMAT_MESSAGE_IGNORE_INSERTS = 0x00000200
    47 ; ===============================================================================================================================
    48 
    49 ; #CURRENT# =====================================================================================================================
    50 ; _EventLog__Backup
    51 ; _EventLog__Clear
    52 ; _EventLog__Close
    53 ; _EventLog__Count
    54 ; _EventLog__DeregisterSource
    55 ; _EventLog__Full
    56 ; _EventLog__Notify
    57 ; _EventLog__Oldest
    58 ; _EventLog__Open
    59 ; _EventLog__OpenBackup
    60 ; _EventLog__Read
    61 ; _EventLog__RegisterSource
    62 ; _EventLog__Report
    63 ; ===============================================================================================================================
    64 
    65 ; #INTERNAL_USE_ONLY# ===========================================================================================================
    66 ; __EventLog_DecodeCategory
    67 ; __EventLog_DecodeComputer
    68 ; __EventLog_DecodeData
    69 ; __EventLog_DecodeDate
    70 ; __EventLog_DecodeDesc
    71 ; __EventLog_DecodeEventID
    72 ; __EventLog_DecodeSource
    73 ; __EventLog_DecodeStrings
    74 ; __EventLog_DecodeTime
    75 ; __EventLog_DecodeTypeStr
    76 ; __EventLog_DecodeUserName
    77 ; ===============================================================================================================================
    78 
    79 ; #FUNCTION# ====================================================================================================================
    80 ; Author ........: Paul Campbell (PaulIA)
    81 ; Modified.......: Gary Frost (gafrost)
    82 ; ===============================================================================================================================
    83 Func _EventLog__Backup($hEventLog, $sFileName)
    84         Local $aResult = DllCall("advapi32.dll", "bool", "BackupEventLogW", "handle", $hEventLog, "wstr", $sFileName)
    85         If @error Then Return SetError(@error, @extended, False)
    86         Return $aResult[0] <> 0
    87 EndFunc   ;==>_EventLog__Backup
    88 
    89 ; #FUNCTION# ====================================================================================================================
    90 ; Author ........: Paul Campbell (PaulIA)
    91 ; Modified.......: Gary Frost (gafrost)
    92 ; ===============================================================================================================================
    93 Func _EventLog__Clear($hEventLog, $sFileName)
    94         Local $bTemp = False
    95         If StringLen($sFileName) = 0 Then
    96                 $sFileName = @TempDir & "\_EventLog_tempbackup.bak"
    97                 $bTemp = True
    98         EndIf
    99         Local $aResult = DllCall("advapi32.dll", "bool", "ClearEventLogW", "handle", $hEventLog, "wstr", $sFileName)
    100         If @error Then Return SetError(@error, @extended, False)
    101         If $bTemp Then FileDelete($sFileName)
    102         Return $aResult[0] <> 0
    103 EndFunc   ;==>_EventLog__Clear
    104 
    105 ; #FUNCTION# ====================================================================================================================
    106 ; Author ........: Paul Campbell (PaulIA)
    107 ; Modified.......: Gary Frost (gafrost)
    108 ; ===============================================================================================================================
    109 Func _EventLog__Close($hEventLog)
    110         Local $aResult = DllCall("advapi32.dll", "bool", "CloseEventLog", "handle", $hEventLog)
    111         If @error Then Return SetError(@error, @extended, False)
    112         Return $aResult[0] <> 0
    113 EndFunc   ;==>_EventLog__Close
    114 
    115 ; #FUNCTION# ====================================================================================================================
    116 ; Author ........: Paul Campbell (PaulIA)
    117 ; Modified.......:
    118 ; ===============================================================================================================================
    119 Func _EventLog__Count($hEventLog)
    120         Local $aResult = DllCall("advapi32.dll", "bool", "GetNumberOfEventLogRecords", "handle", $hEventLog, "dword*", 0)
    121         If @error Then Return SetError(@error, @extended, -1)
    122         If $aResult[0] = 0 Then Return -1
    123         Return $aResult[2]
    124 EndFunc   ;==>_EventLog__Count
    125 
    126 ; #INTERNAL_USE_ONLY# ===========================================================================================================
    127 ; Name...........: __EventLog_DecodeCategory
    128 ; Description ...: Decodes an event category for an event record
    129 ; Syntax.........: __EventLog_DecodeCategory ( $tEventLog )
    130 ; Parameters ....: $tEventLog   - tagEVENTLOGRECORD structure
    131 ; Return values .: Success      - Event category
    132 ; Author ........: Paul Campbell (PaulIA)
    133 ; Modified.......: Gary Frost (gafrost)
    134 ; Remarks .......: This function is used internally
    135 ; Related .......:
    136 ; Link ..........:
    137 ; Example .......:
    138 ; ===============================================================================================================================
    139 Func __EventLog_DecodeCategory($tEventLog)
    140         Return DllStructGetData($tEventLog, "EventCategory")
    141 EndFunc   ;==>__EventLog_DecodeCategory
    142 
    143 ; #INTERNAL_USE_ONLY# ===========================================================================================================
    144 ; Name...........: __EventLog_DecodeComputer
    145 ; Description ...: Decodes the computer name from an event log record
    146 ; Syntax.........: __EventLog_DecodeComputer ( $tEventLog )
    147 ; Parameters ....: $tEventLog   - tagEVENTLOGRECORD structure
    148 ; Return values .: Success      - Computer name
    149 ; Author ........: Paul Campbell (PaulIA)
    150 ; Modified.......: Gary Frost (gafrost)
    151 ; Remarks .......: This function is used internally
    152 ; Related .......:
    153 ; Link ..........:
    154 ; Example .......:
    155 ; ===============================================================================================================================
    156 Func __EventLog_DecodeComputer($tEventLog)
    157         Local $pEventLog = DllStructGetPtr($tEventLog)
    158         ; The buffer length doesn't need to extend past UserSidOffset since
    159         ; the string appears before that.
    160         Local $iLength = DllStructGetData($tEventLog, "UserSidOffset") - 1
    161         ; This points to the start of the variable length data.
    162         Local $iOffset = DllStructGetSize($tEventLog)
    163         ; Offset the buffer with the Source string length which appears right
    164         ; before the Computer name.
    165         $iOffset += 2 * (StringLen(__EventLog_DecodeSource($tEventLog)) + 1)
    166         ; Adjust the length to be a difference instead of absolute address.
    167         $iLength -= $iOffset
    168         ; Adjust the buffer to point to the start of the Computer string.
    169         Local $tBuffer = DllStructCreate("wchar Text[" & $iLength & "]", $pEventLog + $iOffset)
    170         Return DllStructGetData($tBuffer, "Text")
    171 EndFunc   ;==>__EventLog_DecodeComputer
    172 
    173 ; #INTERNAL_USE_ONLY# ===========================================================================================================
    174 ; Name...........: __EventLog_DecodeData
    175 ; Description ...: Decodes the event specific binary data from an event log record
    176 ; Syntax.........: __EventLog_DecodeData ( $tEventLog )
    177 ; Parameters ....: $tEventLog   - tagEVENTLOGRECORD structure
    178 ; Return values .: Success      - Array with the following format:
    179 ;                  |[0] - Number of bytes in array
    180 ;                  |[1] - Byte 1
    181 ;                  |[2] - Byte 2
    182 ;                  |[n] - Byte n
    183 ; Author ........: Paul Campbell (PaulIA)
    184 ; Modified.......: Gary Frost (gafrost)
    185 ; Remarks .......: This function is used internally
    186 ; Related .......:
    187 ; Link ..........:
    188 ; Example .......:
    189 ; ===============================================================================================================================
    190 Func __EventLog_DecodeData($tEventLog)
    191         Local $pEventLog = DllStructGetPtr($tEventLog)
    192         Local $iOffset = DllStructGetData($tEventLog, "DataOffset")
    193         Local $iLength = DllStructGetData($tEventLog, "DataLength")
    194         Local $tBuffer = DllStructCreate("byte[" & $iLength & "]", $pEventLog + $iOffset)
    195         Local $aData[$iLength + 1]
    196         $aData[0] = $iLength
    197         For $iI = 1 To $iLength
    198                 $aData[$iI] = DllStructGetData($tBuffer, 1, $iI)
    199         Next
    200         Return $aData
    201 EndFunc   ;==>__EventLog_DecodeData
    202 
    203 ; #INTERNAL_USE_ONLY# ===========================================================================================================
    204 ; Name...........: __EventLog_DecodeDate
    205 ; Description ...: Converts an event log time to a date string
    206 ; Syntax.........: __EventLog_DecodeDate ( $iEventTime )
    207 ; Parameters ....: $iEventTime  - Event log time to be converted
    208 ; Return values .: Success      - Date string in the format of mm/dd/yyyy
    209 ; Author ........: Paul Campbell (PaulIA)
    210 ; Modified.......: Gary Frost (gafrost)
    211 ; Remarks .......: This function is used internally
    212 ; Related .......:
    213 ; Link ..........:
    214 ; Example .......:
    215 ; ===============================================================================================================================
    216 Func __EventLog_DecodeDate($iEventTime)
    217         Local $tInt64 = DllStructCreate("int64")
    218         Local $pInt64 = DllStructGetPtr($tInt64)
    219         Local $tFileTime = DllStructCreate($tagFILETIME, $pInt64)
    220         DllStructSetData($tInt64, 1, ($iEventTime * 10000000) + 116444736000000000)
    221         Local $tLocalTime = _Date_Time_FileTimeToLocalFileTime($tFileTime)
    222         Local $tSystTime = _Date_Time_FileTimeToSystemTime($tLocalTime)
    223         Local $iMonth = DllStructGetData($tSystTime, "Month")
    224         Local $iDay = DllStructGetData($tSystTime, "Day")
    225         Local $iYear = DllStructGetData($tSystTime, "Year")
    226         Return StringFormat("%02d/%02d/%04d", $iMonth, $iDay, $iYear)
    227 EndFunc   ;==>__EventLog_DecodeDate
    228 
    229 ; #INTERNAL_USE_ONLY# ===========================================================================================================
    230 ; Name...........: __EventLog_DecodeDesc
    231 ; Description ...: Decodes the description strings for an event record
    232 ; Syntax.........: __EventLog_DecodeDesc ( $tEventLog )
    233 ; Parameters ....: $tEventLog   - tagEVENTLOGRECORD structure
    234 ; Return values .: Success      - Description
    235 ; Author ........: Paul Campbell (PaulIA)
    236 ; Modified.......: Gary Frost (gafrost)
    237 ; Remarks .......: This function is used internally
    238 ; Related .......:
    239 ; Link ..........:
    240 ; Example .......:
    241 ; ===============================================================================================================================
    242 Func __EventLog_DecodeDesc($tEventLog)
    243         Local $aStrings = __EventLog_DecodeStrings($tEventLog)
    244         Local $sSource = __EventLog_DecodeSource($tEventLog)
    245         Local $iEventID = DllStructGetData($tEventLog, "EventID")
    246         Local $sKey = "HKLM\SYSTEM\CurrentControlSet\Services\Eventlog\" & $__g_sSourceName_Event & "\" & $sSource
    247         Local $aMsgDLL = StringSplit(_WinAPI_ExpandEnvironmentStrings(RegRead($sKey, "EventMessageFile")), ";")
    248 
    249         Local $iFlags = BitOR($__EVENTLOG_FORMAT_MESSAGE_FROM_HMODULE, $__EVENTLOG_FORMAT_MESSAGE_IGNORE_INSERTS)
    250         Local $sDesc = ""
    251         For $iI = 1 To $aMsgDLL[0]
    252                 Local $hDLL = _WinAPI_LoadLibraryEx($aMsgDLL[$iI], $__EVENTLOG_LOAD_LIBRARY_AS_DATAFILE)
    253                 If $hDLL = 0 Then ContinueLoop
    254                 Local $tBuffer = DllStructCreate("wchar Text[4096]")
    255                 _WinAPI_FormatMessage($iFlags, $hDLL, $iEventID, 0, $tBuffer, 4096, 0)
    256                 _WinAPI_FreeLibrary($hDLL)
    257                 $sDesc &= DllStructGetData($tBuffer, "Text")
    258         Next
    259 
    260         If $sDesc = "" Then
    261                 For $iI = 1 To $aStrings[0]
    262                         $sDesc &= $aStrings[$iI]
    263                 Next
    264         Else
    265                 Local $sDescTEST=$sDesc
    266                 if $aStrings[0] > 9 Then msgbox(0,"INSERTION TEMPLATE BROKEN",$sDesc);ADDED TO SHOW PROBLEM
    267                 For $iI = 1 To $aStrings[0]
    268                         $sDesc = StringReplace($sDesc, "%" & $iI, $aStrings[$iI]);CHANGE TO $sDesc = StringReplace($sDesc, "%" & $iI, $aStrings[$iI],1) to fix
    269                 if $aStrings[0] > 9  and $iI <2 or $iI = $aStrings[0] Then msgbox(0,"INSERTION TEMPLATE BROKEN",$sDesc);ADDED TO SHOW PROBLEM
    270                 Next
    271 if $aStrings[0] > 9 Then msgbox(0,"INSERTION TEMPLATE FIXED",$sDescTEST);ADDED TO SHOW PROBLEM
    272              For $iI = 1 To $aStrings[0]
    273                         $sDescTEST = StringReplace($sDescTEST, "%" & $iI, $aStrings[$iI],1);CHANGED FROM $sDesc = StringReplace($sDesc, "%" & $iI, $aStrings[$iI])
    274                 if $aStrings[0] > 9  and $iI <2 or $iI = $aStrings[0] Then msgbox(0,"INSERTION TEMPLATE FIXED",$sDescTEST);ADDED TO SHOW PROBLEM
    275                 Next
    276         EndIf
    277         Return StringStripWS($sDesc, $STR_STRIPLEADING + $STR_STRIPTRAILING)
    278 EndFunc   ;==>__EventLog_DecodeDesc
    279 
    280 ; #INTERNAL_USE_ONLY# ===========================================================================================================
    281 ; Name...........: __EventLog_DecodeEventID
    282 ; Description ...: Decodes an event ID for an event record
    283 ; Syntax.........: __EventLog_DecodeEventID ( $tEventLog )
    284 ; Parameters ....: $tEventLog   - tagEVENTLOGRECORD structure
    285 ; Return values .: Success      - Event ID
    286 ; Author ........: Paul Campbell (PaulIA)
    287 ; Modified.......: Gary Frost (gafrost)
    288 ; Remarks .......: This function is used internally
    289 ; Related .......:
    290 ; Link ..........:
    291 ; Example .......:
    292 ; ===============================================================================================================================
    293 Func __EventLog_DecodeEventID($tEventLog)
    294         Return BitAND(DllStructGetData($tEventLog, "EventID"), 0x7FFF)
    295 EndFunc   ;==>__EventLog_DecodeEventID
    296 
    297 ; #INTERNAL_USE_ONLY# ===========================================================================================================
    298 ; Name...........: __EventLog_DecodeSource
    299 ; Description ...: Decodes the event source from an event log record
    300 ; Syntax.........: __EventLog_DecodeSource ( $tEventLog )
    301 ; Parameters ....: $tEventLog   - tagEVENTLOGRECORD structure
    302 ; Return values .: Success      - Source name
    303 ; Author ........: Paul Campbell (PaulIA)
    304 ; Modified.......: Gary Frost (gafrost)
    305 ; Remarks .......: This function is used internally
    306 ; Related .......:
    307 ; Link ..........:
    308 ; Example .......:
    309 ; ===============================================================================================================================
    310 Func __EventLog_DecodeSource($tEventLog)
    311         Local $pEventLog = DllStructGetPtr($tEventLog)
    312         ; The buffer length doesn't need to extend past UserSidOffset since
    313         ; the string appears before that.
    314         Local $iLength = DllStructGetData($tEventLog, "UserSidOffset") - 1
    315         ; This points to the start of the variable length data.
    316         Local $iOffset = DllStructGetSize($tEventLog)
    317         ; Adjust the length to be a difference instead of absolute address.
    318         $iLength -= $iOffset
    319         ; Initialize the buffer to the start of the variable length data
    320         Local $tBuffer = DllStructCreate("wchar Text[" & $iLength & "]", $pEventLog + $iOffset)
    321         Return DllStructGetData($tBuffer, "Text")
    322 EndFunc   ;==>__EventLog_DecodeSource
    323 
    324 ; #INTERNAL_USE_ONLY# ===========================================================================================================
    325 ; Name...........: __EventLog_DecodeStrings
    326 ; Description ...: Decodes the insertion strings from an event log record
    327 ; Syntax.........: __EventLog_DecodeStrings ( $tEventLog )
    328 ; Parameters ....: $tEventLog   - tagEVENTLOGRECORD structure
    329 ; Return values .: Success      - Array with the following format:
    330 ;                  |[0] - Number of strings in array
    331 ;                  |[1] - String 1
    332 ;                  |[2] - String 2
    333 ;                  |[n] - String n
    334 ; Author ........: Paul Campbell (PaulIA)
    335 ; Modified.......: Gary Frost (gafrost)
    336 ; Remarks .......: This function is used internally
    337 ; Related .......:
    338 ; Link ..........:
    339 ; Example .......:
    340 ; ===============================================================================================================================
    341 Func __EventLog_DecodeStrings($tEventLog)
    342         Local $pEventLog = DllStructGetPtr($tEventLog)
    343         Local $iNumStrs = DllStructGetData($tEventLog, "NumStrings")
    344         Local $iOffset = DllStructGetData($tEventLog, "StringOffset")
    345         ; The data offset is used to calculate buffer sizes.
    346         Local $iDataOffset = DllStructGetData($tEventLog, "DataOffset")
    347         Local $tBuffer = DllStructCreate("wchar Text[" & $iDataOffset - $iOffset & "]", $pEventLog + $iOffset)
    348 
    349         Local $aStrings[$iNumStrs + 1]
    350         $aStrings[0] = $iNumStrs
    351         For $iI = 1 To $iNumStrs
    352                 $aStrings[$iI] = DllStructGetData($tBuffer, "Text")
    353                 $iOffset += 2 * (StringLen($aStrings[$iI]) + 1)
    354                 $tBuffer = DllStructCreate("wchar Text[" & $iDataOffset - $iOffset & "]", $pEventLog + $iOffset)
    355         Next
    356         Return $aStrings
    357 EndFunc   ;==>__EventLog_DecodeStrings
    358 
    359 ; #INTERNAL_USE_ONLY# ===========================================================================================================
    360 ; Name...........: __EventLog_DecodeTime
    361 ; Description ...: Converts an event log time to a date time
    362 ; Syntax.........: __EventLog_DecodeTime ( $iEventTime )
    363 ; Parameters ....: $iEventTime  - Event log time to be converted
    364 ; Return values .: Success      - Time string in the format of hh:mm:ss am/pm
    365 ; Author ........: Paul Campbell (PaulIA)
    366 ; Modified.......: Gary Frost (gafrost)
    367 ; Remarks .......: This function is used internally
    368 ; Related .......:
    369 ; Link ..........:
    370 ; Example .......:
    371 ; ===============================================================================================================================
    372 Func __EventLog_DecodeTime($iEventTime)
    373         Local $tInt64 = DllStructCreate("int64")
    374         Local $pInt64 = DllStructGetPtr($tInt64)
    375         Local $tFileTime = DllStructCreate($tagFILETIME, $pInt64)
    376         DllStructSetData($tInt64, 1, ($iEventTime * 10000000) + 116444736000000000)
    377         Local $tLocalTime = _Date_Time_FileTimeToLocalFileTime($tFileTime)
    378         Local $tSystTime = _Date_Time_FileTimeToSystemTime($tLocalTime)
    379         Local $iHours = DllStructGetData($tSystTime, "Hour")
    380         Local $iMinutes = DllStructGetData($tSystTime, "Minute")
    381         Local $iSeconds = DllStructGetData($tSystTime, "Second")
    382         Local $sAMPM = "AM"
    383         If $iHours < 12 Then
    384                 If $iHours = 0 Then
    385                         $iHours = 12
    386                 EndIf
    387         Else
    388                 $sAMPM = "PM"
    389                 If $iHours > 12 Then
    390                         $iHours -= 12
    391                 EndIf
    392         EndIf
    393         Return StringFormat("%02d:%02d:%02d %s", $iHours, $iMinutes, $iSeconds, $sAMPM)
    394 EndFunc   ;==>__EventLog_DecodeTime
    395 
    396 ; #INTERNAL_USE_ONLY# ===========================================================================================================
    397 ; Name...........: __EventLog_DecodeTypeStr
    398 ; Description ...: Decodes an event type to an event string
    399 ; Syntax.........: __EventLog_DecodeTypeStr ( $iEventType )
    400 ; Parameters ....: $iEventType  - Event type
    401 ; Return values .: Success      - String indicating the event type
    402 ;                  Failure      - Unknown event type ID
    403 ; Author ........: Paul Campbell (PaulIA)
    404 ; Modified.......:
    405 ; Remarks .......: This function is used internally
    406 ; Related .......:
    407 ; Link ..........:
    408 ; Example .......:
    409 ; ===============================================================================================================================
    410 Func __EventLog_DecodeTypeStr($iEventType)
    411         Select
    412                 Case $iEventType = $EVENTLOG_SUCCESS
    413                         Return "Success"
    414                 Case $iEventType = $EVENTLOG_ERROR_TYPE
    415                         Return "Error"
    416                 Case $iEventType = $EVENTLOG_WARNING_TYPE
    417                         Return "Warning"
    418                 Case $iEventType = $EVENTLOG_INFORMATION_TYPE
    419                         Return "Information"
    420                 Case $iEventType = $EVENTLOG_AUDIT_SUCCESS
    421                         Return "Success audit"
    422                 Case $iEventType = $EVENTLOG_AUDIT_FAILURE
    423                         Return "Failure audit"
    424                 Case Else
    425                         Return $iEventType
    426         EndSelect
    427 EndFunc   ;==>__EventLog_DecodeTypeStr
    428 
    429 ; #INTERNAL_USE_ONLY# ===========================================================================================================
    430 ; Name...........: __EventLog_DecodeUserName
    431 ; Description ...: Decodes the user name from an event log record
    432 ; Syntax.........: __EventLog_DecodeUserName ( $tEventLog )
    433 ; Parameters ....: $tEventLog   - tagEVENTLOGRECORD structure
    434 ; Return values .: Success      - User name
    435 ; Author ........: Paul Campbell (PaulIA)
    436 ; Modified.......: Gary Frost (gafrost)
    437 ; Remarks .......: This function is used internally
    438 ; Related .......:
    439 ; Link ..........:
    440 ; Example .......:
    441 ; ===============================================================================================================================
    442 Func __EventLog_DecodeUserName($tEventLog)
    443         Local $pEventLog = DllStructGetPtr($tEventLog)
    444         If DllStructGetData($tEventLog, "UserSidLength") = 0 Then Return ""
    445         Local $pAcctSID = $pEventLog + DllStructGetData($tEventLog, "UserSidOffset")
    446         Local $aAcctInfo = _Security__LookupAccountSid($pAcctSID)
    447         If IsArray($aAcctInfo) Then Return $aAcctInfo[1]
    448         Return ''
    449 EndFunc   ;==>__EventLog_DecodeUserName
    450 
    451 ; #FUNCTION# ====================================================================================================================
    452 ; Author ........: Paul Campbell (PaulIA)
    453 ; Modified.......: Gary Frost (gafrost)
    454 ; ===============================================================================================================================
    455 Func _EventLog__DeregisterSource($hEventLog)
    456         Local $aResult = DllCall("advapi32.dll", "bool", "DeregisterEventSource", "handle", $hEventLog)
    457         If @error Then Return SetError(@error, @extended, False)
    458         Return $aResult[0] <> 0
    459 EndFunc   ;==>_EventLog__DeregisterSource
    460 
    461 ; #FUNCTION# ====================================================================================================================
    462 ; Author ........: Paul Campbell (PaulIA)
    463 ; Modified.......: Gary Frost (gafrost)
    464 ; ===============================================================================================================================
    465 Func _EventLog__Full($hEventLog)
    466         Local $aResult = DllCall("advapi32.dll", "bool", "GetEventLogInformation", "handle", $hEventLog, "dword", 0, "dword*", 0, "dword", 4, "dword*", 0)
    467         If @error Then Return SetError(@error, @extended, False)
    468         Return $aResult[3] <> 0
    469 EndFunc   ;==>_EventLog__Full
    470 
    471 ; #FUNCTION# ====================================================================================================================
    472 ; Author ........: Paul Campbell (PaulIA)
    473 ; Modified.......: Gary Frost (gafrost)
    474 ; ===============================================================================================================================
    475 Func _EventLog__Notify($hEventLog, $hEvent)
    476         Local $aResult = DllCall("advapi32.dll", "bool", "NotifyChangeEventLog", "handle", $hEventLog, "handle", $hEvent)
    477         If @error Then Return SetError(@error, @extended, False)
    478         Return $aResult[0] <> 0
    479 EndFunc   ;==>_EventLog__Notify
    480 
    481 ; #FUNCTION# ====================================================================================================================
    482 ; Author ........: Paul Campbell (PaulIA)
    483 ; Modified.......: Gary Frost (gafrost)
    484 ; ===============================================================================================================================
    485 Func _EventLog__Oldest($hEventLog)
    486         Local $aResult = DllCall("advapi32.dll", "bool", "GetOldestEventLogRecord", "handle", $hEventLog, "dword*", 0)
    487         If @error Then Return SetError(@error, @extended, 0)
    488         Return $aResult[2]
    489 EndFunc   ;==>_EventLog__Oldest
    490 
    491 ; #FUNCTION# ====================================================================================================================
    492 ; Author ........: Paul Campbell (PaulIA)
    493 ; Modified.......: Gary Frost (gafrost)
    494 ; ===============================================================================================================================
    495 Func _EventLog__Open($sServerName, $sSourceName)
    496         $__g_sSourceName_Event = $sSourceName
    497         Local $aResult = DllCall("advapi32.dll", "handle", "OpenEventLogW", "wstr", $sServerName, "wstr", $sSourceName)
    498         If @error Then Return SetError(@error, @extended, 0)
    499         Return $aResult[0]
    500 EndFunc   ;==>_EventLog__Open
    501 
    502 ; #FUNCTION# ====================================================================================================================
    503 ; Author ........: Paul Campbell (PaulIA)
    504 ; Modified.......: Gary Frost (gafrost)
    505 ; ===============================================================================================================================
    506 Func _EventLog__OpenBackup($sServerName, $sFileName)
    507         Local $aResult = DllCall("advapi32.dll", "handle", "OpenBackupEventLogW", "wstr", $sServerName, "wstr", $sFileName)
    508         If @error Then Return SetError(@error, @extended, 0)
    509         Return $aResult[0]
    510 EndFunc   ;==>_EventLog__OpenBackup
    511 
    512 ; #FUNCTION# ====================================================================================================================
    513 ; Author ........: Paul Campbell (PaulIA)
    514 ; Modified.......: Gary Frost (gafrost)
    515 ; ===============================================================================================================================
    516 Func _EventLog__Read($hEventLog, $bRead = True, $bForward = True, $iOffset = 0)
    517         Local $iReadFlags, $aEvent[15]
    518         $aEvent[0] = False; in cas of error
    519 
    520         If $bRead Then
    521                 $iReadFlags = $EVENTLOG_SEQUENTIAL_READ
    522         Else
    523                 $iReadFlags = $EVENTLOG_SEEK_READ
    524         EndIf
    525         If $bForward Then
    526                 $iReadFlags = BitOR($iReadFlags, $EVENTLOG_FORWARDS_READ)
    527         Else
    528                 $iReadFlags = BitOR($iReadFlags, $EVENTLOG_BACKWARDS_READ)
    529         EndIf
    530 
    531         ; First call gets the size for the buffer.  A fake buffer is passed because
    532         ; the function demands the buffer be non-NULL even when requesting the size.
    533         Local $tBuffer = DllStructCreate("wchar[1]")
    534         Local $aResult = DllCall("advapi32.dll", "bool", "ReadEventLogW", "handle", $hEventLog, "dword", $iReadFlags, "dword", $iOffset, _
    535                         "struct*", $tBuffer, "dword", 0, "dword*", 0, "dword*", 0)
    536         If @error Then Return SetError(@error, @extended, $aEvent)
    537 
    538         ; Allocate the buffer and repeat the call obtaining the information.
    539         Local $iBytesMin = $aResult[7]
    540         $tBuffer = DllStructCreate("wchar[" & $iBytesMin + 1 & "]")
    541         $aResult = DllCall("advapi32.dll", "bool", "ReadEventLogW", "handle", $hEventLog, "dword", $iReadFlags, "dword", $iOffset, _
    542                         "struct*", $tBuffer, "dword", $iBytesMin, "dword*", 0, "dword*", 0)
    543         If @error Or Not $aResult[0] Then Return SetError(@error, @extended, $aEvent)
    544 
    545         Local $tEventLog = DllStructCreate($tagEVENTLOGRECORD, DllStructGetPtr($tBuffer))
    546         $aEvent[0] = True
    547         $aEvent[1] = DllStructGetData($tEventLog, "RecordNumber")
    548         $aEvent[2] = __EventLog_DecodeDate(DllStructGetData($tEventLog, "TimeGenerated"))
    549         $aEvent[3] = __EventLog_DecodeTime(DllStructGetData($tEventLog, "TimeGenerated"))
    550         $aEvent[4] = __EventLog_DecodeDate(DllStructGetData($tEventLog, "TimeWritten"))
    551         $aEvent[5] = __EventLog_DecodeTime(DllStructGetData($tEventLog, "TimeWritten"))
    552         $aEvent[6] = __EventLog_DecodeEventID($tEventLog)
    553         $aEvent[7] = DllStructGetData($tEventLog, "EventType")
    554         $aEvent[8] = __EventLog_DecodeTypeStr(DllStructGetData($tEventLog, "EventType"))
    555         $aEvent[9] = __EventLog_DecodeCategory($tEventLog)
    556         $aEvent[10] = __EventLog_DecodeSource($tEventLog)
    557         $aEvent[11] = __EventLog_DecodeComputer($tEventLog)
    558         $aEvent[12] = __EventLog_DecodeUserName($tEventLog)
    559         $aEvent[13] = __EventLog_DecodeDesc($tEventLog)
    560         $aEvent[14] = __EventLog_DecodeData($tEventLog)
    561         Return $aEvent
    562 EndFunc   ;==>_EventLog__Read
    563 
    564 ; #FUNCTION# ====================================================================================================================
    565 ; Author ........: Paul Campbell (PaulIA)
    566 ; Modified.......: Gary Frost (gafrost)
    567 ; ===============================================================================================================================
    568 Func _EventLog__RegisterSource($sServerName, $sSourceName)
    569         $__g_sSourceName_Event = $sSourceName
    570         Local $aResult = DllCall("advapi32.dll", "handle", "RegisterEventSourceW", "wstr", $sServerName, "wstr", $sSourceName)
    571         If @error Then Return SetError(@error, @extended, 0)
    572         Return $aResult[0]
    573 EndFunc   ;==>_EventLog__RegisterSource
    574 
    575 ; #FUNCTION# ====================================================================================================================
    576 ; Author ........: Paul Campbell (PaulIA)
    577 ; Modified.......: Gary Frost (gafrost)
    578 ; ===============================================================================================================================
    579 Func _EventLog__Report($hEventLog, $iType, $iCategory, $iEventID, $sUserName, $sDesc, $aData)
    580         Local $tSID = 0
    581 
    582         If $sUserName <> "" Then
    583                 $tSID = _Security__GetAccountSid($sUserName)
    584         EndIf
    585 
    586         Local $iData = $aData[0]
    587         Local $tData = DllStructCreate("byte[" & $iData & "]")
    588         Local $iDesc = StringLen($sDesc) + 1
    589         Local $tDesc = DllStructCreate("wchar[" & $iDesc & "]")
    590         Local $tPtr = DllStructCreate("ptr")
    591         DllStructSetData($tPtr, 1, DllStructGetPtr($tDesc))
    592         DllStructSetData($tDesc, 1, $sDesc)
    593         For $iI = 1 To $iData
    594                 DllStructSetData($tData, 1, $aData[$iI], $iI)
    595         Next
    596         Local $aResult = DllCall("advapi32.dll", "bool", "ReportEventW", "handle", $hEventLog, "word", $iType, "word", $iCategory, _
    597                         "dword", $iEventID, "struct*", $tSID, "word", 1, "dword", $iData, "struct*", $tPtr, "struct*", $tData)
    598         If @error Then Return SetError(@error, @extended, False)
    599         Return $aResult[0] <> 0
    600 EndFunc   ;==>_EventLog__Report
    601 
    602 
    603 ;++++++++++++++++++++++++END OF INCLUDE
    604 
    605 
    606 
    607 
    608 
    609 
    610 
    611 
    612 
    613 
    614 
    615 
    616 
    617 
    618 
    619 
    620 
    621 Example()
    622 
    623 Func Example()
    624   Local Const $GUI_EVENT_CLOSE = -3
    625    Local Const $WS_VSCROLL = 0x00200000
    626     Local $hEventLog, $aEvent
    627 
    628     ; Create GUI
    629     GUICreate("EventLog", 400, 300)
    630 Local $idMemo = GUICtrlCreateEdit("", 2, 2, 396, 300,$WS_VSCROLL)
    631     GUICtrlSetFont($idMemo, 9, 400, 0, "Courier New")
    632     GUISetState(@SW_SHOW)
    633         Local $i=0
    634 
    635     ; Read most current event record
    636     $hEventLog = _EventLog__Open("", "Security")
    637     Do
    638 
    639     $aEvent = _EventLog__Read($hEventLog, True, False)
    640      $i+=1
    641    ; $hEventLog = _EventLog__Open("", "System")
    642     ; $aEvent = _EventLog__Read($hEventLog)
    643     ; $aEvent = _EventLog__Read($hEventLog, True, False)
    644    ; MemoWrite($idMemo,"Result ............: " & $aEvent[0])
    645    ; MemoWrite($idMemo,"Record number .....: " & $aEvent[1])
    646    ; MemoWrite($idMemo,"Submitted .........: " & $aEvent[2] & " " & $aEvent[3])
    647    ; MemoWrite($idMemo,"Generated .........: " & $aEvent[4] & " " & $aEvent[5])
    648    ; MemoWrite($idMemo,"Event ID ..........: " & $aEvent[6])
    649    ; MemoWrite($idMemo,"Type ..............: " & $aEvent[8])
    650    ; MemoWrite($idMemo,"Category ..........: " & $aEvent[9])
    651    ; MemoWrite($idMemo,"Source ............: " & $aEvent[10])
    652    ; MemoWrite($idMemo,"Computer ..........: " & $aEvent[11])
    653    ; MemoWrite($idMemo,"Username ..........: " & $aEvent[12])
    654    ; MemoWrite($idMemo,"Description .......: " & $aEvent[13])
    655    Until $i=_EventLog__Count ( $hEventLog )-1
    656    _EventLog__Close($hEventLog)
    657 
    658    Do
    659          ; Loop until the user exits.
    660    Until GUIGetMsg() = $GUI_EVENT_CLOSE or $i=10
    661 
    662 
    663 
    664 
    665 EndFunc   ;==>Example
    666 
    667 ; Write a line to the memo control
    668 Func MemoWrite($idMemo,$sMessage)
    669     GUICtrlSetData($idMemo, $sMessage & @CRLF, 1)
    670 EndFunc   ;==>MemoWrite
     1<snip>