启动进程并等待结束:
方法1, 用ShellExecuteEx函数
SHELLEXECUTEINFO sei;
// 启动进程
ZeroMemory(&sei, sizeof(SHELLEXECUTEINFO));
sei.cbSize = sizeof(SHELLEXECUTEINFO);
sei.fMask = SEE_MASK_NOCLOSEPROCESS;
sei.hwnd = NULL;
sei.lpVerb = NULL;
sei.lpFile = "Calc.exe";
sei.lpParameters = "";
sei.lpDirectory = NULL;
sei.nShow = SW_SHOW;
sei.hInstApp = NULL;
ShellExecuteEx(&sei);
// 加入下面这句就是等待该进程结束
WaitForSingleObject(ShExecInfo.hProcess, INFINITE);
// 也可以直接关闭这个进程,只要保留sei.hProcess就行
TerminateProcess(sei.hProcess, 0);
方法2, 用CreateProcess函数
PROCESS_INFORMATION pi;
STARTUPINFO si;
ZeroMemory(&si, sizeof(si));
si.cb = sizeof(STARTUPINFO);
if(CreateProcess("D:\\Winnt\\Notepad.exe", NULL,
NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi))
{
WaitForSingleObject(pi.hProcess, INFINITE);
CloseHandle(pi.hThread);
CloseHandle(pi.hProcess);
}
else
{
MessageBox(Handle, "无法启动进程!", "Error", MB_OK);
}
另外:
VOID ExitProcess(UINT uExitCode); // exit code for all threads
uExitCode是退出码, 也就是返回值, 返回给操作系统.一般根据返回值判断运行是否成功.