Solved

Listing the running applications with memory & processor usage

Posted on 2004-04-06
14
220 Views
Last Modified: 2010-04-05
Please check the original Q at:
http://www.experts-exchange.com/Programming/Programming_Languages/Delphi/Q_20940915.html


points for this particular Q will not be awarded, I'll ask for it's deletion in a couple of days.

Thank you.
0
Comment
Question by:Esopo
  • 8
  • 4
14 Comments
 
LVL 11

Expert Comment

by:shaneholmes
ID: 10770846
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, TLHelp32, StdCtrls;

type
  TForm1 = class(TForm)
    ListBox1: TListBox;
    Button1: TButton;
    procedure FormActivate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

 function GetWindowHandle(ProcessID: DWORD): HWND;
 function GetProcessID(ExeFile: String): DWORD;
 function FileTimeToDateTime(FileTime : TFileTime) : TDateTime;

var
  Form1: TForm1;

implementation

{$R *.dfm}

function FileTimeToDateTime(FileTime : TFileTime) : TDateTime;
var
  SysTime : TSystemTime;
begin
  FileTimeToSystemTime(FileTime, SysTime);
  with SysTime do
    Result := EncodeDate(wYear, wMonth, wDay)
              + EncodeTime(wHour, wMinute, WSecond, wMilliseconds);
end;


function GetProcessID(ExeFile: String): DWORD;
var
  Snapshot: THandle;
  ProcessEntry: TProcessEntry32;
begin
  Snapshot := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
  if Snapshot <> 0 then
  try
    ProcessEntry.dwSize := SizeOf(ProcessEntry);
    if Process32First(Snapshot, ProcessEntry) then
    repeat
      if AnsiCompareText(ExeFile, ProcessEntry.szExeFile) = 0 then
      begin
        Result := ProcessEntry.th32ProcessID;
        Exit;
      end;
    until not Process32Next(Snapshot, ProcessEntry);
  finally
    CloseHandle(Snapshot);
  end;
  Result := 0;
end;

function GetWindowHandle(ProcessID: DWORD): HWND;
var
  Handle: HWND;

  function EnumWindowProc(hwnd: HWND; lParam: LPARAM): BOOL;
  var
    ProcessID: DWORD;
  begin
    GetWindowThreadProcessId(hwnd, @ProcessID);
    Result := (ProcessID <> DWORD(lParam));
    if not Result then
      Handle := HWND;
  end;

begin
  if EnumWindows(@EnumWindowProc, ProcessID) then
    Result := Handle
  else
    Result := 0;
end;



procedure TForm1.FormActivate(Sender: TObject);
var
  hSnapshot : THandle;
  ProcessEntry : TProcessEntry32;
  done : BOOL;
begin
  try
    hSnapshot := CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, 0 );
    ProcessEntry.dwSize := sizeof( TProcessEntry32 );
    done := Process32First( hSnapshot, ProcessEntry );
    while ( done = true ) do begin
     ListBox1.Items.Add((ProcessEntry.szExeFile));
      done := Process32Next( hSnapshot, ProcessEntry );
    end;
  finally
    CloseHandle( hSnapshot );
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
 PH : THandle;
 hProcess : THandle;
 lpCreationTime,
 lpExitTime,
 lpKernelTime,
 lpUserTime : TFileTime;
begin
  hProcess:= GetProcessId(ListBox1.Items[ListBox1.ItemIndex]);
  PH := OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, hProcess);
  if PH <> 0 then
  GetProcessTimes(PH, lpCreationTime, lpExitTime, lpKernelTime, lpUserTime);
  ShowMessage(DateTimeToStr(FileTimeToDateTime(lpCreationTime)));
end;

end.
0
 
LVL 11

Expert Comment

by:shaneholmes
ID: 10770902
Sorry,

//select  process in Listbox and click button
procedure TForm1.Button1Click(Sender: TObject);
var
 PH : THandle;
 hProcess : THandle;
 lpCreationTime,
 lpExitTime,
 lpKernelTime,
 lpUserTime : TFileTime;
begin
  hProcess:= GetProcessId(ListBox1.Items[ListBox1.ItemIndex]);
  PH := OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, hProcess);
  if PH <> 0 then
  GetProcessTimes(PH, lpCreationTime, lpExitTime, lpKernelTime, lpUserTime);
  //example of showing TFileTime variable
  ShowMessage(DateTimeToStr(FileTimeToDateTime(lpCreationTime)));
end;

Shane
0
 
LVL 14

Author Comment

by:Esopo
ID: 10770954
Shane, this will list all running process and the button tells the creation time, is that right?
0
 
LVL 11

Expert Comment

by:shaneholmes
ID: 10770977
Yes, bu there are other variables as well:

lpExitTime, lpKernelTime, lpUserTime

Shane
0
 
LVL 14

Author Comment

by:Esopo
ID: 10771012
Yes, i was looking into that. I did a simple changing with the lpCreationTime in the ShowMessage in your code, but the resulting numbers I don't get.
What are lpExitTime, lpKernelTime, lpUserTime, and how can I use them?
0
 
