如何在 Windows 中捕获控制台 I/O(不是 stdio)?
我为什么要这么做?因为我正在编写自己的 cmd.exe 替代品!我可以 CreateProcess 并将句柄传递给 stdin、stdout 和 stderr 的管道,并且我了解如何使用 AllocConsole、FreeConsole 和 AttachConsole,但是如何告诉子程序我的程序是控制台?
更多澄清细节:我有一个可以显示文本并接受输入的窗口。我显示文本“C:>”例如。用户输入“foo.exe”。我在路径上找到 foo.exe,并对其调用 CreateProcess。如果它写入标准输出,我会在屏幕上显示该文本。如果它从标准输入读取,我会传入用户按下的任何击键。当进程终止时,我再次打印提示。到目前为止,一切都很好。这一切都有效。但后来我尝试了 foo2.exe,它在 CONOUT$ 上执行 CreateFile,但由于没有附加到该进程的控制台而失败。 cmd.exe 做了什么才能让它的子进程将其视为控制台?
Why would I want to? Because I'm writing my own cmd.exe replacement! I can CreateProcess and pass in handles to pipes for stdin and stdout and stderr, and I see how I can AllocConsole, FreeConsole, and AttachConsole, but how do I tell child programs that my program is the console?
More detail for clarification: I've got a window that can display text and accept input. I display the text "C:>" for instance. The user types "foo.exe". I locate foo.exe on the path, and call CreateProcess on it. If it writes to stdout, I display that text on the screen. If it reads from stdin, I pass in whatever keystrokes the user has pressed. When the process terminates, I print the prompt again. So far, so good. It all works. But then I try foo2.exe, which does a CreateFile on CONOUT$, which fails because there is no console attached to the process. What does cmd.exe do to make it's children see it as a console?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不确定是否真的可以在不注入子进程并挂钩控制台 api 和 WriteFile 的情况下创建完整的控制台替换。
另一方面,cmd.exe 的替换应该是可能的,只需确保您实际创建了一个控制台程序,并且您应该获得一个控制台,并且 CreateProcess 应确保子进程获得相同的控制台,而不会弄乱 i/o 句柄。
I'm not sure if it actually possible to create a full console replacement without injecting into the child process and hooking the console api's and WriteFile.
A cmd.exe replacement on the other hand should be possible, just make sure you actually created a console program and you should get a console and CreateProcess should make sure the child process gets the same console without messing with the i/o handles.
这就是你所追求的? http://msdn.microsoft.com/ en-us/library/ms682079%28v=VS.85%29.aspx
您的请求有点模糊,尽管我想这就是为什么您还没有得到您想要的东西。
This what you're after? http://msdn.microsoft.com/en-us/library/ms682079%28v=VS.85%29.aspx
Your request is kinda vague, although I guess that's why you haven't already got what you're looking for.