Click here to Skip to main content
15,896,310 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I found this code somewhere in the forum website and so it's not mine code. The code will run and when click on the prompt button and command prompt will display in the GUI windows but when I click on the cmd button; nothing happen.

Does anybody knows how to get the code to works? Again, the goal is to launch the program; and by clicking button I can opened/embedded "Command Prompt" in the appl and use the "cmd" button to send command to the Command Prompt.

*****Code***************


C#
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.IO;
using System.Threading;

namespace picBoxApp
{
    /// <summary>
    /// Description of MainForm.
    /// </summary>
    public partial class MainForm : Form
    {
        public MainForm()
        {
            //
            // The InitializeComponent() call is required for Windows Forms designer support.
            //
            InitializeComponent();

            //
            // TODO: Add constructor code after the InitializeComponent() call.
            //
            // Killing a process
            // Process p= Process.GetProcessById(PID);
            // p.Kill();

        }

        private void timer1_Tick(object sender, EventArgs e)
        //void Timer1Tick(object sender, EventArgs e)
        {
            System.IntPtr winParent;
            System.IntPtr x;
            System.IntPtr winHandle = Usr32.FindWin("ConsoleWindowClass", "MY OWN CONSOLE");
            if (winHandle != System.IntPtr.Zero)
            {
                winParent = Usr32.GetParent(winHandle);
                x = Usr32.SetParent(winHandle, this.pictureBox1.Handle);
                x = Usr32.SetWindowPos(winHandle, 0, -5, -35, 649, 555, 0);
                this.timer1.Enabled = false;
            }
        }

        private void exit_Click(object sender, EventArgs e)
        //void BtnExitClick(object sender, EventArgs e)
        {
            //System.Diagnostics.Process.GetProcessById
            //this.timer1.Enabled = false;
            if (Usr32.MYCMDPID != 0)
            {
                try
                {
                    System.Diagnostics.Process p = System.Diagnostics.Process.GetProcessById(Usr32.MYCMDPID);
                    if (p != null)
                    {
                        p.Kill();
                    }
                }
                catch
                {
                    //MessageBox.Show("Process terminated Abnormally", "Error", 200);
                }
            }
            Application.Exit();


        }

        private void Start_Click(object sender, EventArgs e)
       //void BtnStartClick(object sender, System.EventArgs e)
        {
            //MessageBox.Show("Hello World", "Hello");
            //string cmd1 = "generate business card";
            //MessageBox.Show (cmd1 , "Command One");
            //string pid;

            //uint pid;
            //CmdWindow pid = new CmdWindow();

            if (Usr32.MYCMDPID != 0)
            {
                try
                {
                    System.Diagnostics.Process p = System.Diagnostics.Process.GetProcessById(Usr32.MYCMDPID);
                    if (p != null)
                    {
                        p.Kill(

      );
                    }
                }
                catch
                {
                    //MessageBox.Show("Starting a new Process!", "Error");
                }
            }
            Process mycmd = new Process();
            mycmd.StartInfo.UseShellExecute = false;
            mycmd.StartInfo.Arguments += " /K TITLE MY OWN CONSOLE";
            mycmd.StartInfo.FileName = "cmd.exe";
            mycmd.StartInfo.CreateNoWindow = false;
  
            //Thread.Sleep(100);
            //SendKeys.Send("CD \\Twidl {Enter}");
            //Thread.Sleep(100);
            //SendKeys.Send("cls {Enter}");
            //Thread.Sleep(100);
            //SendKeys.Send("twidl.bat {Enter}");
            //mycmd.StartInfo.ErrorDialog = false;
            //mycmd.StartInfo.RedirectStandardError = true;
            //mycmd.StartInfo.RedirectStandardInput = true;
            //mycmd.StartInfo.RedirectStandardOutput = true;
            //mycmd.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
            // Do not show command prompt window separately
            //psi.CreateNoWindow = true;
            //psi.WindowStyle = ProcessWindowStyle.Hidden;
            //redirect all standard inout to program


            mycmd.Start();

            this.timer1.Enabled = true;
            //mycmd.Close();
            //MessageBox.Show("Process Id : " + bob.Id + " and Process Name : " + bob.ProcessName , "Process Details");
            //link the streams to standard inout of process
            //StreamWriter inputWriter = mycmd.StandardInput;
            //StreamReader outputReader = mycmd.StandardOutput;
            //StreamReader errorReader = mycmd.StandardError;
            //send command to cmd prompt and wait for command to execute with thread sleep
            //inputWriter.WriteLine("DIR\r\n");

            Usr32.MYCMDPID = mycmd.Id;



        }
        private void cmd_Click(object sender, EventArgs e)
        //void BtnCommandClick(object sender, System.EventArgs e)
        {

            Process p = Process.GetProcessById(Usr32.MYCMDPID);
            //Process p = Process.GetWindow(Usr32.MYCMDPID);
            IntPtr pid;
            pid = p.Handle;
            Usr32.SetForegroundWindow(pid);
            Thread.Sleep(300);
            SendKeys.Send("DIR \r\n");
            //SendKeys.Send("CD \\Twidl {Enter}");
            //Thread.Sleep(800);
            //SendKeys.Send("twidl.bat {Enter}");
        }

        
   
