获取所有运行进程的列表(使用processName + ProcessPath + ProcessTitle) [英] Get list of all runing processes (with processName+ProcessPath+ProcessTitle)

查看:719
本文介绍了获取所有运行进程的列表(使用processName + ProcessPath + ProcessTitle)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要做什么:


获取所有运行进程的列表: processName (不含$ code > .exe ) ProcessFullFilePath ProcessMainWindowTitle 类似:

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




非常重要:
i需要不会产生任何异常的工作代码( try catch





< br>
我的代码:


i使用 TLHelp32 获取名称列表:

  var handler:THandle; 
data:TProcessEntry32;

函数GetName:string;
var i:byte;
begin
结果:='';
i:= 0;
while data.szExeFile [i]< ''do
begin
结果:= Result + data.szExeFile [i];
Inc(i);
结束
结束
begin
Application.ShowMainForm:= False;
处理程序:= CreateToolhelp32Snapshot(TH32CS_SNAPALL,0);
如果Process32First(处理程序,数据)然后
开始
ShowMessage(GetName());
while Process32Next(handler,data)do
ShowMessage(GetName());
结束





这还不够,
我需要:名称路径标题

解决方案

此功能不错:

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

问题是 data.szExeFile [i]< ''总是评估 True 。这是因为 data.szExeFile [i] 是一种类型为 char 的单个字符,而 ''是空字符串,从不等于单个字符。



其实你可以实现 GetName 这样:

  function GetName:string; 
begin
结果:= data.szExeFile;
结束

您还需要初始化数据您按照文档中所述调用 Process32First 。你需要写:

  data.dwSize:= SizeOf(data); 

在您调用 Process32First 之前。 >

这是您的程序的调试版本:

  {$ APPTYPE CONSOLE} 

使用
Windows,TlHelp32;

var
快照:THandle;
pe:TProcessEntry32;

begin
快照:= CreateToolhelp32Snapshot(TH32CS_SNAPALL,0);
try
pe.dwSize:= SizeOf(pe);
如果Process32First(Snapshot,pe)然后
,而Process32Next(Snapshot,pe)做
Writeln(pe.szExeFile);
finally
CloseHandle(Snapshot);
结束
Readln;
结束。

请注意,这不会给您可执行文件的完整路径。要获得完整路径,您需要使用 GetModuleFileNameEx 。这反过来需要一个进程句柄,您可以通过调用 OpenProcess 从进程ID获取。并且进程ID在 TProcessEntry32 记录中找到。



对于主窗口标题,这更棘手。许多进程将不会有主窗口。甚至那些甚至那些,你怎么知道哪个窗口是主窗口?一个进程可能有多个顶级窗口,只有应用程序本身才知道哪一个在概念上是主窗口。其实可能没有一个主窗口。如果有两个顶级窗口,应用程序可能没有偏好哪个主窗口。



这就是说,你可以做的是枚举所有的顶级窗口,调用 EnumWindows 。然后为每个顶级窗口调用 GetWindowThreadProcessId 。这可以让您了解创建每个顶级窗口的应用程序的进程ID,并从那里您可以将您需要的任何信息拼接在一起。所以,你的问题的那一部分没有一个明确的答案。在进步之前,您将需要准确地找出所需的内容。


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

解决方案

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.

这篇关于获取所有运行进程的列表(使用processName + ProcessPath + ProcessTitle)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