如何等待 ShellExecute 运行? [英] How to wait for ShellExecute to run?

查看:58
本文介绍了如何等待 ShellExecute 运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设法在 VC++ 中使用 ShellExecute 来启动文档.现在我希望运行一个命令行工具来接收一些参数,并在后台运行(隐藏的,而不是最小化的)并让它阻塞我的程序流,这样我就可以等待它完成.如何更改以下命令行:

I have manages to use ShellExecute in VC++ in order to launch a document. Now I wish to run a command-line tool that receives some arguments, and to run in the background (as hidden, not minimized) and let it block my program flow, so that i'll be able to wait for it to finish. How to i alter the command-line of:

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

问题是,我有将 html 转换为 pdf 的工具,我希望一旦该工具完成,也就是 pdf 准备就绪,有另一个 ShellExecute 来查看它.

The problem is, I have tool that converts html to pdf, and I wish that once the tool finished, aka pdf is ready, to have another ShellExecute to view it.

推荐答案

有一个 CodeProject 文章 展示了如何使用 ShellExecuteEx 而不是 ShellExecute:

There is a CodeProject article that shows how, by using ShellExecuteEx instead of 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);
CloseHandle(ShExecInfo.hProcess);

关键点是标志 SEE_MASK_NOCLOSEPROCESS正如 MSDN 所说

用于指示 hProcess 成员接收进程句柄.此句柄通常用于允许应用程序找出使用 ShellExecuteEx 创建的进程何时终止

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.

这篇关于如何等待 ShellExecute 运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