Article View: borland.public.cppbuilder.nativeapi
Article #13934Redirecting consolle output
From: Stefano Bonifazi
Date: Tue, 09 Sep 2003 11:53
Date: Tue, 09 Sep 2003 11:53
83 lines
3700 bytes
3700 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 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:
<3f5dbf35@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-06!sn-xit-09!supernews.com!news.maxwell.syr.edu!newsfeed.wirehub.nl!news.kweb.on.ca!KWeb!newsgroups.borland.com!not-for-mailShow Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 10cc67,dd9e2d0375cad493,start X-Google-Attributes: gid10cc67,public X-Google-ArrivalTime: 2003-09-09 04:56:23 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 13:53:24 +0200 Lines: 86 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: <3f5dbf35@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1063108405 212.141.199.32 (9 Sep 2003 04:53:25 -0700) Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-06!sn-xit-09!supernews.com!news.maxwell.syr.edu!newsfeed.wirehub.nl!news.kweb.on.ca!KWeb!newsgroups.borland.com!not-for-mail