Hi *
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?