        //void BtnAttachConsoleClick(object sender, EventArgs e)
        private void attachpic_Click(object sender, EventArgs e)
        {
            this.timer1.Enabled = true;
        }

        private void chkBoxConsole_CheckedChanged(object sender, EventArgs e)
        {
            string mytitle;
            if (chkBoxConsole.Checked)
            {
                mytitle = "MY CONSOLE";
                Usr32.AllocConsole();
                System.Console.Title = mytitle;


            }
            else
                Usr32.FreeConsole();
        }

        
  
    }
    public class Usr32
    {
        #region Class Variables
        public const int SM_CXSCREEN = 0;
        public const int SM_CYSCREEN = 0;
        public static int MYCMDPID = 0;
        #endregion

        #region Class Functions
        [DllImport("user32.dll", EntryPoint = "FindWindow")]
        public static extern IntPtr FindWin(string lpClassName, string lpWindowName);

        [DllImport("user32.dll", EntryPoint = "SetParent")]
        public static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);

        [DllImport("user32.dll", EntryPoint = "GetParent")]
        public static extern IntPtr GetParent(IntPtr hWnd);

        [DllImport("user32.dll", EntryPoint = "SetWindowPos")]
        public static extern IntPtr SetWindowPos(IntPtr hWnd, int hWndInsertAfter, int x, int Y, int cx, int cy, int wFlags);

        [DllImport("user32.dll", EntryPoint = "ShowWindow")]
        public static extern IntPtr ShowWindow(IntPtr hWnd, long nCmdShow);

        [DllImport("user32.dll", EntryPoint = "CloseWindow")]
        public static extern IntPtr CloseWindow(IntPtr hWnd);

        [DllImport("user32.dll", EntryPoint = "DestroyWindow")]
        public static extern IntPtr DestroyWindow(IntPtr hWnd);

        [DllImport("User32.dll")]
        public static extern int SetForegroundWindow(IntPtr hwnd);

        [DllImport("user32.dll")]
        public static extern uint SendMessage(IntPtr hWnd, uint nMessage, uint wParam, uint lParam);

        [DllImport("user32.dll")]
        public static extern uint PostMessage(IntPtr hWnd, uint nMessage, uint wParam, uint lParam);

        // my lines below are needed to make the console window visible

        [DllImport("kernel32.dll")]
        public static extern Boolean AllocConsole();

        [DllImport("kernel32.dll")]
        public static extern Boolean FreeConsole();


        #endregion
    }



}
Posted
Updated 22-Mar-12 19:36pm
v2
Comments
Sergey Alexandrovich Kryukov 22-Mar-12 23:08pm    
Two words: dirty stuff. Why doing it? (Should I ask: why wasting time on it?)
--SA
Shahin Khorshidnia 23-Mar-12 5:40am    
Wow!

1 solution

If you didn't get it here, go where you did get it, and ask there - the person who wrote it will have a better understanding of it that we will. We would have to wade through your code dump, and work it out, like you should.

If you did get it here, then don't post this under Quick Answers - if you got the code from an article, then there is a "new message" button at the bottom of that article, which causes an email to be sent to the author. They are then alerted that you wish to speak to them.
Posting this here relies on them "dropping by" and realising it is for them.
 
Share this answer
 
Comments
Shahin Khorshidnia 23-Mar-12 5:39am    
+5I love your answers, OriginalGriff!
:D

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900