Solved

List Active Programms

Posted on 2000-03-05
11
152 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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

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…
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…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

758 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

21 Experts available now in Live!

Get 1:1 Help Now