如何获取当前运行的进程的全路经???我的代码好像有问题。请指教!(50分)

  • 主题发起人 主题发起人 mazheng
  • 开始时间 开始时间
M

mazheng

Unregistered / Unconfirmed
GUEST, unregistred user!
unit Unit1;<br><br>interface<br><br>uses<br> &nbsp;Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,<br> &nbsp;Dialogs, StdCtrls, ComCtrls, ExtCtrls, Buttons,<br> &nbsp;//----------------------<br> &nbsp;WinSvc,TlHelp32, Menus, PsApi;<br><br>type<br> &nbsp;TForm1 = class(TForm)<br> &nbsp; &nbsp;ListBox1: TListBox;<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><br> &nbsp;gbCanClose: Boolean;<br> &nbsp;SvcHandle: SC_HANDLE;<br> &nbsp;porFile: String;<br> &nbsp;ESS: Array of TEnumServiceStatus;<br> &nbsp;nBytesNeeded,nServices,nResumeHandle: Cardinal;<br> &nbsp;i: Integer;<br> &nbsp;pList1,pList2,pList3 : TStringList;<br><br><br>implementation<br><br>{$R *.DFM}<br><br> &nbsp;function &nbsp; GetProcessFullFileName(pID:Integer):String;<br> &nbsp;var &nbsp; s:String;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;hProcess:THandle;<br> &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Result:='';<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;SetLength(s,256);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;hProcess:=OpenProcess(PROCESS_ALL_ACCESS &nbsp; or &nbsp; PROCESS_QUERY_INFORMATION &nbsp; ,FALSE,pID);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if(hProcess&gt;0) &nbsp; then<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if(GetModuleFileNameEx(hProcess,0,PChar(s),255)&gt;0) &nbsp; then<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;SetLength(s, StrLen(PCHAR(s)));<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Result:=s;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;CloseHandle(hProcess);<br> &nbsp;end;<br><br>procedure EnumProcess(List: TListBox);<br>//枚举进程<br>var<br> ContinueLoop &nbsp; &nbsp;: BOOL;<br> FSnapshotHandle : THandle;<br> FProcessEntry32 : TProcessEntry32;<br>begin<br> FSnapshotHandle := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);<br> try<br> &nbsp; List.Clear;<br> &nbsp; FProcessEntry32.dwSize := Sizeof(FProcessEntry32);<br> &nbsp; ContinueLoop &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; := Process32First(FSnapshotHandle,FProcessEntry32);<br><br> &nbsp; while integer(ContinueLoop)&lt;&gt;0 do<br> &nbsp; begin<br> &nbsp; &nbsp; //进程名称<br> &nbsp; &nbsp; List.Items.Add(FProcessEntry32.szExeFile);<br> &nbsp; &nbsp; List.Items.Add('-');<br> &nbsp; &nbsp; //包含全路经的进程名称<br> &nbsp; &nbsp; // 但不知道为何返回空? 请指教<br> &nbsp; &nbsp; List.Items.add(GetProcessFullFileName(FProcessEntry32.th32ProcessID) );<br><br> &nbsp; &nbsp; ContinueLoop &nbsp;:= &nbsp;Process32Next(FSnapshotHandle,FProcessEntry32);<br> &nbsp; end;<br> finally<br> &nbsp; CloseHandle(FSnapshotHandle);<br> end;<br>end;<br>procedure TForm1.FormCreate(Sender: TObject);<br>begin<br> EnumProcess(ListBox1);<br>end;<br><br>end.
 
你的程序不要放在oncreate事件中,窗体正在建立,怎么处理你的代码嘛!<br>还有就是你声明的几个TStringList,用完了记住释放就可以了!
 
和这么样关系吧
 
你测试了再来说,我已经测试过了,问题就是这个.
 
