Get list of all runing processes (with processName+ProcessPath+ProcessTitle) [closed]

This function is no good:

function GetName: string;
var i:byte;
  Result := '';
  i := 0;
  while data.szExeFile[i] <> '' do
    Result := Result + data.szExeFile[i];

The problem is that data.szExeFile[i] <> '' always evaluates True. That is because data.szExeFile[i] is a single character of type char, and '' is the empty string which is never equal to a single character.

In fact you can implement GetName like this:

function GetName: string;
  Result := data.szExeFile;

You do also need to initialise data before you call Process32First as described in the documentation. You need to write:

data.dwSize := SizeOf(data);

before you call Process32First.

Here is a debugged version of your program:


  Windows, TlHelp32;

  Snapshot: THandle;
  pe: TProcessEntry32;

  Snapshot := CreateToolhelp32Snapshot(TH32CS_SNAPALL, 0);
    pe.dwSize := SizeOf(pe);
    if Process32First(Snapshot, pe) then
      while Process32Next(Snapshot, pe) do

Note that this will not give you the full path to the executable file. To get the full path you need to use GetModuleFileNameEx. That in turn needs a process handle which you get from the process ID by calling OpenProcess. And the process ID is found in the TProcessEntry32 record.

As for the main window title, that’s more tricky. Many processes will not have a main window. What’s more even those that do, how to you know which window is the main window? A process may have multiple top level windows and only the application itself knows which one is conceptually the main window. In fact there may be no single main window. If there are two top level windows the application may have no preference for which one is the main window.

That said, what you can do is to enumerate all the top level windows with a call to EnumWindows. Then for each top level window call GetWindowThreadProcessId. This allows you to find out the process ID of the application that created each top-level window and from there you should be able to stitch together whatever information you decide that you need. So, that part of your question has no single definitive answer. You will need to work out exactly what you want before you progress.

Leave a Comment