LVL 11

Expert Comment

by:shaneholmes
ID: 10771043
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 14

Author Comment

by:Esopo
ID: 10771122
I was actually reading that article, but although it explains how to use the values, it doesn't explain what Kernel mode and User mode are. Do you know?

I'll keep researching.
0
 
LVL 11

Expert Comment

by:shaneholmes
ID: 10771161
No I don't sorry!

However, if you search google groups foe (Delphi +  lpCreationTime) or any of the other values, you will see some of the information that is gathered using these values - Example , Computing CPU useage time, etc.

Shane
0
 
LVL 11

Expert Comment

by:shaneholmes
ID: 10771382
Did you look at the properties of TProcessEntry32;

var
 ProcessEntry : TProcessEntry32;

Shane
0
 
LVL 11

Expert Comment

by:shaneholmes
ID: 10771415
I just found this


http://www.volweb.cz/pvones/delphi/
http://www.volweb.cz/pvones/delphi/ToolHlpViewer.htm

It says the code can be downloaded from the JEDI page

http://delphi-jedi.org/Jedi:JCLDOWNLOADS

Im gonna download it and look!

Shane
0
 
LVL 11

Expert Comment

by:shaneholmes
ID: 10771427
Yup, all those applications (ToolHelp Viewer), and their source code is available in the download (JCL)

Shane
0
 
LVL 6

Accepted Solution

by:
Amir Azhdari earned 100 total points
ID: 10772624
hi Esopo , some changes to shaneholmes's code , it'll retrieve memory usage for each process(don't forget to use psapi unit)


unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, TLHelp32, StdCtrls,psapi;

type
  TForm1 = class(TForm)
    ListBox1: TListBox;
    Button1: TButton;
    Label1: TLabel;
    procedure FormActivate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

 function GetProcessID(ExeFile: String): DWORD;
 function FileTimeToDateTime(FileTime : TFileTime) : TDateTime;

var
  Form1: TForm1;

implementation

{$R *.dfm}

function FileTimeToDateTime(FileTime : TFileTime) : TDateTime;
var
  SysTime : TSystemTime;
begin
  FileTimeToSystemTime(FileTime, SysTime);
  with SysTime do
    Result := EncodeDate(wYear, wMonth, wDay)
              + EncodeTime(wHour, wMinute, WSecond, wMilliseconds);
end;


function GetProcessID(ExeFile: String): DWORD;
var
  Snapshot: THandle;
  ProcessEntry: TProcessEntry32;
begin
  Snapshot := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
  if Snapshot <> 0 then
  try
    ProcessEntry.dwSize := SizeOf(ProcessEntry);
    if Process32First(Snapshot, ProcessEntry) then
    repeat
      if AnsiCompareText(ExeFile, ProcessEntry.szExeFile) = 0 then
      begin
        Result := ProcessEntry.th32ProcessID;
        Exit;
      end;
    until not Process32Next(Snapshot, ProcessEntry);
  finally
    CloseHandle(Snapshot);
  end;
  Result := 0;
end;


procedure TForm1.FormActivate(Sender: TObject);
var
  hSnapshot : THandle;
  ProcessEntry : TProcessEntry32;
  done : BOOL;
begin
  try
    hSnapshot := CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, 0 );
    ProcessEntry.dwSize := sizeof( TProcessEntry32 );
    done := Process32First( hSnapshot, ProcessEntry );
    while ( done = true ) do begin
     ListBox1.Items.Add((ProcessEntry.szExeFile));
      done := Process32Next( hSnapshot, ProcessEntry );
    end;
  finally
    CloseHandle( hSnapshot );
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
 PH : THandle;
 hProcess : THandle;
 lpCreationTime,
 lpExitTime,
 lpKernelTime,
 lpUserTime : TFileTime;
 pmc:pprocess_memory_counters;
 cb:integer;

begin
  hProcess:= GetProcessId(ListBox1.Items[ListBox1.ItemIndex]);
  PH := OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, hProcess);
  if PH <> 0 then
  GetProcessTimes(PH, lpCreationTime, lpExitTime, lpKernelTime, lpUserTime);


// ********************* Memory Usage ********
cb:=sizeof(_process_memory_counters);
getmem(pmc,cb);
pmc^.cb:=cb;
if getprocessmemoryinfo(PH,pmc,cb)then
label1.caption:=inttostr(pmc^.WorkingSetSize)+' Bytes'
else
label1.caption:='unable to retrieve memory usage structure';
freemem(pmc);

// *****************************

  ShowMessage(DateTimeToStr(FileTimeToDateTime(lpCreationTime)));

end;


end.
0
 
LVL 14

Author Comment

by:Esopo
ID: 11642163
Hey guys!

I'm back. I know I deserve a name-calling session for leaving this Q out in the open for so long, but I had some issues that parted me from the net for a while.

Anyway, I'm back, got my hands on this project again and plan to see it through very soon. I'll be burning my brain with the code, hopefully we can come up with a function that will serve the delphi community for many years to come... (I feel I owe, you know?)

I'll be back with my findings very soon.

Esopo.
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now