#include "stdafx.h"
#include "windows.h"
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
STARTUPINFO myStartup;
PROCESS_INFORMATION myInfo;
BOOL bReturn;
char readBuf[100];
DWORD bytesRead = 0;
HANDLE hRead,hWrite,hTmp;
SECURITY_ATTRIBUTES ss;
ss.nLength = sizeof( SECURITY_ATTRIBUTES );
ss.bInheritHandle = TRUE;
ss.lpSecurityDescriptor = NULL;
//create Anonymous Pipe
if( CreatePipe( &hRead , &hWrite , &ss , 0 ) )
cout<<"Pipe Created OK"<<endl;
else
cout<<"Pipe Creation Error:"<<GetLastError()<<endl;
hTmp = GetStdHandle( STD_OUTPUT_HANDLE );
SetStdHandle( STD_OUTPUT_HANDLE , hWrite );
GetStartupInfo( &myStartup );
//很多书上没有写这段代码,导致下面的进程并没有把输出写到指定的管道
myStartup.hStdError = hWrite;
myStartup.hStdOutput = hWrite;
myStartup.wShowWindow = SW_HIDE;
myStartup.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
//get server address
bReturn = CreateProcess( _T("..//debug//logger.exe") ,NULL , NULL , NULL , TRUE ,
NULL , NULL , NULL , &myStartup , &myInfo );
SetStdHandle( STD_OUTPUT_HANDLE , hTmp );
CloseHandle( hWrite );
while( ReadFile( hRead , readBuf , 100 , &bytesRead , NULL ))
{
readBuf[bytesRead] = '/0';
printf( "Read %d bytes from pipe[%s]" , bytesRead , readBuf );
}
if( GetLastError() == ERROR_BROKEN_PIPE )
printf( "Pipe Closed by child process/n" );
else
printf( "Read Error:%d/n", GetLastError() );
CloseHandle( myInfo.hProcess );
CloseHandle( myInfo.hThread );
int iii;
cin>>iii;
return 0;
}
//////////////////////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "windows.h"
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
int i = 0;
for( ;; )
{
//printf( "Logging %d/n", i );
cout<<flush<<"Output Line:"<<i<<endl;
cerr<<flush<<"Error line"<<i<<endl;
_flushall();
}
return 1;
}