博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
重定位子进程的标准输出至管道
阅读量:6936 次
发布时间:2019-06-27

本文共 3266 字,大约阅读时间需要 10 分钟。

 

Win7 VC6

 

1、

子进程 代码:

1 #include 
2 #include
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 
2 #include
3 4 #include
5 #include
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 : 424a5010 Sub01 : stderr : 424a7011 12 Sub01 : GetStdHandle(STD_INPUT_HANDLE) return : ffffffff13 Sub01 : GetStdHandle(STD_OUTPUT_HANDLE) return : 38    // ZC: 可以看到子,这个值可父进程的 管道hWrite 的值是一样的。这是一个HANDLE。14 Sub01 : GetStdHandle(STD_ERROR_HANDLE) return : ffffffff15 16 17 218 119 Press any key to continue

 

 

 

X

 

转载于:https://www.cnblogs.com/CodeSkill/p/4949799.html

你可能感兴趣的文章
函数练习
查看>>
python学习第一天 -----2019年4月15日
查看>>
如何用node命令和webpack命令传递参数 转载
查看>>
51nod 1105:第K大的数
查看>>
POJ 3368 Frequent values 线段树区间合并
查看>>
Ubuntu Linux 下优化 swap 交换分区及调整swap大小
查看>>
“undefined reference to JNI_GetCreatedJavaVM”和“File format not recognized”错误原因分析...
查看>>
Spring定时任务的配置
查看>>
了解清除警告
查看>>
SBJson
查看>>
XML文档操作工具类
查看>>
时间复杂度O(n),空间复杂度O(1)的排序
查看>>
终于知道10月27-28-29这3天为什么调整了
查看>>
TTDebug 快速打印 log ------rect point size
查看>>
合唱队形(DP)
查看>>
Java中创建对象的几种方式
查看>>
上传下载优化版本
查看>>
UESTC 1698 The Game
查看>>
visual studio 多行编辑 列编辑
查看>>
明日草稿
查看>>