低电平控制台输入和重定向 [英] Low Level Console Input and Redirection

查看:231
本文介绍了低电平控制台输入和重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图发送命令到的cmd.exe 应用程序的使用水平低的读/写功能的控制台输入。我没有问题使用阅读文本(刮)在在 ReadConsole ...()和 WriteConsole()功能具有连接到控制台的过程,但我还没有想出如何写,例如目录键,让控制台间preT它作为一个发送命令。

I'm trying to send commands to to the input of a cmd.exe application using the low level read/write console functions. I have no trouble reading the text (scraping) using the ReadConsole...() and WriteConsole() functions after having attached to the process console, but I've not figured out how to write for example "dir" and have the console interpret it as a sent command.

下面是一个有点我的code的:

Here's a bit of my code:

CreateProcess(NULL, "cmd.exe", NULL, NULL, FALSE, CREATE_NEW_CONSOLE, NULL, NULL, &si, &pi);
AttachConsole(pi.dwProcessId);

strcpy(buffer, "dir");
WriteConsole(GetStdHandle(STD_INPUT_HANDLE), buffer, strlen(buffer), &charRead, NULL);

STARTUPINFO 过程属性都设置为零,除,当然,在 .CB 属性。

STARTUPINFO attributes of the process are all set to zero, except, of course, the .cb attribute.

屏幕上无任何变化,但是我发现了一个错误6:无效句柄 WriteConsole 返回 STD_INPUT_HANDLE 。如果我写(STD_OUTPUT_HANDLE)我得到我的 DIR 写在屏幕上,但没有当然的发生。我猜 SetConsoleM​​ode()可能会有所帮助,但我试过很多模式组合,没有任何帮助。我也创建了输入等待一个快速的控制台应用程序( scanf()的),并回送什么进去,没有工作。

Nothing changes on the screen, however I'm getting an Error 6: Invalid Handle returned from WriteConsole to STD_INPUT_HANDLE. If I write to (STD_OUTPUT_HANDLE) I do get my dir written on the screen, but nothing of course happens. I'm guessing SetConsoleMode() might be of help, but I've tried many mode combinations, nothing helped. I've also created a quick console application that waits for input (scanf()) and echoes back whatever goes in, didn't work.

我也试着输入到 scanf()的 promp然后偷看使用 PeekConsoleInput()输入缓冲区,返回0,但 INPUT_RECORD 数组为空。

I've also tried typing into the scanf() promp and then peek into the input buffer using PeekConsoleInput(), returns 0, but the INPUT_RECORD array is empty.

我知道,周围有另一种方式利用本 WriteConsoleInput()直接注INPUT_RECORD结构事件到控制台,但这将是太长了,我将不得不每个键preSS送进去。

I'm aware that there is another way around this using WriteConsoleInput() to directly inject INPUT_RECORD structured events into the console, but this would be way too long, I'll have to send each keypress into it.

我希望这个问题是清楚的。请让我知道如果你需要任何进一步的信息。感谢您的帮助。

I hope the question is clear. Please let me know if you need any further information. Thanks for your help.

更新1:

我能够发送键presses到 CMD 使用过程 WriteConsoleInput() INPUT_RECORD 结构,然而, AttachConsole 有时会抛出 ERROR_GEN_FAILURE#31:连接到系统上的设备不能正常工作,因此, INPUT_RECORD 不会被发送(错误6:无效句柄 )。 睡眠(1000)的CreateProcess() AttachConsole()解决这个问题。字符 DIR 键入是自动的,但我无法弄清楚如何发送返回键:

I am able to send keypresses to a cmd process using WriteConsoleInput() with INPUT_RECORD structs, however, the AttachConsole sometimes throws ERROR_GEN_FAILURE #31: A device attached to the system is not functioning., and thus the INPUT_RECORD are not sent (Error 6: Invalid Handle). Sleep(1000) after CreateProcess() before AttachConsole() solves this. The characters dir are typed in automatically, but I can't figure out how to send the RETURN key:

