如何得到另一个正在运行中的程序的进程号(菜鸟一个,请详细一点,谢谢)?!(100分)

  • 主题发起人 主题发起人 keiven
  • 开始时间 开始时间
K

keiven

Unregistered / Unconfirmed
GUEST, unregistred user!
如何得到另一个正在运行中的程序的进程号(菜鸟一个,请详细一点,谢谢)?!
 
前提是什么,是知道程序名还是窗口句柄?
 
TLHelp32
procedure TfrmMain.spdRefresh;
var
ContinueLoop:BOOL;
NewItem : TListItem;
begin
ListView.Items.Clear;
FSnapshotHandle:=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
FProcessEntry32.dwSize:=Sizeof(FProcessEntry32);
ContinueLoop:=Process32First(FSnapshotHandle,FProcessEntry32);
while integer(ContinueLoop)<>0 do
begin
NewItem:=ListView.Items.add;
NewItem.Caption:=ExtractFileName(FProcessEntry32.szExeFile);
NewItem.subItems.Add(format('%.8d',[FProcessEntry32.th32ProcessID]));//(IntToHex(FProcessEntry32.th32ProcessID,4));
NewItem.subItems.Add(FProcessEntry32.szExeFile);
NewItem.subItems.Add(NewItem.Caption);
ContinueLoop:=Process32Next(FSnapshotHandle,FProcessEntry32);
end;
CloseHandle(FSnapshotHandle);
end;
 
>>zw84611 我可以得到窗口名和 hanle
>>yuki2003 你提供的代码调试不通过
 
一个简单的读进程的类,进程列表结果在GetProcessID中

unit ProcessList;

interface
uses
Windows, Messages, SysUtils, Variants, Classes, Controls,
ExtCtrls, StdCtrls,Tlhelp32;

type
TProcessList=class(tpersistent)
Private
function GetProcessID:TStringList;//取得进程id

Published
property ProcessIDList:TStringList read GetProcessID;
end;

implementation
function TProcessList.GetProcessID:TStringList;//取得进程id
var
ContinueLoop:BOOL;
FSnapshotHandle:THandle;
FProcessEntry32:TProcessEntry32;
const
PROCESS_TERMINATE=$0001;
begin
result:=TstringList.create;
FSnapshotHandle:=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
FProcessEntry32.dwSize:=Sizeof(FProcessEntry32);
ContinueLoop:=Process32First(FSnapshotHandle,FProcessEntry32);
{Result:=('th32ProcessId'+floattostr(FProcessEntry32.th32ProcessId)+' '+
'cntUsage '+ floattostr(FProcessEntry32.cntUsage)+' '+
'cntThreads '+ floattostr(FProcessEntry32.cntThreads)+' th32ParentProcessID '+
floattostr(FProcessEntry32.th32ParentProcessID)+' '+
FProcessEntry32.szExeFile);}
result.add(floattostr(FProcessEntry32.th32ProcessId)+ //'th32ProcessId'
','+FProcessEntry32.szExeFile);
while integer(ContinueLoop)<>0 do
begin
//Result:=Integer(TerminateProcess(OpenProcess(PROCESS_TERMINATE,BOOL(0),FProcessEntry32.th32ProcessID),0));
ContinueLoop:=Process32Next(FSnapshotHandle,FProcessEntry32);
if integer(ContinueLoop)<>0 then begin
{detailed
result:=result+('th32ProcessId'+floattostr(FProcessEntry32.th32ProcessId)+
',cntUsage '+floattostr(FProcessEntry32.cntUsage)+
',cntThreads '+ floattostr(FProcessEntry32.cntThreads)+
',th32ParentProcessID '+floattostr(FProcessEntry32.th32ParentProcessID)+
','+FProcessEntry32.szExeFile);}
result.add(floattostr(FProcessEntry32.th32ProcessId)+ //'th32ProcessId'
','+FProcessEntry32.szExeFile);

end;
end;
CloseHandle(FSnapshotHandle);
end;


end.
 
>>yuki2003 你提供的代码调试不通过
不好意思,这是我代码中的一部分,有两个外部变量
加进TLHelp32
添加一个ListView1,Button1

procedure TForm1.Button1Click(Sender: TObject);
var
ContinueLoop: BOOL;
NewItem: TListItem;
NewColumn: TListColumn;
FSnapshotHandle: THandle;
FProcessEntry32: TProcessEntry32;
begin
NewColumn := ListView1.Columns.Add;
NewColumn.Caption := 'FileName';
NewColumn := ListView1.Columns.Add;
NewColumn.Caption := 'th32ProcessID';
NewColumn := ListView1.Columns.Add;
NewColumn.Caption := 'szExeFile';
NewColumn := ListView1.Columns.Add;
NewColumn.Caption := 'FileName';
ListView1.ViewStyle := vsReport;
ListView1.Items.Clear;
FSnapshotHandle := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
FProcessEntry32.dwSize := Sizeof(FProcessEntry32);
ContinueLoop := Process32First(FSnapshotHandle, FProcessEntry32);
while integer(ContinueLoop) <> 0 do
begin
NewItem := ListView1.Items.add;
NewItem.Caption := ExtractFileName(FProcessEntry32.szExeFile);
NewItem.subItems.Add(format('%.8d', [FProcessEntry32.th32ProcessID]));
//(IntToHex(FProcessEntry32.th32ProcessID,4));
NewItem.subItems.Add(FProcessEntry32.szExeFile);
NewItem.subItems.Add(NewItem.Caption);
ContinueLoop := Process32Next(FSnapshotHandle, FProcessEntry32);
end;
CloseHandle(FSnapshotHandle);
end;

end;
 
谢谢,我已接受
 
多人接受答案了。
 
后退
顶部