Embed External Application in C# Form

986 views Asked by At

I am trying to embed an external program in my exe using C#. This seems to work in a few cases but not in others. Is there a difference between the 2 EXE's?

Here is my code.

Process p = new Process();
p.StartInfo.FileName = "calc.exe"; // Does not work.
// p.StartInfo.FileName = "notepad.exe"; // works.
p.Start();
if (!p.HasExited)
{
       p.WaitForInputIdle();
       SetParent(p.MainWindowHandle, this.panel1.Handle);
       bool ret = MoveWindow(p.MainWindowHandle, 0, 10, this.panel1.Width, this.panel1.Height - 10, true);
}

I have also tried adding & removing WaitForInputIdle & Thread.Sleep. But it doesn't seem to change the behavior.

Behavior:

  1. Notepad.exe: The application opens inside the panel. I can drag it around within the panel but it does not go beyond the panel

  2. Calc.exe: Opens outside the process. It is not bound to the panel.

I have 3 exe's provided by the customer that needs to be embedded. I am using "calc.exe" and "notepad.exe" only for the purpose of providing a reproducable code.

OS: Windows 10 Visual Studio Community Edition: 16.9.4

If C# is not the way to go, any suggestions on what would best suit this requirement.

Thank you.

Edit 1: Based on few more search, I changed the style from WS_POPUP to WS_CHILD. But still not success.

Remove parent of window or form

Code:

            uint style = GetWindowLong(p.MainWindowHandle, GWL_STYLE);
            style = (style | WS_POPUP) & (~WS_CHILD);
            SetWindowLong(p.MainWindowHandle, GWL_STYLE, style);

One strange thing I found though was that GetParent was always returning 0 (meaning root application), but SetParent was returning a different result. I thought SetParent should return the current parent, which would be same as GetParent.

Any clue will be appreciated.

0

There are 0 answers