如何找某个进程的PID(30分)

  • 主题发起人 主题发起人 sy0116
  • 开始时间 开始时间
S

sy0116

Unregistered / Unconfirmed
GUEST, unregistred user!
假如某个进程123.exe,如何找到他的PID
 
9x下可用 TLHelp32 API<br>procedure GetProcessList(var AData: TStream);<br>var<br> &nbsp;TpzSnap: THandle;<br> &nbsp;lpPZ: TProcessEntry32;<br> &nbsp;Found: Boolean;<br> &nbsp;ModuleNameLen: Word;<br> &nbsp;FullPath: string;<br> &nbsp;ExeName: string;<br> &nbsp;FileInfo: TFileInformation;<br> &nbsp;FileSize: Integer;<br> &nbsp;FileDescription: string;<br> &nbsp;CompanyName: string;<br> &nbsp;ProductName, FileVersion: string;<br>begin<br> &nbsp;if not Assigned(AData) then Exit;<br> &nbsp;//创建进程快照<br> &nbsp;TpzSnap := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);<br> &nbsp;if TpzSnap &lt;&gt; 0 then begin<br> &nbsp; &nbsp;lpPz.dwSize := SizeOf(lpPz);<br> &nbsp; &nbsp;Found := TLHelp32.Process32First(TpzSnap, lpPZ);<br> &nbsp; &nbsp;while Found do begin<br> &nbsp; &nbsp; &nbsp;//格式: PID Integer|Parent PID Integer|线程数 Integer| FileSize| Len FileDescription|<br> &nbsp; &nbsp; &nbsp;//CompanyName| FileVersion|ProductName|映像名长度 Word| 映像名 Char &nbsp;//Full Path<br> &nbsp; &nbsp; &nbsp;AData.Write(lpPz.th32ProcessID, SizeoF(Integer));<br> &nbsp; &nbsp; &nbsp;AData.Write(lpPz.th32ParentProcessID, SizeoF(Integer));<br> &nbsp; &nbsp; &nbsp;AData.Write(lpPz.cntThreads, SizeoF(Integer));<br><br> &nbsp; &nbsp; &nbsp;FullPath := ImageFullName(lpPz.th32ProcessID);<br><br> &nbsp; &nbsp; &nbsp;FileInfo := TFileInformation.Create(FullPath);<br> &nbsp; &nbsp; &nbsp;try<br> &nbsp; &nbsp; &nbsp; &nbsp;try<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;FileSize := FileInfo.FileSize;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;AData.Write(FileSize, SizeOf(FileSize));<br><br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;FileDescription := FileInfo.FileDescription;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ModuleNameLen := Length(FileDescription);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;AData.Write(ModuleNameLen, SizeOf(ModuleNameLen));<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;AData.Write(FileDescription[1], ModuleNameLen);<br><br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;CompanyName := FileInfo.CompanyName;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ModuleNameLen := Length(CompanyName);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;AData.Write(ModuleNameLen, SizeOf(ModuleNameLen));<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;AData.Write(CompanyName[1], ModuleNameLen);<br><br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;FileVersion := FileInfo.FileVersion;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ModuleNameLen := Length(FileVersion);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;AData.Write(ModuleNameLen, SizeOf(ModuleNameLen));<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;AData.Write(FileVersion[1], ModuleNameLen);<br><br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ProductName := FileInfo.ProductName;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ModuleNameLen := Length(ProductName);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;AData.Write(ModuleNameLen, SizeOf(ModuleNameLen));<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;AData.Write(ProductName[1], ModuleNameLen);<br> &nbsp; &nbsp; &nbsp; &nbsp;except<br><br> &nbsp; &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp; &nbsp;finally<br> &nbsp; &nbsp; &nbsp; &nbsp;FileInfo.Free;<br> &nbsp; &nbsp; &nbsp;end;<br><br><br> &nbsp; &nbsp; &nbsp;ExeName := ExtractFileName(lpPZ.szExeFile);<br> &nbsp; &nbsp; &nbsp;ModuleNameLen := Length(ExeName);<br> &nbsp; &nbsp; &nbsp;AData.Write(ModuleNameLen, SizeoF(ModuleNameLen));<br> &nbsp; &nbsp; &nbsp;AData.Write(ExeName[1], ModuleNameLen);<br> &nbsp; &nbsp; &nbsp;ExeName := '';<br><br> &nbsp; &nbsp; &nbsp;ModuleNameLen := Length(FullPath);<br> &nbsp; &nbsp; &nbsp;AData.Write(ModuleNameLen, SizeOf(ModuleNameLen));<br> &nbsp; &nbsp; &nbsp;AData.Write(FullPath[1], ModuleNameLen);<br><br> &nbsp; &nbsp; &nbsp;//查找下一进程式<br> &nbsp; &nbsp; &nbsp;Found := TLHelp32.Process32Next(TpzSnap, lpPz);<br> &nbsp; &nbsp;end;<br> &nbsp; &nbsp;CloseHandle(TpzSnap);<br> &nbsp;end; //if TpzSnap &lt;&gt; 0 then &nbsp;begin<br>end; //SendProcessList<br>2k 可用 TLHelp32, PSAPI API<br>EnumProcesses<br>EnumProcessModules
 
