Solved

List Active Programms

Posted on 2000-03-05
11
153 Views
Last Modified: 2010-04-04
How do I list ALL active programs runing, visible and invisible? how can I close, hide, show focus a certine program?
0
Comment
Question by:SwOoPe
11 Comments
 
LVL 17

Accepted Solution

by:
inthe earned 100 total points
ID: 2586056
hi thee try this:

unit Unit1;

interface

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

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

var
  Form1: TForm1;
 
implementation
 
{$R *.DFM}

type
  EnumWindowsProc = function (Hwnd: THandle;Param: Pointer): Bool; stdcall;

function GetWindowExeName(Handle: THandle): String;
var
  PE: TProcessEntry32;
  Snap: THandle;
  ProcessId: cardinal;
begin
  GetWindowThreadProcessId(Handle,@ProcessId);
  Snap:= CreateToolHelp32Snapshot(TH32CS_SNAPPROCESS, 0);
  if Snap <> 0 then begin
    if Process32First(Snap, PE) then
      if PE.th32ProcessID = ProcessId then
        Result:= String(PE.szExeFile)
      else while Process32Next(Snap, PE) do
        if PE.th32ProcessID = ProcessId then begin
          Result:= String(PE.szExeFile);
          break;
        end;
    CloseHandle(Snap);
  end;
end;

function GetTitle (Hwnd: THandle; Param: Pointer): Bool; stdcall;
 var
  Text,TempString: string;
begin
  If (GetWindowLong(Hwnd,GWL_HWNDPARENT)=0) and (IsWindowVisible(Hwnd) or IsIconic(Hwnd))then
  begin
TempString := GetWindowExeName(Hwnd);
  SetLength (Text, 100);
  GetWindowText (Hwnd, PChar (Text), 100);
  Form1.ListBox1.Items.Add (IntToStr (Hwnd) + ' : '  + TempString + ' : ' +text);
  Result := True;
  end;
end;
 
procedure TForm1.Button1Click(Sender: TObject);
var
  EWProc: EnumWindowsProc;
begin
  ListBox1.Items.Clear;
  EWProc := GetTitle;
  EnumWindows (@EWProc, 0);
end;
 
procedure TForm1.Button2Click(Sender: TObject);
var
wnd :Hwnd;
temp :string;
EWProc: EnumWindowsProc;
ClickedOK :  boolean;
begin
clickedOK := InputQuery('Which Process do U wanna Kill','Process Number',temp);
if ClickedOK then
begin
Wnd := hwnd(strToInt(temp));
PostMessage(Wnd,WM_SYSCOMMAND,SC_CLOSE, 0);
  sleep(100);
  ListBox1.Items.Clear;
  EWProc := GetTitle;
  EnumWindows (@EWProc, 0);
end;
end;
 
end.


just add 2 more buttons .
note that the toolhelp32 functions arent available on all versions of windows ao probably better to see madshi's enumstuff unit:
http://nettrash.com/users/madshi/
with it you can do like:
uses Enumstuff;

Procedure Killapp(exeFile: string);
var pl : TProcessList;
    i1 : integer;
     h : thandle;
begin
  pl:=GetProcessList;
  for i1:=0 to high(pl) do
    if CompareText(ExtractFileName(pl[i1].name),ExtractFileName(exeFile))=0 then begin
 h := openprocess(PROCESS_TERMINATE,false,pl[i1].pid);
   TerminateProcess(h,0);
   closehandle(h);
      exit;
    end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
killapp('notepad.exe')
end;

and add the processlists to a Tmemo for instance.. etc..
0
 
LVL 17

Expert Comment

by:inthe
ID: 2586060
opps missed some stuff .
where i put
"just add 2 more buttons "
they are for show and hide so you would put the button1 code on all three buttons click event but on 2 of the buttons you would replace the line:
"PostMessage(Wnd,WM_SYSCOMMAND,SC_CLOSE, 0); "
with
showwindow(wnd,sw_hide);
and
showwindow(wnd,sw_show);

cheers Barry
0
 

Author Comment

by:SwOoPe
ID: 2593450
It seems like your sample just shows whats visible in the taskbar. I ned to get all programs.

The other source seems very good, but unfortenetly im a litle bit to stupid to figure it out. The source code U posted was easy to use.
0
 
LVL 12

Expert Comment

by:rwilson032697
ID: 2594250
Listening
0
 
LVL 2

Expert Comment

by:craig_capel
ID: 2596138
listening....., pointless, however if you want to see the biggest thread going, the biggest one i have ever seen is this... check it out :)

