1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126:
| function GetEventIDText(EventID: DWORD; msgfile: string; pelr: PEVENTLOGRECORD): string; type PVA_LIST = ^VA_LIST; VA_LIST = array[0..0] of Pointer; var hLib: THandle; ret, flags, nSize: DWORD; ppc, pc, lpc: PChar; i: Integer; begin result := ''; flags := FORMAT_MESSAGE_FROM_HMODULE or FORMAT_MESSAGE_ALLOCATE_BUFFER or FORMAT_MESSAGE_ARGUMENT_ARRAY or FORMAT_MESSAGE_IGNORE_INSERTS; nSize := ExpandEnvironmentStrings(@msgfile[1], nil, 0) + 2; GetMem(pc, nSize); if Assigned(pc) then try ZeroMemory(pc, nSize); ExpandEnvironmentStrings(@msgfile[1], pc, nSize); for i := lstrlen(pc) - 1 downto 0 do if pc[i] = ';' then pc[i] := #0; lpc := pc; while lpc[0] <> #0 do begin hLib := LoadLibraryEx(lpc, 0, DONT_RESOLVE_DLL_REFERENCES); inc(lpc, lstrlen(lpc) + 1); if hLib <> 0 then try ret := FormatMessage(flags, Pointer(hLib), EventID, LANG_USER_DEFAULT, @ppc, 0, nil); if ((ret = 0) and (GetLastError = ERROR_MR_MID_NOT_FOUND) and (lpc[0] <> #0)) then Continue; finally FreeLibrary(hLib); end; end; if ret <> 0 then SetString(result, ppc, lstrlen(ppc)); if Assigned(ppc) then LocalFree(THandle(ppc)); finally Freemem(pc); end; end;
function GetEventRecordString(pelr: PEVENTLOGRECORD; el: PChar): MYEVENTLOGRECORD;
const elkey = 'SYSTEM\CurrentControlSet\Services\Eventlog\'; var ft: FILETIME; pc: PChar; uName, dName: array[0..MAX_PATH - 1] of Char; err, uSize, dSize, use: DWORD; key: HKEY; temps: string; begin ZeroMemory(@result, sizeof(result)); result.RecordNumber := pelr^.RecordNumber; result.EventID := pelr^.EventID; ft := UnixTimeToFileTime(pelr^.TimeGenerated); FileTimeToLocalFileTime(ft, result.LocalTimeGenerated); ft := UnixTimeToFileTime(pelr^.TimeWritten); FileTimeToLocalFileTime(ft, result.LocalTimeWritten); result.EventType := pelr^.EventType; result.EventCategory := pelr^.EventCategory; if pelr^.DataLength <> 0 then begin SetLength(result.Data, pelr^.DataLength); CopyMemory(@result.Data[1], PAdd(pelr, pelr^.DataOffset), pelr^.DataLength); end; pc := PAdd(pelr, sizeof(pelr^)); SetString(result.SourceName, pc, lstrlen(pc)); inc(pc, lstrlen(pc) + 1); SetString(result.ComputerName, pc, lstrlen(pc)); uSize := sizeof(uName); dSize := sizeof(dName); if pelr^.UserSidLength <> 0 then if LookUpAccountSid(pc, PAdd(pelr, pelr^.UserSidOffset), uName, uSize, dName, dSize, use) then result.UserName := Format('\\%s\%s', [@dName, @uName]); temps := elkey + string(el) + '\' + result.SourceName; err := RegOpenKey(HKEY_LOCAL_MACHINE, @temps[1], key); if err = ERROR_SUCCESS then try dSize := 0; RegQueryValueEx(key, 'EventMessageFile', nil, nil, nil, @dSize); begin GetMem(pc, dSize); if Assigned(pc) then try ZeroMemory(pc, dSize); if RegQueryValueEx(key, 'EventMessageFile', nil, nil, PByte(pc), @dSize) = ERROR_SUCCESS then SetString(result.SourceFile, pc, lstrlen(pc)); finally FreeMem(pc); end; end; finally RegCloseKey(key); end; if result.SourceFile <> '' then result.MessageText := GetEventIDText(result.EventID, result.SourceFile, pelr); end; |