Using a handle to collect output from CreateProcess()

Posted by Stef on Stack Overflow See other posts from Stack Overflow or by Stef
Published on 2010-04-20T10:21:42Z Indexed on 2010/04/20 10:43 UTC
Read the original article Hit count: 443

Filed under:
|
|
|

Hi

I am using CreateProcess() to run an external console application in Windows from my GUI application. I would like to somehow gather the output to know whether there were errors. Now I know I have to do something with hStdOutput, but I fail to understand what. I am new to c++ and an inexperienced programmer and I actually don't know what to do with a handle or how to light a pipe.

How do I get the output to some kind of variable (or file)?

This is what I have a the 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 it:) It works ;-)

The Question: How do I use hStdOutput to read the output to a file (for instance)?

© Stack Overflow or respective owner

Related posts about c++

Related posts about createprocess