请问如何从任务管理器的应用程序列表中获知该应用程序属于哪个进程
如我们打开DELPHI后,任务管理器的应用程序列表中将出现DELPHI7这个程序,且该程序状态为正在运行,而进程中也必然出现delphi32.exe的进程,且DELPHI7这个程序是由DELPHI32.EXE这个进程产生的。如果现在应用程序列表中出现了名称为AAA的程序,我如何从进程列表中获知该程序是由哪个进程产生的呢,还请不吝赐教 问题点数:200、回复次数:4Top
1 楼niat97222(Freeman)回复于 2006-03-06 22:35:48 得分 0
啊,这里没有人来吗Top
2 楼aiirii(ari-http://spaces.msn.com/members/aiirii/)回复于 2006-03-06 22:49:23 得分 80
//还有别忘了在 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比较一下,可以找到!Top
3 楼liangqingzhi(老之)回复于 2006-03-06 23:12:23 得分 80
列举任务栏、进程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.Top
4 楼ly_liuyang(Liu Yang LYSoft http://lysoft.7u7.net)回复于 2006-03-07 10:57:43 得分 40
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.Top




