Solved

Running Applications ?

Posted on 2002-05-05
7
158 Views
Last Modified: 2010-04-04
How can I get the running Application.
Like the Taskmonitor form WinNt do.
I working with Win98/ME

Thanx
0
Comment
Question by:Claud
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 22

Expert Comment

by:Mohammed Nasman
ID: 6990264
Hello

  Here's code will return the all processes, I got it from Code central in borland site


unit GetProcess;

interface

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

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

var
  Form1: TForm1;

implementation

{$R *.dfm}
uses
  TLHelp32, PsApi;

function RunningProcessesList(List: TStrings): Boolean;

  function BuildListTH: Boolean;
  var
    SnapProcHandle: THandle;
    ProcEntry: TProcessEntry32;
    NextProc: Boolean;
  begin
    SnapProcHandle := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
    if SnapProcHandle <> THandle(-1) then
    begin
      Result := True;
      ProcEntry.dwSize := Sizeof(ProcEntry);
      NextProc := Process32First(SnapProcHandle, ProcEntry);
      while NextProc do
      begin
        List.AddObject(ProcEntry.szExeFile, Pointer(ProcEntry.th32ProcessID));
        NextProc := Process32Next(SnapProcHandle, ProcEntry);
      end;
      CloseHandle(SnapProcHandle);
    end else
      Result := False;
  end;

  function BuildListPS: Boolean;
  var
    PIDs: array[0..1024] of DWORD;
    Handle: THandle;
    Needed: DWORD;
    I: Integer;
    ModuleFileName: array[0..MAX_PATH] of Char;
  begin
    Result := EnumProcesses(@PIDs, Sizeof(PIDs), Needed);
    if not Result then Exit;
    for I := 0 to (Needed div Sizeof(DWORD)) - 1 do
      if PIDs[I] <> 0 then
      begin
        Handle := OpenProcess(PROCESS_QUERY_INFORMATION or PROCESS_VM_READ, False, PIDs[I]);
        if Handle <> 0 then
        begin
          if GetModuleFileNameEx(Handle, 0, ModuleFileName, Sizeof(ModuleFileName)) = 0 then
            List.AddObject('[System]', Pointer(INVALID_HANDLE_VALUE))
          else
            List.AddObject(ModuleFileName, Pointer(PIDs[I]));
          CloseHandle(Handle);
        end;
      end;
  end;

begin
  Assert(Assigned(List));
  List.BeginUpdate;
  try
    List.Clear;
    if (Win32Platform = VER_PLATFORM_WIN32_NT) and (Win32MajorVersion = 4) then
      Result := BuildListPS
    else
      Result := BuildListTH;
  finally
    List.EndUpdate;
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  RunningProcessesList(ListBox1.Items);
end;

procedure TForm1.FormShow(Sender: TObject);
begin
  Button1Click(nil);
end;

end.
0
 
LVL 8

Expert Comment

by:Cesario
ID: 6990333
Dear Mnasman ;-)

your code will return the Tabsheet with Process in the Taskmonitor but the Application TabSheet.

Its possible to get this form the Taskbar.

Cesario
0
 
LVL 8

Accepted Solution

by:
Cesario earned 200 total points
ID: 6990507
Claud, if you are looking for the runing tasks so just use GetTasksList function (JclSysInfo unit) from JCL:
http://delphi-jedi.org/Jedi:CODELIBJCL


Try this Exmaple:
function GetTasksList(const List: TStrings): Boolean;

  function EnumWindowsProc(Wnd: HWND; List: TStrings): Boolean; stdcall;
  var
    ParentWnd: HWND;
    ExStyle: DWORD;
    Caption: array [0..255] of Char;
  begin
    if IsWindowVisible(Wnd) then
    begin
      ParentWnd := GetWindowLong(Wnd, GWL_HWNDPARENT);
      ExStyle := GetWindowLong(Wnd, GWL_EXSTYLE);
      if ((ParentWnd = 0) or (ParentWnd = GetDesktopWindow)) and
        ((ExStyle and WS_EX_TOOLWINDOW = 0) or (ExStyle and WS_EX_APPWINDOW <> 0)) and
        (GetWindowText(Wnd, Caption, SizeOf(Caption)) > 0) then
          List.AddObject(Caption, Pointer(Wnd));
    end;
    Result := True;
  end;

begin
  Result := EnumWindows(@EnumWindowsProc, Integer(List));
end;

good luck
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 7

Expert Comment

by:God_Ares
ID: 6991164
btw.. if you want to know how much cpu time all processes have been using, don't try to find some api call (i did that) it's a calculation..

check all processes and see how much time they used int a intervall

procedure TForm1.Button2Click(Sender: TObject);

Function x(t:_systemtime):String;
var s:string;
Begin
  s:= IntToStr(t.wYear);
  s:=s + '-' + IntToStr(t.wMonth);
  s:=s + '-' + IntToStr(t.wDay);
  s:=s + ' @ ' + IntToStr(t.wHour);
  s:=s + ':' + IntToStr(t.wMinute);
  s:=s + '.' + IntToStr(t.wSecond);
  result := s;
end;


var a,b,c,d : _filetime;
    t:_systemtime;
begin
//this is it!
 GetProcessTimes(GetCurrentProcess
                ,a
                ,b
                ,c
                ,d);


// specifies the process of interest
//    LPFILETIME lpCreationTime,     // when the process was created
//    LPFILETIME lpExitTime,     // when the process exited
//    LPFILETIME lpKernelTime,     // time the process has spent in kernel mode
//    LPFILETIME lpUserTime      // time the process has spent in user mode
//   );
  memo1.Clear;
  FileTimeToSystemTime(a,t); memo1.lines.add('Created ' + x(t));
  FileTimeToSystemTime(b,t); memo1.lines.add('Exited ' + x(t));
  FileTimeToSystemTime(c,t); memo1.lines.add('kernel ' + x(t));
  FileTimeToSystemTime(d,t); memo1.lines.add('user ' + x(t));

end;


just a comment.
0
 
LVL 7

Expert Comment

by:God_Ares
ID: 6991166
I'm so sorry

GetProcessTimes
Windows 95/98/Me: Unsupported.
0
 

Author Comment

by:Claud
ID: 7033511
Thanx Expers. But Jedi Code was very usful. Cesario the Points are 4u.
0
 

Author Comment

by:Claud
ID: 7065708
Thanx, for the running code...

tia

0

Featured Post

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

770 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