Reading from a Pipe
-
Hi friends, I am developing a program for windows 7 with C++ and I am using VS2010. I create a a pair of pipes for redirecting output of Cmd.exe to my program like:
void CpipesDlg::OnBnClickedBtnredirstd()
{
char Cmd[]="dir *.*\r\n";
char Buff[129];
CString str;
HANDLE hStdIn_Read, hStdIn_Write;
HANDLE hStdOut_Read, hStdOut_Write;
SECURITY_ATTRIBUTES sAttr;STARTUPINFOA StartInf; PROCESS\_INFORMATION procInf; DWORD dwBytesToWrite,dwBytesReadFrom; sAttr.nLength = sizeof(sAttr); sAttr.bInheritHandle = TRUE; sAttr.lpSecurityDescriptor = NULL; CreatePipe(&hStdIn\_Read,&hStdIn\_Write,&sAttr,0); CreatePipe(&hStdOut\_Read,&hStdOut\_Write,&sAttr,0); //SetHandleInformation(hStdIn\_Read, HANDLE\_FLAG\_INHERIT, 0); //SetHandleInformation(hStdIn\_Write, HANDLE\_FLAG\_INHERIT, 0); memset(&StartInf,0, sizeof(StartInf)); memset(&procInf,0,sizeof(procInf)); StartInf.cb = sizeof(StartInf); StartInf.dwFlags = STARTF\_USESTDHANDLES; StartInf.hStdError = hStdOut\_Write; StartInf.hStdOutput = hStdOut\_Write; StartInf.hStdInput = hStdIn\_Read; WriteFile(hStdIn\_Write,Cmd,sizeof(Cmd),&dwBytesToWrite,NULL); if(!CreateProcessA(NULL,"cmd.exe",NULL,NULL,TRUE,NORMAL\_PRIORITY\_CLASS | CREATE\_NO\_WINDOW ,NULL,NULL,&StartInf,&procInf)) { MessageBoxA(m\_hWnd, "Can't Create Process","Error",MB\_OK | MB\_ICONERROR); } WriteFile(hStdIn\_Write,Cmd,sizeof(Cmd),&dwBytesToWrite,NULL); BOOL bSUCCESS =TRUE; Sleep(100); while(bSUCCESS) { BOOL bResult = ReadFile(hStdOut\_Read,Buff,70,&dwBytesReadFrom,NULL); if(!bResult) { break; } Buff\[dwBytesReadFrom\]=0; str+= Buff; bSUCCESS = dwBytesReadFrom!=0; } m\_Disp = str; UpdateData(FALSE); CloseHandle(hStdIn\_Read); CloseHandle(hStdIn\_Write); CloseHandle(hStdOut\_Read); CloseHandle(hStdOut\_Write);
}
and pipe works but there is a problem when I try to read from pipe using ReadFile and the problem is that I don't know how many bytes I should read. The above code blocks when it gets last ReadFile call and there is nothing to read. Actually a deadlock is made. Do you have any Idea to solve the problem? Do you know how I can get the number of ready bytes for reading? Or can I change the behavior of ReadFile for returning immediately? I was wondering if you could help me! I am looking forward to hearing from you. :laugh:
-
Hi friends, I am developing a program for windows 7 with C++ and I am using VS2010. I create a a pair of pipes for redirecting output of Cmd.exe to my program like:
void CpipesDlg::OnBnClickedBtnredirstd()
{
char Cmd[]="dir *.*\r\n";
char Buff[129];
CString str;
HANDLE hStdIn_Read, hStdIn_Write;
HANDLE hStdOut_Read, hStdOut_Write;
SECURITY_ATTRIBUTES sAttr;STARTUPINFOA StartInf; PROCESS\_INFORMATION procInf; DWORD dwBytesToWrite,dwBytesReadFrom; sAttr.nLength = sizeof(sAttr); sAttr.bInheritHandle = TRUE; sAttr.lpSecurityDescriptor = NULL; CreatePipe(&hStdIn\_Read,&hStdIn\_Write,&sAttr,0); CreatePipe(&hStdOut\_Read,&hStdOut\_Write,&sAttr,0); //SetHandleInformation(hStdIn\_Read, HANDLE\_FLAG\_INHERIT, 0); //SetHandleInformation(hStdIn\_Write, HANDLE\_FLAG\_INHERIT, 0); memset(&StartInf,0, sizeof(StartInf)); memset(&procInf,0,sizeof(procInf)); StartInf.cb = sizeof(StartInf); StartInf.dwFlags = STARTF\_USESTDHANDLES; StartInf.hStdError = hStdOut\_Write; StartInf.hStdOutput = hStdOut\_Write; StartInf.hStdInput = hStdIn\_Read; WriteFile(hStdIn\_Write,Cmd,sizeof(Cmd),&dwBytesToWrite,NULL); if(!CreateProcessA(NULL,"cmd.exe",NULL,NULL,TRUE,NORMAL\_PRIORITY\_CLASS | CREATE\_NO\_WINDOW ,NULL,NULL,&StartInf,&procInf)) { MessageBoxA(m\_hWnd, "Can't Create Process","Error",MB\_OK | MB\_ICONERROR); } WriteFile(hStdIn\_Write,Cmd,sizeof(Cmd),&dwBytesToWrite,NULL); BOOL bSUCCESS =TRUE; Sleep(100); while(bSUCCESS) { BOOL bResult = ReadFile(hStdOut\_Read,Buff,70,&dwBytesReadFrom,NULL); if(!bResult) { break; } Buff\[dwBytesReadFrom\]=0; str+= Buff; bSUCCESS = dwBytesReadFrom!=0; } m\_Disp = str; UpdateData(FALSE); CloseHandle(hStdIn\_Read); CloseHandle(hStdIn\_Write); CloseHandle(hStdOut\_Read); CloseHandle(hStdOut\_Write);
}
and pipe works but there is a problem when I try to read from pipe using ReadFile and the problem is that I don't know how many bytes I should read. The above code blocks when it gets last ReadFile call and there is nothing to read. Actually a deadlock is made. Do you have any Idea to solve the problem? Do you know how I can get the number of ready bytes for reading? Or can I change the behavior of ReadFile for returning immediately? I was wondering if you could help me! I am looking forward to hearing from you. :laugh:
Well, I've compiled your code and invoke it. Whenever I invoke it, it didn't stop but I find that your code work well when during debug. About your problem, I think there is no answer. Because c++ stopped reading when It meets zero during read char arrays. So your code is right, I think. In my opinion, you could control your reading bytes which is set 70. :)