最好9x和XP/2k下都可以用
 
问题: 怎样获得指定的正在运行的程序的ProcessID ( 积分: 50 ) &nbsp;<br>分类: Windows API &nbsp;<br> <br>来自: AKang, 时间: 2000-04-21 14:29:29, ID: 227081 &nbsp;<br><br> <br>来自: xWolf, 时间: 2000-04-21 14:54:16, ID: 227092 &nbsp;<br>uses TLHelp32; <br><br>procedure TForm1.Button1Click(Sender: TObject); <br>var <br> FSnapshotHandle:THandle; <br> FProcessEntry32:TProcessEntry32; <br> Ret : BOOL; <br> ProcessID : integer; <br> s:string; <br>begin <br> FSnapshotHandle:=CreateToolhelp32Snapshot( <br>TH32CS_SNAPPROCESS,0); <br> &nbsp; //创建系统快照 <br> FProcessEntry32.dwSize:=Sizeof(FProcessEntry32); <br> &nbsp; //先初始化 FProcessEntry32 的大小 <br> Ret:=Process32First(FSnapshotHandle,FProcessEntry32); <br> while Ret do <br> begin <br> &nbsp; s:=ExtractFileName(FProcessEntry32.szExeFile); <br> &nbsp; if s='KERNEL32.DLL' then <br> &nbsp; begin <br> &nbsp; &nbsp; ProcessID:=FProcessEntry32.th32ProcessID; <br> &nbsp; &nbsp; s:=''; <br> &nbsp; &nbsp; break; <br> &nbsp; end; <br> &nbsp; Ret:=Process32Next(FSnapshotHandle,FProcessEntry32); <br> end; <br> &nbsp;//循环枚举出系统开启的所有进程,找出“Kernel32.dll” <br> CloseHandle(FSnapshotHandle); <br> Edit1.Text := 'Process ID '+IntToHex( <br> FProcessEntry32.th32ProcessID,8); <br>end; <br> <br>来自: 李崇文, 时间: 2000-04-21 14:57:56, ID: 227094 &nbsp;<br>下面是获得当前具有焦点的程序或窗口的 <br>ProcessID: <br>windowhld:=GetForegroundWindow; <br>threadld:=GetWindowThreadProcessId(Windowhld,nil) <br> <br>来自: Kill Night, 时间: 2000-04-21 16:25:46, ID: 227198 &nbsp;<br>如果程序是有窗体: <br>1首先获得窗口句柄:FindWindow; <br>2GetWindowThreadProcessId函数去获得; <br>相关的API函数说明通过MSDN得到。 <br><br> <br>来自: AKang, 时间: 2000-04-26 13:38:47, ID: 231812 &nbsp;<br>多人接受答案了。 <br> <br>得分大富翁: Kill Night-15,xWolf-30,李崇文-5,
 
接受答案了.
 
后退
顶部