关于CreateProcess函数的使用(200分)

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

whitelion

Unregistered / Unconfirmed
GUEST, unregistred user!
&nbsp; &nbsp; &nbsp; &nbsp;STARTUPINFO * mystartupinfo=new STARTUPINFO;<br>&nbsp; &nbsp; &nbsp; &nbsp; PROCESS_INFORMATION * myprocess_information=new PROCESS_INFORMATION;<br>&nbsp; &nbsp; &nbsp; &nbsp; mystartupinfo-&gt;dwFlags=STARTF_USESHOWWINDOW;<br>&nbsp; &nbsp; &nbsp; &nbsp; mystartupinfo-&gt;wShowWindow=SW_SHOWNORMAL;<br>&nbsp; &nbsp; &nbsp; &nbsp; if (!<br>&nbsp; &nbsp; &nbsp; &nbsp; CreateProcess(NULL,cargu,NULL,NULL,true,<br>&nbsp; &nbsp; &nbsp; &nbsp; CREATE_DEFAULT_ERROR_MODE &nbsp;NORMAL_PRIORITY_CLASS CREATE_NEW_CONSOLE,<br>&nbsp; &nbsp; &nbsp; &nbsp; NULL,NULL,mystartupinfo,myprocess_information))<br>&nbsp; &nbsp; &nbsp; &nbsp; {<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ShowMessage("压缩失败,系统进行第二套方案...");<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; delete mystartupinfo;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; delete myprocess_information;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return;<br>&nbsp; &nbsp; &nbsp; &nbsp; }<br>&nbsp; &nbsp; &nbsp; &nbsp; else<br>&nbsp; &nbsp; &nbsp; &nbsp; {<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; WaitForSingleObject(myprocess_information-&gt;hProcess,INFINITE);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; GetExitCodeProcess(myprocess_information,&amp;s);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; delete mystartupinfo;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; delete myprocess_information;<br>&nbsp; &nbsp; &nbsp; &nbsp; }<br>以上是一段bcb代码,为什么这个函数有时候好用,有什么不好用(可能在第一次执行时好用,但连续运行几次后就不能运行了)
 
看看我在http://www.delphibbs.com/delphibbs/dispq.asp?lid=688273<br>中回答,我也用到了CreateProcess函数,希望对你有所帮助
 
首先你必须明确一点:正确使用CreateProcess()函数执行外部命令必须结合WaitForSingle()函数。<br>同时注意调用SetEnvironmentVariable()来设置相关的环境变量。<br>接下来,我们来具体看你的程序代码,其实你可以尝试设定一个变量用来检测进程是否触发,<br>自动判断后触发进程。 <br>你应该加入OnTimer事件并设定一个Boolean,如processExit:boolean;<br>并令processExit:=false; <br>&nbsp; &nbsp; processExit:=GetExitCodeProcess(myprocess_information,&amp;s); <br>这样通过(processExit and 进程终止时的退出码(即lpExitCode)&lt;&gt;STILL_ACTIVE)方可正确判<br>断进程的中止。然后在程序结尾再调用CreateProcess重新创建进程。方可正确进行继续的操作。<br>所以最好的方法是单独定义一个过程(进程调用),每次中止结束进程后再次调用该过程即可!<br><br>
 
多人接受答案了。
 
后退
顶部