需要将命令发送到C#中的嵌入式命令提示符 [英] Need to send the command to embedded Command Prompt in C#

查看:45
本文介绍了需要将命令发送到C#中的嵌入式命令提示符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在论坛网站的某个地方找到了此代码,因此它不是我的代码.该代码将运行,并且在单击提示按钮和命令提示符时将在GUI窗口中显示,但是当我单击cmd按钮时;什么都没发生.

有人知道如何使代码正常工作吗?同样,目标是启动该程序.通过单击按钮,我可以在应用程序中打开/嵌入命令提示符",并使用"cmd"按钮将命令发送到命令提示符.

*****代码***************


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***************


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
    }



}

推荐答案

如果您没有在这里得到它,请到哪里得到它,然后问问-编写它的人将会我们会更好地理解它.我们将不得不遍历您的代码转储,然后像您应该的那样进行解决.

如果您确实在此处得到它,则不要在快速解答"下发布该信息-如果您从某篇文章中获得了代码,那么该文章的底部会出现一个新消息"按钮,这将导致发送电子邮件给作者.然后会通知他们您希望与他们交谈.
将其发布到此处依赖于他们的下落"并意识到它是针对他们的.
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.


这篇关于需要将命令发送到C#中的嵌入式命令提示符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