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
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}
private void timer1_Tick(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)
{
if (Usr32.MYCMDPID != 0)
{
try
{
System.Diagnostics.Process p = System.Diagnostics.Process.GetProcessById(Usr32.MYCMDPID);
if (p != null)
{
p.Kill();
}
}
catch
{
}
}
Application.Exit();
}
private void Start_Click(object sender, EventArgs e)
{
if (Usr32.MYCMDPID != 0)
{
try
{
System.Diagnostics.Process p = System.Diagnostics.Process.GetProcessById(Usr32.MYCMDPID);
if (p != null)
{
p.Kill(
);
}
}
catch
{
}
}
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;
mycmd.Start();
this.timer1.Enabled = true;
Usr32.MYCMDPID = mycmd.Id;
}
private void cmd_Click(object sender, EventArgs e)
{
Process p = Process.GetProcessById(Usr32.MYCMDPID);
IntPtr pid;
pid = p.Handle;
Usr32.SetForegroundWindow(pid);
Thread.Sleep(300);
SendKeys.Send("DIR \r\n");
}
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);
[DllImport("kernel32.dll")]
public static extern Boolean AllocConsole();
[DllImport("kernel32.dll")]
public static extern Boolean FreeConsole();
#endregion
}
}