Unit MainForm;

{
  Implementace reakci na udalosti hlavniho formulare aplikace VAD.
}

Interface

Uses
  Windows, Messages, SysUtils, Variants, Classes,
  Graphics, Controls, Forms,
  Dialogs, ComCtrls, ExtCtrls, Menus;

Type
  TForm1 = Class (TForm)
    Panel1: TPanel;
    Panel2: TPanel;
    ListView1: TListView;
    ListView2: TListView;
    MainMenu1: TMainMenu;
    Akce1: TMenuItem;
    Vybratproces1: TMenuItem;
    N1: TMenuItem;
    Konec1: TMenuItem;
    Procedure FormClose(Sender: TObject; var Action: TCloseAction);
    Procedure FormCreate(Sender: TObject);
    Procedure ListView1Data(Sender: TObject; Item: TListItem);
    Procedure Konec1Click(Sender: TObject);
    Procedure Vybratproces1Click(Sender: TObject);
    procedure ListView1SelectItem(Sender: TObject; Item: TListItem;
      Selected: Boolean);
  Private
    FVadList : TList;
    FProcessId : DWORD;
    Procedure ShowProcessVADS;
    Procedure ShowSelectedVADInfo(Item:TListItem);
  end;

Var
  Form1: TForm1;

Implementation

{$R *.DFM}

Uses
  Utils, VadLib, SelectProcessForm;

Procedure TForm1.ShowProcessVADS;
{
  Nacte seznam virtualnich deskriptoru procesu, jehoz PID je ulozeno
  v atributu FProcessId do seznamu TList ulozenem v promenne FVadList.

  Nasledne donuti ovladaci prvek ListView tyto polozky zobrazit.
}
Var
  Ret : Boolean;
  I : Integer;
begin
ListView1.Items.Count := 0;
For I := 0 To FVadList.Count - 1 Do
  MemFree(FVadList[I]);

FVadList.Clear;
Ret := VadLibGetVadListForProcess(FProcessId, FVadList);
If Ret Then
  ListView1.Items.Count := FVadList.Count
Else begin
  Error('Nepodarilo se ziskat seznam VADu ciloveho procesu!');
  For I := 0 To FVadList.Count - 1 Do
    MemFree(FVadList[I]);

  FVadList.Clear;
  end;
end;

Procedure TForm1.Vybratproces1Click(Sender: TObject);
{
  Zajisti zobrazeni formulare pro vyber procesu. PID tatko vybraneho
  procesu zapise do atributu FProcessId a zajisti zobrazeni jeho
  virtualnich deskriptoru.
}
begin
With TSelectProcess.Create(Application) Do
  begin
  ShowModal;
  If Not Canceled Then
    begin
    Form1.Caption := Format('VAD – %s (%d)', [ProcessName, ProcessId]);
    FProcessId := ProcessId;
    ShowProcessVADs;
    end;

  Free;
  end;
end;

Procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
{
  Rutina je volana pri zavirani formulare. Zrusi seznam virtualnch
  deskriptoru a provede uklid knihovny pro praci s VADy.
}
begin
ListView1.Items.Count := 0;
FVadList.Free;
VadLibFinit;
end;

Procedure TForm1.FormCreate(Sender: TObject);
{
  Rutina je volana tesne po vytvoreni formulare. Inicializuje knihovnu
  pro praci s VADy, naplni atribut FProcessId cislem aktualniho procesu,
  jehoz seznam VADu nasledne zobrazi v ovladacim prvku Listview.
}
Var
  Ret : Boolean;
begin
Ret := VadLibInit;
If Ret Then
  begin
  FVadList := TList.Create;
  FProcessId := GetCurrentProcessId;
  Caption := Format('VAD – %s (%d)', [ExtractFileName(ParamStr(0)), FProcessId]);
  ShowProcessVADS;
  end Else Error('Nepodarilo se nacist ovladac vad.sys!');
end;

procedure TForm1.Konec1Click(Sender: TObject);
{
  Reakce na kliknuti na tlacitko "Konec" v hlavnim menu.
}
begin
Close;
end;

Function BooleanToStr(B:Boolean):WideString;
{
  Pomocna funkce prevadejici hodnotu typu Boolean na retezec
}
begin
If B Then
  Result := '1'
Else Result := '0';
end;

Procedure TForm1.ShowSelectedVADInfo(Item:TListItem);
{
  Zobrazi informace o prave oznacenem VADu do bocniho ovladaciho
  prvku ListView.

  POZOR: Rutina nekontroluje, o jaky VAD se jedna. Zobrazuje vzdy
  hodnoty vsech priznaku (i tech neplatnych v danem kontextu, ktere
  maji ale vzdy hodnotu 0).
}
Var
  VR : PVadRecord;
begin
VR := FVadList[Item.Index];
With ListView2 Do
  begin
  Items[0].SubItems[0] := BooleanToStr(VR.NoChange);
  Items[1].SubItems[0] := BooleanToStr(VR.MemCommit);
  Items[2].SubItems[0] := IntToHex(VR.Protection, 8);
  Items[3].SubItems[0] := BooleanToStr(VR.PrivateMemory);
  Items[4].SubItems[0] := IntToHex(VR.FileOffset, 8);
  Items[5].SubItems[0] := BooleanToStr(VR.SecNoChange);
  Items[6].SubItems[0] := BooleanToStr(VR.OneSecured);
  Items[7].SubItems[0] := BooleanToStr(VR.MultipleSecured);
  Items[8].SubItems[0] := BooleanToStr(VR.ReadOnly);
  Items[9].SubItems[0] := BooleanToStr(VR.LongVad);
  Items[10].SubItems[0] := BooleanToStr(VR.Extendable);
  Items[11].SubItems[0] := BooleanToStr(VR.Inherit);
  Items[12].SubItems[0] := BooleanToStr(VR.CopyOnWrite);
  Items[13].SubItems[0] := IntToHex(VR.PreferredMode, 8);
  Items[14].SubItems[0] := BooleanToStr(VR.Teb);
  end;
end;

Procedure TForm1.ListView1Data(Sender: TObject; Item: TListItem);
{
  Zobrazuje zakladni informace o VADu reprezentovanem
  polozkou Item. Tim zajistuje vykresleni spravnych udaju do
  hlavniho ovladaciho prvku ListView.
}
Var
  VR : PVadRecord;
begin
VR := FVadList[Item.Index];
Item.Caption := IntToHex(VR.StartAddress, GetPointerLength * 2);
Item.SubItems.Add(IntToHex(VR.EndAddress, GetPointerLength * 2));
Item.SubItems.Add(Format('0x%s B', [IntToHex(VR.CommitCharge, GetPointerLength * 2)]));
Item.SubItems.Add(Format('%s', [VadLibVadTypeToStr(VR.VadType)]));
Item.SubItems.Add(Format('%s', [VadLibVadStructureToStr(VR.VadStructureType)]));
end;

procedure TForm1.ListView1SelectItem(Sender: TObject; Item: TListItem;
  Selected: Boolean);
begin
If Selected Then
  ShowSelectedVADInfo(Item);
end;

End.

