Article View: borland.public.cppbuilder.nativeapi
Article #3697Re: GUI < I/O Redirection With Pipe> Console App
From: sez
Date: Fri, 28 Apr 2006 03:26
Date: Fri, 28 Apr 2006 03:26
155 lines
5273 bytes
5273 bytes
Thank you Gambit. Your solution has given some error until I made a few
corrections.
Now it's working like a tornado. You had your name written on the special
thanks section of my program. Congratulations!
Let's share the code:
Add your form this items in design time:
Button1, Button2, Memo1, Timer1
and set Timer1 enabled false.
.h file:
private: // User declarations
HANDLE hProcess;
HANDLE hStdInRead;
HANDLE hStdInWrite;
HANDLE hStdOutRead;
HANDLE hStdOutWrite;
void __fastcall CloseHandles();
.cpp file:
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
PROCESS_INFORMATION pi = {0};
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
BOOL CALLBACK closeit(HWND hwnd,LPARAM lParam)
{
DWORD hwnd_pid;
GetWindowThreadProcessId(hwnd, &hwnd_pid);
if (hwnd_pid == pi.dwProcessId) {
SendMessage(hwnd, WM_CLOSE, 0,0);
return false;
}
return true;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::CloseHandles()
{
if(hProcess) {
CloseHandle(hProcess);
hProcess = NULL;
}
if(hStdInRead) {
CloseHandle(hStdInRead);
hStdInRead = NULL;
}
if(hStdInWrite) {
CloseHandle(hStdInWrite);
hStdInWrite = NULL;
}
if(hStdOutRead) {
CloseHandle(hStdOutRead);
hStdOutRead = NULL;
}
if(hStdInWrite) {
CloseHandle(hStdOutWrite);
hStdOutWrite = NULL;
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
SECURITY_ATTRIBUTES sa = {0};
sa.nLength = sizeof(SECURITY_ATTRIBUTES);
sa.bInheritHandle = TRUE;
sa.lpSecurityDescriptor = NULL;
if(!CreatePipe(&hStdInRead, &hStdInWrite, &sa, 0)) {
CloseHandles();
RaiseLastWin32Error();
}
if(!CreatePipe(&hStdOutRead, &hStdOutWrite, &sa, 0)) {
CloseHandles();
RaiseLastWin32Error();
}
STARTUPINFO si = {0};
si.cb = sizeof(STARTUPINFO);
si.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
si.wShowWindow = SW_HIDE;
si.hStdInput = hStdInRead;
si.hStdOutput = hStdOutWrite;
si.hStdError = hStdOutWrite;
if(!CreateProcess("c:\\windows\\system32\\cmd.exe", NULL, NULL, NULL, TRUE,
0,
NULL, NULL, &si, &pi)) {
CloseHandles();
RaiseLastWin32Error();
}
CloseHandle(pi.hThread);
hProcess = pi.hProcess;
Timer1->Enabled = true;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
if(!hStdInWrite) return;
AnsiString Data = "ECHO THANK YOU GAMBIT!!!\n";
char *ptr = Data.c_str();
DWORD TotalBytes = Data.Length();
DWORD BytesSent = 0;
while(TotalBytes) {
if(!WriteFile(hStdInWrite, ptr, TotalBytes, &BytesSent, NULL))
RaiseLastWin32Error();
ptr += BytesSent;
TotalBytes -= BytesSent;
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Timer1Timer(TObject *Sender)
{
DWORD TotalBytes = 0;
if(!PeekNamedPipe(hStdOutRead, NULL, 0, NULL, &TotalBytes, NULL))
RaiseLastWin32Error();
if(TotalBytes) {
char Data[1024];
DWORD BytesRead = 0;
while(TotalBytes) {
if(!ReadFile(hStdOutRead, Data, sizeof(Data)-1, &BytesRead, NULL))
RaiseLastWin32Error();
Memo1->SelStart = Memo1->GetTextLen();
Memo1->SelText = AnsiString(Data, BytesRead);
TotalBytes -= BytesRead;
}
} else if(WaitForSingleObject(hProcess, 0) == WAIT_OBJECT_0)
CloseHandles();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormClose(TObject *Sender, TCloseAction &Action)
{
EnumWindows((WNDENUMPROC)closeit, 0);
}
//---------------------------------------------------------------------------
Technical Details
Message-ID:
<44518b59@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news2.google.com!news3.google.com!news4.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mailReferences:
<4450ebe7@newsgroups.borland.com> <44510d38$1@newsgroups.borland.com> <44513fe0@newsgroups.borland.com> <44515604$1@newsgroups.borland.com>Show Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,db8b207bb1b4febd X-Google-Attributes: gid10cc67,public X-Google-Language: ENGLISH,ASCII-7-bit Reply-To: "sez" <www@sezginserin.com> From: "sez" <www@sezginserin.com> Newsgroups: borland.public.cppbuilder.nativeapi References: <4450ebe7@newsgroups.borland.com> <44510d38$1@newsgroups.borland.com> <44513fe0@newsgroups.borland.com> <44515604$1@newsgroups.borland.com> Subject: Re: GUI < I/O Redirection With Pipe> Console App Date: Fri, 28 Apr 2006 06:26:08 +0300 Lines: 157 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2900.2869 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2869 X-RFC2646: Format=Flowed; Original NNTP-Posting-Host: 81.214.79.223 Message-ID: <44518b59@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1146194777 81.214.79.223 (27 Apr 2006 20:26:17 -0700) Path: arc1-ca.pugleaf.net!g2news2.google.com!news3.google.com!news4.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail