在Windows中写入正在运行的进程的stdin [英] Write to stdin of a running process in windows
问题描述
我想从Windows中的外部进程将数据写入现有进程的 stdin
。
我发现对于Linux也有类似的问题,但我想知道如何在Windows中执行相同的操作。
如何从Python中不同的本地/远程进程将数据流式传输到程序的STDIN中?
https://serverfault.com/questions/443297/write到运行进程使用管道的标准输入
我尝试了此代码,但我出现错误。我还尝试运行该程序,并使用此代码向该代码发送 stdin
,但再次出错。
CMD:
type my_input_string | app.exe -start
my_input_string | app.exe-开始
app.exe-开始< pas.txt
在python中:
p = subprocess.Popen(' C:\app.exe -start',stdin = subprocess.PIPE,Universal_newlines = True,shell = True)
grep_stdout = p .communicate(input ='my_input_string')[0]
我得到的错误是:
ReadConsole()
失败:句柄无效。
在C#中:
尝试
{
var startInfo = new ProcessStartInfo();
startInfo.RedirectStandardInput = true;
startInfo.FileName = textBox1.Text;
startInfo.Arguments = textBox2.Text;
startInfo.UseShellExecute = false;
var process = new Process();
process.StartInfo = startInfo;
process.Start();
Thread.Sleep(1000);
var streamWriter = process.StandardInput;
streamWriter.WriteLine( 1);
}
catch(异常例外)
{
textBox4.Text = ex.Message + \r\n + ex.Source;
}
在C#中,使用上面的代码, App.exe
(启动其他进程的命令行应用程序)崩溃,但是在C#应用程序中我没有任何异常。我认为这是因为 UseShellExecute = false
。
如果我选择请勿在后台运行应用当我运行C#应用程序时,我可以找到进程并使用 sendkeys
向其中发送 my_input_string
,但这不是这不是一个好主意,因为用户在运行GUI时可以看到命令行。
如何发送 stdin
,仅使用CMD,python脚本或C#,没有任何错误?
如果要启动,然后提供来自c#的输入,您可以执行以下操作:
var startInfo = new ProcessStartInfo( path / to / executable );
startInfo.RedirectStandardInput = true;
startInfo.UseShellExecute = false;
var process = new Process();
process.StartInfo = startInfo;
process.Start();
var streamWriter = process.StandardInput;
streamWriter.WriteLine(我正在提供输入!);
如果您需要写入已经运行的应用程序的标准输入,我怀疑这样做是否容易使用.net类,因为Process类不会为您提供 StandardInput
(它会抛出 InvalidOperationException
)
编辑:
向 ProcessStartInfo()
I want to write data to an existing process's stdin
from an external process in Windows.
I found similar questions for Linux, but I want to know how I can do the same in Windows.
How to write data to existing process's STDIN from external process?
How do you stream data into the STDIN of a program from different local/remote processes in Python?
https://serverfault.com/questions/443297/write-to-stdin-of-a-running-process-using-pipe
I tried with this code but I got an error. I also tried running the program, sending stdin
to that with this code, but again, it errored.
In CMD:
type my_input_string | app.exe -start
my_input_string | app.exe -start
app.exe -start < pas.txt
In python:
p = subprocess.Popen('"C:\app.exe" -start', stdin=subprocess.PIPE, universal_newlines=True, shell=True)
grep_stdout = p.communicate(input='my_input_string')[0]
The error I get is:
ReadConsole()
failed: The handle is invalid.
And in C#:
try
{
var startInfo = new ProcessStartInfo();
startInfo.RedirectStandardInput = true;
startInfo.FileName = textBox1.Text;
startInfo.Arguments = textBox2.Text;
startInfo.UseShellExecute = false;
var process = new Process();
process.StartInfo = startInfo;
process.Start();
Thread.Sleep(1000);
var streamWriter = process.StandardInput;
streamWriter.WriteLine("1");
}
catch (Exception ex)
{
textBox4.Text = ex.Message+"\r\n"+ex.Source;
}
In C#, with the code above, App.exe
(command line application which starts the other process) crashes, but in C# application I don't have any exception. I think that is because of UseShellExecute = false
.
If I choose "don't run app in background" when I run the C# app, I can find the process and use sendkeys
to send my_input_string
to it, but this isn't a good idea because the user can see the commandline when it's running the GUI.
How can I send stdin
, only using CMD, a python script, or C#, without any errors?
If you're launching, and then supplying the input from c#, you can do something like this:
var startInfo = new ProcessStartInfo("path/to/executable");
startInfo.RedirectStandardInput = true;
startInfo.UseShellExecute = false;
var process = new Process();
process.StartInfo = startInfo;
process.Start();
var streamWriter = process.StandardInput;
streamWriter.WriteLine("I'm supplying input!");
If you need to write to the standard input of an application already running, I doubt that is easy to do with .net classes, as the Process class will not give you the StandardInput
(it will instead throw an InvalidOperationException
)
Edit:
Added parameter to ProcessStartInfo()
这篇关于在Windows中写入正在运行的进程的stdin的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!