请问如何从任务管理器的应用程序列表中获知该应用程序属于哪个进程

niat97222 2006-03-06 10:21:03
如我们打开DELPHI后,任务管理器的应用程序列表中将出现DELPHI7这个程序,且该程序状态为正在运行,而进程中也必然出现delphi32.exe的进程,且DELPHI7这个程序是由DELPHI32.EXE这个进程产生的。如果现在应用程序列表中出现了名称为AAA的程序,我如何从进程列表中获知该程序是由哪个进程产生的呢,还请不吝赐教
...全文
162 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
ly_liuyang 2006-03-07
  • 打赏
  • 举报
回复
The GetWindowThreadProcessId function retrieves the identifier of the thread that created the specified window and, optionally, the identifier of the process that created the window. This function supersedes the GetWindowTask function.
老之 2006-03-06
  • 打赏
  • 举报
回复
列举任务栏、进程ID、路径的程序:

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls,PSAPI;

type
TForm1 = class(TForm)
Button1: TButton;
ListBox1: TListBox;
Label1: TLabel;
procedure Button1Click(Sender: TObject);
procedure ListBox1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.DFM}

function EnumWindowsProc (Wnd: HWND; lb: TListbox): BOOL; stdcall;
var
caption: array [0..128] of Char;
begin
Result := True;
if IsWindowVisible(Wnd) and

((GetWindowLong(Wnd, GWL_HWNDPARENT) = 0) or
(HWND(GetWindowLong(Wnd, GWL_HWNDPARENT)) = GetDesktopWindow)) and
((GetWindowLong(Wnd, GWL_EXSTYLE) and WS_EX_TOOLWINDOW) = 0)
then begin
SendMessage( Wnd, WM_GETTEXT, Sizeof( caption ), integer(@caption));
lb.Items.AddObject( caption, TObject( Wnd ));
end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
listbox1.clear;
EnumWindows( @EnumWindowsProc, integer( listbox1 ));
end;

procedure TForm1.ListBox1Click(Sender: TObject);
var
theClassname,path: array [0..255] of Char;
Wnd,hModule,hProcess: HWND;
tid, pid,Needed: DWORD;
begin
with Sender as TListbox do begin
If ItemIndex >= 0 then begin
Wnd:= HWND(Items.Objects[ itemindex ]);
If Wnd <> 0 then begin
Windows.GetClassname( Wnd, theClassname, Sizeof( classname ));
tid := GetWindowThreadProcessID( Wnd, @pid );
hProcess:=OpenProcess(PROCESS_QUERY_INFORMATION or PROCESS_VM_READ,False,PID);
if hProcess>0 then
begin
EnumProcessModules(hProcess,@hModule,SizeOf(hModule),Needed);
GetModuleFileNameEx(hProcess,hModule,Path,SizeOf(Path));
GetShortPathName(Path,Path,256);
end;
label1.caption :=
Format(
'HWND: %8.8x'#13#10+
'Class: %s'#13#10+
'Process ID: %8.8x'#13#10+
'Thread ID: %8.8x'#13#10+
'Path:%s',
[Wnd, theClassname, pid, tid, path] );
end;
end;
end;
end;

end.
aiirii 2006-03-06
  • 打赏
  • 举报
回复
//还有别忘了在 uses 部分加上TLHelp32单元
function GetAppName(const AWindowHandle: THandle): string;
var
PI: DWORD;
ContinueLoop:BOOL;
SnapshotHandle:THandle;
ProcessEntry32:TProcessEntry32;
begin
Result := '';
GetWindowThreadProcessId(AWindowHandle, @PI);

SnapshotHandle := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
ProcessEntry32.dwSize := Sizeof(ProcessEntry32);
ContinueLoop := Process32First(SnapshotHandle,ProcessEntry32);
while ContinueLoop do
begin
if ProcessEntry32.th32ProcessID = PI then
begin
Result := ProcessEntry32.szExeFile;
break;
end;
ContinueLoop:=Process32Next(SnapshotHandle, ProcessEntry32);
end;
CloseHandle(SnapshotHandle);
end;

用以上的方法找到窗口HANDLE和PROCESSID后,用GETWINDOWTHREADPROCESS这个API函数可以得到创建窗口的PROCESSID,再和PROCESSID比较一下,可以找到!
niat97222 2006-03-06
  • 打赏
  • 举报
回复
啊,这里没有人来吗

1,183

社区成员

发帖
与我相关
我的任务
社区描述
Delphi Windows SDK/API
社区管理员
  • Windows SDK/API社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