2011-05-08 40 views
0

我閱讀了許多關於如何發送命令到cmd提示符的答案。使用SendMessage從Windows窗體應用程序發送字符串到命令提示符(cmd)

但我不想使用StreamWriter或類似的東西來輸入和獲取輸出。

我想使用SendMessage發送我的字符串或說命令到cmd提示窗口。

任何人都可以請幫忙嗎?

只是爲了給我的應用程序的細節。 1.我的應用程序是一個WinForm應用程序。 2.它有4-5個按鈕。 3. Button1在Button5關閉或退出命令提示符窗口時打開命令提示符窗口。 4.按鈕2,3,4是命令按鈕。當用戶單擊Button2命令時1將被髮送到命令提示符窗口。類似的,當點擊按鈕3和4時,命令2和命令3被髮送到相同的命令提示符窗口。

讓我知道是否有人有書面的代碼發送字符串到命令提示符。

感謝和問候,

拉胡爾

+0

您知道,您可以直接通過使用合適的參數調用cmd.exe(或可執行文件本身)來直接在命令提示符下執行程序。這真的是一種不必要的複雜和迂迴的方式來做簡單的事情。 – siride 2011-05-08 19:16:05

+2

你聽起來很容易確定使用錯誤的函數來做到這一點。不是一個聰明的方式來提問。 – 2011-05-08 20:33:37

回答

2

我已經得到了解決方案。
我用PostMessage()。
Cmd有句柄,你可以發送字符串給它。
我只是努力尋找正確的方法來獲得處理。
現在我得到了一個。

/* 
* Created by SharpDevelop. 
* User: Rahul 
* Date: 5/12/2011 
* Time: 1:49 AM 
* 
* To change this template use Tools | Options | Coding | Edit Standard Headers. 
*/ 
using System; 
using System.Collections.Generic; 
using System.Drawing; 
using System.Windows.Forms; 
using System.Diagnostics; 
using System.Runtime.InteropServices; 
using System.Text; 

namespace GetChildWindows 
{ 
/// <summary> 
/// Description of MainForm. 
/// </summary> 
public partial class MainForm : Form 
{ 
    [DllImport("user32")] 
    [return: MarshalAs(UnmanagedType.Bool)] 
    public static extern bool EnumChildWindows(IntPtr window, EnumWindowProc callback, IntPtr i); 

    [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)] 
    static extern int GetClassName(IntPtr hWnd, StringBuilder lpClassName, int nMaxCount); 

    [DllImport("user32.dll", SetLastError = true)] 
    static extern IntPtr FindWindow(string lpClassName, string lpWindowName); 
    IntPtr hWnd = FindWindow(null, "Untitled - Notepad"); 

    [DllImport("user32.dll", SetLastError = true)] 
    static extern bool PostMessage(IntPtr hWnd, [MarshalAs(UnmanagedType.U4)] uint Msg, IntPtr wParam, IntPtr lParam); 


    [DllImport("user32.dll", SetLastError = true)] 
    static extern bool PostMessage(IntPtr hWnd, [MarshalAs(UnmanagedType.U4)] uint Msg, int wParam, int lParam);   

    const int WM_KEYDOWN = 0x0100; 
    const int WM_KEYUP = 0x0101; 
    const int WM_CHAR = 0x0102; 

    public static IntPtr cmdHwnd = IntPtr.Zero; 

