重定位子进程的标准输出至管道

 

Win7 VC6

 

1、

子进程 代码:

 1 #include <windows.h>
 2 #include <stdio.h>
 3 
 4 int main()
 5 {
 6     Sleep(1000);
 7 
 8     printf("Sub01 : *stdin : %x\n", *stdin);
 9     printf("Sub01 : *stdout : %x\n", *stdout);
10     printf("Sub01 : *stderr : %x\n", *stderr);
11     printf("\n");
12 
13     printf("Sub01 : stdin : %x\n", stdin);
14     printf("Sub01 : stdout : %x\n", stdout);
15     printf("Sub01 : stderr : %x\n", stderr);
16     printf("\n");
17 
18     printf("Sub01 : GetStdHandle(STD_INPUT_HANDLE) return : %x\n", GetStdHandle(STD_INPUT_HANDLE));
19     printf("Sub01 : GetStdHandle(STD_OUTPUT_HANDLE) return : %x\n", GetStdHandle(STD_OUTPUT_HANDLE));
20     printf("Sub01 : GetStdHandle(STD_ERROR_HANDLE) return : %x\n", GetStdHandle(STD_ERROR_HANDLE));
21     printf("\n");
22 
23 
24     return 0;
25 }

 

2、

父进程 代码:

 1 #include <windows.h>
 2 #include <stdio.h>
 3 
 4 #include <io.h>
 5 #include <Fcntl.h>
 6 
 7 int main()
 8 {
 9 // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
10     // 创建 管道
11 
12     HANDLE hRead = 0, hWrite = 0;
13 
14     SECURITY_ATTRIBUTES sa = {0};
15     sa.nLength = sizeof(SECURITY_ATTRIBUTES);
16     sa.lpSecurityDescriptor = NULL;
17     sa.bInheritHandle = true;
18     if (! CreatePipe(&hRead, &hWrite, &sa, 0))
19         {return 0;}
20     printf("Parent 01 - hWrite : %x, &hWrite : %x\n", hWrite, &hWrite);
21 
22 // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
23     // 创建 子进程
24 
25     STARTUPINFO si = {0};
26     si.cb = sizeof(STARTUPINFO);
27     GetStartupInfo(&si);
28     si.wShowWindow = SW_SHOW;
29     si.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
30     si.hStdOutput = hWrite;
31     printf("Parent 01 - &si.hStdOutput : %x\n", &si.hStdOutput);
32 
33     PROCESS_INFORMATION pi = {0};
34     if (! CreateProcess(NULL, "Console_Sub_VC6_01.exe", NULL, NULL, true, 0, NULL, NULL, &si, &pi))
35     {
36         return 0;
37     }
38     CloseHandle(hWrite); // ZC: 这里不关闭的话,就少关闭了1次,当子进程退出时 下面的ReadFile还会继续等待数据。
39 
40     // *** *** *** 
41     // 接收 子进程 的标准输出
42 
43     while (true)
44     {
45         printf("1\n");
46         fflush(stdout);
47 
48         DWORD bytesRead = 0;
49         char cBuffer[4096] = {0};
50         //Sleep(2000);
51         if (! ReadFile(hRead, &cBuffer[0], 4095, &bytesRead, NULL))
52             {break;}
53         cBuffer[bytesRead] = '\0';
54         printf("%s\n", cBuffer);
55 
56         printf("2\n");
57         fflush(stdout);
58     }
59 
60     return 0;
61 }

 

3、

父进程中的输出信息:

 1 Parent 01 - hWrite : 38, &hWrite : 18ff40  // ZC: 总感觉这个句柄值怪怪的,觉得太短了。难道句柄的值都不长?
 2 Parent 01 - &si.hStdOutput : 18ff2c
 3 1
 4 Sub01 : *stdin : 4285e0
 5 Sub01 : *stdout : 4d2cb8  // ZC: 这个值,我猜 应该是 由管道hWrite(HANDLE) 创建而来的 流的指针。
 6 Sub01 : *stderr : 0
 7 
 8 Sub01 : stdin : 424a30
 9 Sub01 : stdout : 424a50
10 Sub01 : stderr : 424a70
11 
12 Sub01 : GetStdHandle(STD_INPUT_HANDLE) return : ffffffff
13 Sub01 : GetStdHandle(STD_OUTPUT_HANDLE) return : 38    // ZC: 可以看到子,这个值可父进程的 管道hWrite 的值是一样的。这是一个HANDLE。
14 Sub01 : GetStdHandle(STD_ERROR_HANDLE) return : ffffffff
15 
16 
17 2
18 1
19 Press any key to continue

 

 

 

X

 

posted @ 2015-11-09 14:07  CodeSkill  阅读(328)  评论(0编辑  收藏  举报