Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4621416
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T02:41:53+00:00 2026-05-22T02:41:53+00:00

I read many answers on how to send command to cmd prompt. But I

  • 0

I read many answers on how to send command to cmd prompt.

But I do not want to use StreamWriter or similar stuff to input and get output.

I want to use SendMessage to send my string or say command to the cmd prompt window.

Can anyone please help on this?

Just to give detail about my application.
1. My application is a WinForm App.
2. It has 4-5 buttons.
3. Button1 opens the command prompt window while Button5 closes or exits the command prompt window.
4. Button 2,3,4 are command buttons. When user clicks the Button2 command 1 is send to command prompt window. Similar when button 3 and 4 are clicked command 2 and command 3 are sent to the same command prompt window.

Let me know if anybody has a written code which sends string to command prompt.

Thanks and regards,

Rahul

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T02:41:54+00:00Added an answer on May 22, 2026 at 2:41 am

    I have got the solution.
    I used PostMessage().
    Cmd has handle and you can send string to it.
    I was just struggling to find the right way to get the handle.
    Now I got one.

    /*
     * 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)
        {
    
        }
    }
    } 
    

    Thanks to all!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've read many answers to similar questions, but still didn't get to the answer
I've read through many of the questions on SO about this, but many answers
I have read many questions about the facebook login but until not I didnt
I read as many answers as I could for this, but they seem to
I've Googled this and read many posts, but there are so many different answers
I have read many questions and answers, but I couldn´t find solution for my
There are many similar questions, but nothing that answers this specifically after googling around
Disclaimer: on SO I found many similar questions and some answers but none solved
I read many answers on this topic and this is what i have understood
I have read many answers and blog posts on the jQuery queues... and specifically

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.