3

i have a c# powered windows form app and i want to run a exe inside it. that program is another seperate executable file. lets say that that exe is not a dot net app, but written in another language, does it matter ?

so i would want that program to lie inside my winforms, how can i do it?? Also, can i place it anywhere in the winforms? are there like properties already included by default? thanks. hage u tried doing these? if so what are the steps to take? thank you stackoverflow for making a nice developer community. i really cherish this

vvavepacket
  • 1,882
  • 4
  • 25
  • 38
  • Setting a name wouldn't hurt either. – Diego Mijelshon Oct 14 '11 at 02:26
  • Duplicate of [How can I run another application within a panel of my C# program?](https://stackoverflow.com/questions/758494/how-can-i-run-another-application-within-a-panel-of-my-c-sharp-program)? – Sam Hobbs Oct 13 '17 at 00:09

2 Answers2

6

You can do this by PInvoking SetParent():

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

First you need to start the third party application within your application:

var clientApplication = Process.Start("PATH_TO_YOUR_EXECUTABLE");

then set its MainWindowHandle to your main window handle:

SetParent(clientApplication.MainWindowHandle, YourMainWindowOrAContainerControl.Handle);
fardjad
  • 20,031
  • 6
  • 53
  • 68
3
    [DllImport("user32.dll")]
        static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);    

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {

                Process p = Process.Start(textBox1.Text);
                p.WaitForInputIdle();
                SetParent(p.MainWindowHandle, panel1.Handle);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
}

Reference to fardjad but it can only run small programs like notepad, firefox, safari

ArtKorchagin
  • 4,801
  • 13
  • 42
  • 58
donbok
  • 31
  • 1