如何使用WaitForMultipleObjects和GetExitCodeProcess函数(50分)

  • 主题发起人 主题发起人 kitty_chen
  • 开始时间 开始时间
K

kitty_chen

Unregistered / Unconfirmed
GUEST, unregistred user!
WaitForMultipleObjects和GetExitCodeProcess函数的参数<br>搞不太懂,不知哪位高手能详细解说一下,最好能给我举个例子?
 
GetExitCodeProcess<br>The GetExitCodeProcess function retrieves the termination status of the specified process. <br><br>BOOL GetExitCodeProcess(<br>&nbsp; HANDLE hProcess, &nbsp; &nbsp; // handle to the process<br>&nbsp; LPDWORD lpExitCode &nbsp; // address to receive termination status<br>);<br>&nbsp;<br>Parameters<br>hProcess <br>Handle to the process. <br>Windows NT: The handle must have PROCESS_QUERY_INFORMATION access. <br><br>lpExitCode <br>Pointer to a 32-bit variable to receive the process termination status. <br>Return Values<br>If the function succeeds, the return value is nonzero.<br><br>If the function fails, the return value is zero. To get extended error information, call GetLastError. <br><br>Remarks<br>If the specified process has not terminated, the termination status returned is STILL_ACTIVE. If the process has terminated, the termination status returned may be one of the following: <br><br>The exit value specified in the ExitProcess or TerminateProcess function. <br>The return value from the main or WinMain function of the process. <br>The exception value for an unhandled exception that caused the process to terminate. <br>QuickInfo<br>&nbsp; Windows NT: Requires version 3.1 or later.<br>&nbsp; Windows: Requires Windows 95 or later.<br>&nbsp; Windows CE: Requires version 1.0 or later.<br>&nbsp; Header: Declared in winbase.h.<br>&nbsp; Import Library: Use kernel32.lib.<br><br>
 
GetExitCodeProcess是要结束进程是调用的,由于一个进程含有多个线程,所以直接<br>关闭进程是比较不安全的,最好通过关闭该进程下的所有线程。
 
sorry,是访问进程的返回码;<br>waitformultipleobjects,是使线程同步,当多个线程对同一个物体进行操作时,需<br>要等待其它线程结束才能执行时才用。
 
我是对这两个函数的参数搞不太懂,运行时出错,能否给我举个例子?
 
可否把你的程序帖出来瞧瞧。
 
我的程序:<br>Function TForm1.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>begin<br>&nbsp; StrPCopy(zAppName,FileName);<br>&nbsp; GetDir(0,WorkDir);<br>&nbsp; StrPCopy(zCurDir,WorkDir);<br>&nbsp; FillChar(StartupInfo,Sizeof(StartupInfo),#0);<br>&nbsp; StartupInfo.cb:=Sizeof(StartupInfo);<br><br>&nbsp; StartupInfo.dwFlags:=STARTF_USESHOWWINDOW;<br>&nbsp; StartupInfo.wShowWindow:=Visibility;<br>&nbsp; if not CreateProcess(nil,<br>&nbsp; &nbsp; zAppName, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//待执行程序名<br>&nbsp; &nbsp; nil, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //进程安全级别<br>&nbsp; &nbsp; nil, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //线程安全级别<br>&nbsp; &nbsp; false, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //继承标志<br>&nbsp; &nbsp; CREATE_NEW_CONSOLE or &nbsp; //创建标志<br>&nbsp; &nbsp; NORMAL_PRIORITY_CLASS,<br>&nbsp; &nbsp; nil, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//pointer to new environment block<br>&nbsp; &nbsp; nil, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //当前路径<br>&nbsp; &nbsp; StartupInfo, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //STARTUPINFO 纪录信息<br>&nbsp; &nbsp; ProcessInfo) then Result:= -1 &nbsp;//PROCESS_INF 纪录信息<br>&nbsp; else begin<br>&nbsp; &nbsp; WaitforSingleObject(ProcessInfo.hProcess,INFINITE);<br>&nbsp; &nbsp; GetExitCodeProcess(ProcessInfo.hProcess,Result);<br>&nbsp; end;<br>end;<br>编译时出错误提示:<br>GetExitCodeProcess(ProcessInfo.hProcess,Result);<br>Type of actual and formal var parameters must be identical<br><br>另外我如果用此函数调用计算器WaitforSingleObject(ProcessInfo.hProcess,INFINITE)没有问题,<br>但调Foxpro的EXE &nbsp;WaitforSingleObject函数会死机,<br>所以我想用waitformultipleobjects函数试试,但参数不太明白。<br>
 
参数类型给错了,可以试试如下:<br>var MyResult:DWord;<br>GetExitCodeProcess(ProcessInfo.hProcess,MyResult);
 
To Kill Night:<br>&nbsp; &nbsp; 我试了如下:<br>&nbsp; &nbsp; &nbsp;var MyResult:DWord;<br>&nbsp; &nbsp; &nbsp;GetExitCodeProcess(ProcessInfo.hProcess,MyResult);<br>没有问题了,谢谢!<br>不知哪位高手能给我举个waitformultipleobjects函数的例子?
 
其实WaitforSingleObject和waitformultipleobjects有着明显的区别,下面有一<br>段C的程序:<br>HANDLE g_hEventsProcIdle[10]<br>WaitForMultipleObjects(10,g_hEventsProcIdle,TRUE,INFINITE);<br>WaitForMultipleObjects需要知道所有的线程号,还是建议用WaitforSingleObject。
 
接受答案了.
 

Similar threads

后退
顶部