首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用SendMessage从Windows窗体应用程序向命令提示符(cmd)发送字符串

使用SendMessage从Windows窗体应用程序向命令提示符(cmd)发送字符串
EN

Stack Overflow用户
提问于 2011-05-09 01:51:38
回答 3查看 5.9K关注 0票数 0

我读了很多关于如何发送命令到命令提示符的答案。

但我不想使用StreamWriter或类似的东西来输入和获取输出。

我想使用SendMessage将我的字符串或说命令发送到命令提示符窗口。

有没有人能帮个忙?

只是为了给出我的申请的细节。1.我的应用程序是WinForm应用程序。2.有4-5个按钮。3.当Button5关闭或退出命令提示符窗口时,Button1会打开命令提示符窗口。4.按钮2、3、4是命令按钮。当用户单击Button2命令时,命令1将被发送到命令提示符窗口。类似地,当单击按钮3和4时,命令2和命令3被发送到相同的命令提示符窗口。

让我知道如果有人写的代码,发送字符串到命令提示符。

感谢并致以问候

拉胡尔

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2011-05-13 11:34:04

我已经有了解决方案。

我使用了PostMessage()。

Cmd有句柄,你可以向它发送字符串。

我只是在努力寻找正确的方法来获得句柄。

现在我有了一个。

代码语言:javascript
复制
/*
 * 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)
    {

    }
}
} 

感谢所有人!

票数 2
EN

Stack Overflow用户

发布于 2011-05-09 03:04:49

使用SendMessage()似乎不太可能做到这一点。SendMessage()需要一个窗口句柄,我不认为cmd.exe有合适的窗口句柄来接收消息。

我的建议是寻找一种方法来解决你的问题,而不是决定你想要什么解决方案,并试图让它适合问题。

票数 1
EN

Stack Overflow用户

发布于 2011-05-09 02:50:50

从你的解释中(至少对我来说不是),我不清楚为什么你不能在你描述的场景中使用streamWriter。你能详细说明一下吗?

我会查看Process类,以便通过标准输入/输出与其他程序进行对话

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5929152

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档