3

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.

Sudhir B
  • 59
  • 1
  • 4
  • 2
    could you please elaborate on "does not work"? – Franz Gleichmann May 21 '21 at 09:51
  • 1
    @Franz Gleichmann, I have edited the message to explain what I mean by works and does not work. The question looks better now. Thank you for pointing it out. – Sudhir B May 21 '21 at 10:03
  • 1
    I am suprised you are able to *embed* (host inside your application UI) Notepad (it's rather a [hack](https://stackoverflow.com/a/44818598/1997232)). In general this shouldn't be possible at all. When you are developing both application you can use plugin architecture or similar approach to make it possible. – Sinatr May 21 '21 at 10:11
  • 2
    I believe Calc is Windows Store (UWP) app where Notepad is Win32 exe, so they might have different behavior. – Nilay Mehta May 21 '21 at 10:15
  • @Sinatr: I do not think it is a hack. The API is provided by the OS along with User Guide on how to use it.. I am using it exactly as it should be used. – Sudhir B May 21 '21 at 10:31
  • @Nilay, That is interesting. I was not aware of this fact. However that may not be the reason as the exes that I am trying to embed is legacy applications and their behavior is more like calc.exe then notepad.exe – Sudhir B May 21 '21 at 10:33
  • The [SetParent](https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-setparent) is not intented to be used with main windows of other processes (therefore a *hack*). The fact what it works for a simple single-window application like Notepad won't let you to host anything more complex or custom. – Sinatr May 21 '21 at 10:35
  • @Sintar, You seem to have a point there. So I checked again and I did not find anywhere that a external process cannot be a child to another process. Links: 1. https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-setparent 2. https://stackoverflow.com/questions/3459874/good-or-evil-setparent-win32-api-between-different-processes If you do think that this is a hack and should not be implemented, request you kindly guide me on any other way of implementing this with legacy applications. Thanks a lot for your inputs. I really appreciate. – Sudhir B May 21 '21 at 11:03
  • Years ago I tried to do this. Some applications are resistant to being embedded because their windows are non-standard or created with incompatible options. At the time, I couldn't find a universal way of doing this. – Kit May 27 '21 at 16:09
  • @Kit, Thank you for your response. Since I have the source code, I am planing to recreate those applications with different windows settings. Hopefully I should be able to solve this problem. I got this direction based on your response. Thank you once again. – Sudhir B Jun 02 '21 at 12:46

0 Answers0