2

所以我有一个隐藏的进程(java命令行应用程序),输出和输入被重定向。我可以很容易地读取输出并且可以正常工作,但是当我发送命令时它不起作用。

我认为我已经确定输入没有被重定向,因为:

(A) 当我发送WriteLine(//command here); Flush没有命令被程序确认时

(B) 当我取消隐藏 cmd 窗口 ( StartInfo.CreateNoWindow = false;) 时,我可以输入命令并运行它们(在 cmd 窗口中),即使 StandardInput 正在被重定向 ( StartInfo.RedirectStandardInput = true;)

这是代码:

namespace bukkit
{
    public partial class Form1 : Form
{
    private static StringBuilder _txt = new StringBuilder();
    private static bool _scrolled = false;
    Process mncrft = new Process();

    public Form1()
    {
        InitializeComponent();

        mncrft.StartInfo.WorkingDirectory = Path.GetTempPath();
        mncrft.StartInfo.Arguments = "-Xmx512M -Xms512M -jar C:\\mncrft\\mncrft.jar";
        mncrft.StartInfo.FileName = "java.exe";
        mncrft.StartInfo.UseShellExecute = false;
        mncrft.StartInfo.RedirectStandardOutput = true;
        mncrft.StartInfo.RedirectStandardError = true;
        mncrft.StartInfo.RedirectStandardInput = true;
        mncrft.StartInfo.CreateNoWindow = false;
        mncrft.ErrorDataReceived += build_ErrorDataReceived;
        //mncrft.OutputDataReceived += build_ErrorDataReceived;
        mncrft.EnableRaisingEvents = true;
        //mncrft.StandardInput.NewLine = "\r\n";
        mncrft.Start();
        mncrft.BeginOutputReadLine();
        mncrft.BeginErrorReadLine();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        _txt.AppendLine("Starting Minecraft...");
    }

    private void Form1_Close(object sender, EventArgs e)
    {
        mncrft.Close();
    }

    static void build_ErrorDataReceived(object sender, DataReceivedEventArgs e)
    {
        string msg = e.Data;
        if (msg != null && msg.Length > 0)
        {
            _txt.AppendLine(msg);
            _scrolled = false;
        }
    }

    private void mainTimer_Tick(object sender, EventArgs e)
    {
        if (_txt.Length > 0)
        {
            txtOutput.Text = _txt.ToString();

            // scroll down
            if (_scrolled == false)
            {
                txtOutput.SelectionStart = txtOutput.Text.Length;
                txtOutput.ScrollToCaret();
                _scrolled = true;
            }
        }
    }

    private void Execute_Click(object sender, EventArgs e)
    {
        if (textBox1.Text.Length > 0)
        {
            mncrft.StandardInput.WriteLine(textBox1.Text);
            mncrft.StandardInput.Flush();
        }
    }
}
}

如何重定向输入以便发送命令?

谢谢,亚当

PS:如果这令人困惑,只需发表评论,我会很乐意澄清。

已回答

感谢 Tim,替换以下行:

mncrft.StartInfo.FileName = "java.exe";
mncrft.StartInfo.Arguments = "-Xmx512M -Xms512M -jar C:\\mncrft\\mncrft.jar";

和:

mncrft.StartInfo.FileName = "java";
mncrft.StartInfo.Arguments = "-Xmx512M -Xms512M -jar C:\\mncrft\\bukkit.jar -nojline";
4

3 回答 3

5

好吧,我整天都在搞这个,我找到了答案。当你用你的应用程序启动 bukkit 时,你需要在你的过程参数中包含“-nojline”。这使得 bukkit 的输入与标准输入一起正常工作。

https://github.com/Bukkit/CraftBukkit/commit/22a44d47ac48fb65bb61fb823c84bff9494f5033

于 2011-07-12T20:29:12.137 回答
0

This is very weird because if the Input Stream didn't redirect (for some reason) then you would get an exception the moment you try to do anything with the Process.StandartInput property, and if it did really redirect then you wouldn't be able to enter commands in the cmd window and execute them !

If the problem is because you're making a WinForms application (as Fadrian mentioned) then try making it a WPF application (I personally prefer WPF over WinForms), if you never coded a WPF app before then download the code I linked in my blog post here (yes, the one you visited earlier) and modify it so that it loads your Bukkit Server instead of the regular Minecraft Server, if it didn't work then the problem might be with the Bukkit Server, at that point I recommend that you go to Bukkit's forum and ask there.

于 2011-05-12T00:28:18.227 回答
0

亚当,我不确定你的问题的答案,但我过去确实遇到过类似的问题,并且了解到 winform 应用程序在使用控制台的标准输入和标准输出时的行为确实略有不同。在使用对 AttachConsole 的 API 调用(以及释放它的 FreeConsole)在线阅读了一些帖子后,我解决了我的问题。也许这将是一个很好的起点,可以作为您问题的解决方案进行调查。

[DllImport("kernel32.dll")]
static extern bool AttachConsole(int dwProcessId);
于 2011-05-08T14:07:33.510 回答