home *** CD-ROM | disk | FTP | other *** search
/ Chip 2002 March / Chip_2002-03_cd1.bin / zkuste / delphi / kompon / d56 / MSYSINFO.ZIP / Demos / GUI / ProcessProps.pas < prev    next >
Pascal/Delphi Source File  |  2002-01-04  |  13KB  |  381 lines

  1. unit ProcessProps;
  2.  
  3. interface
  4.  
  5. uses
  6.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  7.   MiTeC_PerfLibNT, ComCtrls, StdCtrls, ExtCtrls;
  8.  
  9. type
  10.   TpropProcess = class(TFrame)
  11.     pc: TPageControl;
  12.     tsGeneral: TTabSheet;
  13.     tsPerformance: TTabSheet;
  14.     tsMemory: TTabSheet;
  15.     Panel1: TPanel;
  16.     Image1: TImage;
  17.     Bevel1: TBevel;
  18.     Bevel2: TBevel;
  19.     Bevel3: TBevel;
  20.     lPID: TLabel;
  21.     Label2: TLabel;
  22.     eFile: TEdit;
  23.     bProcProps: TButton;
  24.     lHandles: TLabel;
  25.     lThreads: TLabel;
  26.     lModules: TLabel;
  27.     lPriority: TLabel;
  28.     tsModules: TTabSheet;
  29.     tsThread: TTabSheet;
  30.     Panel2: TPanel;
  31.     lvPerf: TListView;
  32.     Panel3: TPanel;
  33.     lvMem: TListView;
  34.     lPPID: TLabel;
  35.     stPID: TStaticText;
  36.     stPPID: TStaticText;
  37.     stModules: TStaticText;
  38.     stThreads: TStaticText;
  39.     stName: TEdit;
  40.     stHandles: TStaticText;
  41.     stPriority: TStaticText;
  42.     lvHeap: TListView;
  43.     Panel4: TPanel;
  44.     lvMods: TListView;
  45.     Panel5: TPanel;
  46.     lvThds: TListView;
  47.     lHeapID: TLabel;
  48.     stHeapID: TStaticText;
  49.     Splitter1: TSplitter;
  50.     Panel6: TPanel;
  51.     Panel7: TPanel;
  52.     bModProps: TButton;
  53.     ModsPanel: TPanel;
  54.     Panel8: TPanel;
  55.     Panel9: TPanel;
  56.     ThdsPanel: TPanel;
  57.     bKill: TButton;
  58.     procedure cmProcProps(Sender: TObject);
  59.     procedure cmModProps(Sender: TObject);
  60.     procedure cmKill(Sender: TObject);
  61.   private
  62.     FProcess: TObject;
  63.     FPerfLib: TPerfLibNT;
  64.   public
  65.     property Process: TObject read FProcess write FProcess;
  66.     property PerfLib: TPerfLibNT read FPerfLib write FPerfLib;
  67.     procedure Refresh;
  68.   end;
  69.  
  70. implementation
  71.  
  72. uses MiTeC_Routines, MiTeC_EnumsNT, MiTeC_Enums9x;
  73.  
  74. {$R *.DFM}
  75.  
  76. { TpropProcess }
  77.  
  78. const
  79.   SNotAvail = 'Not Available';
  80.   SEmpty = '-';
  81.  
  82.   SWARN = 'WARNING: Terminating a process can cause undesired '#13#10+
  83.                     'results including loss of data and system instability. The '#13#10+
  84.                     'process will not be given the chance to save its state or '#13#10+
  85.                     'data before it is terminated. Are you sure you want to '#13#10+
  86.                     'terminate the process?';
  87.   SCannotTerm = 'Cannot terminate this process.';
  88.  
  89.  
  90. procedure TpropProcess.Refresh;
  91. var
  92.   i: integer;
  93.   p95: P95Process;
  94.   pNT: PNTProcess;
  95. begin
  96.   pc.ActivePage:=tsGeneral;
  97.   Splitter1.Visible:=Is2K or not IsNT;
  98.   lvHeap.Visible:=Is2K or not IsNT;
  99.   lvMem.Visible:=IsNT;
  100.   if not lvMem.Visible then
  101.     lvHeap.Align:=alClient;
  102.   tsPerformance.TabVisible:=IsNT;
  103.   if IsNT then begin
  104.     if Assigned(FPerfLib) then begin
  105.       pNT:=PNTProcess(Process);
  106.       GetNTProcessInfo(pNT^,PerfLib,pNT^.PID,True);
  107.       with pNT^ do begin
  108.         bKill.Enabled:=OpenProcess(PROCESS_TERMINATE,False,PID)<>0;
  109.         stName.Text:=Name;
  110.         stPID.Caption:=Format('%d (0x%0:x)',[PID]);
  111.         eFile.Text:=ImageName;
  112.         bProcProps.Enabled:=FileExists(ImageName);
  113.         eFile.Hint:=eFile.Text;
  114.         stModules.Caption:=Format('%d',[Modules.Count]);
  115.         stThreads.Caption:=Format('%d',[ThreadCount]);
  116.         stHandles.Caption:=Format('%d',[HandleCount]);
  117.         stPriority.Caption:=Format('%d',[PriorityBase]);
  118.         if Is2K then begin
  119.           stPPID.Caption:=Format('%d (0x%0:x)',[ParentPID]);
  120.           stHeapID.Caption:=Format('%d (0x%0:x)',[HeapID]);
  121.         end;
  122.  
  123.         with lvPerf, Items do begin
  124.           BeginUpdate;
  125.           with Add do begin
  126.             Caption:='Elapsed Time';
  127.             SubItems.Add(FormatSeconds(ElapsedTime,True,False,True));
  128.           end;
  129.           with Add do begin
  130.             Caption:='CPU Time';
  131.             SubItems.Add(FormatSeconds(CPUTime,True,False,True));
  132.           end;
  133.           with Add do begin
  134.             Caption:='Kernel Time';
  135.             SubItems.Add(FormatSeconds(KernelTime,True,False,True));
  136.           end;
  137.           with Add do begin
  138.             Caption:='User Time';
  139.             SubItems.Add(FormatSeconds(UserTime,True,False,True));
  140.           end;
  141.           if Is2K then begin
  142.             with Add do begin
  143.               Caption:='';
  144.               SubItems.Add('');
  145.             end;
  146.             with Add do begin
  147.               Caption:='IO Read Operations/sec';
  148.               SubItems.Add(FormatFloat('#,#0',IOReadOps));
  149.             end;
  150.             with Add do begin
  151.               Caption:='IO Write Operations/sec';
  152.               SubItems.Add(FormatFloat('#,#0',IOWriteOps));
  153.             end;
  154.             with Add do begin
  155.               Caption:='IO Data Operations/sec';
  156.               SubItems.Add(FormatFloat('#,#0',IOWriteOps));
  157.             end;
  158.             with Add do begin
  159.               Caption:='IO Other Operations/sec';
  160.               SubItems.Add(FormatFloat('#,#0',IOOtherOps));
  161.             end;
  162.             with Add do begin
  163.               Caption:='IO Read Bytes/sec';
  164.               SubItems.Add(FormatFloat('#,#0',IOReadB));
  165.             end;
  166.             with Add do begin
  167.               Caption:='IO Write Bytes/sec';
  168.               SubItems.Add(FormatFloat('#,#0',IOWriteB));
  169.             end;
  170.             with Add do begin
  171.               Caption:='IO Data Bytes/sec';
  172.               SubItems.Add(FormatFloat('#,#0',IODataB));
  173.             end;
  174.             with Add do begin
  175.               Caption:='IO Other Bytes/sec';
  176.               SubItems.Add(FormatFloat('#,#0',IOOtherB));
  177.             end;
  178.           end;
  179.           EndUpdate;
  180.         end;
  181.  
  182.         with lvMem, Items do begin
  183.           BeginUpdate;
  184.           with Add do begin
  185.             Caption:='Page Fault Count';
  186.             SubItems.Add(FormatFloat('#,#0 B/sec',PageFaultCount));
  187.             SubItems.Add(SEmpty);
  188.           end;
  189.           with Add do begin
  190.             Caption:='Private Space';
  191.             SubItems.Add(FormatFloat('#,#0 B',PrivateSpace));
  192.             SubItems.Add(SEmpty);
  193.           end;
  194.           with Add do begin
  195.             Caption:='Working Set Size';
  196.             SubItems.Add(FormatFloat('#,#0 B',WorkingSetSize));
  197.             SubItems.Add(FormatFloat('#,#0 B',PeakWorkingSetSize));
  198.           end;
  199.           with Add do begin
  200.             Caption:='Paged Pool Size';
  201.             SubItems.Add(FormatFloat('#,#0 B',PagedPoolSize));
  202.             SubItems.Add(SEmpty);
  203.           end;
  204.           with Add do begin
  205.             Caption:='NonPaged Pool Size';
  206.             SubItems.Add(FormatFloat('#,#0 B',NonPagedPoolSize));
  207.             SubItems.Add(SEmpty);
  208.           end;
  209.           with Add do begin
  210.             Caption:='Virtual Address Space Size';
  211.             SubItems.Add(FormatFloat('#,#0 B',VirtualAddressSpaceSize));
  212.             SubItems.Add(FormatFloat('#,#0 B',PeakVirtualAddressSpaceSize));
  213.           end;
  214.           with Add do begin
  215.             Caption:='Page File Usage';
  216.             SubItems.Add(FormatFloat('#,#0 B',PageFileUsage));
  217.             SubItems.Add(FormatFloat('#,#0 B',PeakPageFileUsage));
  218.           end;
  219.           EndUpdate;
  220.         end;
  221.  
  222.         if Is2K then begin
  223.           lvHeap.Items.BeginUpdate;
  224.           for i:=0 to HeapLists.Count-1 do
  225.             with lvHeap, Items do begin
  226.               with Add do begin
  227.                 Caption:=Format('0x%x',[PHeapList(HeapLists.Objects[i])^.ID]);
  228.                 SubItems.Add(PHeapList(HeapLists.Objects[i])^.szFlags);
  229.  
  230.               end;
  231.             end;
  232.           lvHeap.Items.EndUpdate;
  233.         end;
  234.  
  235.         lvMods.Items.BeginUpdate;
  236.         for i:=0 to Modules.Count-1 do
  237.           with lvMods, Items, PNTModule(Modules.Objects[i])^ do begin
  238.             with Add do begin
  239.               Caption:=Name;
  240.               if Is2K then
  241.                 SubItems.Add(Format('0x%x',[ID]))
  242.               else
  243.                 SubItems.Add(SEmpty);
  244.               SubItems.Add(Format('0x%x',[EntryPoint]));
  245.               SubItems.Add(Format('0x%x',[LoadAddress]));
  246.               SubItems.Add(FormatFloat('#,#0',ImageSize));
  247.               if Is2K then begin
  248.                 SubItems.Add(FormatFloat('#,#0',BaseSize));
  249.                 SubItems.Add(Format('0x%x',[Handle]));
  250.                 SubItems.Add(Format('%d',[GlobalUsage]));
  251.                 SubItems.Add(Format('%d',[ProcessUsage]));
  252.               end else begin
  253.                 SubItems.Add(SEmpty);
  254.                 SubItems.Add(SEmpty);
  255.                 SubItems.Add(SEmpty);
  256.                 SubItems.Add(SEmpty);
  257.               end;
  258.               SubItems.Add(ImageName);
  259.             end;
  260.           end;
  261.         lvMods.Items.EndUpdate;
  262.         ModsPanel.Caption:=Format('%d items',[lvMods.Items.Count]);
  263.  
  264.         lvThds.Items.BeginUpdate;
  265.         for i:=0 to Threads.Count-1 do
  266.           with lvThds, Items, PNTThread(Threads.Objects[i])^ do begin
  267.             with Add do begin
  268.               Caption:=Format('0x%x',[ID]);
  269.               SubItems.Add(szStatus);
  270.               SubItems.Add(Format('%d',[PriorityBase]));
  271.               SubItems.Add(Format('%d',[PriorityDelta]));
  272.               SubItems.Add(szWaitReason);
  273.               SubItems.Add(Format('0x%x',[StartAddr]));
  274.               SubItems.Add(Format('%d',[CntxtSwtcs]));
  275.               SubItems.Add(FormatSeconds(CPUTime,True,False,True));
  276.               SubItems.Add(SEmpty);
  277.             end;
  278.           end;
  279.         lvThds.Items.EndUpdate;
  280.         ThdsPanel.Caption:=Format('%d items',[lvThds.Items.Count]);
  281.       end;
  282.     end;
  283.   end else begin
  284.     p95:=P95Process(Process);
  285.     p95^:=Get95ProcessInfo(p95.PID,True);
  286.     with p95^ do begin
  287.       bKill.Enabled:=OpenProcess(PROCESS_TERMINATE,False,PID)<>0;
  288.       stName.Text:=Name;
  289.       stPID.Caption:=Format('%d (0x%0:x)',[PID]);
  290.       eFile.Text:=ImageName;
  291.       bProcProps.Enabled:=FileExists(ImageName);
  292.       eFile.Hint:=eFile.Text;
  293.       stModules.Caption:=Format('%d',[Modules.Count]);
  294.       stThreads.Caption:=Format('%d',[ThreadCount]);
  295.       stPriority.Caption:=Format('%d',[Priority]);
  296.       stPPID.Caption:=Format('%d (0x%0:x)',[ParentPID]);
  297.       stHeapID.Caption:=Format('%d (0x%0:x)',[HeapID]);
  298.  
  299.       lvHeap.Items.BeginUpdate;
  300.       for i:=0 to HeapLists.Count-1 do
  301.         with lvHeap, Items do begin
  302.           with Add do begin
  303.             Caption:=Format('0x%x',[PHeapList(HeapLists.Objects[i])^.ID]);
  304.             SubItems.Add(PHeapList(HeapLists.Objects[i])^.szFlags);
  305.           end;
  306.         end;
  307.       lvHeap.Items.EndUpdate;
  308.  
  309.       lvMods.Items.BeginUpdate;
  310.       for i:=0 to Modules.Count-1 do
  311.         with lvMods, Items, P95Module(Modules.Objects[i])^ do begin
  312.           with Add do begin
  313.             Caption:=Name;
  314.             SubItems.Add(Format('0x%x',[ID]));
  315.             SubItems.Add(Format('0x%x',[EntryPoint]));
  316.             SubItems.Add(SEmpty);
  317.             SubItems.Add(SEmpty);
  318.             SubItems.Add(FormatFloat('#,#0',BaseSize));
  319.             SubItems.Add(Format('0x%x',[Handle]));
  320.             SubItems.Add(Format('%d',[GlobalUsage]));
  321.             SubItems.Add(Format('%d',[ProcessUsage]));
  322.             SubItems.Add(ImageName);
  323.           end;
  324.         end;
  325.       lvMods.Items.EndUpdate;
  326.       ModsPanel.Caption:=Format('%d items',[lvMods.Items.Count]);
  327.  
  328.       lvThds.Items.BeginUpdate;
  329.       for i:=0 to Threads.Count-1 do
  330.         with lvThds, Items, P95Thread(Threads.Objects[i])^ do begin
  331.           with Add do begin
  332.             Caption:=Format('0x%x',[ID]);
  333.             SubItems.Add(SEmpty);
  334.             SubItems.Add(Format('%d',[PriorityBase]));
  335.             SubItems.Add(Format('%d',[PriorityDelta]));
  336.             SubItems.Add(SEmpty);
  337.             SubItems.Add(SEmpty);
  338.             SubItems.Add(SEmpty);
  339.             SubItems.Add(SEmpty);
  340.             SubItems.Add(Format('%d',[Usage]));
  341.           end;
  342.         end;
  343.       lvThds.Items.EndUpdate;
  344.       ThdsPanel.Caption:=Format('%d items',[lvThds.Items.Count]);
  345.     end;
  346.   end;
  347. end;
  348.  
  349. procedure TpropProcess.cmProcProps(Sender: TObject);
  350. begin
  351.   if FileExists(eFile.Text) then
  352.     DisplayPropDialog(Self.Parent.Handle,eFile.Text);
  353. end;
  354.  
  355. procedure TpropProcess.cmModProps(Sender: TObject);
  356. begin
  357.   if Assigned(lvMods.Selected) and FileExists(lvMods.Selected.SubItems[8]) then
  358.     DisplayPropDialog(Self.Parent.Handle,lvMods.Selected.SubItems[8]);
  359. end;
  360.  
  361. procedure TpropProcess.cmKill(Sender: TObject);
  362. var
  363.   ph :thandle;
  364.   pid :integer;
  365. begin
  366.   if IsNT then
  367.     pid:=PNTProcess(Process)^.PID
  368.   else
  369.     pid:=P95Process(Process)^.PID;
  370.   ph:=openprocess(PROCESS_TERMINATE,false,pid);
  371.   if (ph<>0) then begin
  372.     if (MessageDlg(stName.Text+#13#10#13#10+SWARN,mtWarning,[mbYes,mbNo],0)=mryes) then begin
  373.       TerminateProcess(ph,0);
  374.       Refresh;
  375.     end;
  376.   end else
  377.     MessageDlg(SCannotTerm,mtInformation,[mbOK],0);
  378. end;
  379.  
  380. end.
  381.