redirecting output from console?
-
I use some console application running it from inside VC project and I want to redirect its output to file bool runprocess(wchar_t *cmnd) { SECURITY_ATTRIBUTES sattr; memset(&sattr,0,sizeof(sattr)); sattr.nLength = sizeof(sattr); sattr.bInheritHandle = true; STARTUPINFO sInfo; memset(&sInfo,0,sizeof(sInfo)); PROCESS_INFORMATION pInfo; memset(&pInfo,0,sizeof(pInfo)); sInfo.cb = sizeof(sInfo); sInfo.dwFlags = STARTF_USESTDHANDLES; bool res = CreateProcess(0,cmnd,0,0,true,NORMAL_PRIORITY_CLASS|CREATE_NO_WINDOW,0,0,&sInfo,&pInfo); if(res) WaitForSingleObject(pInfo.hProcess, INFINITE); return res; } runprocess(L"consoleapp.exe >output"); But the file output is not created though consoleapp worked normally? How to get its output without reading hStdInput, hStdOutput from STARTUPINFO structure?
9ine
-
I use some console application running it from inside VC project and I want to redirect its output to file bool runprocess(wchar_t *cmnd) { SECURITY_ATTRIBUTES sattr; memset(&sattr,0,sizeof(sattr)); sattr.nLength = sizeof(sattr); sattr.bInheritHandle = true; STARTUPINFO sInfo; memset(&sInfo,0,sizeof(sInfo)); PROCESS_INFORMATION pInfo; memset(&pInfo,0,sizeof(pInfo)); sInfo.cb = sizeof(sInfo); sInfo.dwFlags = STARTF_USESTDHANDLES; bool res = CreateProcess(0,cmnd,0,0,true,NORMAL_PRIORITY_CLASS|CREATE_NO_WINDOW,0,0,&sInfo,&pInfo); if(res) WaitForSingleObject(pInfo.hProcess, INFINITE); return res; } runprocess(L"consoleapp.exe >output"); But the file output is not created though consoleapp worked normally? How to get its output without reading hStdInput, hStdOutput from STARTUPINFO structure?
9ine
Since you are using the
STARTF_USESTDHANDLES
flag, you also need to open files for standard input, output, and error, and set theirHANDLE
values in theSTARTUPINFO
structure. Also, remove the "> output" from the command line, since you are already doing the redirection.
Software Zen:
delete this;