🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

Re: GUI < I/O Redirection With Pipe> Console App

#3695
From: Remy Lebeau (TeamB)
Date: Thu, 27 Apr 2006 23:35
150 lines
4870 bytes
"sez" <www@sezginserin.com> wrote in message
news:44513fe0@newsgroups.borland.com...

> I had seen this example before.
> It is only reading output via Button1Click.
> I need a second button like Button2 which
> sends some input to console.

Simply create a second pipe when calling CreateProcess(), and assign that
pipe to the hStdIn member of the STARTUPINFO structure.  You can then use
WriteFile() to send data to the console app when needed.  I've told you this
several times now.

For example:

    private:
        HANDLE hProcess;
        HANDLE hStdInRead;
        HANDLE hStdInWrite;
        HANDLE hStdOutRead;
        HANDLE hStdOutWrite;
        void __fastcall CloseHandles();

    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;
        st.hStdInput = hStdInRead;
        si.hStdOutput = hStdOutWrite;
        si.hStdError = hStdOutWrite;

        PROCESS_INFORMATION pi = {0};
        if( !CreateProcess("YourProgram.exe", NULL, NULL, NULL, TRUE, 0,
NULL, NULL, &si, &pi) )
        {
            CloseHandles();
            RaiseLastWin32Error();
        }

        CloseHandle(pi.hThread);
        hProcess = si.hProcess;

        Timer1->Enabled = true;
    }

    void __fastca;; TForm1::Button2Click(TObject *Sender)
    {
        if( !hStdInWrite )
            return;

        AnsiString Data = "whatever";
        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)
    {
        Timer1->Enabled = false;

        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, min(sizeof(Data),
TotalBytes), &BytesRead, NULL) )
                    RaiseLastWin32Error();

                Memo1->SelStart = Memo1->GetTextLen();
                Memo1->SelText = AnsiString(Data, BytesRead);
                TotalBytes -= BytesRead;
            }

            Timer1->Enabled = true;
        }
        else if( WaitForSingleObject(hProcess, 0) == WAIT_OBJECT_0 )
            CloseHandles();
    }


Gambit
Technical Details
Message-ID: <44515604$1@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news2.google.com!news3.google.com!newshub.sdsu.edu!logbridge.uoregon.edu!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mail
References: <4450ebe7@newsgroups.borland.com> <44510d38$1@newsgroups.borland.com> <44513fe0@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
From: "Remy Lebeau \(TeamB\)" <no.spam@no.spam.com>
Newsgroups: borland.public.cppbuilder.nativeapi
References: <4450ebe7@newsgroups.borland.com> <44510d38$1@newsgroups.borland.com> <44513fe0@newsgroups.borland.com>
Subject: Re: GUI < I/O Redirection With Pipe> Console App
Date: Thu, 27 Apr 2006 16:35:41 -0700
Lines: 152
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1409
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1409
NNTP-Posting-Host: 68.69.17.41
Message-ID: <44515604$1@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1146181124 68.69.17.41 (27 Apr 2006 16:38:44 -0700)
Path: arc1-ca.pugleaf.net!g2news2.google.com!news3.google.com!newshub.sdsu.edu!logbridge.uoregon.edu!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mail