【问题标题】:run a foreign exe inside a windows form app在 Windows 窗体应用程序中运行外部 exe
【发布时间】:2011-12-07 10:09:01
【问题描述】:

我有一个 c# 驱动的 windows 窗体应用程序,我想在其中运行一个 exe。该程序是另一个单独的可执行文件。可以说那个exe不是点网应用程序,而是用另一种语言编写的,有关系吗?

所以我希望该程序位于我的 winforms 中,我该怎么做?另外,我可以把它放在winforms的任何地方吗?默认情况下是否已经包含类似的属性?谢谢。你试过做这些吗?如果是这样,要采取什么步骤?感谢 stackoverflow 打造了一个不错的开发者社区。我真的很珍惜这个

【问题讨论】:

标签: c#


【解决方案1】:

您可以通过 PInvoking SetParent() 来做到这一点:

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

首先你需要在你的应用中启动第三方应用:

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

然后将其MainWindowHandle 设置为您的主窗口句柄:

SetParent(clientApplication.MainWindowHandle, YourMainWindowOrAContainerControl.Handle);

【讨论】:

    【解决方案2】:
        [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);
                }
            }
    }
    

    参考 fardjad 但它只能运行记事本、firefox、safari 等小程序

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多