ir[0].EventType = KEY_EVENT;
ir[0].Event.KeyEvent.bKeyDown = TRUE;
ir[0].Event.KeyEvent.dwControlKeyState = 0;
ir[0].Event.KeyEvent.uChar.UnicodeChar = '\n';
ir[0].Event.KeyEvent.wRepeatCount = 1;
ir[0].Event.KeyEvent.wVirtualKeyCode = VK_RETURN;
ir[0].Event.KeyEvent.wVirtualScanCode = MapVirtualKey(VK_RETURN, MAPVK_VK_TO_VSC);
ir[1].EventType = KEY_EVENT;
ir[1].Event.KeyEvent.bKeyDown = FALSE;
ir[1].Event.KeyEvent.dwControlKeyState = 0;
ir[1].Event.KeyEvent.uChar.UnicodeChar = '\n';
ir[1].Event.KeyEvent.wRepeatCount = 1;
ir[1].Event.KeyEvent.wVirtualKeyCode = VK_RETURN;
ir[1].Event.KeyEvent.wVirtualScanCode = MapVirtualKey(VK_RETURN, MAPVK_VK_TO_VSC);

WriteConsoleInput(GetStdHandle(STD_INPUT_HANDLE), ir, 2, &charRead);

WriteConsoleInput 收益 0 ,但没有发生在控制台上,我已经尝试设置 SetConsoleM​​ode() ENABLE_PROCESSED_INPUT | ENABLE_LINE_INPUT | ENABLE_ECHO_INPUT 及其组合,没有结果,虽然。如果我preSS从键盘输入,但是,自动类型的 DIR 命令执行(不像时候,我刚 WriteConsole()),所以我想我在正确的轨道上。

WriteConsoleInput returns 0, but nothing happens in the console, I've tried setting SetConsoleMode() to ENABLE_PROCESSED_INPUT | ENABLE_LINE_INPUT | ENABLE_ECHO_INPUT and a combination thereof, with no results, though. If I press enter from the keyboard, however, the automatically-typed dir command executes (unlike the times when I just WriteConsole()), so I guess I'm on the right track.

不SSH送过来的实际键presses并得到实际的屏幕缓冲区(如TAB和CTRL + C CTRL + D的工作)?我之类的东西后我。

Doesn't SSH send over the actual keypresses and gets the actual screen buffer (like TAB, and CTRL+C CTRL+D work)? I'm after something of the like.

更新2:

我发现这个问题与注射return命令。本来应该是 IR [1] .Event.KeyEvent.uChar.AsciiChar ='\\ r'; 即一个 \\ r 而不是一个 \\ n ,超级简单。

I found the problem with injecting the return command. Should have been ir[1].Event.KeyEvent.uChar.AsciiChar = '\r'; i.e. an \r instead of a \n, super simple.

这似乎是没有用的办法 WriteConsole()来输入命令,应该得到通过发送 WriteConsoleInput() INPUT_RECORDs或通过创建管道(这并不总是完美的,但伟大的最直接的应用程序)。用一个很大的优势 WriteConsoleInput()是一个发送 VK_UP VK_DOWN ,进入控制台历史记录,(如果我们是在CMD)和 VK_TAB 为自动完成,所有的CTRL + _序列,ESC键和功能键甚至是鼠标点击。

It seems that there is no way of using WriteConsole() to input commands, one should get by by sending WriteConsoleInput() INPUT_RECORDs or by creating pipes (which are not always perfect, but great for most straight forward applications). One great advantage of using WriteConsoleInput() is that you one can send VK_UP and VK_DOWN, to access console history, (if we're in CMD) and VK_TAB for auto-completion, all CTRL+_ sequences, ESC and FUNCTION keys and even MOUSE CLICKS.

点击此处了解详情:的http:// msdn.microsoft.com/en-us/library/ms687403%28v=vs.85%29.aspx
再加上这里的例子吨: http://controllingtheinter.net/forums/viewtopic .PHP F = 116安培; T = 366

如果任何人有其他伟大的想法随时在凑钱。感谢所有那些谁在这个很感兴趣。希望这可以帮助别人的未来。

If anyone has other great ideas feel free to chip in. Thank you to all those who took interest in this. Hope this helps someone in the future.

推荐答案

现在你要写信给你自己的标准输入处理,而不是一个cmd.exe进程。你必须做很多工作来重定向一个输入句柄。它需要一个管道。这里有一个知识库文章,显示了样板code。

Right now you're trying to write to your own stdin handle, not the one for the cmd.exe process. You'll have to do a lot more work to redirect the input handle for that one. It requires a pipe. Here's a KB article that shows the boilerplate code.

BTW:总是的检查API函数的返回值

Btw: always check the return value of API functions.

这篇关于低电平控制台输入和重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