如何在xp系统下取得当前所以运行进程的完整路径?(50分)

  • 主题发起人 主题发起人 黑米
  • 开始时间 开始时间

黑米

Unregistered / Unconfirmed
GUEST, unregistred user!
我已经获得所有进程的processid了,也尝试用GetModuleFileName来获取路径,但是得到的只有本程序的完整路径,好像是GetModuleFileName不能在nt系统中使用只能在98中使用;请问有没有能在xp中使用的函数呢?
 
GetModuleFileNameEx
 
GetModuleFileNameEx<br>Requires Windows NT 4.0 or later; Win9x/ME: Not supported<br>The GetModuleFileNameEx function retrieves the fully qualified path for the specified module.
 
能否给个例子详细说明一下,我的e文不好的。
 
unit Unit1;<br><br>interface<br><br>uses<br> &nbsp;Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,<br> &nbsp;Dialogs, TLHelp32, StdCtrls, ShellAPI, psapi;<br><br>type<br> &nbsp;TForm1 = class(TForm)<br> &nbsp; &nbsp;Button1: TButton;<br> &nbsp; &nbsp;Memo1: TMemo;<br> &nbsp; &nbsp;procedure Button1Click(Sender: TObject);<br> &nbsp;private<br> &nbsp; &nbsp;{ Private declarations }<br> &nbsp; &nbsp;procedure KillCommandProcess;<br> &nbsp;public<br> &nbsp; &nbsp;{ Public declarations }<br> &nbsp;end;<br><br>var<br> &nbsp;Form1: TForm1;<br><br>implementation<br><br>{$R *.dfm}<br><br>function GetFileNameFromWindow(hw:HWND):string;<br>var<br>PidResult:LongInt;<br>Pid:Cardinal;<br>hProcess:THandle;<br>hMod:HMODULE;<br>cbNeeded:Cardinal;<br>buf:array [0..MAX_PATH] of char;<br>begin<br>ResuLt:='';<br>PidResult:=GetWindowThreadProcessId(hw,Pid);<br>if PidResult=0 then Exit;<br>hProcess := OpenProcess(PROCESS_QUERY_INFORMATION or<br>PROCESS_VM_READ,FALSE,Pid);<br>if hProcess=0 then Exit;<br>try<br>if not EnumProcessModules(hProcess,@hMod,sizeof(hMod),cbNeeded) then Exit;<br>FillChar(buf,MAX_PATH+1,#0);<br>GetModuleFileNameEx(hProcess,hMod,buf,MAX_PATH);<br>Result:=StrPas(buf);<br>finally<br>CloseHandle(hProcess);<br>end;<br>end;<br><br>procedure TForm1.KillCommandProcess;<br>var<br> ProcessSnapShotHandle: THandle;<br> ProcessEntry: TProcessEntry32;<br> ProcessHandle: THandle;<br> Ret: BOOL;<br>begin<br> ProcessSnapShotHandle:=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);<br> if ProcessSnapShotHandle&gt;0 then<br> begin<br> &nbsp; ProcessEntry.dwSize:=SizeOf(TProcessEntry32);<br> &nbsp; Ret:=Process32First(ProcessSnapShotHandle, ProcessEntry);<br> &nbsp; while Ret do<br> &nbsp; begin<br> &nbsp; &nbsp; Memo1.Lines.Add(GetFileNameFromWindow(ProcessEntry.th32ProcessID));<br> &nbsp; &nbsp; Ret:=Process32Next(ProcessSnapShotHandle, ProcessEntry)<br> &nbsp; end;<br> &nbsp; CloseHandle(ProcessSnapShotHandle)<br> end<br>end;<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>begin<br> &nbsp;KillCommandProcess;<br>end;<br><br>end.
 
我在使用以下语句后:<br>GetModuleFileNameEx(HInstance,p.ProcessID,FProcessEntry32.szExeFile,sizeof(FProcessEntry32.szExeFile));<br>显示的路径都只有8个字符,这是怎么回事?
 
sizeof(FProcessEntry32.szExeFile)改为256
 
改成256也是一样啊
 
楼主参考一下我给的代码吧 那个例子是<br>很久以前收集的一个例子 主要针对在NT4<br>以后系统要使用PsAPI单元
 
我的源代码是:<br>unit ntprocess;<br><br>interface<br><br>uses<br> &nbsp;Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,<br> &nbsp;Dialogs,TLHelp32, Grids, ValEdit, StdCtrls,psapi;<br><br>type<br> &nbsp;TForm1 = class(TForm)<br> &nbsp; &nbsp;ValueListEditor1: TValueListEditor;<br> &nbsp; &nbsp;procedure FormCreate(Sender: TObject);<br> &nbsp;private<br> &nbsp; &nbsp;{ Private declarations }<br> &nbsp;public<br> &nbsp; &nbsp;{ Public declarations }<br> &nbsp;end;<br><br>var<br> &nbsp;Form1: TForm1;<br> &nbsp;MyProcess : Tlist; // 返回的进程信息在MyProcess这个TList中。<br><br>type<br>TProcessInfo = Record<br>ExeFile : String;<br>ProcessID : DWORD;<br>end;<br>pProcessInfo = ^TProcessInfo;<br>implementation<br><br>{$R *.dfm}<br><br>procedure TForm1.FormCreate(Sender: TObject);<br>var<br>p : pProcessInfo;<br>IsLoopContinue:BOOL;<br>FSnapshotHandle:THandle;<br>FProcessEntry32:TProcessEntry32;<br><br>begin<br>MyProcess := TList.Create; <br>MyProcess.Clear; <br>FSnapshotHandle:=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);<br>FProcessEntry32.dwSize:=Sizeof(FProcessEntry32); <br>IsLoopContinue:=Process32First(FSnapshotHandle,FProcessEntry32); <br>while integer(IsLoopContinue)&lt;&gt;0 do<br>begin<br>New(p);<br>p.ExeFile := FProcessEntry32.szExeFile;<br>p.ProcessID := FProcessEntry32.th32ProcessID;<br>MyProcess.Add(p); <br>GetModuleFileNameEx(HInstance,p.ProcessID,FProcessEntry32.szExeFile,256);<br><br>valuelisteditor1.InsertRow(p.ExeFile,FProcessEntry32.szExeFile,true);<br>IsLoopContinue:=Process32Next(FSnapshotHandle,FProcessEntry32); <br>end;<br>CloseHandle(FSnapshotHandle); <br><br><br>end;<br><br>end.
 
procedure TForm1.FormCreate(Sender: TObject);<br>var<br>p : pProcessInfo;<br>IsLoopContinue:BOOL;<br>FSnapshotHandle:THandle;<br>FProcessEntry32:TProcessEntry32;<br><br>Pid:Cardinal;<br>hProcess:THandle;<br>hMod:HMODULE;<br>cbNeeded:Cardinal;<br>buf:array [0..MAX_PATH] of char;<br><br>label aa;<br>begin<br>MyProcess := TList.Create;<br>MyProcess.Clear;<br>FSnapshotHandle:=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);<br>FProcessEntry32.dwSize:=Sizeof(FProcessEntry32);<br>IsLoopContinue:=Process32First(FSnapshotHandle,FProcessEntry32);<br>while integer(IsLoopContinue)&lt;&gt;0 do<br>begin<br>New(p);<br>p.ExeFile := FProcessEntry32.szExeFile;<br>p.ProcessID := FProcessEntry32.th32ProcessID;<br>MyProcess.Add(p);<br><br>Pid := p.ProcessID;<br>hProcess := OpenProcess(PROCESS_QUERY_INFORMATION or<br>PROCESS_VM_READ,FALSE,Pid);<br>if hProcess=0 then goto aa;<br>if not EnumProcessModules(hProcess,@hMod,sizeof(hMod),cbNeeded) then goto aa;<br>FillChar(buf,MAX_PATH+1,#0);<br>GetModuleFileNameEx(hProcess,hMod,buf,MAX_PATH);<br><br><br>Memo1.Lines.Add(StrPas(buf));<br>aa:<br>IsLoopContinue:=Process32Next(FSnapshotHandle,FProcessEntry32);<br>end;<br>CloseHandle(FSnapshotHandle);<br><br><br>end;
 
谢谢各位了!
 
后退
顶部