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


相关推荐

  • WPF TextBox使用密码样式

    WPF TextBox使用密码样式WPF的Textbox并没有password样式,可以手动更改<TextBoxWidth=”200″Margin=”10,0,0,0″Text=”{BindingappData.CurrentUser.LoginPwd}”Background=”Transparent”><TextBo

    2022年7月25日
  • MySQL常用命令大全(完整)

    MySQL常用命令大全(完整)打开Linux或MacOS的Terminal(终端)直接在终端中输入windows快捷键win+R,输入cmd,直接在cmd上输入1、mysql服务的启动和停止netstopmysqlnetstartmysql启动失败可按快捷键win+R,输入services.msc,找到MySQL服务器的名称启动2、登陆mysqlmysql(…

    2022年6月30日
  • SplitContainer 控件作用[通俗易懂]

    SplitContainer 控件作用[通俗易懂]原文出处:http://blog.csdn.net/xiaobai1593/article/details/7356973作用:(Splitter的简化)使用 SplitContainer 控件可分隔容器(如 Form)的显示区域,并使用户可以调整已添加到 SplitContainer 面板中的控件的大小。当用户将鼠标指针移到拆分器上时,光标将发生变化,以指示可以调整 SplitC

    2022年7月18日
  • mac datagrip2021.4 激活码_通用破解码「建议收藏」

    mac datagrip2021.4 激活码_通用破解码,https://javaforall.net/100143.html。详细ieda激活码不妨到全栈程序员必看教程网一起来了解一下吧!

    2022年3月17日
  • UML时序图知识

    UML时序图知识1.时序图(SequenceDiagrams)时序图描述对象之间消息的发送顺序,强调时间顺序。时序图是一个二维图,横轴表示对象,纵轴表示时间,消息在各对象之间横向传递,依照时间顺序纵向排列。用箭头表示消息、用竖虚线表示对象生命线。2.时序图的作用展示对象之间交互的顺序。将交互行为建模为消息传递,通过描述消息是如何在对象间发送和接收的来动态展示对象之间的交互;相对于其他UML图,时序图更强调交互的时间顺序;可以直观的描述并发进程。3.时序图组成元素角色(Actor)系统

    2022年6月18日
  • java自定义注解和使用[通俗易懂]

    自定义注解@Target自定义注解的使用范围ElementType.METHOD:方法声明ElementType.TYPE:类、接口(包括注解类型)或enum声明ElementType.CONSTRUCTOR:构造器的声明ElementType.FIELD:域声明(包括enum实例)ElementType.LOCAL_VARIABLE:局部变量声明ElementType.PACKA…

    2022年4月13日

发表回复

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

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