关于如何在c++中启动外部的exe程序,之前看到在百度一搜就看到了:
ShellExecute(this->m_hWnd,"open","calc.exe","","", SW_SHOW );
经验证果然可以,一条语句直接就启动了。之后我想在我的代码结束时也把这个exe程序给关闭了,按照网上的做法直接TerminateProcess(HINSTANCE,0)就不行了,参数根本就不能是HINSTANCE类型;然后使用sendMessage(WM_Close,...)也不行,原因应该是我的exe程序根本就没有窗口;最后使用了ShellExecuteEx,总算搞定了。
附上代码:
启动:
SHELLEXECUTEINFO ShExecInfo;
ShExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);
ShExecInfo.fMask = SEE_MASK_NOCLOSEPROCESS ;
ShExecInfo.hwnd = NULL;
ShExecInfo.lpVerb = NULL;
ShExecInfo.lpFile = "xxx.exe"; //can be a file as well
ShExecInfo.lpParameters = "";
ShExecInfo.lpDirectory = NULL;
ShExecInfo.nShow = SW_SHOW;
ShExecInfo.hInstApp = NULL;
ShellExecuteEx(&ShExecInfo);
关闭:
if( ShExecInfo.hProcess != NULL)
{
TerminateProcess(ShExecInfo.hProcess,0);
ShExecInfo.hProcess = NULL;
}