Thread View: borland.public.cppbuilder.nativeapi
8 messages
8 total messages
Started by sez
Thu, 27 Apr 2006 16:05
GUI < I/O Redirection With Pipe> Console App
Author: sez
Date: Thu, 27 Apr 2006 16:05
Date: Thu, 27 Apr 2006 16:05
2 lines
955 bytes
955 bytes
Do you have an example for redirection console apps outputs to GUI app and GUI apps inputs to console app...
Re: GUI < I/O Redirection With Pipe> Console App
Author: Remy Lebeau (TeamB)
Date: Thu, 27 Apr 2006 17:09
Date: Thu, 27 Apr 2006 17:09
15 lines
1454 bytes
1454 bytes
"sez" <www@sezginserin.com> wrote in message news:4450ebe7@newsgroups.borland.com... > Do you have an example for redirection console apps outputs > to GUI app and GUI apps inputs to console app... Such examples have been posted many times before. Please go to http://www.deja.com and search through the newsgroup archives. In particular, you need to use CreatePipe() along with the hStdIn/Out/Err members of the STRARTUPINFO structure for CreateProcess(), and then use Read/WriteFile() to exchange data over the pipes. Gambit
Re: GUI < I/O Redirection With Pipe> Console App
Author: sez
Date: Thu, 27 Apr 2006 18:00
Date: Thu, 27 Apr 2006 18:00
13 lines
1502 bytes
1502 bytes
hello remy, didn't you post below message to me in private mail before? deja.com opens google groups' main page. I think there is some other direct links instead of your suggestion. > Such examples have been posted many times before. Please go to > http://www.deja.com and search through the newsgroup archives. In > particular, you need to use CreatePipe() along with the hStdIn/Out/Err > members of the STRARTUPINFO structure for CreateProcess(), and then use > Read/WriteFile() to exchange data over the pipes. > > > Gambit
Re: GUI < I/O Redirection With Pipe> Console App
Author: Michel Leunen
Date: Thu, 27 Apr 2006 18:30
Date: Thu, 27 Apr 2006 18:30
17 lines
1462 bytes
1462 bytes
sez wrote: > Do you have an example for redirection console apps outputs to GUI app and > GUI apps inputs to console app... Look here to redirect the output of a console to a TMemo. This could give you a start: http://www.leunen.com/cbuilder/redirect.html Michel -- ---------------------------------------- Michel Leunen mailto: see my homepage. C++Builder, BCC5.5.1 Web site: http://www.leunen.com/ ----------------------------------------
Re: GUI < I/O Redirection With Pipe> Console App
Author: Remy Lebeau (TeamB)
Date: Thu, 27 Apr 2006 20:44
Date: Thu, 27 Apr 2006 20:44
12 lines
1243 bytes
1243 bytes
"sez" <www@sezginserin.com> wrote in message news:445106b5@newsgroups.borland.com... > deja.com opens google groups' main page. Google acquired Deja years ago, but the Deja domain still exists. Google/Deja has an extensive publically-searchable newsgroup archive. The Borland archive goes back almost 10 years. Gambit
Re: GUI < I/O Redirection With Pipe> Console App
Author: sez
Date: Thu, 27 Apr 2006 22:04
Date: Thu, 27 Apr 2006 22:04
8 lines
1304 bytes
1304 bytes
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. Thank you Michel. > Look here to redirect the output of a console to a TMemo. This could give > you a start: http://www.leunen.com/cbuilder/redirect.html
Re: GUI < I/O Redirection With Pipe> Console App
Author: Remy Lebeau (TeamB)
Date: Thu, 27 Apr 2006 23:35
Date: Thu, 27 Apr 2006 23:35
150 lines
4870 bytes
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
Re: GUI < I/O Redirection With Pipe> Console App
Author: 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);
}
//---------------------------------------------------------------------------
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads