如何知道一个用CreateProcess的进程是否运行完毕(100分)

I

ioi2000

Unregistered / Unconfirmed
GUEST, unregistred user!
如何知道一个用CreateProcess的进程是否运行完毕。<br>我是要知道他是否结束的状态
 
用WaitforSingleObject(ProcessInfo.hProcess,INFINITE);<br><br>//-------------------------------------------------------------<br>function WinExecAndWait32(FileName:String; Visibility : integer):integer;<br>var<br>&nbsp; zAppName:array[0..512] of char;<br>&nbsp; zCurDir:array[0..255] of char;<br>&nbsp; WorkDir:String;<br>&nbsp; StartupInfo:TStartupInfo;<br>&nbsp; ProcessInfo:TProcessInformation;<br>&nbsp; //MyResult:DWORD;<br><br>&nbsp; hStdIn:THandle; // standard input handle<br>&nbsp; //inputBuffer:INPUT_RECORD; &nbsp;//buffer to hold a single console input record<br>&nbsp; irMacroBuf:INPUT_RECORD; // array of input events<br>&nbsp; dwBytesWritten : DWORD;<br><br>begin<br>&nbsp; SetConsoleTitle('s');<br>&nbsp; StrPCopy(zAppName,FileName);<br>&nbsp; //MainForm.Caption := zappname;<br>&nbsp; GetDir(0,WorkDir);<br>&nbsp; StrPCopy(zCurDir,WorkDir);<br>&nbsp; FillChar(StartupInfo,Sizeof(StartupInfo),#0);<br>&nbsp; StartupInfo.cb := Sizeof(StartupInfo);<br>&nbsp; hStdIn:= StartupInfo.hStdInput;<br>&nbsp; StartupInfo.hStdInput:= hStdIn;<br>&nbsp; StartupInfo.lpTitle:=@zAppName;<br>&nbsp; StartupInfo.dwFlags := StartupInfo.dwFlags or STARTF_USESTDHANDLES;<br><br>&nbsp; WriteConsoleInput(hStdIn,irMacroBuf,1,dwBytesWritten);<br><br>&nbsp; StartupInfo.dwFlags := StartupInfo.dwFlags or STARTF_USESHOWWINDOW;<br>&nbsp; StartupInfo.wShowWindow := Visibility;<br>&nbsp; if not CreateProcess(nil,<br>&nbsp; &nbsp; zAppName, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{ pointer to command line string }<br>&nbsp; &nbsp; nil, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; { pointer to process security attributes }<br>&nbsp; &nbsp; nil, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; { pointer to thread security attributes }<br>&nbsp; &nbsp; false, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; { handle inheritance flag }<br>&nbsp; &nbsp; CREATE_NEW_CONSOLE or &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{ creation flags }<br>&nbsp; &nbsp; NORMAL_PRIORITY_CLASS,<br>&nbsp; &nbsp; nil, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; { pointer to new environment block }<br>&nbsp; &nbsp; nil, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; { pointer to current directory name }<br>&nbsp; &nbsp; StartupInfo, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; { pointer to STARTUPINFO }<br>&nbsp; &nbsp; ProcessInfo) then Result := -1 { pointer to PROCESS_INF }<br><br>&nbsp; else begin<br>&nbsp; &nbsp; WaitforSingleObject(ProcessInfo.hProcess,INFINITE);<br>&nbsp; &nbsp; Result:=1;<br>&nbsp; end;<br>end;<br>
 
这个我知道<br>我想知道有没有什么api可以知道这个进程是正在运行,还是已经结束。
 
你运行上面的过程,在这个过程结束前,该进程都在运行。
 
我是要一个检测函数,运行一次就判断一次这个进程是正在运行,还是已经结束。
 
你加一个全局bool变量finished,在这个过程前赋值为false,过程后赋值为true,判断<br>finished的值就可以了。当然要把上面的过程放在一个单独的线程里。
 
扫描一下这个进程是否存在不就知道了?
 
如何“扫描一下这个进程是否存在不就知道了”
 
