用什么标识 可以方便地找到特定程序??(20分)

  • 主题发起人 主题发起人 wab
  • 开始时间 开始时间
W

wab

Unregistered / Unconfirmed
GUEST, unregistred user!
有两个程序,一个实际程序,一个监视程序<br>用什么方法可以让监视程序方便地获得实际程序的句柄,除了标题外
 
使用FindApp,参数是程序的路径,就可以返回进程的ID<br>&nbsp; &nbsp; function FindApp(AppName: String): Integer;<br>&nbsp; &nbsp; function KillApp(ProcessId: Integer): Integer;<br>&nbsp; &nbsp; function IsRun(ProcessId: Integer): Boolean;<br>&nbsp; &nbsp; function IsBusy(ProcessId: Integer): Integer;<br>&nbsp; &nbsp; function RunApp(AppName, CmdLine: String; nCmdShow: Integer): Integer;<br><br>function TCcfRunMonitor2.FindApp(AppName: String): Integer;<br>var<br>&nbsp; PShot: THandle;<br>&nbsp; Pe: TProcessEntry32;<br>&nbsp; CanNext: Boolean;<br>&nbsp; ProcessId: Integer;<br>&nbsp; Found: Boolean;<br>&nbsp; function FindModule: Boolean;<br>&nbsp; var<br>&nbsp; &nbsp; MShot: THandle;<br>&nbsp; &nbsp; HasNext: Boolean;<br>&nbsp; &nbsp; Me: TModuleEntry32;<br>&nbsp; &nbsp; MfName: String;<br>&nbsp; begin<br>&nbsp; &nbsp; Result := False;<br>&nbsp; &nbsp; MShot := CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, ProcessId);<br>&nbsp; &nbsp; Me.dwSize := SizeOf(Me);<br>&nbsp; &nbsp; HasNext := Module32First(MShot, Me);<br>&nbsp; &nbsp; while HasNext do<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; MfName := Me.szExePath;<br>&nbsp; &nbsp; &nbsp; if SameText(MfName, AppName) then<br>&nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; Result := True;<br>&nbsp; &nbsp; &nbsp; &nbsp; Break;<br>&nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; HasNext := Module32Next(MShot, Me); &nbsp; &nbsp;<br>&nbsp; &nbsp; end;<br>&nbsp; &nbsp; CloseHandle(MShot);<br>&nbsp; end;<br>begin<br>&nbsp; PShot := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);<br>&nbsp; Pe.dwSize := SizeOf(Pe);<br>&nbsp; ProcessId := 0;<br>&nbsp; Found := False;<br>&nbsp; CanNext := Process32First(PShot, Pe);<br>&nbsp; while CanNext do<br>&nbsp; begin<br>&nbsp; &nbsp; ProcessId := Pe.th32ProcessID;<br>&nbsp; &nbsp; if ProcessId &gt; 0 then<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; Found := FindModule;<br>&nbsp; &nbsp; &nbsp; if Found then Break;<br>&nbsp; &nbsp; end;<br>&nbsp; &nbsp; CanNext := Process32Next(PShot, Pe);<br>&nbsp; end;<br>&nbsp; CloseHandle(PShot);<br>&nbsp; if Found then Result := ProcessId else Result := 0;end;<br><br>function TCcfRunMonitor2.GetServiceController: TServiceController;<br>begin<br>&nbsp; Result := ServiceController;<br>end;<br><br>function TCcfRunMonitor2.IsBusy(ProcessId: Integer): Integer;<br>var<br>&nbsp; Ph: THandle;<br>begin<br>&nbsp; Ph := OpenProcess(PROCESS_ALL_ACCESS, false, ProcessId);<br>&nbsp; if Ph &lt;&gt; 0 then<br>&nbsp; begin<br>&nbsp; &nbsp; if WaitForInputIdle(Ph, 10) = WAIT_TIMEOUT then<br>&nbsp; &nbsp; &nbsp; Result := 1<br>&nbsp; &nbsp; else<br>&nbsp; &nbsp; &nbsp; Result := 0;<br>&nbsp; &nbsp; CloseHandle(Ph);<br>&nbsp; end<br>&nbsp; else Result := -1;<br>end;<br><br>function TCcfRunMonitor2.IsRun(ProcessId: Integer): Boolean;<br>var<br>&nbsp; Ph: THandle;<br>begin<br>&nbsp; Ph := OpenProcess(PROCESS_ALL_ACCESS, false, ProcessId);<br>&nbsp; if Ph &lt;&gt; 0 then<br>&nbsp; begin<br>&nbsp; &nbsp; Result := WaitForSingleObject(Ph, 0) = WAIT_TIMEOUT;<br>&nbsp; &nbsp; CloseHandle(Ph);<br>&nbsp; end<br>&nbsp; else Result := False;<br>end;<br><br>function TCcfRunMonitor2.KillApp(ProcessId: Integer): Integer;<br>var<br>&nbsp; Ph: THandle;<br>begin<br>&nbsp; Ph := OpenProcess(PROCESS_ALL_ACCESS, false, ProcessId);<br>&nbsp; if Ph &lt;&gt; 0 then<br>&nbsp; begin<br>&nbsp; &nbsp; Result := Integer(TerminateProcess(Ph, 2701));<br>&nbsp; &nbsp; CloseHandle(Ph);<br>&nbsp; end<br>&nbsp; else Result := 0;<br>end;<br><br>function TCcfRunMonitor2.RunApp(AppName, CmdLine: String;<br>&nbsp; nCmdShow: Integer): Integer;<br>var<br>&nbsp; Sti: TStartupInfo;<br>&nbsp; Psi: TProcessInformation;<br>begin<br>&nbsp; FillMemory(@Sti, SizeOf(Sti), 0);<br>&nbsp; Sti.wShowWindow := nCmdShow;<br>&nbsp; Sti.dwFlags := STARTF_USEFILLATTRIBUTE;<br>&nbsp; Sti.dwFillAttribute := FOREGROUND_INTENSITY or BACKGROUND_BLUE;<br>&nbsp; if CreateProcess(PChar(AppName), PChar(CmdLine),<br>&nbsp; &nbsp; nil, nil, False,<br>&nbsp; &nbsp; 0, nil, PChar(ExtractFilePath(AppName)),<br>&nbsp; &nbsp; Sti, Psi) then<br>&nbsp; begin<br>&nbsp; &nbsp; Result := Psi.dwProcessId;<br>&nbsp; end<br>&nbsp; else Result := 0;<br>end;<br>
 
还没测试,不过正是我所需要的,先给你分了,不行再请教您!!!
 
后退
顶部