- 注册时间
- 2006-2-26
- 最后登录
- 2008-10-24
⑥精研
- 积分
- 2470
|
如题……
嘛,用WIN32 API就会怎么搞,CreateProcess的参数里可以指定子进程stdin/stdout的HANDLE,但换CRT就不懂了……
这个MSDN的例子:- // crt_BeepFilter.C
- // arguments: crt_beeper.exe
- #include <windows.h>
- #include <process.h>
- #include <memory.h>
- #include <string.h>
- #include <stdio.h>
- #include <fcntl.h>
- #include <io.h>
- #define OUT_BUFF_SIZE 512
- #define READ_FD 0
- #define WRITE_FD 1
- #define BEEP_CHAR 7
- char szBuffer[OUT_BUFF_SIZE];
- int Filter(char* szBuff, ULONG nSize, int nChar)
- {
- char* szPos = szBuff + nSize -1;
- char* szEnd = szPos;
- int nRet = nSize;
- while (szPos > szBuff)
- {
- if (*szPos == nChar)
- {
- memmove(szPos, szPos+1, szEnd - szPos);
- --nRet;
- }
- --szPos;
- }
- return nRet;
- }
- int main(int argc, char** argv)
- {
- int nExitCode = STILL_ACTIVE;
- if (argc >= 2)
- {
- HANDLE hProcess;
- int fdStdOut;
- int fdStdOutPipe[2];
- // Create the pipe
- if(_pipe(fdStdOutPipe, 512, O_NOINHERIT) == -1)
- return 1;
- // Duplicate stdout file descriptor (next line will close original)
- fdStdOut = _dup(_fileno(stdout));
- // Duplicate write end of pipe to stdout file descriptor
- if(_dup2(fdStdOutPipe[WRITE_FD], _fileno(stdout)) != 0)
- return 2;
- // Close original write end of pipe
- close(fdStdOutPipe[WRITE_FD]);
- // Spawn process
- hProcess = (HANDLE)spawnvp(P_NOWAIT, argv[1],
- (const char* const*)&argv[1]);
- // Duplicate copy of original stdout back into stdout
- if(_dup2(fdStdOut, _fileno(stdout)) != 0)
- return 3;
- // Close duplicate copy of original stdout
- close(fdStdOut);
- if(hProcess)
- {
- int nOutRead;
- while (nExitCode == STILL_ACTIVE)
- {
- nOutRead = read(fdStdOutPipe[READ_FD],
- szBuffer, OUT_BUFF_SIZE);
- if(nOutRead)
- {
- nOutRead = Filter(szBuffer, nOutRead, BEEP_CHAR);
- fwrite(szBuffer, 1, nOutRead, stdout);
- }
- if(!GetExitCodeProcess(hProcess,(unsigned long*)&nExitCode))
- return 4;
- }
- }
- }
- return nExitCode;
- }
复制代码
不明白他哪里重定向了子进程的HANDLE…… |
|