http://www.experts-exchange.com/jsp/qShow.jsp?ta=newcompusr&qid=10168979 

see if anyone here can help?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 17

Expert Comment

by:inthe
ID: 2597694
ok try this instead,whichever item is selected in listbox 2 will be the one that is hidden/shown or killed.
the listbox1 is hidden on formcreate and is there primarily to hold the handles of each item.
you could add third listbox(not hidden to add the path of exe to just uncomment the line i added for tat if you want it):



unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, ExtCtrls,TlHelp32, ComCtrls;

type
  TForm1 = class(TForm)
    Button2: TButton;
    Button3: TButton;
    Button1: TButton;
    ListBox1: TListBox;
    ListBox2: TListBox;
    procedure FormCreate(Sender: TObject);
    procedure ListBox2MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  i : integer;
implementation

{$R *.DFM}

type
  EnumWindowsProc = function (Hwnd: THandle;Param: Pointer): Bool; stdcall;

function GetWindowExeName(Handle: THandle): String;
var
  PE: TProcessEntry32;
  Snap: THandle;
  ProcessId: cardinal;
begin
  GetWindowThreadProcessId(Handle,@ProcessId);
  Snap:= CreateToolHelp32Snapshot(TH32CS_SNAPPROCESS, 0);
  if Snap <> 0 then begin
    if Process32First(Snap, PE) then
      if PE.th32ProcessID = ProcessId then
        Result:= String(PE.szExeFile)
      else while Process32Next(Snap, PE) do
        if PE.th32ProcessID = ProcessId then begin
          Result:= String(PE.szExeFile);
          break;
        end;
    CloseHandle(Snap);
  end;
end;

function GetTitle (Hwnd: THandle; Param: Pointer): Bool; stdcall;
 var
  Text,TempString: string;
begin
  If (GetWindowLong(Hwnd,GWL_HWNDPARENT)=0) then
  begin
TempString := GetWindowExeName(Hwnd);
  SetLength (Text, 100);
  GetWindowText (Hwnd, PChar (Text), 100);
  if text > ' ' then  begin
  Form1.Listbox1.Items.Add(IntToStr(Hwnd));
  Form1.Listbox2.Items.Add(text);
//Form1.listbox3.items.add(tempstring);//to add path also
  end;
  Result := True;
  end;
end;

procedure TForm1.FormCreate(Sender: TObject);
var
  EWProc: EnumWindowsProc;
begin
  Listbox1.Visible := false;
  ListBox1.Items.Clear;
  Listbox2.Items.Clear;
  EWProc := GetTitle;
  EnumWindows (@EWProc, 0);
button1.caption := 'hide item';
button2.caption := 'show item';
button3.caption := 'kill item';
end;

procedure TForm1.ListBox2MouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin  //get  a handle to selected item
i := ListBox1.ItemAtPos(Point(x,y),True);
end;

procedure TForm1.Button1Click(Sender: TObject);
var
wnd :Hwnd;
begin
Wnd := hwnd(strToInt(listbox1.items.Strings[i]));
Showwindow(Wnd,SW_HIDE);
end;

procedure TForm1.Button2Click(Sender: TObject);
var
wnd :Hwnd;
begin
Wnd := hwnd(strToInt(listbox1.items.Strings[i]));
Showwindow(Wnd,SW_SHOW);
end;

procedure TForm1.Button3Click(Sender: TObject);
var
wnd :Hwnd;
temp :string;
EWProc: EnumWindowsProc;
ClickedOK :  boolean;
begin
Wnd := hwnd(strToInt(listbox1.items.Strings[i]));
PostMessage(Wnd,WM_SYSCOMMAND,SC_CLOSE, 0);
sleep(100);
ListBox1.Items.Clear;
Listbox2.Items.Clear;
EWProc := GetTitle;
EnumWindows (@EWProc, 0);
end;

end.
0
 
LVL 17

Expert Comment

by:inthe
ID: 2634837
hello ..
0
 
LVL 2

Expert Comment

by:craig_capel
ID: 2636183
Hi.... how are you
0
 
LVL 17

Expert Comment

by:inthe
ID: 2637734
not too bad thanks ;-)

having some nice weather lately here ..
summer is coming :o)
0
 
LVL 2

Expert Comment

by:craig_capel
ID: 2637802
hehe, always has nice weather here, registered 32c about 90f in your terms here today.... :)
0
 
LVL 17

Expert Comment

by:inthe
ID: 2638161
thanks for the "c" really appreciate it ,why do we bother ...
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
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 is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

863 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

28 Experts available now in Live!

Get 1:1 Help Now