Solved

List Active Programms

Posted on 2000-03-05
11
155 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Multiple image collision 13 88
Find and Replace Stream with 0s 8 73
Sending files from  idTcpServer Socket to idTcpClient 2 52
delphi popmenu non latine charcters 3 35
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…
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…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

733 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