假設需要執行的程序如下:
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.shnenglu.com/humanchao/archive/2007/12/28/39815.html
posted on 2012-07-10 08:28
王海光 閱讀(774)
評論(0) 編輯 收藏 引用 所屬分類:
MFC