Re: asking about pipe line
thanks a lot ,
I changed the code ,
but it still could not work ,y?
#include "stdafx.h"
#include "tryPipe3.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// The one and only application object
CWinApp theApp;
using namespace std;
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
PROCESS_INFORMATION pi;
#define BUFSIZE 4096
char ReadBuf[BUFSIZE];
DWORD ReadNum;
HANDLE hReadFromChild;
HANDLE hWriteToParent;
HANDLE hReadFromParent;
HANDLE hWriteToChild;
SECURITY_ATTRIBUTES sa = {sizeof(SECURITY_ATTRIBUTES), NULL, TRUE};
BOOL bRet = CreatePipe(&hReadFromChild, &hWriteToParent, &sa, 0);
if (bRet)
printf("success!\n");
else
{
printf("error:%d\n", GetLastError());
return 0;
}
bRet = CreatePipe(&hReadFromParent,&hWriteToChild,&sa,0);//y null?
if (bRet)
printf("success!\n");
else
{
printf("error:%d\n", GetLastError());
return 0;
}
HANDLE hWriteToStderr;
::DuplicateHandle(GetCurrentProcess(), hWriteToParent,
GetCurrentProcess(), &hWriteToStderr,
0, TRUE, DUPLICATE_SAME_ACCESS);
STARTUPINFO si = { sizeof(STARTUPINFO) };
si.dwFlags = STARTF_USESTDHANDLES;
si.hStdInput = hReadFromParent;
si.hStdOutput = hWriteToParent;
si.hStdError = hWriteToStderr;
bRet = CreateProcessA(NULL, "sort /r", NULL, NULL, TRUE, NULL, NULL,
NULL, &si, &pi);
if (bRet)
printf("succeeded create process!\n");
else
{
printf("fail to create process:%d\n", GetLastError());
::CloseHandle(hReadFromChild);
::CloseHandle(hWriteToParent);
::CloseHandle(hWriteToStderr);
::CloseHandle(hReadFromParent);
::CloseHandle(hReadFromParent);
return 0;
}
::CloseHandle(hWriteToChild);
::CloseHandle(hReadFromParent);
::CloseHandle(hWriteToStderr);
DWORD count1;
char str1[] = "c\na\nb\n";
WriteFile(hWriteToChild,str1,strlen(str1),&count1,NULL);
printf("delta len%d\n",strlen(str1)-count1);
while (ReadFile(hReadFromChild, ReadBuf, BUFSIZE-1,&ReadNum, NULL))
{
ReadBuf[ReadNum]='\0';
printf("get from pipe[\n%s\n]read%dbytes\n", ReadBuf, ReadNum);
}
if (GetLastError() == ERROR_BROKEN_PIPE) // =CA=E4=B3=F6=D0=C5=CF=A2
printf("pipe closed by the sub-process\n");
else
printf("error :%d\n", GetLastError());
::CloseHandle(hReadFromChild);
::CloseHandle(hWriteToChild);
::CloseHandle(pi.hProcess);
::CloseHandle(pi.hThread);
return 0;
}