Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2653570
  • 博文数量: 416
  • 博客积分: 10220
  • 博客等级: 上将
  • 技术积分: 4193
  • 用 户 组: 普通用户
  • 注册时间: 2006-12-15 09:47
文章分类

全部博文(416)

文章存档

2022年(1)

2021年(1)

2020年(1)

2019年(5)

2018年(7)

2017年(6)

2016年(7)

2015年(11)

2014年(1)

2012年(5)

2011年(7)

2010年(35)

2009年(64)

2008年(48)

2007年(177)

2006年(40)

我的朋友

分类: C/C++

2009-10-16 15:16:05

1。把调试通过的代码转移过来了.
2。注意client程序要用fflush或cout方式,若直接用prinf测试可能输出无结果
3. 能双向传递参数

// pipeChild.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include
#include
#include
using namespace std;
//int main()
//{
// 
// int a, b ;
// while ( cin >> a >> b && ( a || b ) )
// {
//  fprintf(stdout, "hello world a=%d, b=%d\t", a, b);
//  fflush(stdout);
//  cout << a + b << endl ;
// }
//
// return 0;
//}

 

//#define CREATE_ORG
#ifdef CREATE_ORG
int main()
{
 for (int i = 0; i < 100; i++)
 {
  fprintf(stdout,"Hello World- %d\n",i);
  fflush(stdout);
  Sleep(100);

  
 }
 return 0;
}
#else
//#define LPTSTR char *

const int BUFSIZE = 4096 ;
HANDLE  hChildStdinRd, hChildStdinWr, hChildStdinWrDup,
  hChildStdoutRd,hChildStdoutWr,hChildStdoutRdDup,
  hSaveStdin,    hSaveStdout;

BOOL CreateChildProcess(LPTSTR);
VOID WriteToPipe(LPTSTR);
VOID ReadFromPipe(LPTSTR);
VOID ErrorExit(LPTSTR);
VOID ErrMsg(LPTSTR, BOOL);
void main( int argc, char *argv[] )

 // 处理输入参数
 if ( argc != 4 )
  return ;
 // 分别用来保存命令行,输入文件名(CPP/C),输出文件名(保存编译信息)
 LPTSTR lpProgram = new char[ strlen(argv[1])+1 ] ;
 strcpy ( lpProgram, argv[1] ) ;
 LPTSTR lpInputFile = new char[ strlen(argv[2])+1 ];
 strcpy ( lpInputFile, argv[2] ) ;
 LPTSTR lpOutputFile = new char[ strlen(argv[3])+1 ] ;
 strcpy ( lpOutputFile, argv[3] ) ;   

 SECURITY_ATTRIBUTES saAttr;
 saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
 saAttr.bInheritHandle = TRUE;
 saAttr.lpSecurityDescriptor = NULL;

 /************************************************
 *    redirecting child process's STDOUT  *
 ************************************************/
 hSaveStdout = GetStdHandle(STD_OUTPUT_HANDLE);

 if (! CreatePipe(&hChildStdoutRd, &hChildStdoutWr, &saAttr, 0))
  ErrorExit("Stdout pipe creation failed\n");

 if (! SetStdHandle(STD_OUTPUT_HANDLE, hChildStdoutWr))
  ErrorExit("Redirecting STDOUT failed");

 BOOL fSuccess = DuplicateHandle(
  GetCurrentProcess(),
  hChildStdoutRd,
  GetCurrentProcess(),
  &hChildStdoutRdDup ,
  0,
  FALSE,
  DUPLICATE_SAME_ACCESS);
 if( !fSuccess )
  ErrorExit("DuplicateHandle failed");
 CloseHandle(hChildStdoutRd);

 /************************************************
 *    redirecting child process's STDIN    *
 ************************************************/
 hSaveStdin = GetStdHandle(STD_INPUT_HANDLE);

 if (! CreatePipe(&hChildStdinRd, &hChildStdinWr, &saAttr, 0))
  ErrorExit("Stdin pipe creation failed\n");

 if (! SetStdHandle(STD_INPUT_HANDLE, hChildStdinRd))
  ErrorExit("Redirecting Stdin failed");

 fSuccess = DuplicateHandle(
  GetCurrentProcess(),
  hChildStdinWr,
  GetCurrentProcess(),
  &hChildStdinWrDup,
  0,
  FALSE,                
  DUPLICATE_SAME_ACCESS);
 if (! fSuccess)
  ErrorExit("DuplicateHandle failed");
 CloseHandle(hChildStdinWr);  

 /************************************************
 *      创建子进程(即启动SAMPLE.EXE)    *
 ************************************************/
 fSuccess = CreateChildProcess( lpProgram );
 if ( !fSuccess )
  ErrorExit("Create process failed");

 // 父进程输入输出流的还原设置
 if (! SetStdHandle(STD_INPUT_HANDLE, hSaveStdin))
  ErrorExit("Re-redirecting Stdin failed\n");
 //if (! SetStdHandle(STD_OUTPUT_HANDLE, hSaveStdout))
 // ErrorExit("Re-redirecting Stdout failed\n");

 WriteToPipe( lpInputFile ) ;
 ReadFromPipe( lpOutputFile );
 delete lpProgram ;
 delete lpInputFile ;
 delete lpOutputFile ;
}

