C++ MFC ShellExecute执行脚本

目前知道三种方式:WinExec,ShellExecute ,CreateProcess。


1. 阻塞式调用,调用时进程阻塞

    

void CMFCcodeDlg::CallShellScript(CString path, CString fbat, CString param)
{
	USES_CONVERSION; 

	SHELLEXECUTEINFO   ShExecInfo   =   {0}; 
	ShExecInfo.cbSize   =   sizeof(SHELLEXECUTEINFO); 
	ShExecInfo.fMask   =   SEE_MASK_NOCLOSEPROCESS; 
	ShExecInfo.hwnd   =   NULL; 
	ShExecInfo.lpVerb   =   NULL; 
	ShExecInfo.lpFile   =  fbat;
	ShExecInfo.lpParameters   =  param; 
	ShExecInfo.lpDirectory   =   path; 
	ShExecInfo.nShow   =   SW_HIDE; 
	ShExecInfo.hInstApp   =   NULL; 
	ShellExecuteEx(&ShExecInfo); 

	// 等待脚本返回
	WaitForSingleObject(ShExecInfo.hProcess,INFINITE); 
}

CallShellScript(NULL, batFile, allparam); //TODO: check path



2. 多线程调用

    

DWORD WINAPI CallBatThreadFunc(HANDLE Thread)
{
	// 调用脚本文件
	SHELLEXECUTEINFO   ShExecInfo   =   {0}; 
	ShExecInfo.cbSize   =   sizeof(SHELLEXECUTEINFO); 
	ShExecInfo.fMask   =   SEE_MASK_NOCLOSEPROCESS; 
	ShExecInfo.hwnd   =   NULL; 
	ShExecInfo.lpVerb   =   NULL; 
	// TODO: 添加bat文件的完整路径
	ShExecInfo.lpFile   =   _T("call.bat");
	ShExecInfo.lpParameters   =   g_strPort; 
	ShExecInfo.lpDirectory   =   NULL; 
	ShExecInfo.nShow   =   SW_HIDE; 
	ShExecInfo.hInstApp   =   NULL; 
	ShellExecuteEx(&ShExecInfo); 

	// 等待脚本返回
	WaitForSingleObject(ShExecInfo.hProcess,INFINITE); 
	
	return 0;
}



	HANDLE Thread;
	DWORD dwThreadId;
	//调用脚本文件
	Thread=::CreateThread(NULL,0,CallBatThreadFunc,NULL,0,&dwThreadId);

    

你可能感兴趣的:(MFC)