renegat
February 12th, 2009, 12:12 PM
Hello. I'm developing my program in c++ in which I'm using cmd.exe; I know that this is command line. In my program I'm creating process and connecting it to my program with pipes (to have access to std out i std in of cmd.exe.
The only problem is that after running cmd.exe with parameters (/C dir for example) process ends. Is there any possibility to make cmd.exe not to end after one command?
Igor Vartanov
February 12th, 2009, 04:09 PM
cmd.exe /K dir
renegat
February 12th, 2009, 07:08 PM
Thanks. It is true what you wrote. After your reply I've checked if that could be that simple. It is supposed to work. But it doesn't (why?). Here's my code.
I found out that cmd.exe terminates prematurely when I'm using pipes. Otherwise it's working as it should.
void shell::createPipes() {
SECURITY_ATTRIBUTES saAttr;
saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
saAttr.bInheritHandle = TRUE;
saAttr.lpSecurityDescriptor = NULL;
CreatePipe( &outread, &outwrite, &saAttr, 0) ;
SetHandleInformation(outread, HANDLE_FLAG_INHERIT, 0);
CreatePipe(&inread, &inwrite, &saAttr, 0);
SetHandleInformation(inread, HANDLE_FLAG_INHERIT, 0);
}
bool shell::runCmd(string params,bool wait) {
STARTUPINFO siStartInfo;
BOOL bSuccess = FALSE;
ZeroMemory( &piProcInfo, sizeof(PROCESS_INFORMATION) );
ZeroMemory( &siStartInfo, sizeof(STARTUPINFO) );
siStartInfo.cb = sizeof(STARTUPINFO);
siStartInfo.hStdError = outwrite;
siStartInfo.hStdOutput = outwrite;
siStartInfo.hStdInput = inread;
siStartInfo.dwFlags |= STARTF_USESTDHANDLES| STARTF_USESHOWWINDOW;
siStartInfo.wShowWindow = SW_HIDE;
siStartInfo.dwFlags |= STARTF_USESHOWWINDOW;
siStartInfo.wShowWindow = SW_HIDE;
bSuccess = CreateProcess(...);
//...
Also i found article http://www.codeguru.com/cpp/misc/misc/article.php/c277
I tried using using this code too , but with creation flag DETACHED_PROCESS cmd.exe ends even without redirected handles.
Nevertheless I think my code isn't bad. It comes from http://msdn.microsoft.com/en-us/library/ms682499(VS.85).aspx and It is supposed to work.