zoukankan      html  css  js  c++  java
  • 重定位子进程的标准输出至管道

    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
    ", *stdin);
     9     printf("Sub01 : *stdout : %x
    ", *stdout);
    10     printf("Sub01 : *stderr : %x
    ", *stderr);
    11     printf("
    ");
    12 
    13     printf("Sub01 : stdin : %x
    ", stdin);
    14     printf("Sub01 : stdout : %x
    ", stdout);
    15     printf("Sub01 : stderr : %x
    ", stderr);
    16     printf("
    ");
    17 
    18     printf("Sub01 : GetStdHandle(STD_INPUT_HANDLE) return : %x
    ", GetStdHandle(STD_INPUT_HANDLE));
    19     printf("Sub01 : GetStdHandle(STD_OUTPUT_HANDLE) return : %x
    ", GetStdHandle(STD_OUTPUT_HANDLE));
    20     printf("Sub01 : GetStdHandle(STD_ERROR_HANDLE) return : %x
    ", GetStdHandle(STD_ERROR_HANDLE));
    21     printf("
    ");
    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
    ", 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
    ", &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
    ");
    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] = '';
    54         printf("%s
    ", cBuffer);
    55 
    56         printf("2
    ");
    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

  • 相关阅读:
    Linux培训教程lgzip命令详解和使用实例
    Linux 新手应该知道的一些求助命令
    “变态教育创导者”兄弟连教育新三板挂牌上市
    linux中more命令如何使用
    html上标与下标应用
    linux命令大全之cal命令详解(显示日历)
    成为java高手的八大条件
    mysql 1055
    MySQL更改口令报错ERROR 1064
    centos7.5 安装mysql8.0
  • 原文地址:https://www.cnblogs.com/CodeSkill/p/4949799.html
Copyright © 2011-2022 走看看