CreatePipe()等函数创建管道来操纵控制台

CreatePipe()等函数创建管道来操纵控制台#include  #include    #define BUFSIZE 4096   HANDLE hChildStdinRd, hChildStdinWr, hChildStdinWrDup,    hChildStdoutRd, hChildStdoutWr, hChildStdoutRdDup,    hInputFile, hStdout;   BO

大家好,又见面了,我是你们的朋友全栈君。

#include <stdio.h> 
#include <windows.h> 
  
#define BUFSIZE 4096 
  
HANDLE 
hChildStdinRd, hChildStdinWr, hChildStdinWrDup, 
   
hChildStdoutRd, hChildStdoutWr, hChildStdoutRdDup, 
   
hInputFile, hStdout; 
  
BOOL 
CreateChildProcess(
VOID
); 
VOID 
WriteToPipe(
VOID
); 
VOID 
ReadFromPipe(
VOID
); 
VOID 
ErrorExit(
LPTSTR
); 
VOID 
ErrMsg(
LPTSTR

BOOL
); 
  
DWORD 
main(
int 
argc, 
char 
*argv[]) 
   
SECURITY_ATTRIBUTES saAttr; 
   
BOOL 
fSuccess; 
  
// Set the bInheritHandle flag so pipe handles are inherited. 
  
   
saAttr.nLength = 
sizeof
(SECURITY_ATTRIBUTES); 
   
saAttr.bInheritHandle = TRUE; 
   
saAttr.lpSecurityDescriptor = NULL; 
  
// Get the handle to the current STDOUT. 
  
   
hStdout = GetStdHandle(STD_OUTPUT_HANDLE); 
  
// Create a pipe for the child process's STDOUT. 
  
   
if 
(! CreatePipe(&hChildStdoutRd, &hChildStdoutWr, &saAttr, 0)) 
      
ErrorExit(
"Stdout pipe creation failed\n"
); 
  
// Create noninheritable read handle and close the inheritable read 
// handle. 
 
    
fSuccess = DuplicateHandle(GetCurrentProcess(), hChildStdoutRd,
        
GetCurrentProcess(), &hChildStdoutRdDup , 0,
        
FALSE,
        
DUPLICATE_SAME_ACCESS);
    
if
( !fSuccess )
        
ErrorExit(
"DuplicateHandle failed"
);
    
CloseHandle(hChildStdoutRd);
 
// Create a pipe for the child process's STDIN. 
  
   
if 
(! CreatePipe(&hChildStdinRd, &hChildStdinWr, &saAttr, 0)) 
      
ErrorExit(
"Stdin pipe creation failed\n"
); 
  
// Duplicate the write handle to the pipe so it is not inherited. 
  
   
fSuccess = DuplicateHandle(GetCurrentProcess(), hChildStdinWr, 
      
GetCurrentProcess(), &hChildStdinWrDup, 0, 
      
FALSE,                  
// not inherited 
      
DUPLICATE_SAME_ACCESS); 
   
if 
(! fSuccess) 
      
ErrorExit(
"DuplicateHandle failed"
); 
  
   
CloseHandle(hChildStdinWr); 
  
// Now create the child process. 
    
   
fSuccess = CreateChildProcess();
   
if 
(! fSuccess) 
      
ErrorExit(
"Create process failed"
); 
 
// Get a handle to the parent's input file. 
  
   
if 
(argc == 1) 
      
ErrorExit(
"Please specify an input file"
);
 
   
hInputFile = CreateFile(argv[1], GENERIC_READ, 0, NULL, 
      
OPEN_EXISTING, FILE_ATTRIBUTE_READONLY, NULL); 
  
   
if 
(hInputFile == INVALID_HANDLE_VALUE) 
      
ErrorExit(
"CreateFile failed\n"
); 
  
// Write to pipe that is the standard input for a child process. 
  
   
WriteToPipe(); 
  
// Read from pipe that is the standard output for child process. 
  
   
ReadFromPipe(); 
  
   
return 
0; 
  
BOOL 
CreateChildProcess() 
   
PROCESS_INFORMATION piProcInfo; 
   
STARTUPINFO siStartInfo;
   
BOOL 
bFuncRetn = FALSE; 
  
// Set up members of the PROCESS_INFORMATION structure. 
  
   
ZeroMemory( &piProcInfo, 
sizeof
(PROCESS_INFORMATION) );
  
// Set up members of the STARTUPINFO structure. 
  
   
ZeroMemory( &siStartInfo, 
sizeof
(STARTUPINFO) );
   
siStartInfo.cb = 
sizeof
(STARTUPINFO); 
   
siStartInfo.hStdError = hChildStdoutWr;
   
siStartInfo.hStdOutput = hChildStdoutWr;
   
siStartInfo.hStdInput = hChildStdinRd;
   
siStartInfo.dwFlags |= STARTF_USESTDHANDLES;
  
// Create the child process. 
     
   
bFuncRetn = 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 
    
   
if 
(bFuncRetn == 0) 
      
ErrorExit(
"CreateProcess failed"
);
   
else 
   
{
      
CloseHandle(piProcInfo.hProcess);
      
CloseHandle(piProcInfo.hThread);
      
return 
bFuncRetn;
   
}
}
  
VOID 
WriteToPipe(
VOID
   
DWORD 
dwRead, dwWritten; 
   
CHAR 
chBuf[BUFSIZE]; 
  
// Read from a file and write its contents to a pipe. 
  
   
for 
(;;) 
   
      
if 
(! ReadFile(hInputFile, chBuf, BUFSIZE, &dwRead, NULL) || 
         
dwRead == 0) 
break
      
if 
(! WriteFile(hChildStdinWrDup, chBuf, dwRead, 
         
&dwWritten, NULL)) 
break
   
  
// Close the pipe handle so the child process stops reading. 
  
   
if 
(! CloseHandle(hChildStdinWrDup)) 
      
ErrorExit(
"Close pipe failed"
); 
  
VOID 
ReadFromPipe(
VOID
   
DWORD 
dwRead, dwWritten; 
   
CHAR 
chBuf[BUFSIZE]; 
 
// Close the write end of the pipe before reading from the 
// read end of the pipe. 
  
   
if 
(!CloseHandle(hChildStdoutWr)) 
      
ErrorExit(
"CloseHandle failed"
); 
  
// Read output from the child process, and write to parent's STDOUT. 
  
   
for 
(;;) 
   
      
if
( !ReadFile( hChildStdoutRdDup, chBuf, BUFSIZE, &dwRead, 
         
NULL) || dwRead == 0) 
break
      
if 
(! WriteFile(hStdout, chBuf, dwRead, &dwWritten, NULL)) 
         
break
   
  
VOID 
ErrorExit (
LPTSTR 
lpszMessage) 
   
fprintf
(stderr, 
"%s\n"
, lpszMessage); 
   
ExitProcess(0); 
}
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/159417.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • pycharm修改环境_pycharm更改环境

    pycharm修改环境_pycharm更改环境因为有时需要在Terminal中调试代码,而且调试前需要配置环境变量,虽然操作不麻烦,但用起来总不习惯.特别是项目比较多时,需要频繁进到设置里面去改.以前设置环境变量的操作如下图:Settings-Tools-Terminal-ProjectSettings-Enviromentvariables添加环境变量比如我们有一个文件env.list里面的内容如下:IN_HOST=’192.168.0.6’#数据库主机IPIN_PORT=8888#数据库端口IN_US

    2022年8月27日
    4
  • 使用ctk库

    使用ctk库上篇文章写的如何生成一个简易ctk动态库https://blog.csdn.net/qq_16238157/article/details/86602476这篇文章写如何简易的使用交代路径下图上篇文章已经写过关于ctk动态库如何生成下面介绍一下各个文件夹:CTK文件夹:是ctk的源码ctkWork文件夹:用vs编译生成的ctk插件myCTK文件夹:是网上找的一个调用ct…

    2022年6月3日
    70
  • sftp特定端口连接「建议收藏」

    sftp特定端口连接「建议收藏」默认走22端口如果需要修改端口号sftp-oPort=55288root@192.168.0.254使用-o选项来指定端口号.-oPort=远程端口号

    2022年9月14日
    3
  • pycharm 激活码在线[最新免费获取]

    (pycharm 激活码在线)2021最新分享一个能用的的激活码出来,希望能帮到需要激活的朋友。目前这个是能用的,但是用的人多了之后也会失效,会不定时更新的,大家持续关注此网站~IntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,下面是详细链接哦~https://javaforall.net/100143.html…

    2022年3月28日
    60
  • 图像处理算法工程师——1必备技能总结——2面试题大全[通俗易懂]

    图像处理算法工程师——1必备技能总结——2面试题大全[通俗易懂]图像算法工程师三重境界:一、传统图像算法工程师:主要涉及图形处理,包括形态学、图像质量、相机成像之3A算法、去雾处理、颜色空间转换、滤镜等,主要在安防公司或者机器视觉领域,包括缺陷检测;二、现代图像算法工程师:涉及模式识别,主要表现的经验为Adaboost、SVM的研究与应用,特征选取与提取,包括智能驾驶的研究与应用、行人检测、人脸识别;三、人工智能时代图像算法工程师:…

    2022年5月17日
    57
  • Idea快捷键大全_零之轨迹超详细攻略

    Idea快捷键大全_零之轨迹超详细攻略4.1、字体设置file–>settings–>输入font–>设置字体样式以及字号大小。4.2、快速生成main方法psvm、main4.3、快速生成System.out.println()sout4.4、注意:IDEA是自动保存,不需要ctrl+s4.5、删除一行ctrl+y4.6、怎么运行:代码上右键–>run或者点击左侧的绿色箭头。ctrl+shift+F104.7、左侧窗口中的列表怎么展开?怎么关闭?左箭头关闭。

    2022年9月28日
    2

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号