unit untEnumProc;<br><br><br>interface<br>uses Windows, Messages, SysUtils,StrUtils, Variants, Classes,Dialogs,TLHelp32;<br><br><br><br>Procedure SwitchToThisWindow(hwnd:HWND;bRestore:Integer);stdcall;<br><br>type TEnumProc=class<br> &nbsp; &nbsp; private<br> &nbsp; &nbsp; &nbsp; &nbsp;FProcCount:Integer; //系统进程总数<br> &nbsp; &nbsp; &nbsp; &nbsp;FProcList:TList; //系统进程列表<br> &nbsp; &nbsp; public<br> &nbsp; &nbsp; &nbsp; &nbsp;constructor Create;<br> &nbsp; &nbsp; &nbsp; &nbsp;destructor Destroy;<br><br> &nbsp; &nbsp; &nbsp; &nbsp;procedure AllProcs; //枚举所有进程(结果放入FProcList)<br> &nbsp; &nbsp; &nbsp; &nbsp;function &nbsp;GetParentProcID(hProc:THandle):THandle; //根据 子进程ID 获取 父进程ID<br> &nbsp; &nbsp; &nbsp; &nbsp;function GetNameFromID(hProcName:THandle):string; &nbsp; &nbsp;//根据 进程ID 获得 进程名<br> &nbsp; &nbsp; &nbsp; &nbsp;function GetParentProcName(hProc:THandle):string; //根据 子进程ID 获取 父进程名<br> &nbsp; &nbsp; &nbsp; &nbsp;Function GetIDFromName(ProcName:String):THandle; //根据进程名 获取 进程ID.<br> &nbsp; &nbsp; &nbsp; &nbsp;function GetPrior(hProc:THandle):LongInt; &nbsp;//根据进程ID 获得进程优先级<br><br> &nbsp; &nbsp; &nbsp; &nbsp;//激活窗体(变成前台窗体).<br> &nbsp; &nbsp; &nbsp; &nbsp;function ActWindow(sWinName:string):Boolean;overload;//指定窗口名<br> &nbsp; &nbsp; &nbsp; &nbsp;procedure ActWindow(hFrmHandle:THandle);overload;//指定窗口句柄<br><br> &nbsp; &nbsp; published<br> &nbsp; &nbsp; &nbsp; &nbsp;{只读属性}<br> &nbsp; &nbsp; &nbsp; &nbsp;property ProcCount:Integer &nbsp;read FProcCount; &nbsp; //进程总数<br> &nbsp; &nbsp; &nbsp; &nbsp;property ProcInfo:TList read FProcList; //所有进程的信息列表.<br><br>end;<br><br>implementation<br><br>Procedure SwitchToThisWindow(hwnd:HWND;bRestore:Integer);external 'user32.dll' name 'SwitchToThisWindow';stdcall;<br><br>{ TEnumProc }<br>function TEnumProc.ActWindow(sWinName: string):Boolean;<br>var<br> &nbsp;hHandle:THandle;<br>begin<br> &nbsp;Result:=False;<br> &nbsp;hHandle:=FindWindow(nil,PAnsiChar(sWinName));<br> &nbsp;if hHandle=0 then Exit; //没有名称为sWinName的窗体,则退出<br><br> &nbsp;Result:=True;<br> &nbsp;ActWindow(hHandle);<br>end;<br><br>procedure TEnumProc.ActWindow(hFrmHandle: THandle);<br>var<br> &nbsp;i:Integer;<br>begin<br> &nbsp;SwitchToThisWindow(hFrmHandle,1);<br> &nbsp;for i:=0 to 5 do<br> &nbsp;begin<br> &nbsp; &nbsp; FlashWindow(hFrmHandle,LongBool(True));<br> &nbsp; &nbsp; Sleep(300);<br> &nbsp;end;<br><br>end;<br><br>{ &nbsp; TPROCESSENTRY32的结构. 下面是VB里的声明~ &nbsp;}<br>{Private Type PROCESSENTRY32<br> &nbsp;dwSize As Long &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '结构大小<br> &nbsp;cntUsage As Long &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '自身引用记数<br> &nbsp;th32ProcessID As Long &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'此进程ID<br> &nbsp;th32DefaultHeapID As Long &nbsp; &nbsp; &nbsp;'进程默认堆ID<br> &nbsp;th32ModuleID As Long &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '进程模块ID。DLL的模块ID与进程ID相同<br> &nbsp;cntThreads As Long &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '开启的线程计数<br> &nbsp;th32ParentProcessID As Long &nbsp; &nbsp;'父进程ID<br> &nbsp;pcPriClassBase As Long &nbsp; &nbsp; &nbsp; &nbsp; '线程优先权<br> &nbsp;dwFlags As Long &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'preserve<br> &nbsp;szExeFile As String * MAX_PATH 'full name<br>End Type}<br>procedure TEnumProc.AllProcs;<br>var<br> &nbsp;hSnapShot:THandle;<br> &nbsp;bExist:Boolean;<br> &nbsp;curProcName:String;<br> &nbsp;pProcess :PPROCESSENTRY32;<br><br> &nbsp;i:Integer;<br>begin<br> &nbsp; if Assigned(FProcList) then &nbsp; &nbsp; {释放以前分配的内存}<br> &nbsp; &nbsp; if FProcList.count&gt;0 then<br> &nbsp; &nbsp; &nbsp; for i:=FProcList.count-1 downto 0 do<br> &nbsp; &nbsp; &nbsp; begin<br> &nbsp; &nbsp; &nbsp; &nbsp; FreeMem(PPROCESSENTRY32(FProcList.Items));<br> &nbsp; &nbsp; &nbsp; &nbsp; FProcList.Delete(i);<br> &nbsp; &nbsp; &nbsp; end;<br><br> &nbsp; hSnapShot := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); &nbsp;//创建进程快照<br> &nbsp; If hSnapShot = 0 Then Exit;<br> &nbsp; GetMem(pProcess,SizeOf(TProcessEntry32));<br> &nbsp; //FillChar(pProcess,SizeOf(TProcessEntry32),0);<br> &nbsp; //ShowMessage(IntToStr(SizeOf(TProcessEntry32)));<br> &nbsp; pProcess^.dwSize := SizeOf(TProcessEntry32);<br> &nbsp; bExist:=Process32First(hSnapShot, pProcess^);<br> &nbsp; if (not bExist) then FreeMem(pProcess);<br> &nbsp; While (bExist) do<br> &nbsp; begin<br> &nbsp; &nbsp; &nbsp; &nbsp;FProcList.Add(pProcess); //找到的进程的信息加进列表<br><br> &nbsp; &nbsp; &nbsp; &nbsp;GetMem(pProcess,SizeOf(TProcessEntry32)); &nbsp;//getMem必须用freeMem释放<br> &nbsp; &nbsp; &nbsp; &nbsp;//FillChar(pProcess,SizeOf(TProcessEntry32),0);<br> &nbsp; &nbsp; &nbsp; &nbsp;pProcess^.dwSize := SizeOf(TProcessEntry32);<br> &nbsp; &nbsp; &nbsp; &nbsp;bExist:=Process32Next(hSnapShot, pProcess^);<br> &nbsp; end;<br> &nbsp; CloseHandle(hSnapShot);<br><br> &nbsp; FProcCount:=FProcList.Count;<br>end;<br><br>constructor TEnumProc.Create;<br>begin<br><br> &nbsp;FProcList:=TList.Create;<br> &nbsp;FProcCount:=-1; {为表示没有调用AllProcs函数,设FProcCount为-1 .<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;请务必保证在使用ProcCount属性前,间接或直接调用AllProcs函数. }<br>end;<br><br>destructor TEnumProc.Destroy;<br>var<br> &nbsp;i:Integer;<br>begin<br> &nbsp;for i:=FProcList.count-1 downto 0 do<br> &nbsp; &nbsp; &nbsp;FreeMem(PPROCESSENTRY32(FProcList.Items));<br> &nbsp;FProcList.Free;<br> &nbsp;inherited;<br>end;<br><br>function TEnumProc.GetIDFromName(ProcName: String): THandle;<br>var<br> &nbsp;i:Integer;<br> &nbsp;curProcName:string;<br> &nbsp;pProcess:PPROCESSENTRY32;<br>begin<br> &nbsp;Result:=0; &nbsp;//没有找到<br> &nbsp;AllProcs;<br><br> &nbsp;for i:=0 to FProcList.Count-1 do<br> &nbsp;begin<br> &nbsp; &nbsp;pProcess:=PPROCESSENTRY32(FProcList.Items);<br> &nbsp; &nbsp;curProcName:= pProcess.szExeFile;<br> &nbsp; &nbsp;if Pos('.',curProcName)&gt;0 then<br> &nbsp; &nbsp; &nbsp; curProcName:= LeftStr(curProcName,Pos('.',curProcName)-1); //smss.exe--&gt;smss<br> &nbsp; &nbsp; &nbsp; <br> &nbsp; &nbsp;If LowerCase(curProcName) = LowerCase(Trim(ProcName)) Then<br> &nbsp; &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Result:= pProcess^.th32ProcessID;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;break;<br> &nbsp; &nbsp; &nbsp; &nbsp;end;<br> &nbsp;end;<br><br>end;<br><br>function TEnumProc.GetNameFromID(hProcName: THandle):string ;<br>var<br> &nbsp;i:Integer;<br> &nbsp;pProcess:PPROCESSENTRY32;<br>begin<br> &nbsp; Result:=''; &nbsp;//没有找到<br> &nbsp; AllProcs;<br><br> &nbsp; for i:=0 to FProcList.Count-1 do<br> &nbsp; begin<br> &nbsp; &nbsp; &nbsp;pProcess:=PPROCESSENTRY32(FProcList.Items);<br> &nbsp; &nbsp; &nbsp;if &nbsp;pProcess.th32ProcessID=hProcName then<br> &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Result:=pProcess.szExeFile;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Exit;<br> &nbsp; &nbsp; &nbsp;end;<br> &nbsp; end;<br>end;<br><br>function TEnumProc.GetParentProcID(hProc: THandle): THandle;<br>var<br> &nbsp;i:Integer;<br> &nbsp;pProcess:PPROCESSENTRY32;<br>begin<br> &nbsp;Result:=0;<br> &nbsp;AllProcs;<br><br> &nbsp;for i:=0 to FProcList.Count-1 do<br> &nbsp; begin<br> &nbsp; &nbsp; &nbsp;pProcess:=PPROCESSENTRY32(FProcList.Items);<br> &nbsp; &nbsp; &nbsp;if pProcess.th32ProcessID=hProc then<br> &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp; Result:=pProcess.th32ParentProcessID;<br> &nbsp; &nbsp; &nbsp; &nbsp; Exit;<br> &nbsp; &nbsp; &nbsp;end;<br> &nbsp; end;<br><br>end;<br><br>function TEnumProc.GetParentProcName(hProc: THandle): string;<br>var<br> &nbsp;hHandle:THandle;<br>begin<br> &nbsp;hHandle:=GetParentProcID(hProc);<br> &nbsp;Result:=GetNameFromID(hHandle);<br>end;<br><br>function TEnumProc.GetPrior(hProc: THandle): LongInt;<br>var<br> &nbsp;i:Integer;<br> &nbsp;pProcess:PPROCESSENTRY32;<br>begin<br> &nbsp;Result:=0;<br> &nbsp;AllProcs;<br><br> &nbsp;for i:=0 to FProcList.Count-1 do<br> &nbsp; begin<br> &nbsp; &nbsp; &nbsp;pProcess:=PPROCESSENTRY32(FProcList.Items);<br> &nbsp; &nbsp; &nbsp;if pProcess.th32ProcessID=hProc then<br> &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp; Result:=pProcess.pcPriClassBase;<br> &nbsp; &nbsp; &nbsp; &nbsp; Exit;<br> &nbsp; &nbsp; &nbsp;end;<br> &nbsp; end;<br>end;<br><br>end.
 
实验表明PROCESSENTRY32结构中的szExeFile并不是全路径,只是应用程序名----比如: IE.exe
 
后退
顶部