    public MainForm() 
    { 
     // 
     // The InitializeComponent() call is required for Windows Forms designer support. 
     // 
     InitializeComponent(); 

     // 
     // TODO: Add constructor code after the InitializeComponent() call. 
     // 

     foreach (IntPtr child in GetChildWindows(FindWindow(null, "WinPlusConsole"))) 
     { 
      StringBuilder sb = new StringBuilder(100); 
      GetClassName(child, sb, sb.Capacity); 


      if (sb.ToString() == "ConsoleWindowClass") 
      { 
//     uint wparam = 0 << 29 | 0; 
//     string msg = "Hello"; 
//     int i = 0; 
//     for (i = 0 ; i < msg.Length ; i++) 
//     { 
//      //PostMessage(child, WM_KEYDOWN, (IntPtr)Keys.Enter, (IntPtr)wparam); 
//      PostMessage(child, WM_CHAR, (int)msg[i], 0); 
//     } 
//     PostMessage(child, WM_KEYDOWN, (IntPtr)Keys.Enter, (IntPtr)wparam); 

       cmdHwnd = child; 
      } 
     } 

} 

/// <summary> 
    /// Returns a list of child windows 
    /// </summary> 
    /// <param name="parent">Parent of the windows to return</param> 
    /// <returns>List of child windows</returns> 
    public static List<IntPtr> GetChildWindows(IntPtr parent) 
    { 
     List<IntPtr> result = new List<IntPtr>(); 
     GCHandle listHandle = GCHandle.Alloc(result); 
     try 
     { 
      EnumWindowProc childProc = new EnumWindowProc(EnumWindow); 
      EnumChildWindows(parent, childProc, GCHandle.ToIntPtr(listHandle)); 
     } 
     finally 
     { 
      if (listHandle.IsAllocated) 
      listHandle.Free(); 
     } 
     return result; 
    } 

    /// <summary> 
    /// Callback method to be used when enumerating windows. 
    /// </summary> 
    /// <param name="handle">Handle of the next window</param> 
    /// <param name="pointer">Pointer to a GCHandle that holds a reference to the list to fill</param> 
    /// <returns>True to continue the enumeration, false to bail</returns> 

    private static bool EnumWindow(IntPtr handle, IntPtr pointer) 
    { 
     GCHandle gch = GCHandle.FromIntPtr(pointer); 
     List<IntPtr> list = gch.Target as List<IntPtr>; 
     if (list == null) 
     { 
      throw new InvalidCastException("GCHandle Target could not be cast as List<IntPtr>"); 
     } 
     list.Add(handle); 
     // You can modify this to check to see if you want to cancel the operation, then return a null here 
     return true; 
    } 

    /// <summary> 
    /// Delegate for the EnumChildWindows method 
    /// </summary> 
    /// <param name="hWnd">Window handle</param> 
    /// <param name="parameter">Caller-defined variable; we use it for a pointer to our list</param> 
    /// <returns>True to continue enumerating, false to bail.</returns> 
    public delegate bool EnumWindowProc(IntPtr hWnd, IntPtr parameter); 





    void BtnHelloClick(object sender, EventArgs e) 
    { 
     uint wparam = 0 << 29 | 0; 
       string msg = "Hello"; 
       int i = 0; 
       for (i = 0 ; i < msg.Length ; i++) 
       { 
        //PostMessage(child, WM_KEYDOWN, (IntPtr)Keys.Enter, (IntPtr)wparam); 
        PostMessage(cmdHwnd, WM_CHAR, (int)msg[i], 0); 
       } 
       PostMessage(cmdHwnd, WM_KEYDOWN, (IntPtr)Keys.Enter, (IntPtr)wparam); 
    } 

    void BtnCommandClick(object sender, EventArgs e) 
    { 
     uint wparam = 0 << 29 | 0; 
       string msg = textBox1.Text; 
       int i = 0; 
       for (i = 0 ; i < msg.Length ; i++) 
       { 
        //PostMessage(child, WM_KEYDOWN, (IntPtr)Keys.Enter, (IntPtr)wparam); 
        PostMessage(cmdHwnd, WM_CHAR, (int)msg[i], 0); 
       } 
       PostMessage(cmdHwnd, WM_KEYDOWN, (IntPtr)Keys.Enter, (IntPtr)wparam); 
    } 

    void TextBox1TextChanged(object sender, EventArgs e) 
    { 

    } 
} 
} 


感謝所有!

0

它不清楚從explenation(至少不是我)爲什麼你不能在你所描述的場景中使用的StreamWriter。你能詳細說明一下嗎?

我會檢查出Process類通過標準輸入/輸出談論其他程序

1

這似乎不太可能,你將能夠與SendMessage()來實現這一目標。 SendMessage()需要窗口句柄,我不認爲cmd.exe有一個合適的窗口句柄來接收消息。

我的建議是尋找一種方法來解決您的問題,而不是決定您想要什麼解決方案並嘗試使其適合問題。

+0

SendMessage不起作用,但是PostMessage會這樣做,所以cmd.exe的確有一個合適的窗口句柄來接收消息。 – ThreeStarProgrammer57 2018-02-07 10:57:12

相關問題