- 比较麻烦,可以看看MSDN里面的例子。首先要保存旧的标准输入输出handle。然后设置管道的属性,管道必须是可以继承的。建立了一个管道以后,把写管道设置成标准输出。然后通过CreateProcess建立子进程。然后再把读管道设置成标准输入。则子程序对标准输出的写就会在附近成的标准读里面获得。不过改了这个handle好像对printf,gets这些没有影响的。还不如用_pipe。下面是例子,改自MSDN:
- //main.cpp
- #include <Windows.h>
- #include <Winbase.h>
- #include <stdio.h>
- #include <stdlib.h>
- void ErrorExit(const char * lpszInfo)
- {
- perror(lpszInfo);
- exit(1);
- }
- int main(void)
- {
- HANDLE hMainStdinRd, hChildStdoutWr,hMainStdoutWr, hChildStdinRd,
- hSaveStdin, hSaveStdout;
- SECURITY_ATTRIBUTES saAttr;
- saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
- saAttr.bInheritHandle = TRUE;
- saAttr.lpSecurityDescriptor = NULL;
- hSaveStdout = GetStdHandle(STD_OUTPUT_HANDLE);
- hSaveStdin = GetStdHandle(STD_INPUT_HANDLE);
- if (! CreatePipe(&hMainStdinRd, &hChildStdoutWr, &saAttr, 0))
- ErrorExit("Stdout pipe creation failed/n");
- if (! SetStdHandle(STD_OUTPUT_HANDLE, hChildStdoutWr))
- ErrorExit("Redirecting Child STDOUT failed");
- if (! CreatePipe(&hChildStdinRd, &hMainStdoutWr, &saAttr, 0))
- ErrorExit("Stdin pipe creation failed/n");
- if (! SetStdHandle(STD_INPUT_HANDLE, hChildStdinRd))
- ErrorExit("Redirecting Child Stdin failed");
- PROCESS_INFORMATION piProcInfo;
- STARTUPINFO siStartInfo;
- ZeroMemory( &piProcInfo, sizeof(PROCESS_INFORMATION) );
- ZeroMemory( &siStartInfo, sizeof(STARTUPINFO) );
- siStartInfo.cb = sizeof(STARTUPINFO);
- if (!CreateProcess(NULL,
- "child", // command line
- NULL, // process security attributes
- NULL, // primary thread security attributes
- TRUE, // handles are inherited
- 0, // creation flags
- NULL, // use parent's environment
- NULL, // use parent's current directory
- &siStartInfo, // STARTUPINFO pointer
- &piProcInfo)) // receives PROCESS_INFORMATION
- ErrorExit("Create process failed");
- char buf[1024] = "Hello world";
- DWORD dwWritten;
- if (! SetStdHandle(STD_OUTPUT_HANDLE, hMainStdoutWr))
- ErrorExit("Redirecting Local STDOUT failed");
- if (! SetStdHandle(STD_INPUT_HANDLE, hMainStdinRd))
- ErrorExit("Redirecting Local Stdin failed");
- if (! WriteFile(hMainStdoutWr,buf,strlen(buf),&dwWritten, NULL))
- ErrorExit("Write Error/n");
- if (! ReadFile(hMainStdinRd,buf,1023,&dwWritten, NULL))
- ErrorExit("Read Error/n");
- perror(buf);
- SetStdHandle(STD_OUTPUT_HANDLE, hSaveStdout);
- SetStdHandle(STD_INPUT_HANDLE, hSaveStdin);
- return 1;
- }
- //child.cpp
- #include <windows.h>
- #include <stdio.h>
- #include <stdlib.h>
- #define BUFSIZE 1024
- int main(void)
- {
- perror("Begin to work");
- char buf[BUFSIZE];
- HANDLE hStdin, hStdout;
- DWORD dwRead, dwWritten;
- BOOL fSuccess;
- hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
- hStdin = GetStdHandle(STD_INPUT_HANDLE);
- if ((hStdout == INVALID_HANDLE_VALUE) ||
- (hStdin == INVALID_HANDLE_VALUE))
- {
- perror("Error handle");
- exit(0);
- }
- fSuccess = ReadFile(hStdin, buf, BUFSIZE, &dwRead, NULL);
- if (! fSuccess || dwRead == 0)
- {
- perror("Error Read");
- exit(1);
- }
- // Write to standard output.
- buf[dwRead] = '/0';
- dwRead = strlen(strcat(buf,"Child Get it/n"));
- fSuccess = WriteFile(hStdout, buf, dwRead, &dwWritten, NULL);
- if (! fSuccess)
- {
- perror("Error Write");
- exit(1);
- }
- return 1;
- }
如何把标准输出重定向为指定缓冲区 http://topic.csdn.net/t/20020111/16/472320.html
最新推荐文章于 2024-01-11 20:04:21 发布