怎样获得指定的正在运行的程序的ProcessID(50分)

  • 主题发起人 主题发起人 AKang
  • 开始时间 开始时间
uses TLHelp32;<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>var<br>&nbsp; FSnapshotHandle:THandle;<br>&nbsp; FProcessEntry32:TProcessEntry32;<br>&nbsp; Ret : BOOL;<br>&nbsp; ProcessID : integer;<br>&nbsp; s:string;<br>begin<br>&nbsp; FSnapshotHandle:=CreateToolhelp32Snapshot(<br>TH32CS_SNAPPROCESS,0);<br>&nbsp; &nbsp; //创建系统快照<br>&nbsp; FProcessEntry32.dwSize:=Sizeof(FProcessEntry32);<br>&nbsp; &nbsp; //先初始化 FProcessEntry32 的大小<br>&nbsp; Ret:=Process32First(FSnapshotHandle,FProcessEntry32);<br>&nbsp; while Ret do<br>&nbsp; begin<br>&nbsp; &nbsp; s:=ExtractFileName(FProcessEntry32.szExeFile);<br>&nbsp; &nbsp; if s='KERNEL32.DLL' then<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; ProcessID:=FProcessEntry32.th32ProcessID;<br>&nbsp; &nbsp; &nbsp; s:='';<br>&nbsp; &nbsp; &nbsp; break;<br>&nbsp; &nbsp; end;<br>&nbsp; &nbsp; Ret:=Process32Next(FSnapshotHandle,FProcessEntry32);<br>&nbsp; end;<br>&nbsp; &nbsp;//循环枚举出系统开启的所有进程,找出“Kernel32.dll”<br>&nbsp; CloseHandle(FSnapshotHandle);<br>&nbsp; Edit1.Text := 'Process ID '+IntToHex(<br>&nbsp; FProcessEntry32.th32ProcessID,8);<br>end;
 
下面是获得当前具有焦点的程序或窗口的<br>ProcessID: <br>windowhld:=GetForegroundWindow;<br>threadld:=GetWindowThreadProcessId(Windowhld,nil)
 
如果程序是有窗体:<br>1首先获得窗口句柄:FindWindow;<br>2GetWindowThreadProcessId函数去获得;<br>相关的API函数说明通过MSDN得到。<br>
 
多人接受答案了。
 
后退
顶部