1

I am trying to trigger and application (in this case SAP2000) inside a C# program so that the application is shown in a panel. I have searched in different posts and it seems that the 'SetParent()' strategy is the most suitable for this purpose. However it is also true that some users have reported to have some problems with it and different approaches of the 'SetParent()' strategy have been defined. Below you can find mine:

Despite high 'delay' values defined in 'proc.WaitForInputIdle(1000)' and 'Thread.Sleep(10000)', the application (SAP2000) not always is shown in the panel and when it does the application buttons can be pressed but they don't react (see picture).

My question is: is 'SetParent()' a suitable method to run this kind of 'heavy' applications in a C# program panel? Or is it necessary another strategy for this case? If so, I would really appreciate any advice to deal with this issue.

Thanks in advance. Regards

using System;
using System.Linq;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.Threading;

namespace Prueba_OAPI_CSi_SAP
{
    public partial class Form1 : Form
    {        
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {

            Process proc = Process.Start("C:\\Program Files\\Computers and Structures\\SAP2000 22\\SAP2000.exe");
            if (proc != null)
            {
                proc.WaitForInputIdle(1000);
                while (proc.MainWindowHandle == IntPtr.Zero)
                {
                    Thread.Sleep(10000);
                    proc.Refresh();
                }

                OpcionesTeclado.SetParent(proc.MainWindowHandle, panel2.Handle);
                OpcionesTeclado.MessageBeep(1);
                this.BringToFront();
            }
        }

    public class OpcionesTeclado
    {               
        [DllImport("user32.dll", SetLastError = true)]
        public static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);

        [DllImport("user32.dll")]
        public static extern bool MessageBeep(int uType);
    }
}

enter image description here

David M
  • 23
  • 3
  • [Is it legal to have a cross-process parent/child or owner/owned window relationship](https://devblogs.microsoft.com/oldnewthing/20130412-00/?p=4683): "It is also technically legal to juggle chainsaws. ... they become near-impossible to manage if one or both of the windows involved is unaware that it is participating in a cross-process window tree." – Damien_The_Unbeliever Apr 14 '21 at 08:47
  • And further - "I often see this question in the context of somebody who wants to grab a window belonging to another process ... That other process was totally unprepared for its window being manipulated in this way" – Damien_The_Unbeliever Apr 14 '21 at 08:47
  • I see, thanks. So, any advice to deal with this issue or I must assume it is not possible this kind of interactions? – David M Apr 14 '21 at 09:37

0 Answers0