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

帅比萌擦擦* 提交于 2019-12-13 11:31:07

问题


what i'am trying to do:

get list of all runing processes with: processName(without .exe) ProcessFullFilePath ProcessMainWindowTitle something like:

process1 c:\p1.exe TheprocessTitle
..............



Very important: i need working code that will not make any exception(try catch)




My Code:
i used TLHelp32 to get list of the names:

var handler: THandle;
    data: TProcessEntry32;

  function GetName: string;
  var i:byte;
  begin
     Result := '';
     i := 0;
     while data.szExeFile[i] <> '' do
     begin
        Result := Result + data.szExeFile[i];
        Inc(i);
     end;
   end;
begin
    Application.ShowMainForm := False;
    handler := CreateToolhelp32Snapshot(TH32CS_SNAPALL, 0);
  if Process32First(handler, data) then
  begin
   ShowMessage(GetName());
    while Process32Next(handler, data) do
       ShowMessage(GetName());
   end;



it's not enough, i need: name path title


回答1:


This function is no good:

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

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;
begin
  Result := data.szExeFile;
end;

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:

{$APPTYPE CONSOLE}

uses
  Windows, TlHelp32;

var
  Snapshot: THandle;
  pe: TProcessEntry32;

begin
  Snapshot := CreateToolhelp32Snapshot(TH32CS_SNAPALL, 0);
  try
    pe.dwSize := SizeOf(pe);
    if Process32First(Snapshot, pe) then
      while Process32Next(Snapshot, pe) do
        Writeln(pe.szExeFile);
  finally
    CloseHandle(Snapshot);
  end;
  Readln;
end.

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.



来源:https://stackoverflow.com/questions/21031046/get-list-of-all-runing-processes-with-processnameprocesspathprocesstitle

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!