从Winform Application向控制台发送消息或命令(cmd.exe)

时间:2012-10-02 08:18:42

标签: c#

  

可能重复:
  SendMessage to .NET Console application

我想问一下如何从Winform应用程序向控制台发送消息或命令。关于我的代码是winform中的控制台应用程序(cmd.exe)。我可以在winform bout中放入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");
}

1 个答案:

答案 0 :(得分:2)

控制台应用程序通常没有消息泵,因此无法接收Windows消息,除非它通过创建消息或手动提取消息来主动协作。除非您拥有源代码,否则这不是一个选项,在这种情况下您不需要。