gpt4 book ai didi

delphi - 如何使用 Delphi 读取 Windows 事件日志的内容

转载 作者:行者123 更新时间:2023-12-03 18:03:16 25 4
gpt4 key购买 nike

是否有允许您读取 Windows 事件日志的类或函数。这是您打开 eventvwr.msc 时看到的日志。最好选择一个特定的日志(在我的例子中是 Windows 日志 下的 Applications 日志),并根据日期和来源放置过滤器。

最佳答案

您可以使用 Win32_NTLogEvent用于读取 Windows 日志内容的 WMI 类。

试试这个例子

{$APPTYPE CONSOLE}

{$R *.res}

uses
SysUtils,
ActiveX,
ComObj,
Variants;


procedure GetLogEvents;
const
wbemFlagForwardOnly = $00000020;
var
FSWbemLocator : OLEVariant;
FWMIService : OLEVariant;
FWbemObjectSet: OLEVariant;
FWbemObject : OLEVariant;
oEnum : IEnumvariant;
iValue : LongWord;
begin;
FSWbemLocator := CreateOleObject('WbemScripting.SWbemLocator');
FWMIService := FSWbemLocator.ConnectServer('localhost', 'root\CIMV2', '', '');
FWbemObjectSet:= FWMIService.ExecQuery('SELECT Category,ComputerName,EventCode,Message,RecordNumber FROM Win32_NTLogEvent Where Logfile="System"','WQL',wbemFlagForwardOnly);
oEnum := IUnknown(FWbemObjectSet._NewEnum) as IEnumVariant;
while oEnum.Next(1, FWbemObject, iValue) = 0 do
begin
Writeln(Format('Category %s',[String(FWbemObject.Category)]));
Writeln(Format('Computer Name %s',[String(FWbemObject.ComputerName)]));
Writeln(Format('EventCode %d',[Integer(FWbemObject.EventCode)]));
Writeln(Format('Message %s',[String(FWbemObject.Message)]));
Writeln(Format('RecordNumber %d',[Integer(FWbemObject.RecordNumber)]));
FWbemObject:=Unassigned;
end;
end;

begin
try
CoInitialize(nil);
try
GetLogEvents;
finally
CoUninitialize;
end;
except
on E:EOleException do
Writeln(Format('EOleException %s %x', [E.Message,E.ErrorCode]));
on E:Exception do
Writeln(E.Classname, ':', E.Message);
end;
Writeln('Press Enter to exit');
Readln;
end.

有关更多示例,请尝试此博客条目 WMI Tasks using Delphi – Event Logs

关于delphi - 如何使用 Delphi 读取 Windows 事件日志的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13139865/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com