Mangala G

Mangala G

  • NA
  • 8
  • 2.1k

Displaying non-windows exe application inside windows form

Oct 26 2018 1:10 AM

How can I embed a non-native application (application installed in windows 7)in a windows form using c#.

What I have tried so far:
  1. public partial class Form1 : Form   
  2.   
  3. {   
  4. Process process = new Process();  
  5.   
  6.  [DllImport("user32.dll", SetLastError = true)]  
  7.     static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);  
  8.   
  9.     [DllImport("user32.dll", SetLastError = true)]  
  10.     internal static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint);  
  11.   
  12.     [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]  
  13.     static extern bool PostMessage(HandleRef hWnd, uint Msg, IntPtr wParam, IntPtr lParam);  
  14.     [DllImport("user32.dll")]  
  15.     static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);  
  16.   
  17.     [DllImport("User32.dll")]  
  18.     private static extern bool SetForegroundWindow(IntPtr hWnd);  
  19.     public Form1()  
  20.     {  
  21.         InitializeComponent();  
  22.         // panel1.Container.Add(process);  
  23.         process.StartInfo.RedirectStandardOutput = true;  
  24.         process.StartInfo.RedirectStandardInput = true;  
  25.         process.StartInfo.UseShellExecute = false;  
  26.         process.StartInfo.CreateNoWindow = false;  
  27.          string path = Path.Combine(Path.GetFullPath(@"C:\Program Files (x86)\Plan-G v3.2.0"), "Plan-G3.exe");  
  28.          process = Process.Start(path);  
  29.   
  30.          Debug.WriteLine(process.MainWindowHandle);  
  31.          while (process.MainWindowHandle == IntPtr.Zero)  
  32.         {  
  33.              Thread.Sleep(1000); // Don't hog the CPU  
  34.             process.Refresh(); // You need this since `MainWindowHandle` is cached  
  35.   
  36.   
  37.         }  
  38.   
  39.         Form1.SetForegroundWindow(process.MainWindowHandle);  
  40.         SetForegroundWindow(process.MainWindowHandle);  
  41.   
  42.         SetParent(process.MainWindowHandle, panel1.Handle);  
  43.   
  44.         MoveWindow(process.MainWindowHandle, 0, 0, panel1.Width - 90, panel1.Height, true);  
  45.   
  46.   
  47.     }  
  48. }

I have also tried:

  1. public partial class Form1 : Form  
  2.     {  
  3.         [DllImport("user32.dll")]  
  4.         static extern uint GetWindowThreadProcessId(IntPtr hWnd, IntPtr ProcessId);  
  5.   
  6.         [DllImport("user32.dll")]  
  7.         private static extern IntPtr GetForegroundWindow();  
  8.   
  9.         [DllImport("kernel32.dll")]  
  10.         static extern uint GetCurrentThreadId();  
  11.         [DllImport("user32.dll")]  
  12.         static extern bool AttachThreadInput(uint idAttach, uint idAttachTo,  
  13.    bool fAttach);  
  14.   
  15.         [DllImport("user32.dll", SetLastError = true)]  
  16.         static extern bool BringWindowToTop(IntPtr hWnd);  
  17.   
  18.         [DllImport("user32.dll", EntryPoint = "ShowWindow", SetLastError = true)]  
  19.         static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);  
  20.   
  21.         [DllImport("user32.dll", SetLastError = true)]  
  22.         static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);  
  23.   
  24.         [DllImport("user32.dll", SetLastError = true)]  
  25.         internal static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint);  
  26.         Process process = new Process();  
  27.         private static void ForceForegroundWindow(IntPtr hWnd)  
  28.   
  29.         {  
  30.   
  31.             uint foreThread = GetWindowThreadProcessId(GetForegroundWindow(), IntPtr.Zero);  
  32.   
  33.             uint appThread = GetCurrentThreadId();  
  34.   
  35.             const int SW_SHOW = 5;  
  36.   
  37.             if (foreThread != appThread)  
  38.   
  39.             {  
  40.   
  41.                 AttachThreadInput(foreThread, appThread, true);  
  42.   
  43.                 BringWindowToTop(hWnd);  
  44.   
  45.                 ShowWindow(hWnd, SW_SHOW);  
  46.   
  47.                 AttachThreadInput(foreThread, appThread, false);  
  48.   
  49.             }  
  50.   
  51.             else  
  52.   
  53.             {  
  54.   
  55.                 BringWindowToTop(hWnd);  
  56.   
  57.                 ShowWindow(hWnd, SW_SHOW);  
  58.   
  59.             }  
  60.   
  61.         }  
  62.         public Form1()  
  63.         {  
  64.             InitializeComponent();  
  65.   
  66.             process.StartInfo.RedirectStandardOutput = true;  
  67.             process.StartInfo.RedirectStandardInput = true;  
  68.             process.StartInfo.UseShellExecute = false;  
  69.             string path = Path.Combine(Path.GetFullPath(@"C:\Program Files (x86)\Plan-G v3.2.0"), "Plan-G3.exe");  
  70.             process = Process.Start(path);  
  71.   
  72.             Debug.WriteLine(process.MainWindowHandle);  
  73.             while (process.MainWindowHandle == IntPtr.Zero)  
  74.             {  
  75.                 Thread.Sleep(1000); // Don't hog the CPU  
  76.                 process.Refresh(); // You need this since `MainWindowHandle` is cached  
  77.   
  78.   
  79.             }  
  80.   
  81.             //Form1.SetForegroundWindow(process.MainWindowHandle);  
  82.             ForceForegroundWindow(process.MainWindowHandle);  
  83.   
  84.             SetParent(process.MainWindowHandle, this.Handle);  
  85.   
  86.             MoveWindow(process.MainWindowHandle, 0, 0, this.Width - 90, this.Height, true);  
  87.                
  88.   
  89.   
  90.         }  
  91.     }  
I am able to get this to work sometimes when i start with debugging and the application opens within the form. But when i start without debugging, the application always opens outside of the form. I don't understand why this happens.

Is what I am trying to accomplish possible? Based on other forum answers, it seems it might not be Thank you