是否可以在WinForms应用程序中运行独立的应用程序?

时间:2013-08-20 14:20:56

标签: c# winforms remote-access remote-desktop

我需要使用WinForms主机应用程序控制一个独立的应用程序,就像其他应用程序在远程桌面上运行一样,而我新开发的主机应用程序是远程桌面主机。 CodeProject文章Remote Desktop using C#.NET鼓舞着我的任务可能性不是零。它解释了如何使用“Microsoft终端服务控件类型库”或MSTSCLib.dll来执行此操作。

Howevere,我不想连接到远程桌面。如果有的话,我想连接到同一台机器上的第二个桌面,如果有必要独立运行托管应用程序,或类似的东西。 MSTSCLib可以实现这一切吗?如果是这样,我需要考虑哪些方面来进一步开发设计?

重要提示:无法访问外部程序代码的约束不再存在。 “客人”节目将仅来自一系列特别定义的节目。

1 个答案:

答案 0 :(得分:1)

我的一位朋友有时候这样做了!

你应该做这样的事情:

首先导入系统DLL:

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

    [DllImport("user32.dll", EntryPoint = "SetWindowLongA", SetLastError = true)]
    private static extern long SetWindowLong(IntPtr hwnd, int nIndex, long dwNewLong);

    [DllImport("user32.dll", SetLastError = true)]
    private static extern bool MoveWindow(IntPtr hwnd, int x, int y, int cx, int cy, bool repaint);

现在声明一个计时器并遵循以下代码:

    private const int GWL_STYLE = (-16);
    private const int WS_VISIBLE = 0x10000000;
    Process p;
 /*Closing Is Timer*/
        private void Closing_Tick(object sender, EventArgs e)
    {


            p.Refresh();
            string a = p.ProcessName;              
                SetParent(p.MainWindowHandle, panel1.Handle);
                SetWindowLong(p.MainWindowHandle, GWL_STYLE, WS_VISIBLE);
              MoveWindow(p.MainWindowHandle, 0, 0, this.Width, this.Height, true);


    }
  void run(string add)
    {
        string addres = add;

        try
        {
            p = Process.Start(addres);
            Thread.Sleep(500); // Allow the process to open it's window
            SetParent(p.MainWindowHandle, panel1.Handle);
            SetWindowLong(p.MainWindowHandle, GWL_STYLE, WS_VISIBLE);
            MoveWindow(p.MainWindowHandle, 0, 0, this.Width, this.Height, true);


        }
        catch
        {
            Closeing.Enabled = false;
            MessageBox.Show(addres + "\n" + "Not Found", "Error",
          MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, MessageBoxOptions.RtlReading);
            Environment.Exit(0);

        }
    }

    private void button1_Click(object sender, EventArgs e)
    {
        Closeing.Enabled = true;
        run(@textBox1.Text); 
    }

运行方法的输入参数是要在应用程序中运行的程序的路径

希望这个帮助! :)