visual-c – 如何等待ShellExecute运行?

我已经设法在VC中使用ShellExecute来启动文档。
现在我希望运行一个接收一些参数的命令行工具,并在后台运行(作为隐藏的,没有最小化的),并阻止我的程序流程,这样我就可以等到它完成了。
如何更改命令行:

ShellExecute(NULL,"open",FULL_PATH_TO_CMD_LINE_TOOL,ARGUMENTS,NULL,SW_HIDE);

问题是,我有工具把html转换成pdf,我希望一旦工具完成,还有一个pdf准备好了,另一个ShellExecute来查看它。

有一个CodeProject article显示如何使用ShellExecuteEx而不是ShellExecute:

SHELLEXECUTEINFO ShExecInfo = {0};
ShExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);
ShExecInfo.fMask = SEE_MASK_NOCLOSEPROCESS;
ShExecInfo.hwnd = NULL;
ShExecInfo.lpVerb = NULL;
ShExecInfo.lpFile = "c:\\MyProgram.exe";        
ShExecInfo.lpParameters = "";   
ShExecInfo.lpDirectory = NULL;
ShExecInfo.nShow = SW_SHOW;
ShExecInfo.hInstApp = NULL; 
ShellExecuteEx(&ShExecInfo);
WaitForSingleObject(ShExecInfo.hProcess,INFINITE);

关键点是标志SEE_MASK_NOCLOSEPROCESS,其中,as MSDN says

Use to indicate that the hProcess member receives the process handle. This handle is typically used to allow an application to find out when a process created with ShellExecuteEx terminates

另外请注意:

The calling application is responsible for closing the handle when it is no longer needed.

翻译自:https://stackoverflow.com/questions/17638674/how-to-wait-for-shellexecute-to-run

转载注明原文:visual-c – 如何等待ShellExecute运行?