Click here to Skip to main content
16,005,734 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: CreateProcess Pin
Tomasz Sowinski14-Aug-02 4:59
Tomasz Sowinski14-Aug-02 4:59 
GeneralRe: CreateProcess Pin
Dave_14-Aug-02 5:02
Dave_14-Aug-02 5:02 
GeneralRe: CreateProcess Pin
Andreas Saurwein14-Aug-02 6:00
Andreas Saurwein14-Aug-02 6:00 
GeneralRe: CreateProcess Pin
Andreas Saurwein14-Aug-02 6:03
Andreas Saurwein14-Aug-02 6:03 
GeneralRe: CreateProcess Pin
Dave_14-Aug-02 6:25
Dave_14-Aug-02 6:25 
GeneralRe: CreateProcess Pin
Andreas Saurwein14-Aug-02 15:02
Andreas Saurwein14-Aug-02 15:02 
GeneralRe: CreateProcess Pin
Dave_15-Aug-02 1:57
Dave_15-Aug-02 1:57 
GeneralRe: CreateProcess Pin
Stuart Dootson14-Aug-02 21:03
professionalStuart Dootson14-Aug-02 21:03 
Hows this - note that there may be some missing headers - I've just cut'n'pasted this out of a working application, so it's missing context...

#include <windows.h>
#include <malloc.h>
#include <string>
namespace std {
#ifdef _UNICODE
  typedef wstring tstring;
#else
  typedef string tstring;
#endif
};
std::tstring RunProcessAndCaptureOutput(LPCTSTR CmdLine, DWORD maxWaitTime = INFINITE)
{
  LPTSTR copyOfCmdLine = LPTSTR(alloca(sizeof(TCHAR) * (lstrlen(CmdLine) + 1)));
  lstrcpy(copyOfCmdLine, CmdLine);
  std::tstring s;
  SECURITY_ATTRIBUTES SecAttr;
  SecAttr.nLength = sizeof(SecAttr);
  SecAttr.lpSecurityDescriptor = NULL;
  SecAttr.bInheritHandle = TRUE;

  HANDLE hRead, hWrite;
  if (::CreatePipe(&hRead, &hWrite, &SecAttr, 0)) {
    STARTUPINFO StartupInfo;
    ZeroMemory(&StartupInfo, sizeof(StartupInfo));
    StartupInfo.cb = sizeof(StartupInfo);
    StartupInfo.dwFlags = STARTF_FORCEOFFFEEDBACK|STARTF_USESTDHANDLES;
    StartupInfo.hStdError = hWrite;
    StartupInfo.hStdOutput = hWrite;
    PROCESS_INFORMATION ProcessInfo;

    if (::CreateProcess(NULL, copyOfCmdLine, NULL, NULL, TRUE, DETACHED_PROCESS, NULL, NULL, &StartupInfo, &ProcessInfo)) {
      std::tstring capturedOutput;
      const DWORD smallWaitTime = 100;
      __int64 ftEnd;
      GetSystemTimeAsFileTime(LPFILETIME(&ftEnd));
      ftEnd += __int64(maxWaitTime) * __int64(10000);
      CloseHandle(hWrite);
      while (::WaitForSingleObject(ProcessInfo.hProcess, smallWaitTime) != WAIT_OBJECT_0) {
        EmptyPipeAndAppendToString(hRead, capturedOutput);
        __int64 ftNow;
        GetSystemTimeAsFileTime(LPFILETIME(&ftNow));
        if (ftNow > ftEnd) {
          s += "Command \"";
          s += CmdLine;
          s += "\" timed out.";
          s += " Partial output received\r\n\r\n";
          break;
        }
      }
      EmptyPipeAndAppendToString(hRead, capturedOutput);
      CloseHandle(ProcessInfo.hProcess);
      CloseHandle(ProcessInfo.hThread);
      s += capturedOutput;
      s += "\r\n\r\n";
    }
    else {
      CloseHandle(hWrite);
      s += "Error creating process with command \"";
      s += CmdLine;
      s += "\"\r\n\r\n";
    }
    CloseHandle(hRead);
  }
  else {
    s += "Error creating pipe\r\n\r\n";
  }
  return s;
}


Stuart Dootson

'Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p'
GeneralRe: CreateProcess Pin
Dave_15-Aug-02 1:55
Dave_15-Aug-02 1:55 
GeneralCFont query Pin
Nish Nishant14-Aug-02 2:34
sitebuilderNish Nishant14-Aug-02 2:34 
GeneralRe: CFont query Pin
Shog914-Aug-02 3:43
sitebuilderShog914-Aug-02 3:43 
GeneralRe: CFont query Pin
Roger Allen14-Aug-02 3:43
Roger Allen14-Aug-02 3:43 
GeneralThanks guys Pin
Nish Nishant14-Aug-02 3:51
sitebuilderNish Nishant14-Aug-02 3:51 
GeneralImage 2 Pin
Mazdak14-Aug-02 2:35
Mazdak14-Aug-02 2:35 
GeneralRe: Image 2 Pin
567890123414-Aug-02 3:30
567890123414-Aug-02 3:30 
GeneralRe: Image 2 Pin
Mazdak14-Aug-02 5:43
Mazdak14-Aug-02 5:43 
Generalhorizontal scrollbar in edit view Pin
Steve L.14-Aug-02 2:17
Steve L.14-Aug-02 2:17 
GeneralCSocket Pin
Zizilamoroso14-Aug-02 1:43
Zizilamoroso14-Aug-02 1:43 
GeneralRe: CSocket Pin
markkuk14-Aug-02 1:51
markkuk14-Aug-02 1:51 
GeneralRe: CSocket Pin
Zizilamoroso14-Aug-02 2:48
Zizilamoroso14-Aug-02 2:48 
GeneralBuilding old style dll using .NET C++ Pin
Dean Adams14-Aug-02 1:43
Dean Adams14-Aug-02 1:43 
GeneralRe: Building old style dll using .NET C++ Pin
Roman Fadeyev14-Aug-02 2:31
Roman Fadeyev14-Aug-02 2:31 
GeneralRe: Building old style dll using .NET C++ Pin
Dean Adams14-Aug-02 9:02
Dean Adams14-Aug-02 9:02 
GeneralRe: Building old style dll using .NET C++ Pin
Roman Fadeyev14-Aug-02 19:02
Roman Fadeyev14-Aug-02 19:02 
GeneralRe: Building old style dll using .NET C++ Pin
Dean Adams14-Aug-02 23:25
Dean Adams14-Aug-02 23:25 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.