*如何得到其它运行着的程序的文件路径*(100分)

  • 主题发起人 消毒水
  • 开始时间

消毒水

Unregistered / Unconfirmed
GUEST, unregistred user!
我们可以用FindWindow找到某一运行中的程序,如下:<br>if FindWindow(nil,'计算器')&lt;&gt;0 then showmessage('程序运行中');<br>那么该如何得到该程序的文件路径呢?
 
呵呵,如果是win98,那么ProcessEntry.szExeFile中就带路径.<br>如果不是,则只有文件名,可以用以下的办法.呵呵.<br>var<br>&nbsp; ProcessSnapShotHandle: THandle;<br>&nbsp; ProcessEntry: TProcessEntry32;<br>&nbsp; ProcessHandle: THandle;<br>&nbsp; Ret: BOOL;<br>&nbsp; s: string;<br>&nbsp; Position: Byte;<br><br>&nbsp; ModuleSnapShotHandle: THandle;<br>&nbsp; ModuleEntry: TModuleEntry32;<br>begin<br>&nbsp; ProcessSnapShotHandle:=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);<br>&nbsp; if ProcessSnapShotHandle&gt;0 then<br>&nbsp; begin<br>&nbsp; &nbsp; ProcessEntry.dwSize:=SizeOf(TProcessEntry32);<br>&nbsp; &nbsp; Ret:=Process32First(ProcessSnapShotHandle, ProcessEntry);<br>&nbsp; &nbsp; while Ret do<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; ModuleSnapShotHandle:=CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, ProcessEntry.th32ProcessID);<br>&nbsp; &nbsp; &nbsp; if ModuleSnapShotHandle&gt;0 then<br>&nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ModuleEntry.dwSize:=SizeOf(TModuleEntry32);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Ret:=Module32First(ModuleSnapShotHandle, ModuleEntry);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if Ret then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;s:=ModuleEntry.szExePath;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;CloseHandle(ModuleSnapShotHandle)<br>&nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; Ret:=Process32Next(ProcessSnapShotHandle, ProcessEntry)<br>&nbsp; &nbsp; end;<br>&nbsp; &nbsp; CloseHandle(ProcessSnapShotHandle);<br>&nbsp; end<br>end;<br>s里就是你要的了.
 
在2K下,我搞定了路径问题,用的是PSAPI,配合ToolHelp32
 
获得你的窗口ID后使用下面的函数<br>function FindProcessExeName(AWndHandle:THandle):string;<br>var<br>&nbsp; PI: DWORD;<br>&nbsp; lppe: TProcessEntry32;<br>&nbsp; ssHandle: THandle;<br>&nbsp; BFound: Boolean;<br>begin<br>&nbsp; result := '';<br>&nbsp; GetWindowThreadProcessId(AWndHandle, @PI);<br>&nbsp; ssHandle := CreateToolHelp32SnapShot(TH32CS_SnapProcess,0);<br>&nbsp; lppe.dwSize := SizeOf(lppe);<br>&nbsp; BFound := Process32First(ssHandle,lppe);<br>&nbsp; while BFound do begin<br>&nbsp; &nbsp; if lppe.th32ProcessID = PI then begin<br>&nbsp; &nbsp; &nbsp; result := lppe.szExeFile;<br>&nbsp; &nbsp; &nbsp; break;<br>&nbsp; &nbsp; end;<br>&nbsp; &nbsp; BFound := Process32Next(ssHandle,lppe);<br>&nbsp; end;<br>&nbsp; CloseHandle(ssHandle);<br>end;<br><br>需要TlHelp32.pas
 
在2K下用ToolHelp32函数是不可能得到路径的。只有文件名呀,<br>要结合PSAP才行
 
呵呵,无忌呀,我贴的代码肯定能找到路径名.
 
嗯,谢谢大家!我回去看看怎样吧!
 
我试了试jlutt-sadan的代码,只有文件名,所以我又加了两行,呵呵<br>var<br>&nbsp; FileName: string;<br>&nbsp; Path: &nbsp; &nbsp; &nbsp;array[0..255] of char;<br>begin<br>&nbsp; FileName := FindProcessExeName(AHandle);<br>&nbsp; FileName := FindExecutable(PChar(FileName), '', Path);<br>&nbsp; Result &nbsp;:= Path;<br>end;
 
顶部