假设需要执行的程序如下:
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/Files/humanchao/ExecExe.rar
posted on 2007-12-28 11:20
胡满超 阅读(5954)
评论(4) 编辑 收藏 引用