控制管道 StdIn 和 StdOut

发布于 2024-12-11 06:25:55 字数 1320 浏览 0 评论 0原文

我已经在网上阅读了一些有关如何重定向程序的标准输入和标准输出的教程,但我需要一些有关如何控制它的帮助。

到目前为止,我有这个:

HANDLE hSTD_OUT_READ;
HANDLE hSTD_OUT_WRITE;
HANDLE hSTD_IN_READ;
HANDLE hSTD_IN_WRITE;
SECURITY_ATTRIBUTES saAttr;
saAttr.nLength = sizeof(saAttr); 
saAttr.bInheritHandle = TRUE; 
saAttr.lpSecurityDescriptor = NULL;
CreatePipe(&hSTD_OUT_READ,&hSTD_OUT_WRITE,&saAttr,0);
SetHandleInformation(hSTD_OUT_READ, HANDLE_FLAG_INHERIT, 0);
CreatePipe(&hSTD_IN_READ,&hSTD_IN_WRITE,&saAttr,0);
SetHandleInformation(hSTD_IN_WRITE, HANDLE_FLAG_INHERIT, 0);
memset(&cmdProcess,0,sizeof(cmdProcess));
memset(&cmdInfo,0,sizeof(cmdInfo));
cmdProcess.cb = sizeof(cmdProcess);
cmdProcess.dwFlags = STARTF_USESTDHANDLES;
cmdProcess.hStdError = hSTD_OUT_WRITE;
cmdProcess.hStdOutput = hSTD_OUT_WRITE;
cmdProcess.hStdInput = hSTD_IN_READ;
CreateProcess(NULL,"cmd.exe",NULL,NULL,TRUE,0,NULL,NULL,&cmdProcess,&cmdInfo);
DWORD BytesWritten;
WriteFile(hSTD_IN_WRITE,&cmd,sizeof(cmd),&BytesWritten,NULL);
CloseHandle(hSTD_OUT_WRITE);
DWORD BytesRecvd;
while(true)
{
    bSUCCESS =  ReadFile(hSTD_OUT_READ,&recvd,sizeof(recvd),&BytesRecvd,NULL);
    printf(recvd);
    if( ! bSUCCESS || BytesRecvd == 0 ) break; 


}

此时,我已经收到了 Windows cmd 打印输出消息,但这不是对我的标准输入“whoami”的预期响应。我必须做什么?

I've read some tutorials on the net on how to redirect stdin and stdout of a program, but I need some help on how to control it.

So far I have this:

HANDLE hSTD_OUT_READ;
HANDLE hSTD_OUT_WRITE;
HANDLE hSTD_IN_READ;
HANDLE hSTD_IN_WRITE;
SECURITY_ATTRIBUTES saAttr;
saAttr.nLength = sizeof(saAttr); 
saAttr.bInheritHandle = TRUE; 
saAttr.lpSecurityDescriptor = NULL;
CreatePipe(&hSTD_OUT_READ,&hSTD_OUT_WRITE,&saAttr,0);
SetHandleInformation(hSTD_OUT_READ, HANDLE_FLAG_INHERIT, 0);
CreatePipe(&hSTD_IN_READ,&hSTD_IN_WRITE,&saAttr,0);
SetHandleInformation(hSTD_IN_WRITE, HANDLE_FLAG_INHERIT, 0);
memset(&cmdProcess,0,sizeof(cmdProcess));
memset(&cmdInfo,0,sizeof(cmdInfo));
cmdProcess.cb = sizeof(cmdProcess);
cmdProcess.dwFlags = STARTF_USESTDHANDLES;
cmdProcess.hStdError = hSTD_OUT_WRITE;
cmdProcess.hStdOutput = hSTD_OUT_WRITE;
cmdProcess.hStdInput = hSTD_IN_READ;
CreateProcess(NULL,"cmd.exe",NULL,NULL,TRUE,0,NULL,NULL,&cmdProcess,&cmdInfo);
DWORD BytesWritten;
WriteFile(hSTD_IN_WRITE,&cmd,sizeof(cmd),&BytesWritten,NULL);
CloseHandle(hSTD_OUT_WRITE);
DWORD BytesRecvd;
while(true)
{
    bSUCCESS =  ReadFile(hSTD_OUT_READ,&recvd,sizeof(recvd),&BytesRecvd,NULL);
    printf(recvd);
    if( ! bSUCCESS || BytesRecvd == 0 ) break; 


}

At this point, I have received the windows cmd printout message but that wasn't the expected respone to my stdin which was "whoami". What do i have to do?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

轮廓§ 2024-12-18 06:25:55

You can use WriteFile to write to your stdin pipe. But you have to create one first with CreatePipe. You also need a different pipe for stdin and stdout or else the cmd.exe will just go into an endless loop of erroring on its own output.

There is a complete example on MSDN:

http://msdn.microsoft.com/en-us/library/ms682499(v=vs.85).aspx

An old SO question that may also help:

Grab Program's Console Output

极度宠爱 2024-12-18 06:25:55

您将 NULL 作为最后两个参数传递给 ReadFile,这是不允许的。 文档

仅当 lpOverlapped 参数不为 NULL 时,该参数 [lpNumberOfBytesRead] 才可以为 NULL。

You passed NULL as the final two parameters to ReadFile, which is not permitted. The documentation says

This parameter [lpNumberOfBytesRead] can be NULL only when the lpOverlapped parameter is not NULL.

桃气十足 2024-12-18 06:25:55

你不断改变你的问题,所以除了最后一个答案之外,其他答案都毫无意义。您收到命令提示符启动消息,因为这是 cmd 打印到标准输出的第一件事。一切都按预期进行。

You keep changing your question, so the answers make no sense except for the last one. You are getting the command prompt startup message because that is the first thing that cmd prints to stdout. Everything is working as expected.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文