使用FindApp,参数是程序的路径,就可以返回进程的ID<br> function FindApp(AppName: String): Integer;<br> function KillApp(ProcessId: Integer): Integer;<br> function IsRun(ProcessId: Integer): Boolean;<br> function IsBusy(ProcessId: Integer): Integer;<br> function RunApp(AppName, CmdLine: String; nCmdShow: Integer): Integer;<br><br>function TCcfRunMonitor2.FindApp(AppName: String): Integer;<br>var<br> PShot: THandle;<br> Pe: TProcessEntry32;<br> CanNext: Boolean;<br> ProcessId: Integer;<br> Found: Boolean;<br> function FindModule: Boolean;<br> var<br> MShot: THandle;<br> HasNext: Boolean;<br> Me: TModuleEntry32;<br> MfName: String;<br> begin<br> Result := False;<br> MShot := CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, ProcessId);<br> Me.dwSize := SizeOf(Me);<br> HasNext := Module32First(MShot, Me);<br> while HasNext do<br> begin<br> MfName := Me.szExePath;<br> if SameText(MfName, AppName) then<br> begin<br> Result := True;<br> Break;<br> end;<br> HasNext := Module32Next(MShot, Me); <br> end;<br> CloseHandle(MShot);<br> end;<br>begin<br> PShot := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);<br> Pe.dwSize := SizeOf(Pe);<br> ProcessId := 0;<br> Found := False;<br> CanNext := Process32First(PShot, Pe);<br> while CanNext do<br> begin<br> ProcessId := Pe.th32ProcessID;<br> if ProcessId > 0 then<br> begin<br> Found := FindModule;<br> if Found then Break;<br> end;<br> CanNext := Process32Next(PShot, Pe);<br> end;<br> CloseHandle(PShot);<br> if Found then Result := ProcessId else Result := 0;end;<br><br>function TCcfRunMonitor2.GetServiceController: TServiceController;<br>begin<br> Result := ServiceController;<br>end;<br><br>function TCcfRunMonitor2.IsBusy(ProcessId: Integer): Integer;<br>var<br> Ph: THandle;<br>begin<br> Ph := OpenProcess(PROCESS_ALL_ACCESS, false, ProcessId);<br> if Ph <> 0 then<br> begin<br> if WaitForInputIdle(Ph, 10) = WAIT_TIMEOUT then<br> Result := 1<br> else<br> Result := 0;<br> CloseHandle(Ph);<br> end<br> else Result := -1;<br>end;<br><br>function TCcfRunMonitor2.IsRun(ProcessId: Integer): Boolean;<br>var<br> Ph: THandle;<br>begin<br> Ph := OpenProcess(PROCESS_ALL_ACCESS, false, ProcessId);<br> if Ph <> 0 then<br> begin<br> Result := WaitForSingleObject(Ph, 0) = WAIT_TIMEOUT;<br> CloseHandle(Ph);<br> end<br> else Result := False;<br>end;<br><br>function TCcfRunMonitor2.KillApp(ProcessId: Integer): Integer;<br>var<br> Ph: THandle;<br>begin<br> Ph := OpenProcess(PROCESS_ALL_ACCESS, false, ProcessId);<br> if Ph <> 0 then<br> begin<br> Result := Integer(TerminateProcess(Ph, 2701));<br> CloseHandle(Ph);<br> end<br> else Result := 0;<br>end;<br><br>function TCcfRunMonitor2.RunApp(AppName, CmdLine: String;<br> nCmdShow: Integer): Integer;<br>var<br> Sti: TStartupInfo;<br> Psi: TProcessInformation;<br>begin<br> FillMemory(@Sti, SizeOf(Sti), 0);<br> Sti.wShowWindow := nCmdShow;<br> Sti.dwFlags := STARTF_USEFILLATTRIBUTE;<br> Sti.dwFillAttribute := FOREGROUND_INTENSITY or BACKGROUND_BLUE;<br> if CreateProcess(PChar(AppName), PChar(CmdLine),<br> nil, nil, False,<br> 0, nil, PChar(ExtractFilePath(AppName)),<br> Sti, Psi) then<br> begin<br> Result := Psi.dwProcessId;<br> end<br> else Result := 0;<br>end;<br>