Waitforsingleobject用于检测系统命令退出
Waitforsingleobject to detect a system command exit
嗨,我想为allegro执行一个系统命令,并等待它完成,因为我需要访问该命令生成的文件。我的代码不工作。有人能帮帮我吗?命令没有运行?如果我执行system(command.c_str())但不等待,它就会工作。
string command = "start allegro -expert -nographic -s " + fileName1 + " " + boardFile1;
bool ret;
bool retwait;
STARTUPINFO startupinfo;
GetStartupInfo (&startupinfo);
PROCESS_INFORMATION pro2info;
LPTSTR cmdL = (LPTSTR)command.c_str();
ret = CreateProcess(NULL, cmdL, NULL, NULL, false, CREATE_NEW_CONSOLE, NULL,NULL, &startupinfo, &pro2info);
cout<<"hProcess: "<<pro2info.hProcess<<endl;
cout<<"dwProcessId: "<<pro2info.dwProcessId <<endl;
//Want to wait till the command executes
while (retwait= WaitForSingleObject (pro2info.hProcess, INFINITE)!=WAIT_OBJECT_0)
cout<<"waitprocess:true"<<endl; //The process is finished;
CloseHandle (pro2info.hProcess);
我认为您不需要在命令开始时使用"start",即使您正在使用标志CREATE_NEW_CONSOLE
。"start"适用于system
(因为system
适用于批处理命令),但是如果您正在创建一个新进程,您应该只指定映像文件的路径。顺便说一下,知道发生了什么事的唯一方法是检查CreateProcess
(最终是GetLastError()
)的返回。
同样,这也不是使用WaitForSingleObject
的最佳方式。你应该抓住WAIT_FAILED
并使用GetLastError()
,否则你的下一篇关于SO的文章将是:"为什么我的程序不等待?": -)
相关文章:
- 提供变量作为 MATLAB 系统命令的输入参数,以便C++可执行文件
- 将系统命令打印到文本文件 C++
- 使用系统命令时发送输入不起作用
- 有没有一种简单的方法可以将系统命令的输出转换为字符串
- 如何将 c ++ 变量的值传递给 hadoop HDFS 的 bash 系统命令?
- 来自C 应用程序的运行系统命令与Commandline不同
- CPP中的管道内部(AWK)系统命令
- C++:如何运行一个系统命令N次(异步)并返回N次执行时间
- 使用 C++ 更改注册表值?(系统命令失败)
- 使用系统命令调用可执行文件
- c++系统命令
- 如何在C++中通过将每个命令分配给线程来同时执行linux系统命令
- C++函数系统(命令)的输出在Linux终端中不显示颜色
- 使用Windows技术检测复制,粘贴,打印系统命令
- 使用系统命令从 Windows 下的 C 程序内部更改目录
- 有没有办法以给定的优先级执行系统命令
- 在Qt创建器运行系统命令中
- 如何使用system()在c中执行2个成功的系统命令,例如连接到ftp并使用程序登录
- C++重置系统命令
- Waitforsingleobject用于检测系统命令退出