从 Win32 应用程序将进程作为同步操作运行
我有一个现有的实用程序应用程序,我们将其称为 util.exe。它是一个命令行工具,它从命令行获取输入,并在磁盘上创建一个文件,比如说一个图像文件,
我想通过运行 util.exe 在另一个应用程序中使用它。但是它需要同步,以便在处理继续时知道文件存在。
例如 (psudeo)
bool CreateImageFile(params)
{
//ret is util.exe program exit code
int ret = runprocess("util.exe",params);
return ret==0;
}
是否有一个 Win32 API 调用将运行该进程并等待其结束?我查看了 CreateProcess,但它一尝试启动就返回,我查看了 ShellExecute,但这看起来有点难看,即使它是同步的。
I have an existing utility application, let's call it util.exe. It's a command-line tool which takes inputs from the command line, and creates a file on disk, let's say an image file
I want to use this within another application, by running util.exe. However it needs to be synchronous so the file is known to exist when processing continues.
e.g (psudeo)
bool CreateImageFile(params)
{
//ret is util.exe program exit code
int ret = runprocess("util.exe",params);
return ret==0;
}
Is there a single Win32 API call that will run the process and wait until it ends? I looked at CreateProcess but it returns as soon as it tries to start, I looked at ShellExecute but that seems a bit ugly even it were synchronous.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
没有单一的 api,但这实际上是 Win32 应用程序的一个更有趣的一般问题。您可以在进程句柄上使用 CreateProcess 或 ShellExecuteEx 和 WaitForSingleObject。此时 GetExitCodeProcess 将为您提供程序的退出代码。有关简单的示例代码,请参阅此处。
然而,这会完全阻塞您的主线程,并可能在某些 Win32 消息传递场景下给您带来严重的死锁问题。假设生成的 exe 执行广播发送消息。在所有窗口都处理完该消息之前它无法继续 - 但您无法继续,因为您在等待它时被阻止。僵局。由于您使用的是纯粹的命令行程序,因此这个问题可能不适用于您。您关心命令行程序是否挂起一段时间吗?
对于普通应用程序来说,最好的通用解决方案可能是将进程启动和等待拆分到一个线程上,并在线程运行完成时将消息发送回主窗口。当您收到消息时,您知道可以安全地继续,并且不存在死锁问题。
There's no single api, but this is actually a more interesting general question for Win32 apps. You can use CreateProcess or ShellExecuteEx and WaitForSingleObject on the process handle. GetExitCodeProcess at that point will give you the program's exit code. See here for simple sample code.
However this blocks your main thread completely, and can give you serious deadlock problems under some Win32 messaging scenarios. Let's say the spawned exe does a broadcast sendmessage. It can't proceed until all windows have processed the message - but you can't proceed because you're blocked waiting for it. Deadlock. Since you're using purely command line programs this issue probably doesn't apply to you though. Do you care if a command line program hangs for a while?
The best general solution for normal apps is probably to split a process launch-and-wait off onto a thread and post a message back to your main window when the thread runs to completion. When you receive the message, you know it is safe to continue, and there are no deadlock issues.
进程句柄是一个可等待的对象,AFAIK。这正是您所需要的。
但是,我建议不要这样做。在 Windows 上启动进程可能会很慢,并且会阻塞您的 UI。考虑使用具有 50 毫秒等待超时的 PeekMessage 循环从 Windows 应用程序中执行此操作。
Process handle is a waitable object, AFAIK. This is exactly what you need.
However, I'd recommend against doing anything like that. Starting process on windows may be slow and it will block your UI. Consider a PeekMessage loop with 50ms wait timeouts to do it from a windows application.