转自:http://blog.csdn.net/weekdawn/article/details/73468334
函数名:OnBnClickedBtnOpenExe
函数功能:通过点击一个按钮运行exe程序,并且等待该exe运行结束
参数:无
返回值:void
额外说明:可以给exe程序传入命令行参数
作者:weekdawn
- void CTestDlg::OnBnClickedBtnOpenExe()
- {
-
- ShExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);
- ShExecInfo.fMask = SEE_MASK_NOCLOSEPROCESS ;
- ShExecInfo.hwnd = this -> m_hWnd;
- ShExecInfo.lpVerb = _T("open");
-
- ShExecInfo.lpFile = _T("qr_scan.exe");
-
- ShExecInfo.lpParameters = _T("test");
-
-
-
- TCHAR exeFullPath[200];
- GetModuleFileName(NULL,exeFullPath,100);
-
- TCHAR drive[100],dir[100],fname[100],ext[20];
- _wsplitpath(exeFullPath,drive,dir,fname,ext);
- CString dirPath;
- dirPath.Format(_T("%s%s"),drive,dir);
-
-
- ShExecInfo.lpDirectory = dirPath;
- ShExecInfo.nShow = SW_HIDE;
- ShExecInfo.hInstApp = NULL;
-
- ShellExecuteEx(&ShExecInfo);
-
- if( ShExecInfo.hProcess != NULL)
- {
-
- WaitForSingleObject(ShExecInfo.hProcess, INFINITE);
-
- TerminateProcess(ShExecInfo.hProcess,0);
- ShExecInfo.hProcess = NULL;
- }
- }