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 | uses |
Add the following function below implementation
and {$R *.lfm}
line:
1 | function EnumWindowsProc(WHandle: HWND; LParM: LParam): LongBool;StdCall;Export; |
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 | begin |
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.)
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 | BOOL EnumWindows( |
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: