假设需要执行的程序如下:
int main(int argc, char* argv[])
{
return argc;
}
执行它,并取得其返回值,我写了一个函数如下:
DWORD WinExecAndWait32( LPCTSTR lpszAppPath, // 执行程序的路径
LPCTSTR lpParameters, // 参数
LPCTSTR lpszDirectory, // 执行环境目录
DWORD dwMilliseconds) // 最大等待时间, 超过这个时间强行终止
{
SHELLEXECUTEINFO ShExecInfo = {0};
ShExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);
ShExecInfo.fMask = SEE_MASK_NOCLOSEPROCESS;
ShExecInfo.hwnd = NULL;
ShExecInfo.lpVerb = NULL;
ShExecInfo.lpFile = lpszAppPath;
ShExecInfo.lpParameters = lpParameters;
ShExecInfo.lpDirectory = lpszDirectory;
ShExecInfo.nShow = SW_HIDE;
ShExecInfo.hInstApp = NULL;
ShellExecuteEx(&ShExecInfo);
// 指定时间没结束
if (WaitForSingleObject(ShExecInfo.hProcess, dwMilliseconds) == WAIT_TIMEOUT)
{ // 强行杀死进程
TerminateProcess(ShExecInfo.hProcess, 0);
return 0; //强行终止
}
DWORD dwExitCode;
BOOL bOK = GetExitCodeProcess(ShExecInfo.hProcess, &dwExitCode);
ASSERT(bOK);
return dwExitCode;
}
我上传了两个工程,希望对大家有所帮助!
下载本文转自:
http://www.cppblog.com/humanchao/archive/2007/12/28/39815.html
posted on 2012-07-10 08:28
王海光 阅读(767)
评论(0) 编辑 收藏 引用 所属分类:
MFC