参数传递是很奇怪,有时候会无法启动,比如:
CreateProcess("c:\\a.exe","aa bb cc",....),显示出错
通过CreateProcess("c:\\a.exe", "\"c:\\a.exe\" c:\\a.exe aa bb cc"...)即可.
HANDLE hRead,hWrite; SECURITY_ATTRIBUTES sa; sa.nLength=sizeof(SECURITY_ATTRIBUTES); sa.bInheritHandle=true; sa.lpSecurityDescriptor=NULL; if(!::CreatePipe(&hRead,&hWrite,&sa,0)) { AfxMessageBox("CreatePipe failed!"); return false; } STARTUPINFO si; si.cb=sizeof(STARTUPINFO); GetStartupInfo(&si); si.dwFlags=STARTF_USESHOWWINDOW|STARTF_USESTDHANDLES; si.wShowWindow=SW_HIDE;//这里设置是否显示DOS窗口 si.hStdError=hWrite; si.hStdOutput=hWrite; si.hStdInput=GetStdHandle(STD_INPUT_HANDLE); PROCESS_INFORMATION pi; LPSTR lpszCmdLine="cmd sname ...";//这里你自己修改吧。 if(!CreateProcess(NULL,lpszCmdLine,NULL,NULL,TRUE,0,NULL,0,&si,&pi)) { AfxMessageBox("CreateProcess failed!"); CloseHandle(hRead); CloseHandle(hWrite); return false; } CloseHandle(hWrite); if(WaitForSingleObject(pi.hProcess,5000)==WAIT_TIMEOUT) { AfxMessageBox("Timeout!"); TerminateThread(pi.hThread,1); TerminateProcess(pi.hProcess,1); CloseHandle(hRead); return false; } char buf[4096]={ 0}; DWORD bytes; if(::ReadFile(hRead,buf,4095,&bytes,NULL)==NULL)//读取程序输出 { AfxMessageBox("ReadFile Failed!"); CloseHandle(pi.hThread); CloseHandle(pi.hProcess); CloseHandle(hRead); return false; } else { Result=buf; } CloseHandle(pi.hThread); CloseHandle(pi.hProcess); CloseHandle(hRead); return true;用CreateProcess创建进程,用管道读取程序输出。用GetExitCodeProcess读取程序退出代码