🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

Redirecting consolle Output

#13935
From: Stefano Bonifazi
Date: Tue, 09 Sep 2003 12:00
83 lines
3856 bytes
Hello Everybody:-)
 I'm working on a project that should be a sort of GUI interface for the
consolle shell (i.e. cmd.exe)
I use an Anonymous pipe for linking my GUI process with cmd.exe (I run it
with CreateProcess!), but it seems I must create a consolle for my GUI
process before, by using AllocConsole(), to get the STD_OUTPUT_HANDLE and
STD_INPUT_HANDLE of my process since a GUI one has those set to NULL.
But I don't want any consolle to pop up, since I want the output to be
redirected to a TMemo instead (I manage to do that, but with a black
consolle in background!:( )!!

I copy a bit of my code here for clarity:

before

AllocConsole();

than I call:

DWORD RedirectShellINOUT()
{
   SECURITY_ATTRIBUTES saAttr;
   BOOL fSuccess;

   saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
   saAttr.bInheritHandle = TRUE;
   saAttr.lpSecurityDescriptor = NULL;

   hSaveStdout = GetStdHandle(STD_OUTPUT_HANDLE);

// Create a pipe for the child process's STDOUT.
   if (! CreatePipe(&hChildStdoutRd, &hChildStdoutWr, &saAttr, 0))
    return -1;
// Set a write handle to the pipe to be STDOUT.
   if (! SetStdHandle(STD_OUTPUT_HANDLE, hChildStdoutWr))
    return -2;
// Create noninheritable read handle and close the inheritable read
// handle.
    fSuccess = DuplicateHandle(GetCurrentProcess(), hChildStdoutRd,
        GetCurrentProcess(), &hChildStdoutRdDup , 0,  FALSE,
DUPLICATE_SAME_ACCESS);
    if( !fSuccess )
     return -3;
    CloseHandle(hChildStdoutRd);
// Save the handle to the current STDIN.
   hSaveStdin = GetStdHandle(STD_INPUT_HANDLE);
// Create a pipe for the child process's STDIN.
   if (! CreatePipe(&hChildStdinRd, &hChildStdinWr, &saAttr, 0))
    return -4;
// Set a read handle to the pipe to be STDIN.
   if (! SetStdHandle(STD_INPUT_HANDLE, hChildStdinRd))
    return -5;
// Duplicate the write handle to the pipe so it is not inherited.
   fSuccess = DuplicateHandle(GetCurrentProcess(), hChildStdinWr,
GetCurrentProcess(), &hChildStdinWrDup, 0,
      FALSE,   DUPLICATE_SAME_ACCESS);
   if (! fSuccess)
    return -6;
   CloseHandle(hChildStdinWr);

// Now create the child process.

   if (! CreateChildProcess()) //this run "cmd.exe"
    return -7;
// After process creation, restore the saved STDIN and STDOUT.
   if (! SetStdHandle(STD_INPUT_HANDLE, hSaveStdin))
    return -8;
   if (! SetStdHandle(STD_OUTPUT_HANDLE, hSaveStdout))
    return -9;
   hInputFile = hSaveStdin;
   if (hInputFile == INVALID_HANDLE_VALUE)
    return -10;

   return 1;
}

than I use two more functions to send the commands and get the output back
through the pipe!

Any idea about how to avoid that horrible consolle window??

Thank You very much in advance!!
Stefano B.
Technical Details
Message-ID: <3f5dc0c3@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-03!sn-xit-04!sn-xit-01!sn-xit-09!supernews.com!news-xfer2.atl.newshosting.com!63.218.45.10.MISMATCH!newshosting.com!news-xfer1.atl.newshosting.com!newsfeed.icl.net!newsfeed.fjserv.net!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 10cc67,8df7fdfe243189b0,start
X-Google-Attributes: gid10cc67,public
X-Google-ArrivalTime: 2003-09-09 05:10:08 PST
Reply-To: "Stefano Bonifazi" <stefboombastic@libero.it>
From: "Stefano Bonifazi" <stefboombastic@libero.it>
Newsgroups: borland.public.cppbuilder.nativeapi
Subject: Redirecting consolle Output
Date: Tue, 9 Sep 2003 14:00:02 +0200
Lines: 87
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1106
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1106
NNTP-Posting-Host: 212.141.199.32
Message-ID: <3f5dc0c3@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1063108803 212.141.199.32 (9 Sep 2003 05:00:03 -0700)
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-03!sn-xit-04!sn-xit-01!sn-xit-09!supernews.com!news-xfer2.atl.newshosting.com!63.218.45.10.MISMATCH!newshosting.com!news-xfer1.atl.newshosting.com!newsfeed.icl.net!newsfeed.fjserv.net!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail