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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • ISP举例_low input lag

    ISP举例_low input lag  从2000年9月底摄像头首次出现在手机上算起,到如今成为诸多智能设备不可或缺的一部分,便携式手机摄像头已经走过了18年的发展历程。随着手机智能化、轻薄化的发展进程,其搭载的摄像头也随之发生了变化,但基本结构并未有太大的改变。通常而言,一个摄像头硬件应包含以下五个部分:外壳(Housing)或者镜头固定物(LensHolder)、镜头(Lens)、红外截止滤波片(IR-cutfilter…

    2025年8月14日
    2
  • Linux apache的运行用户和用户组

    Linux apache的运行用户和用户组

    2021年9月24日
    52
  • 华为电脑如何投屏到电视linux,华为mate10/mate10pro怎么投屏至电视或电脑上面?「建议收藏」

    一、使用华为2代DOCK实现手机连接大屏说明手机投屏输出接口为Type-C,支持通过转换器转换成标准的DP、HDMI、MiniDP、VGA、DVI等接口。考虑到设备的兼容性和信号的转换,请优先选择DP、HDMI接口。1.打开大屏显示器;2.将VGA直连线(两端都是VGA接口)的一端连接大屏,另一端连接至华为2代DOCK;3.将华为2代DOCK(扩展坞)的Type-C接口(USB-C接口)连接到华…

    2022年4月6日
    733
  • Linux| |对于UDP的学习

    Linux| |对于UDP的学习

    2022年2月12日
    42
  • loadrunner-11安装+激活成功教程+汉化(提供安装包,激活成功教程方式,汉化包)

    loadrunner-11安装+激活成功教程+汉化(提供安装包,激活成功教程方式,汉化包)loadrunner-11安装+激活成功教程+汉化(提供安装包,激活成功教程方式,汉化包)一、loadrunner-11安装下载地址: 链接:https://pan.baidu.com/s/10meUz5DfkS8WleLSOalCtQ 提取码:iw0p      由于LR11安装包三个多G,没办法上传到CSDN上,就用百度云去下载,注意下载时选择足够的盘符空间;      …

    2022年7月22日
    36
  • java程序运行机制的特点_Java语言的特点

    java程序运行机制的特点_Java语言的特点特点一:面向对象1、两个基本概念:类、对象2、三大特性:封装、继承、多态特点二:健壮性吸收了C/C++语言的优点,但去掉了其影响程序健壮性的部分(如指针、内存的申请与释放等),提供了一个相对安全的内存管理和访问机制特点三:跨平台性跨平台性:通过Java语言编写的应用程序在不同的系统平台上都可以运行。“Writeonce,RunAnywhere”原理:只要在需要运行java应用程序的操作系…

    2022年7月8日
    15

发表回复

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

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