我现在是用ShellExecute开启了一段程序,不知道怎么关闭,才最好?

我现在是用ShellExecute开启了一段程序,不知道怎么关闭,才最好?,第1张

ShellExecute开启的程序,不是很好控制,建议用CreateProcess开启程序,利用返回的piProcinfo参数使用TerminateProcess来关闭程序。

bFuncRetn = CreateProcess(NULL,

szCmdline, // command line

NULL, // process security attributes

NULL, // primary thread security attributes

TRUE, // handles are inherited

0, // creation flags

NULL, // use parent's environment

NULL, // use parent's current directory

&siStartInfo, // STARTUPINFO pointer

&piProcInfo) // receives PROCESS_INFORMATION

你是说用shellexecute运行了另外一个程序吧?这样运行的程序是独立于你自己的程序的,如果能获得程序窗口的句柄,向窗口发送关闭消息来关闭它。或者用TerminateProcess来强行结束进程。

用ShellExecuteEx可以实现你要求的功能。

SHELLEXECUTEINFO si

memset(&si, 0, sizeof(si))

si.cbSize = sizeof(si)

si.hwnd = NULL

si.lpVerb = _T("open")

si.lpFile =m_strInstallFile

si.lpParameters=NULL

si.nShow = SW_SHOWNORMAL

si.fMask = SEE_MASK_NOCLOSEPROCESS

if (!ShellExecuteEx(&si))

{

MessageBox(NULL,_T("error"),_T("test"),0)

}

WaitForSingleObject(si.hProcess,INFINITE)

//当cmd关闭后,会走到这里。。。

CloseHandle(si.hProcess)

si.hProcess=NULL


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/yw/12025604.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-20
下一篇 2023-05-20

发表评论

登录后才能评论

评论列表(0条)

保存