BOOL CreateChildProcess( LPTSTR lpProgram )
{
 PROCESS_INFORMATION piProcInfo;
 STARTUPINFO siStartInfo;
 BOOL bFuncRetn = FALSE;

 ZeroMemory( &piProcInfo, sizeof(PROCESS_INFORMATION) );
 ZeroMemory( &siStartInfo, sizeof(STARTUPINFO) );
 siStartInfo.cb = sizeof(STARTUPINFO);

 bFuncRetn = CreateProcess ( NULL, lpProgram, NULL, NULL, TRUE, \
  0, NULL, NULL, &siStartInfo, &piProcInfo);
 if (bFuncRetn == 0)
 {
  ErrorExit("CreateProcess failed\n");
  return 0;
 }
 else
 {
  CloseHandle(piProcInfo.hProcess);
  CloseHandle(piProcInfo.hThread);
  return bFuncRetn;
 }
}

VOID WriteToPipe( LPTSTR lpInputFile )
{
 HANDLE hInputFile = CreateFile(lpInputFile, GENERIC_READ, 0, NULL,
  OPEN_EXISTING, FILE_ATTRIBUTE_READONLY, NULL);
 if (hInputFile == INVALID_HANDLE_VALUE)
  return ;

 BOOL fSuccess ;
 DWORD dwRead, dwWritten;
 CHAR chBuf[BUFSIZE] = {0} ;

 for (;;)
 {
  fSuccess = ReadFile( hInputFile, chBuf, BUFSIZE, &dwRead, NULL) ;
  if ( !fSuccess || dwRead == 0)
   break;

  fSuccess = WriteFile( hChildStdinWrDup, chBuf, dwRead, &dwWritten, NULL) ;
  if ( !fSuccess )
   break;
 }

 if (! CloseHandle(hChildStdinWrDup))
  ErrorExit("Close pipe failed\n");

 CloseHandle ( hInputFile ) ;
}

VOID ReadFromPipe( LPTSTR lpOutputFile )
{
 HANDLE hOutputFile = CreateFile( lpOutputFile, GENERIC_READ|GENERIC_WRITE,
  FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
 if (hOutputFile == INVALID_HANDLE_VALUE)
  return ;

 BOOL fSuccess ;
 DWORD dwRead, dwWritten;
 CHAR chBuf[BUFSIZE] = { 0 };

 if (!CloseHandle(hChildStdoutWr))
  ErrorExit("Closing handle failed");

 for (;;)
 {
  fSuccess = ReadFile( hChildStdoutRdDup, chBuf, BUFSIZE, &dwRead, NULL) ;
  if( !fSuccess || dwRead == 0)
  {
   break;
  }
  fSuccess = WriteFile( hOutputFile, chBuf, dwRead, &dwWritten, NULL) ;
  if ( !fSuccess )
   break;
 }

 CloseHandle ( hOutputFile ) ;
}
VOID ErrorExit (LPTSTR lpszMessage)
{
 MessageBox( 0, lpszMessage, 0, 0 );
}


#endif

//用管道从CMD窗口读FFMPEG执行信息的方法
 
阅读(1052) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~