-
Notifications
You must be signed in to change notification settings - Fork 1
/
syeventlogreader.pas
334 lines (296 loc) · 8.41 KB
/
syeventlogreader.pas
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
unit syeventlogreader;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, Windows, registry, dateutils;
const
EVENT_LOG_KEY = 'SYSTEM\CurrentControlSet\Services\EventLog\%s\%s';
DATE_01_01_1970 = 25569;
type
TsyEventLogRecord = record
DateTime: TDateTime;
EventType: cardinal;
Category: cardinal;
Source: string;
Computer: string;
ID: DWORD;
StringCount: DWORD;
SID: PSID;
MessageText: string;
UserName: string;
end;
PsyEventLogRecord = ^TsyEventLogRecord;
TsyOnEventLogRecord = procedure(Sender: TObject; ALogRecord: TsyEventLogRecord) of object;
{ TsyEventLogReader }
TsyEventLogReader = class
private
FEventCount: cardinal;
FLastEventRecord: cardinal;
FEventLogHandle: THandle;
FComputerName: string;
FLogName: string;
FOnEventLogRecord: TsyOnEventLogRecord;
FOnBeginUpdate: TNotifyEvent;
FOnEndUpdate: TNotifyEvent;
procedure SetComputerName(AValue: string);
procedure SetLogName(AValue: string);
procedure OpenLog;
procedure CloseLog;
procedure ReadLog(ARecNumber: integer);
function GetMessageDllPath(ASource: string): string;
function GetFormatMessage(AID: DWORD; var AArgs: PPWideChar; const ADllName: string): string;
public
constructor Create;
destructor Destroy; override;
property ComputerName: string read FComputerName write SetComputerName;
property LogName: string read FLogName write SetLogName;
procedure Start;
procedure Proceed;
procedure Stop;
property EventCount: cardinal read FEventCount write FEventCount;
property OnEventLogRecord: TsyOnEventLogRecord read FOnEventLogRecord write FOnEventLogRecord;
property OnBeginUpdate: TNotifyEvent read FOnBeginUpdate write FOnBeginUpdate;
property OnEndUpdate: TNotifyEvent read FOnEndUpdate write FOnEndUpdate;
end;
implementation
{ TsyEventLogReader }
procedure TsyEventLogReader.SetComputerName(AValue: string);
begin
if FComputerName = AValue then
Exit;
FComputerName := AValue;
end;
procedure TsyEventLogReader.SetLogName(AValue: string);
begin
if FLogName = AValue then
Exit;
FLogName := AValue;
end;
procedure TsyEventLogReader.OpenLog;
var
ALogName: WideString;
AComputerName: WideString;
err: DWORD;
begin
FEventCount := 0;
ALogName := FLogName;
AComputerName := FComputerName;
if FEventLogHandle = 0 then
FEventLogHandle := OpenEventLogW(PWideChar(AComputerName), PWideChar(ALogName));
err := GetLastError;
if err <> 0 then
begin
RaiseLastWin32Error;
end;
end;
procedure TsyEventLogReader.CloseLog;
begin
if FEventLogHandle <> 0 then
begin
CloseEventLog(FEventLogHandle);
FEventLogHandle := 0;
end;
end;
procedure TsyEventLogReader.ReadLog(ARecNumber: integer);
const
MAX_BUFFER = $7FFFF;
var
Flags: DWORD;
Res: WINBOOL;
pnBytesRead: DWORD;
pnMinNumberOfBytesNeeded: DWORD;
pnBytesNeeded: DWORD;
Buffer: Pointer;
LastErr: DWORD;
Count: integer;
i, j: integer;
pRecord: PEVENTLOGRECORD;
pStr: PWideChar;
OutStr: string;
Source: string;
Args, PArgs: ^PWideChar;
MessageDllPath: string;
LogRecord: TsyEventLogRecord;
tmp: string;
_ReadRec: integer;
begin
buffer := nil;
getMem(Buffer, MAX_BUFFER);
// First Read for get required buffer size
res := True;
if ARecNumber = 0 then
Flags := EVENTLOG_SEQUENTIAL_READ or EVENTLOG_FORWARDS_READ
else
Flags := EVENTLOG_SEEK_READ or EVENTLOG_FORWARDS_READ;
_ReadRec := ARecNumber;
while res do
begin
if Assigned(FOnBeginUpdate) then
OnBeginUpdate(Self);
Res := ReadEventLogW(FEventLogHandle, flags, _ReadRec, Buffer, MAX_BUFFER, pnBytesRead, pnMinNumberOfBytesNeeded);
if not res then
begin
LastErr := GetLastError;
case LastErr of
ERROR_INSUFFICIENT_BUFFER:
begin
pnBytesNeeded := pnMinNumberOfBytesNeeded;
ReallocMem(Buffer, pnBytesNeeded);
Res := ReadEventLogW(FEventLogHandle, flags, 0, Buffer, pnBytesNeeded, pnBytesRead, pnMinNumberOfBytesNeeded);
end;
ERROR_HANDLE_EOF:
begin
end;
end;
end
else
begin
// Data processing
pRecord := Buffer;
Args := nil;
while pointer(pRecord) < Buffer + pnBytesRead do
begin
OutStr := '';
pStr := PWideChar(pointer(pRecord) + pRecord^.StringOffset);
GetMem(Args, pRecord^.NumStrings * SizeOf(PWideChar));
PArgs := Args;
for i := 1 to pRecord^.NumStrings do
begin
PArgs^ := pStr;
OutStr := OutStr + pStr;
if i < pRecord^.NumStrings then
OutStr := OutStr + ' ';// #10#13;
j := strlen(pStr);
pStr := pStr + j + 1;
Inc(PArgs);
end;
Source := PWideChar(pointer(pRecord) + SizeOf(TEventLogRecord));
MessageDllPath := GetMessageDllPath(Source);
tmp := '';
if MessageDllPath <> '' then
begin
repeat
I := Pos(';', MessageDllPath);
if I <> 0 then
begin
tmp := tmp + GetFormatMessage(pRecord^.EventID, Args, Copy(MessageDllPath, 1, I - 1));
MessageDllPath := Copy(MessageDllPath, I + 1, MaxInt);
end
else
tmp := tmp + GetFormatMessage(pRecord^.EventID, Args, MessageDllPath);
until I = 0;
end;
if tmp <> '' then
OutStr := tmp;
Freemem(Args);
// Fire EVENT OnEventLogRecord;
if Assigned(OnEventLogRecord) then
begin
LogRecord.MessageText := OutStr;
LogRecord.Source := Source;
LogRecord.DateTime := UniversalTimeToLocal(IncSecond(DATE_01_01_1970, pRecord^.TimeWritten));
LogRecord.ID := pRecord^.EventID;
LogRecord.Category := pRecord^.EventCategory;
LogRecord.EventType := pRecord^.EventType;
FOnEventLogRecord(Self, LogRecord);
end;
FLastEventRecord := pRecord^.RecordNumber;
_ReadRec := FLastEventRecord + 1;
pRecord := PEVENTLOGRECORD(pointer(pRecord) + pRecord^.Length);
Inc(FEventCount);
end;
end;
if Assigned(FOnEndUpdate) then
OnEndUpdate(Self);
end;
Freemem(Buffer);
end;
function TsyEventLogReader.GetMessageDllPath(ASource: string): string;
var
reg: TRegistry;
res: boolean;
tmp: string;
begin
Result := '';
reg := TRegistry.Create(KEY_READ);
try
reg.RootKey := HKEY_LOCAL_MACHINE;
tmp := Format(EVENT_LOG_KEY, [FLogName, ASource]);
res := reg.OpenKey(Format(EVENT_LOG_KEY, [FLogName, ASource]), False);
Result := reg.ReadString('EventMessageFile');
finally
reg.Free;
end;
end;
function TsyEventLogReader.GetFormatMessage(AID: DWORD; var AArgs: PPWideChar; const ADllName: string): string;
var
DllModule: THandle;
Buffer: array [0..4095] of widechar;
Buffer2: PWideChar;
FullDLLName: array [0..MAX_PATH] of char;
wDllName: array [0..MAX_PATH] of char;
OutString: WideString;
err: integer;
FormatFlag: DWORD;
begin
OutString := '';
if ADllName <> '' then
begin
ExpandEnvironmentStrings(PChar(ADllName), FullDLLName, MAX_PATH);
DllModule := LoadLibraryEx(FullDLLName, 0, LOAD_LIBRARY_AS_DATAFILE);
if DllModule <> 0 then
begin
try
try
FormatFlag := FORMAT_MESSAGE_FROM_HMODULE or FORMAT_MESSAGE_FROM_SYSTEM or FORMAT_MESSAGE_ARGUMENT_ARRAY;
// FormatFlag := FORMAT_MESSAGE_FROM_HMODULE or FORMAT_MESSAGE_ARGUMENT_ARRAY;
if FormatMessageW(FormatFlag, Pointer(DllModule), AID, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
@Buffer[0], 4096, @AArgs^) > 0 then
begin
OutString := PWideChar(Buffer);
end
else
err := GetLastError;
except
on e: Exception do
end;
finally
FreeLibrary(DllModule);
end;
end;
end;
Result := OutString;
end;
constructor TsyEventLogReader.Create;
begin
ComputerName := '';
LogName := 'System';
FEventLogHandle := 0;
end;
destructor TsyEventLogReader.Destroy;
begin
Stop;
inherited Destroy;
end;
procedure TsyEventLogReader.Start;
begin
OpenLog;
ReadLog(0);
end;
procedure TsyEventLogReader.Proceed;
var
Offset: DWORD;
Recno: integer;
begin
GetNumberOfEventLogRecords(FEventLogHandle, @Offset);
if FEventCount >= Offset then
exit;
GetOldestEventLogRecord(FEventLogHandle, @Offset);
RecNo := FEventCount + Offset;
ReadLog(Recno);
end;
procedure TsyEventLogReader.Stop;
begin
CloseLog;
end;
end.