Using a handle to collect data from CreateProcess ()

I use CreateProcess () to run an external console application on Windows from my GUI application. I would like to somehow collect the conclusion to find out if there were any errors. Now I know that I need to do something with hStdOutput, but I do not understand what. I am new to C ++ and an inexperienced programmer, and in fact I do not know what to do with a pen or how to light a pipe.

How to get the output of a variable (or file)?

Here is what I have a moment:

void email::run(string path,string cmd){ WCHAR * ppath=new(nothrow) WCHAR[path.length()*2]; memset(ppath,' ',path.length()*2); WCHAR * pcmd= new(nothrow) WCHAR[cmd.length()*2]; memset(pcmd,' ',cmd.length()*2); string tempstr; ToWCHAR(path,ppath); //creates WCHAR from my std::string ToWCHAR(cmd,pcmd); STARTUPINFO info={sizeof(info)}; info.dwFlags = STARTF_USESHOWWINDOW; //hide process PROCESS_INFORMATION processInfo; if (CreateProcess(ppath,pcmd, NULL, NULL, FALSE, 0, NULL, NULL, &info, &processInfo)) { ::WaitForSingleObject(processInfo.hProcess, INFINITE); CloseHandle(processInfo.hProcess); CloseHandle(processInfo.hThread); } delete[](ppath); delete[](pcmd); } 

This code probably makes any decent programmer scream, but (I shouldn't even say that :) This works; -)

Question: How to use hStdOutput to read output to a file (for example)?

+6
c ++ createprocess
source share
3 answers

You want to take a look at this example MSDN code: http://msdn.microsoft.com/en-us/library/ms682499(VS.85).aspx

+6
source share

Microsoft has an example in its knowledge base that demonstrates how to capture the output of a child console process. The basic principle is that the parent process creates the channels (one for the standard descriptor for redirection) and passes the CreateProcess descriptors.

You do not need to modify the child process for this, which is important if you do not have control over the child source.

Additional Information: How to create console processes with redirected standard descriptors

+3
source share

BOOL WINAPI GetExitCodeProcess (__in HANDLE hProcess, __out LPDWORD lpExitCode);

Pass processInfo.hProcess to get the process exit code.

-2
source share

All Articles