🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.cppbuilder.nativeapi
Article #13183

Re: Redirecting stdout

#13183
From: Mike King
Date: Fri, 24 Oct 2003 04:23
91 lines
3431 bytes
"Denville Longhurst" <Denville@metco.co.uk> wrote in message
news:3f979256$1@newsgroups.borland.com...
> Hi
>
> I need to import some legacy code into a new GUI app.  The legacy code
makes
> considerable use of printf() etc.
>
> How do I redirect stdout so that I can display it in a TMemo or whatever
> (display is not a problem, just grabbing the otherwise lost output).  It
is
> not really viable to go through the legacy code and change all the
printf's.
>
> Many thanks,
>
> Denville
> Leeds, England.
>

Try this out... I can't take credit for it... because I didn't create it and
I can't remind how do.

Follow these steps:
  - Create a GUI application
  - Place a TMemo on the form
  - Place a TButton on the form and add a event handler

void __fastcall TForm1::Button1Click(TObject *Sender)
{
  //create pipe for the console stdout
  SECURITY_ATTRIBUTES sa;
  ZeroMemory(&sa,sizeof(SECURITY_ATTRIBUTES));
  sa.nLength=sizeof(SECURITY_ATTRIBUTES);
  sa.bInheritHandle=true;
  sa.lpSecurityDescriptor=NULL;
  HANDLE ReadPipeHandle;
  HANDLE WritePipeHandle;       // not used here
  if(!CreatePipe(&ReadPipeHandle,&WritePipeHandle,&sa,0))
        RaiseLastWin32Error();

  //Create a Console
  STARTUPINFO si;
  ZeroMemory(&si,sizeof(STARTUPINFO));
  si.cb=sizeof(STARTUPINFO);
  si.dwFlags=STARTF_USESHOWWINDOW|STARTF_USESTDHANDLES;
  si.wShowWindow=SW_HIDE;
  si.hStdOutput=WritePipeHandle;
  si.hStdError=WritePipeHandle;

  PROCESS_INFORMATION pi;
  ZeroMemory(&pi,sizeof(PROCESS_INFORMATION));

if(!CreateProcess("c:\\winnt\\system32\\ipconfig.exe",NULL,NULL,NULL,true,0,
NULL,NULL,&si,&pi))
      RaiseLastWin32Error();

  //Read from pipe
  char Data[1024];
  for (;;)
  {
    DWORD BytesRead;
    DWORD TotalBytes;
    DWORD BytesLeft;

    //Check for the presence of data in the pipe
    if(!PeekNamedPipe(ReadPipeHandle,Data,sizeof(Data),&BytesRead,
        &TotalBytes,&BytesLeft))RaiseLastWin32Error();
    //If there is bytes, read them
    if(BytesRead)
    {
      if(!ReadFile(ReadPipeHandle,Data,sizeof(Data)-1,&BytesRead,NULL))
          RaiseLastWin32Error();
      Data[BytesRead]='\0';
      Memo1->Lines->Add(AnsiString(Data));

    }
    else
    {
      //Is the console app terminated?
      if(WaitForSingleObject(pi.hProcess,0)==WAIT_OBJECT_0)break;

    }
  }
  CloseHandle(pi.hThread);
  CloseHandle(pi.hProcess);
  CloseHandle(ReadPipeHandle);
  CloseHandle(WritePipeHandle);
}
//--------------------------------------------------------------------------
-
Technical Details
Message-ID: <3f98a92f@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!fu-berlin.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail
References: <3f979256$1@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 10cc67,f5a2ee1e49a6d81b
X-Google-Attributes: gid10cc67,public
X-Google-ArrivalTime: 2003-10-23 21:39:05 PST
From: "Mike King" <emailMK@excite.com>
Newsgroups: borland.public.cppbuilder.nativeapi
References: <3f979256$1@newsgroups.borland.com>
Subject: Re: Redirecting stdout
Date: Fri, 24 Oct 2003 00:23:38 -0400
Lines: 93
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
NNTP-Posting-Host: 68.34.75.85
Message-ID: <3f98a92f@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1066969391 68.34.75.85 (23 Oct 2003 21:23:11 -0700)
Path: arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!fu-berlin.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail