0

可能重复:
SendMessage 到 .NET 控制台应用程序

我想问一下如何从 Winform 应用程序将消息或命令发送到控制台。关于我的代码,即 winform 中的控制台应用程序(cmd.exe)。我可以在 winform 中放入 cmd.exe,但我无法将消息或命令发送到控制台(cmd.exe)。

请查看我的代码并帮助我

void PictureBox1Click(object sender, EventArgs e)
{
    System.Diagnostics.ProcessStartInfo start = new  
    System.Diagnostics.ProcessStartInfo();
    System.Diagnostics.Process bob = new System.Diagnostics.Process();
    bob.StartInfo.UseShellExecute = true;
    bob.StartInfo.Arguments += " /K TITLE CMD";
    bob.StartInfo.FileName =  "cmd";
    bob.Start();
    this.timer1.Enabled = true;
}


private void button1_Click(object sender, EventArgs e)
{
    System.Diagnostics.Process bob = new System.Diagnostics.Process();
    bob.StartInfo.UseShellExecute = true;
    bob.StartInfo.Arguments += " /K TITLE Command Prompt";
    bob.StartInfo.FileName = "CMD";
    bob.StartInfo.CreateNoWindow = true;
    bob.Start();
    this.timer1.Enabled = true;
}


private void BtnSend_Click(object sender, EventArgs e)
{
    System.IntPtr hwnd ;
    hwnd = Usr32.FindWin("ConsoleWindowClass", "cmd");
    if (hwnd != System.IntPtr.Zero)
    {
        IntPtr hwndChild = IntPtr.Zero;
        hwndChild = Usr32.FindWindowEx((IntPtr)hwnd, new IntPtr(0), null, null);
        if (hwndChild != IntPtr.Zero)
        {
            Usr32.SendMessage(hwndChild, 0x000C, 0, "Hello Everyone");

        }
        else
        {
            MessageBox.Show("Cannot Get the Child Handle :( !");
        }
     }
     else

        MessageBox.Show("Application is not running");
}
4

1 回答 1

2

Console applications typically do not have a message pump and therefore cannot receive Windows messages unless it actively cooperates by creating one or pumping messages manually. This is not an option unless you own the source, which in this case you do not.

于 2012-10-02T08:24:01.087 回答