测试通过......楼主加上自己的代码试试<br><br><br>unit Unit1111;<br><br>interface<br><br>uses<br> Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,<br> Dialogs,tlhelp32, StdCtrls;<br><br>type<br> TForm1 = class(TForm)<br> Button1: TButton;<br> procedure Button1Click(Sender: TObject);<br> private<br> { Private declarations }<br> public<br> { Public declarations }<br> end;<br> procedure FindProcess(const AFilename:string;const PathMatch:Boolean;var ProcessID:<br> DWORD);<br><br>var<br> Form1: TForm1;<br><br>implementation<br><br>{$R *.dfm}<br><br>procedure FindProcess(const AFilename:string;const PathMatch:Boolean;var ProcessID:<br> DWORD);<br> //AFilename为要查找进程的文件名(可以包括路径)<br> //pathMatch 表示查找的时候是否匹配路径<br>var<br> lppe:TProcessEntry32;<br> SsHandle:Thandle;<br> FoundAProc,FoundOK:boolean;<br>begin<br> ProcessID:=0;<br> //创建系统快照<br> SsHandle:=CreateToolHelp32SnapShot(TH32CS_SnapProcess,0);<br> //第一个进程<br> FoundAProc:=Process32First(Sshandle,lppe);<br> //列举所有运行进程,匹配指定的文件名<br> while FoundAProc do<br> begin<br> if PathMatch then<br> FoundOK:=AnsiStrComp(lppe.szExefile,PChar(AFilename))=0<br> else<br> FoundOK:=AnsiStrcomp(PChar(ExtractFilename(lppe.szExeFile)),<br> PChar(ExtractFilename(AFilename)))=0;<br> if FoundOK then<br> begin<br> ProcessID:=lppe.th32ProcessID;<br> break;<br> end;<br> FoundAProc:=Process32Next(Sshandle,lppe);<br> end;<br> CloseHandle(Sshandle);<br>end;<br><br>procedure KillProcess(dwProcessId: DWORD);<br>var<br> ProcHandle: THandle;<br>begin<br> ProcHandle := OpenProcess(1, FALSE, dwProcessID);<br> try<br> if ProcHandle <> 0 then<br> begin<br> if TerminateProcess(ProcHandle, $FFFFFFFF) then<br> WaitForSingleObject(ProcHandle, INFINITE);<br> end;<br> finally<br> CloseHandle(ProcHandle);<br> end;<br>end;<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>var<br> i: Integer;<br> ProcessID: DWord;<br>begin<br> try<br> {ShellExecute(Handle, 'open', PChar('command.com'),<br> PChar('/c 001 <input.txt >p1.txt'), nil, SW_SHOW); }<br> for i := 1 to 50 do<br> begin<br> ProcessID := 0;<br> FindProcess('ntvdm.exe', False, ProcessID);<br> if ProcessID = 0{and FileExists('c:/p1.txt')} then<br> begin<br> ShowMessage('测试成功!');<br> exit;<br> end<br> else Sleep(100);<br> end;<br> ShowMessage('测试失败!');<br> KillProcess(ProcessID);<br> except<br> raise Exception.Create('测试出错!');<br> end;<br>end;<br><br>end.