.NET:将数据注入进程的输入缓冲区
我需要自动化命令行应用程序。它要求用户输入密码。我通过 STDIN 发送密码的所有方法都失败了。现在我尝试使用 .NET 通过包装程序来完成此操作。
我正在启动应用程序创建一个新进程,设置 StartInfo
属性,然后启动该进程:
Dim app_path As String
Dim app_args As String
Dim myProcess As Process = New Process()
myProcess.StartInfo.FileName = app_path
myProcess.StartInfo.Arguments = app_args
myProcess.StartInfo.UseShellExecute = False
myProcess.Start()
我确实尝试使用 StartInfo.RedirectStandardInput
属性,但没有成功。
现在,我遇到了来自 kernel32.dll
的 WriteConsoleInput
函数,如下所示:
Declare Function WriteConsoleInput Lib "kernel32.dll" Alias "WriteConsoleInputA" (ByVal hConsoleInput As Integer, ByVal lpBuffer As String, ByVal nNumberOfCharsToWrite As Integer, ByRef lpNumberOfCharsWritten As Integer) As Boolean
我可以通过 myProcess.Handle
获取进程的句柄代码>属性。但是使用这种方式将输入发送到输入缓冲区也是不可能的。
我发现了这些问题,但没有帮助:
How do I write 'PAGE DOWN' into the console input buffer? (1475353)
Java - 将输入传递到外部 C/C++ 应用程序 (1421273)
使用 stdin 管道控制 Windows 控制台应用程序 (723424 ) )
使用 StraceNtX.exe 我在应用程序等待输入时得到了此输出:
[T4024] GetConsoleMode(f, 12d35c, 12d3af, 77bff894, ...) = 1
[T4024] SetConsoleMode(f, 0, 12d3af, 77bff894, ...) = 1
[T4024] ReadConsoleInputA(f, 12d348, 1, 12d360, ...) = 1
任何人都可以告诉我,还可以尝试什么或如何执行上述操作正确的方法是什么? 谢谢!
根据 Tim Robinsons 的回答,我现在已经得到了这段代码,但它不起作用:
myProcess = New Process()
myProcess.StartInfo.FileName = app_path
myProcess.StartInfo.Arguments = app_args
myProcess.StartInfo.WindowStyle = ProcessWindowStyle.Normal
myProcess.StartInfo.UseShellExecute = False
myProcess.Start()
' Wait for process requesting passwort input
System.Threading.Thread.Sleep(3000)
Dim len As Integer
len = 0
Dim handle As Integer
handle = GetStdHandle(STD_INPUT_HANDLE)
WriteConsoleInput(handle, "Test", 4, len)
我的程序是一个命令行应用程序,应该充当包装器。
输入已发送,但方式不是将其输入到密码字段中,而是在密码字段下方显示新的提示(甚至不显示输入)。
蒂姆,你能给我举个例子吗?
I need to automate an command line application. It asks the user to enter a password. All my approches to send the password via STDIN failed. Now I am trying to do this with an wrapper-programm using .NET.
I am starting the application creating a new process, setting the StartInfo
-properties and then start the process:
Dim app_path As String
Dim app_args As String
Dim myProcess As Process = New Process()
myProcess.StartInfo.FileName = app_path
myProcess.StartInfo.Arguments = app_args
myProcess.StartInfo.UseShellExecute = False
myProcess.Start()
I did try to use the StartInfo.RedirectStandardInput
Property but with no success.
Now I came accross the WriteConsoleInput
function from the kernel32.dll
that I included like this:
Declare Function WriteConsoleInput Lib "kernel32.dll" Alias "WriteConsoleInputA" (ByVal hConsoleInput As Integer, ByVal lpBuffer As String, ByVal nNumberOfCharsToWrite As Integer, ByRef lpNumberOfCharsWritten As Integer) As Boolean
I can get the handle of the process via the myProcess.Handle
property. But sending input to the input-buffer using this way was also not possible.
I found those questions but they did not help:
How do I write ‘PAGE DOWN’ into the console input buffer? (1475353)
Java - passing input into external C/C++ application (1421273)
Controlling a Windows Console App w/ stdin pipe (723424)
Using StraceNtX.exe I got this output for the moment the app is waiting for input:
[T4024] GetConsoleMode(f, 12d35c, 12d3af, 77bff894, ...) = 1
[T4024] SetConsoleMode(f, 0, 12d3af, 77bff894, ...) = 1
[T4024] ReadConsoleInputA(f, 12d348, 1, 12d360, ...) = 1
Can anyone tell me, what else to try or how to do the above the right way?
Thanks!
Based on Tim Robinsons answere I've got this code now, but it does not work:
myProcess = New Process()
myProcess.StartInfo.FileName = app_path
myProcess.StartInfo.Arguments = app_args
myProcess.StartInfo.WindowStyle = ProcessWindowStyle.Normal
myProcess.StartInfo.UseShellExecute = False
myProcess.Start()
' Wait for process requesting passwort input
System.Threading.Thread.Sleep(3000)
Dim len As Integer
len = 0
Dim handle As Integer
handle = GetStdHandle(STD_INPUT_HANDLE)
WriteConsoleInput(handle, "Test", 4, len)
My programm is an commandline application that should act as an wrapper.
The input is send but in a way that it is not typed into the password field but that below the password field a new promt is shown (without even showing the input).
Tim, can you give me an example?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
抱歉回复晚了,最近太忙了。
这是一个完整的代码示例,从我的代码中剥离出来。我希望我没有删除一些重要的东西。
它使用以下模块:
使用此功能,您可以启动进程并等待一行输出(即“密码:”)。然后该函数将输入提供的文本,然后返回。
我希望这有帮助!
问候
SC911
Sorry for my late reply, been very busy.
Here is an full code example, stripped down from my code. I hope I did not delete something crucial.
It uses the following module:
With this function you can start the process and wait for a line of output (i.e. "password:"). Then the function will enter the provided text followed by an return.
I hope this helps!
Regards
sc911
WriteConsoleInput
需要控制台句柄,而不是进程句柄。你的问题是如何获得这个控制台句柄。如果您的进程是控制台应用程序,并且在启动子进程时不重定向任何内容,则子进程将附加到您自己进程的控制台。在这种情况下,您可以:
如果您有 GUI 应用程序,您可以使用
AllocConsole
为自己分配一个控制台。编辑:我一开始没有注意到,但这不是
WriteConsoleInput
的正确定义。它需要一个INPUT_RECORD
数组,而不是一个字符串。一旦我复制来自pinvoke.net的函数原型<,我自己对runas.exe的实验就可以正常工作/a>.WriteConsoleInput
expects a console handle, not a process handle. Your problem is how to get hold of this console handle.If your process is a console app, and you don't redirect anything when starting the child process, then the child process will be attached to your own process's console. In which case, you may be able to:
GetStdHandle(STD_INPUT_HANDLE)
to get hold of your own console handleWriteConsoleInput
If you have a GUI app you can assign yourself a console using
AllocConsole
.Edit: I didn't notice at first, but that's not the right definition for
WriteConsoleInput
. It takes an array ofINPUT_RECORD
, not a string. My own experiment with runas.exe works OK once I copied the function prototype from pinvoke.net.