C++啟動一個程式

喻大帥發表於2016-08-31

呼叫CreateProcess

STARTUPINFO si;
PROCESS_INFORMATION pi;
si.dwFlags =   STARTF_USESHOWWINDOW;
si.wShowWindow = SW_SHOWNORMAL;
ZeroMemory( &si, sizeof(si) );
si.cb = sizeof(si);
ZeroMemory( &pi, sizeof(pi) );
if(!CreateProcess(NULL, (LPTSTR)(LPCTSTR) sExeFilePathAndName,NULL,NULL,FALSE,CREATE_NEW_CONSOLE,NULL,NULL,&si, &pi ))
{
	AfxMessageBox("自動升級程式AutoUPDATe.Exe檔案不存在或損壞!");
	return FALSE;
}
CloseHandle(pi.hThread); // 關閉子程式的主執行緒控制程式碼
WaitForSingleObject(pi.hProcess, INFINITE); // 等待子程式的退出
// 獲取子程式的退出碼
DWORD dwExitCode;
GetExitCodeProcess(pi.hProcess, &dwExitCode);
CloseHandle(pi.hProcess); //關閉子程式控制程式碼

呼叫ShellExecute

ShellExecute(NULL,"open","EXE路徑",NULL,NULL,SW_SHOW);

SHELLEXECUTEINFO ShExecInfo = {0};
ShExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);
ShExecInfo.fMask = SEE_MASK_NOCLOSEPROCESS;
ShExecInfo.hwnd = NULL;
ShExecInfo.lpVerb = NULL;
ShExecInfo.lpFile = strExe; 
ShExecInfo.lpParameters = sParams;  
ShExecInfo.lpDirectory = NULL;
ShExecInfo.nShow = SW_HIDE;
ShExecInfo.hInstApp = NULL;
BOOL ret = ShellExecuteEx(&ShExecInfo);
WaitForSingleObject(ShExecInfo.hProcess,INFINITE);

相關文章