BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

pipe! receive data but cant send...

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (WinAPI)
View previous topic :: View next topic  
Author Message
sez
Guest





PostPosted: Fri Mar 31, 2006 10:03 pm    Post subject: pipe! receive data but cant send... Reply with quote



Hi, this is my data receiving code from one console application to VCL
C++ builder memo.
Can you tell my how to send code from VCL to console application?
Please give a brief example.
Thanks in advance...

void __fastcall TForm1::Button1Click(TObject *Sender)
{
//create pipe for the console stdout
SECURITY_ATTRIBUTES sa;
ZeroMemory(&sa,sizeof(SECURITY_ATTRIBUTES));
sa.nLength=sizeof(SECURITY_ATTRIBUTES);
sa.bInheritHandle=true;
sa.lpSecurityDescriptor=NULL;
HANDLE ReadPipeHandle;
HANDLE WritePipeHandle; // not used here
if(!CreatePipe(&ReadPipeHandle,&WritePipeHandle,&sa,0))
RaiseLastWin32Error();

//Create a Console
STARTUPINFO si;
ZeroMemory(&si,sizeof(STARTUPINFO));
si.cb=sizeof(STARTUPINFO);
si.dwFlags=STARTF_USESHOWWINDOW|STARTF_USESTDHANDLES;
si.wShowWindow=SW_HIDE;
si.hStdOutput=WritePipeHandle;
si.hStdError=WritePipeHandle;

PROCESS_INFORMATION pi;
ZeroMemory(&pi,sizeof(PROCESS_INFORMATION));

if(!CreateProcess("filename.exe",NULL,NULL,NULL,true,0,NULL,NULL,&si,&pi))
RaiseLastWin32Error();

//Read from pipe
char Data[1024];
for (;Wink
{
DWORD BytesRead;
DWORD TotalBytes;
DWORD BytesLeft;

//Check for the presence of data in the pipe
if(!PeekNamedPipe(ReadPipeHandle,Data,sizeof(Data),&BytesRead,
&TotalBytes,&BytesLeft))RaiseLastWin32Error();
//If there is bytes, read them
if(BytesRead)
{
if(!ReadFile(ReadPipeHandle,Data,sizeof(Data)-1,&BytesRead,NULL))

RaiseLastWin32Error();
Data[BytesRead]='\0';
Memo1->Lines->Add(AnsiString(Data));

}
else
{
//Is the console app terminated?
if(WaitForSingleObject(pi.hProcess,0)==WAIT_OBJECT_0)break;

}
}
CloseHandle(pi.hThread);
CloseHandle(pi.hProcess);
CloseHandle(ReadPipeHandle);
CloseHandle(WritePipeHandle);

}
Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (WinAPI) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.