Get all running windows' titles

With this simple little code you can list the windows/software that are running. You can identify if some specific software is running or do some magic tricks with Windows API. It is up to you…

Quick Tutorial

Start Lazarus.

Create a new Application Project (Project -> New Project -> Application -> OK).

Add a TListbox and a TButton in the form.

Switch to source (F12). Add windows in the uses clause.

1
2
uses
..., windows, ...;

Add the following function below implementation and {$R *.lfm} line:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
function EnumWindowsProc(WHandle: HWND; LParM: LParam): LongBool;StdCall;Export;
var Title,ClassName:array[0..128] of char;
    sTitle,sClass,Linia:STRING ;

begin
 Result:=True;

 GetWindowText(wHandle, Title,128);
 GetClassName(wHandle, ClassName,128);

 sTitle:=Title;
 sClass:=ClassName;

 if IsWindowVisible(wHandle) then
 begin
  Linia:=sTitle+'        '+sClass+'       '+IntToHex(wHandle,4);
  Form1.ListBox1.Items.Add(Linia);
 end;

end;

This is the function which will help us to list all the windows. Add its declaration before the implementation line:

1
function EnumWindowsProc(WHandle: HWND; LParM: LParam): LongBool;StdCall;Export;

Double click the button you placed earlier and write:

1
2
3
4
begin
  ListBox1.Clear;
  EnumWindows(@EnumWindowsProc,0);
end;

Do additional changes to beautify your form.

Run It

Press F9 or Run -> Run. The program will compile and run. Click the button. You will see a bunch of programs’ titles that are currently running. Click the button again to get an updated list. (You may also implement a timer to make this automatic.)

Get all the windows' titles with Lazarus+FreePascal

Concept

The above code depends on windows unit, which is not cross-platform. It is strictly based on the Windows API. Our primary procedure is EnumWindows. If we have EnumWindows, why do we have another function? Because it does not return any values. It takes a function’s name/pointer through which the output is sent to our program. The Windows API syntax is:

1
2
3
4
BOOL EnumWindows(
WNDENUMPROC lpEnumFunc, // pointer to callback function
LPARAM lParam // application-defined value
);

A enumeration function should be given, which will, well, enumerate. In our case all windows’ titles.

Ref:
http://delphi.about.com/od/windowsshellapi/l/aa080304a.htm
http://www.lazarus.freepascal.org/index.php?topic=13007.0

Downloads

You can download the source code for the tutorial project and executable/EXE files from the links below: