Hi folks,
I have an issue with anonymous pipes..
Steps I followed :
Created a Pipe with 2 Handles .. "
SECURITY_ATTRIBUTES childstdoutattr;
childstdoutattr.bInheritHandle=true;
childstdoutattr.nLength=sizeof(SECURITY_ATTRIBUTES);
childstdoutattr.lpSecurityDescriptor=NULL;
CreatePipe(&childStdOutRead,&childStdOutWrite,&childstdoutattr,0);" ..
Next .. I created a duplicate handle which is non inheritable
DuplicateHandle(GetCurrentProcess(),childStdOutRead,GetCurrentProcess(),&childStdOutReadDup,DUPLICATE_SAME_ACCESS,false,DUPLICATE_SAME_ACCESS);
next I closed "CloseHandle(childStdOutRead);" as I have dup handle.
next I setted STD_OUTPUT_HANDLE as BOOL
stdhandle=SetStdHandle(STD_OUTPUT_HANDLE,childStdOutWrite);
next I started a child process with security attributes as
si.dwFlags |=STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
si.hStdOutput=childStdOutWrite;si.hStdError=childStdOutWrite;
si.wShowWindow=SW_HIDE;
Next
I closed the writeHandle end .. (The reason why I closed is .. I read
in some sites that as this handle is inheritable and created a process
with sharing parent handle.. I understood that this handle is consisted
with parent and the inherited one is with child. so as there will be
two writed handles , I should close one.)
closeHandle=CloseHandle(childStdOutWrite); char *procName=new char[100];
strcpy(procName,"C:\\CpProj\\OPRedirection\\debug\\OPRedirection.exe");
BOOL
flag_notepad=CreateProcessA(NULL,procName,NULL,NULL,true,0,NULL,NULL,&si,&pi);
Next I used readFile=ReadFile(childStdOutReadDup,&Buf,1023,&bytesRead,NULL); ..
But it is blocking.. so I used
PeekNamedPipe(childStdOutReadDup,&Buf,1023,&bytesRead,&totalBytesAvail,&BytesLeftThisMessage);
.. but it is not reading any bytes..
The entire code is
#include<stdio.h>
#include<windows.h>
#include<tchar.h>
void main()
{
HANDLE output=GetStdHandle(STD_OUTPUT_HANDLE);
STARTUPINFOA si = {sizeof(si)};
si.cb=sizeof(si);
PROCESS_INFORMATION pi;
HANDLE childStdOutWrite,childStdOutRead ,
childStdInRead,childStdInWrite,childStdOutReadDup;
SECURITY_ATTRIBUTES childstdoutattr;
childstdoutattr.bInheritHandle=true;
childstdoutattr.nLength=sizeof(SECURITY_ATTRIBUTES);
childstdoutattr.lpSecurityDescriptor=NULL;
BOOL
pipeCreated=CreatePipe(&childStdOutRead,&childStdOutWrite,&childstdoutattr,0);
BOOL
dupHandle=DuplicateHandle(GetCurrentProcess(),childStdOutRead,GetCurrentProcess(),&childStdOutReadDup,DUPLICATE_SAME_ACCESS,false,DUPLICATE_SAME_ACCESS);
BOOL closeHandle=CloseHandle(childStdOutRead);
BOOL stdhandle=SetStdHandle(STD_OUTPUT_HANDLE,childStdOutWrite);
ZeroMemory(&pi,sizeof(pi));
si.dwFlags |=STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
si.hStdOutput=childStdOutWrite;
si.hStdError=childStdOutWrite;
si.wShowWindow=SW_HIDE;
closeHandle=CloseHandle(childStdOutWrite);
char *procName=new char[100];
strcpy(procName,"C:\\CpProj\\OPRedirection\\debug\\OPRedirection.exe");
BOOL
flag_notepad=CreateProcessA(NULL,procName,NULL,NULL,true,0,NULL,NULL,&si,&pi);
if(!flag_notepad)
{
return;
}
char Buf[1024];
DWORD bytesRead,totalBytesAvail,BytesLeftThisMessage;
BOOL readFile;
while(1)
{
//readFile=ReadFile(childStdOutReadDup,&Buf,1023,&bytesRead,NULL);
PeekNamedPipe(childStdOutReadDup,&Buf,1023,&bytesRead,&totalBytesAvail,&BytesLeftThisMessage);
if(bytesRead!=0)
{
printf("\n %s",Buf);
}
}
}
Thx,
--Gopi
Add more friends to your messenger and enjoy! Go to
http://messenger.yahoo.com/invite/
[Non-text portions of this message have been removed]