控制管道 StdIn 和 StdOut
我已经在网上阅读了一些有关如何重定向程序的标准输入和标准输出的教程,但我需要一些有关如何控制它的帮助。
到目前为止,我有这个:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用 WriteFile 来编写到您的
stdin
管道。但您必须先使用 CreatePipe< 创建一个/a>.您还需要为stdin
和stdout
使用不同的管道,否则cmd.exe
将进入其自身输出错误的无限循环。MSDN 上有一个完整的示例:
http:// /msdn.microsoft.com/en-us/library/ms682499(v=vs.85).aspx
一个旧的SO问题也可能有帮助:
抓取程序的控制台输出
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 forstdin
andstdout
or else thecmd.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
您将
NULL
作为最后两个参数传递给ReadFile
,这是不允许的。 文档说You passed
NULL
as the final two parameters toReadFile
, which is not permitted. The documentation says你不断改变你的问题,所以除了最后一个答案之外,其他答案都毫无意义。您收到命令提示符启动消息,因为这是 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.