一个笨办法是枚举所有进程:<br>procedure TForm1.Button2Click(Sender: TObject);<br>type<br>integer = DWORD; // different versions of psapi.pas floating around<br>var<br>&nbsp; i,j,pidNeeded,modNeeded : Integer;<br>&nbsp; PIDList : array[0..1000] of Integer; // 1000 should be enough<br>&nbsp; MODList : array[0..1000] of HInst;<br>&nbsp; PIDName : array [0..MAX_PATH - 1] of char;<br>&nbsp; MODName : array [0..MAX_PATH - 1] of char;<br>&nbsp; PH : THandle;<br>begin<br><br>&nbsp; // fill an array with process ids<br>&nbsp; if not enumprocesses (@PIDList, 1000, pidNeeded) then<br>&nbsp; begin<br>&nbsp; &nbsp; ListBox1.Items.Add('Need psapi.dll');<br>&nbsp; &nbsp; exit;<br>&nbsp; end;<br><br>&nbsp; // now open each process and its modules<br>&nbsp; for i := 0 to (pidNeeded div sizeof (Integer)- 1) do<br>&nbsp; begin<br>&nbsp; &nbsp; // get a handle to the process<br>&nbsp; &nbsp; PH := OpenProcess (PROCESS_QUERY_INFORMATION or PROCESS_VM_READ, False,<br>&nbsp; &nbsp; PIDList);<br><br>&nbsp; &nbsp; if PH &lt;&gt; 0 then<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; // print the process name<br>&nbsp; &nbsp; &nbsp; if GetModuleBaseName (PH, 0, PIDName, sizeof (PIDName)) &gt; 0 then<br>&nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; ListBox1.Items.Add('process : ' + PIDName);<br><br>&nbsp; &nbsp; &nbsp; &nbsp; // fill an array of modules associated with this process<br>&nbsp; &nbsp; &nbsp; &nbsp; if not EnumProcessModules (PH,@MODList,1000, modNeeded) then modNeeded:= 0;<br><br>&nbsp; &nbsp; &nbsp; &nbsp; // print the modules in the list<br>&nbsp; &nbsp; &nbsp; &nbsp; for j := 0 to (modNeeded div sizeof (hInst) - 1) do<br>&nbsp; &nbsp; &nbsp; &nbsp; if GetModuleFileNameEx (PH, MODList[j], MODName,sizeof(MODName)) &gt; 0 then<br>&nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ListBox1.Items.Add(' module: ' + MODName);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if LowerCase(trim(ExtractFileName(ModName))) = LowerCase('MHK.dll') then Caption := ('Keyboard hook already running');<br>&nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; &nbsp; if PH &gt; 0 then CloseHandle(PH);<br>&nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; end;<br>&nbsp; end;<br>&nbsp; <br>end;<br><br>但其实用我上面说的方法就可以了,没必要搞这么复杂。
 
还是不明白你的意思<br>我只想知道我自己创建的进程在某时刻是在运行,还是已经结束
 
以下可以获得进程执行文件名:<br><br><br>85. 获得进程列表,并终止 Excel 进程<br>procedure TForm1.Button1Click(Sender: TObject);<br>var<br>&nbsp; FSnapshotHandle:THandle;<br>&nbsp; FProcessEntry32:TProcessEntry32;<br>&nbsp; Ret : BOOL;<br>&nbsp; ProcessID : integer;<br>&nbsp; s:string;<br>begin<br>&nbsp; FSnapshotHandle:=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);<br>&nbsp; FProcessEntry32.dwSize:=Sizeof(FProcessEntry32);<br>&nbsp; Ret:=Process32First(FSnapshotHandle,FProcessEntry32);<br>&nbsp; Memo1.clear;<br>&nbsp; while Ret do<br>&nbsp; begin<br>&nbsp; &nbsp; Memo1.lines.add(FProcessEntry32.szExeFile);<br>&nbsp; &nbsp; s:=ExtractFileName(FProcessEntry32.szExeFile);<br>&nbsp; &nbsp; if s='EXCEL.EXE' then<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; ProcessID:=FProcessEntry32.th32ProcessID;<br>&nbsp; &nbsp; &nbsp; TerminateProcess(OpenProcess(PROCESS_TERMINATE,false,ProcessID),1);<br>&nbsp; &nbsp; &nbsp; s:='';<br>&nbsp; &nbsp; end;<br>&nbsp; &nbsp; Ret:=Process32Next(FSnapshotHandle,FProcessEntry32);<br>&nbsp; end;<br>end;<br><br>
 
&gt;&gt;我只想知道我自己创建的进程在某时刻是在运行,还是已经结束<br>那用我前面说的方法就可以了。
 
顶部