方式一:使用ShellExecuteEx创建外部进程,使用TerminateProcess杀死进程。
SHELLEXECUTEINFO ShExecInfo;
ShExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);
ShExecInfo.fMask = SEE_MASK_NOCLOSEPROCESS;
ShExecInfo.hwnd = NULL;
ShExecInfo.lpVerb = NULL;
ShExecInfo.lpFile = "ffplay.exe";
ShExecInfo.lpParameters = "-i 1444.mp4";
ShExecInfo.lpDirectory = NULL;
ShExecInfo.nShow = SW_SHOW;
ShExecInfo.hInstApp = NULL;
ShellExecuteEx(&ShExecInfo);
Sleep(3000);
if (ShExecInfo.hProcess != NULL)
{
TerminateProcess(ShExecInfo.hProcess, 0);
ShExecInfo.hProcess = NULL;
}
方式二:使用CreateProcess创建子进程,使用TerminateProcess杀死进程。
TCHAR szCommandLine[MAX_PATH];
memset(szCommandLine, 0, sizeof(szCommandLine));
lstrcpy(szCommandLine, "ffplay.exe ");//要启动的进程
lstrcat(szCommandLine, "-i 1444.mp4");//传给刚启动的进程的参数
STARTUPINFO si = { sizeof(si) };
PROCESS_INFORMATION pi;
BOOL bRet = CreateProcess(
NULL,//NULL, // name of executable module
szCommandLine, // command line string
NULL, // process attributes
NULL, // thread attributes
FALSE, // handle inheritance option
CREATE_UNICODE_ENVIRONMENT | CREATE_NEW_CONSOLE,//0, UNICODE版本下 // creation flags
NULL, // new environment block
NULL, // current directory name
&si, // startup information
&pi); // process information
if (bRet)
{
Sleep(5000);
TerminateProcess(pi.hProcess, 0);
CloseHandle(pi.hThread);// 关闭子进程的主线程句柄
CloseHandle(pi.hProcess);// 关闭子进程句柄
}
注意:使用ShellExecute方法可以创建外部进程,但是没有找到对应杀死进程的方法。
HINSTANCE hInstance = ShellExecute(NULL, "open", "ffplay.exe", "-i 1444.mp4", "", SW_SHOWNORMAL);