Announcement

Collapse
No announcement yet.

Eventlog auslesen und in lesbarer Form darstellen??

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Eventlog auslesen und in lesbarer Form darstellen??

    Hallo!
    Ich bin, was delphi angeht ein absoluter Neuling. Ich müsste etwas programmieren, was die eventlogs ausliest (System, Security & Application) und in lesbarer Form darstellt. Codeschnipsel wären klasse, aber einfach ein paar Hinweise oder Links würden mir sicherlich auch helfen, da ich noch überhaupt keine Ahnung habe, wie ich das machen soll.
    Wäre super, wenn mir da einer weiter helfen könnte. Achja, ich muß das ganze in Delphi 5 machen.
    Danke im Voraus!

    Gruß

    Matthias

  • #2
    Hallo,

    am einfachsten geht das über <b>WMI</b> (Windows Management Instrumentation). WMI stellt mit <b>WQL</b> eine eigene Abfragesprache zur Verfügung, die an SQL angelehnt ist. Um zum Beispiel die Eventslogs auszulesen, könnte das so aussehen:
    <pre>
    SELECT Type,SourceName,TimeGenerated,Message
    FROM Win32_NTLogEvent
    WHERE LogFile="Application" AND EventCode="100"
    </pre>
    Damit die Programmierhilfe von Delphi auch für die WMI-Interfaces zur Verfügung steht, wird zuerst die Typbibliothek <b>Microsoft WMI Scripting V 1.x Library</b> importiert. Delphi legt dabei auch die VCL-Wrapperkomponenten für WMI an.

    Das folgende Beispielprojekt liest die Eventlogs aus (in Delphi 5 muss nur der Verweis auf die Units <i>Variants</i> und <i>StrUtils</i> entfernt werden):
    <pre>
    <b>unit</b> BspWin32_NTLogEventFrm;
    <br>
    <b>interface</b>
    <br>
    <b>uses</b>
    Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
    Dialogs, StdCtrls, ComCtrls;
    <br>
    <b>type</b>
    TForm1 = <b>class</b>(TForm)
    Button1: TButton;
    ListView1: TListView;
    StatusBar1: TStatusBar;
    Memo1: TMemo;
    <b>procedure</b> Button1Click(Sender: TObject);
    <b>private</b>
    <font color="#003399"><i>{ Private-Deklarationen }</i></font>
    <b>public</b>
    <font color="#003399"><i>{ Public-Deklarationen }</i></font>
    <b>end</b>;
    <br>
    <b>var</b>
    Form1: TForm1;
    <br>
    <b>implementation</b>
    <br>
    <font color="#003399"><i>{$R *.dfm}</i></font>
    <br>
    <b>uses</b> ActiveX, WbemScripting_TLB, StrUtils;
    <br>
    <b>procedure</b> TForm1.Button1Click(Sender: TObject);
    <b>var</b>
    aLoc : ISWbemLocator;
    aSrv : ISWbemServices;
    aObjSet : ISWbemObjectSet;
    pEnum : IEnumVARIANT;
    vOut : OleVariant;
    dwRetrieved : LongWord;
    hRes : HResult;
    sWQL : <b>String</b>;
    aLI : TListItem;
    <b>begin</b>
    ListView1.Items.Clear;
    aLoc := CoSWbemLocator.Create;
    aSrv := aLoc.ConnectServer(<font color="#9933CC">''</font>,<font color="#9933CC">''</font>, <font color="#9933CC">''</font>,<font color="#9933CC">''</font>,<font color="#9933CC">''</font>,<font color="#9933CC">''</font>,0, <b>nil</b>);
    aSrv.Security_.ImpersonationLevel := wbemImpersonationLevelImpersonate;
    aObjSet := aSrv.ExecQuery(Memo1.Text, <font color="#9933CC">'WQL'</font>, 0, <b>nil</b>);
    pEnum := aObjSet.Get__NewEnum <b>as</b> IEnumVARIANT;
    <b>while</b> (TRUE) <b>do</b>
    <b>begin</b>
    hRes := pEnum.Next(1, vOut, dwRetrieved);
    <b>if</b> hRes &lt;&gt; S_OK <b>then</b>
    Break;
    aLI := ListView1.Items.Add;
    aLI.Caption := VarToStr(vOut.<b>Type</b>);
    aLI.SubItems.Add(VarToStr(vOut.SourceName));
    aLI.SubItems.Add(LeftStr(VarToStr(vOut.TimeGenerat ed), 8));
    aLI.SubItems.Add(VarToStr(vOut.Message));
    <b>end</b>;
    <b>end</b>;
    <br>
    <b>end</b>.
    </pre>
    Konfiguration im Objektinspektor:
    <pre>
    <b>object</b> Form1: TForm1
    Left = 308
    Top = 115
    Width = 836
    Height = 334
    Caption = <font color="#9933CC">'Win32_NTLogEvent'</font>
    Color = clBtnFace
    Font.Charset = ANSI_CHARSET
    Font.Color = clWindowText
    Font.Height = -16
    Font.Name = <font color="#9933CC">'Tahoma'</font>
    Font.Style = []
    OldCreateOrder = False
    PixelsPerInch = 96
    TextHeight = 19
    <b>object</b> Button1: TButton
    Left = 8
    Top = 256
    Width = 75
    Height = 25
    Caption = <font color="#9933CC">'Start'</font>
    TabOrder = 0
    OnClick = Button1Click
    <b>end</b>
    <b>object</b> ListView1: TListView
    Left = 0
    Top = 73
    Width = 826
    Height = 177
    Align = alTop
    Columns = &lt;
    item
    Caption = <font color="#9933CC">'Typ'</font>
    Width = 150
    <b>end</b>
    item
    Caption = <font color="#9933CC">'Quelle'</font>
    Width = 100
    <b>end</b>
    item
    Caption = <font color="#9933CC">'Datum'</font>
    Width = 100
    <b>end</b>
    item
    Caption = <font color="#9933CC">'Beschreibung'</font>
    Width = 450
    <b>end</b>&gt;
    Font.Charset = ANSI_CHARSET
    Font.Color = clWindowText
    Font.Height = -15
    Font.Name = <font color="#9933CC">'Tahoma'</font>
    Font.Style = []
    ParentFont = False
    TabOrder = 1
    ViewStyle = vsReport
    <b>end</b>
    <b>object</b> StatusBar1: TStatusBar
    Left = 0
    Top = 286
    Width = 826
    Height = 19
    Panels = &lt;&gt;
    SimplePanel = False
    <b>end</b>
    <b>object</b> Memo1: TMemo
    Left = 0
    Top = 0
    Width = 826
    Height = 73
    Align = alTop
    Lines.Strings = (
    <font color="#9933CC">'SELECT Type,SourceName,TimeGenerated,Message '</font>
    <font color="#9933CC">'FROM Win32_NTLogEvent '</font>
    <font color="#9933CC">'WHERE LogFile=&quot;Application&quot; AND EventCode=&quot;100&quot;'</font>)
    TabOrder = 3
    <b>end</b>
    <b>end</b>
    </pre>
    Siehe auch <i>http://www.entwickler.com/itr/features/psecom,id,110,nodeid,77.html </i&gt

    Comment


    • #3
      Schonmal herzlichen Dank!
      Kriege aber noch die folgende Fehlermeldung, wenn ich deinen Code ausführen möchte:

      [Fataler Fehler] Test.pas(29): Datei nicht gefunden: 'WbemScripting_TLB.dcu'

      Wo kriege ich die Datei her?
      Danke!

      Gruß
      Matthia

      Comment


      • #4
        Den Menüpunkt "Projekt/Typbibliothek" auswählen. In der Liste den Eintrag "Microsoft WMI Scripting V 1.x Library" auswählen und Button "Unit anlegen" klicken

        Comment


        • #5
          Super, danke!
          Habe das Script jetzt mal ein wenig angepasst, allerdings kann ich dir Security-EventLogs nicht auslesen. Da wird immer nichts angezeigt. Woran liegt das?

          Gruß

          Matthia

          Comment

          Working...
          X