Re: asking about pipe line
- From: Joseph M. Newcomer <newcomer@xxxxxxxxxxxx>
- Date: Fri, 30 Nov 2007 10:29:27 -0500
Not quite right, see below...
On Fri, 30 Nov 2007 02:13:42 -0800 (PST), Baby Lion <BabyLion.Liang@xxxxxxxxx> wrote:
here is the corrected code****
// tryPipe3.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "tryPipe3.h"
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
#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?
Yes, you want to use NULL for the security attributes here
****
if (bRet)****
printf("success!\n");
else
{
printf("error:%d\n", GetLastError());
return 0;
}
HANDLE hWriteToChildDup;
::DuplicateHandle(GetCurrentProcess(), hWriteToChild,
GetCurrentProcess(), &hWriteToChildDup,
0, FALSE, DUPLICATE_SAME_ACCESS);
Create the pipes non-inheritable, then create inheritable duplicates, by using TRUE for
the inheritance flag
****
::CloseHandle(hWriteToChild);****
HANDLE hReadFromChildDup;
::DuplicateHandle(GetCurrentProcess(), hReadFromChild,
GetCurrentProcess(), &hReadFromChildDup,
0, FALSE, DUPLICATE_SAME_ACCESS);
::CloseHandle(hReadFromChild);
HANDLE hWriteToStderr;
::DuplicateHandle(GetCurrentProcess(), hWriteToParent,
GetCurrentProcess(), &hWriteToStderr,
0, TRUE, DUPLICATE_SAME_ACCESS);
STARTUPINFO si ;
si.cb= sizeof(STARTUPINFO);
::GetStartupInfo(&si);
//si.dwFlags = STARTF_USESTDHANDLES;
si.dwFlags=STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
si.hStdInput = hReadFromParent;
si.hStdOutput = hWriteToParent;
Instead of the original handles, which should be now non-inheritable, assign the
inheritable duplicates here
****
si.hStdError = hWriteToStderr;****
si.wShowWindow = SW_HIDE;
bRet = CreateProcessA(NULL, "nc -w1 www.baidu.com 80", 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(hWriteToParent);
::CloseHandle(hReadFromParent);
::CloseHandle(hWriteToStderr);
::CloseHandle(hReadFromChildDup);
::CloseHandle(hWriteToChildDup);
return 0;
}
::CloseHandle(hWriteToParent);
::CloseHandle(hReadFromParent);
You would close the inheritable duplicates instead
****
::CloseHandle(hWriteToStderr);****
DWORD count1;
char str1[] = "c\n\ra\n\rb\n\r";
string toWrite;
ifstream fin("baidu.txt");
while(!fin.eof())
{
string temp;
getline(fin,temp);
toWrite.append(temp+string("\n"));
Why not just send it directly to the child pipe here? Why create a separate string
variable, and concatenate everything?
****
cout<<temp<<endl;Joseph M. Newcomer [MVP]
}
fin.close();
WriteFile(hWriteToChildDup,toWrite.c_str(),toWrite.length(),&count1,NULL);
printf("delta len%d\n",toWrite.c_str()-count1);
::CloseHandle(hWriteToChildDup);
while (ReadFile(hReadFromChildDup, 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) // ????????
printf("pipe closed by the sub-process\n");
else
printf("error :%d\n", GetLastError());
::CloseHandle(hReadFromChildDup);
::CloseHandle(pi.hProcess);
::CloseHandle(pi.hThread);
return 0;
}
email: newcomer@xxxxxxxxxxxx
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
.
- References:
- asking about pipe line
- From: Baby Lion
- Re: asking about pipe line
- From: Joseph M . Newcomer
- Re: asking about pipe line
- From: Baby Lion
- Re: asking about pipe line
- From: Baby Lion
- Re: asking about pipe line
- From: Joseph M . Newcomer
- Re: asking about pipe line
- From: Baby Lion
- Re: asking about pipe line
- From: Baby Lion
- asking about pipe line
- Prev by Date: Re: CDialog: number of controls in resource editor
- Next by Date: Re: MFC updates and enhancements
- Previous by thread: Re: asking about pipe line
- Next by thread: CDialog: number of controls in resource editor
- Index(es):
Relevant Pages
|