2012-10-02 52 views
0

可能重复:
SendMessage to .NET Console application发送消息或命令控制台(CMD.EXE)

我想问一下,我怎么能发送消息或命令Winform应用程序的控制台。关于我的代码是winform中的控制台应用程序(cmd.exe)。我可以把cmd.exe放在winform回合中,我无法发送消息或命令到控制台(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"); 
} 
+0

可能适用于您的“解决方法”(取决于所讨论的控制台应用程序)可能是使用其常规标准输入流与它进行交互。请参阅[此SO线程](http://stackoverflow.com/q/11767654/21567)以获取更多信息。 –

回答

2

控制台应用程序通常没有消息泵,因此不能接收Windows消息,除非它主动通过创建一个或手动抽的消息配合。除非你拥有源代码,否则这不是一个选项,在这种情况下你不需要。