3

I would like to open process in within my windows form application.

For example I would like when a user press on button in one of the windows form container the mstsc.exe will open.

And if he will press on the button it will open IE on another container,

[DllImport("user32.dll", SetLastError = true)]
        static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
       private void button1_Click(object sender, EventArgs e)
    {
        Process p = Process.Start("mstsc.exe", @"c:\VPN4.rdp");
        Thread.Sleep(3000);
        p.StartInfo.CreateNoWindow = true;    // new
        SetParent(p.MainWindowHandle, this.panel1.Handle);
        p.StartInfo.WindowStyle = ProcessWindowStyle.Maximized;   //new

    }

It open the process but not in the windows form ,,,

IAbstract
  • 19,551
  • 15
  • 98
  • 146
MoShe
  • 6,197
  • 17
  • 51
  • 77
  • 1
    Why are you setting CreateNoWindow and WindowStyle? Why set the parent if there is no Window? – vcsjones May 03 '12 at 15:37
  • *And if he will press on the button* -- which button are you referring to here? The button on the WinForm should start MSTSC.exe - what is the second button that should open IE? – IAbstract May 03 '12 at 15:38
  • 4
    Sounds like he's trying to wrap this other program into his program. I dont think that's really possible... – Tejs May 03 '12 at 15:40
  • 1
    You can't open other application inside your own window. – SimpleVar May 03 '12 at 15:42
  • "It open the process but not in the windows form ,,," this makes no sense. – Security Hound May 03 '12 at 17:12
  • @Ramhound this does make sense. What they meant by that was simply "The process that I started opens in its own window, but not inside my application's window." – uSeRnAmEhAhAhAhAhA Oct 10 '13 at 23:40
  • @Giovani It didn't make sense to me. It's broken English and even your statement is not clear. Did you really respond to a year old comment to tell me it was clear... Really? – Security Hound Oct 10 '13 at 23:50
  • Maybe this will explain what they're trying to accomplish: `panel1.Controls.Add(notepad.exe);` – uSeRnAmEhAhAhAhAhA Oct 10 '13 at 23:53

2 Answers2

6

You can start another process, and place it's window inside your own.

Try using your forms handle instead of the panels. Here is a short example

private void toolStripButton1_Click(object sender, EventArgs e)
{
  ProcessStartInfo info = new ProcessStartInfo();
  info.FileName = "notepad";
  info.UseShellExecute = true;
  var process = Process.Start(info);

  Thread.Sleep(2000);

  SetParent(process.MainWindowHandle, this.Handle);
}

As for the original questions specific problem, I would guess it is due to remote desktop opening other windows for the application to do it's work, and the MainWindowHandle is not the one you are after.

Try using FindWindowEx in the windows API to search for the correct window. If you search by process ID and title, you ought to find the right one. Stackoverflow and google have a large amount of information. Just search for "C# FindWindowEx"

Phil Martin
  • 466
  • 4
  • 4
1
ProcessStartInfo info = new ProcessStartInfo();
info.FileName = "notepad";
info.UseShellExecute = true;
Process.Start(info);
Romil Kumar Jain
  • 20,239
  • 9
  • 63
  • 92