EventLog.au3 30 KB

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