c# - RDP 客户端 - AxHost.InvalidActiveXStateException

标签 c# winforms activex rdp

我正在开发可以优化我的工作的应用程序,通过 RDP 与我的服务器建立多个连接。那时我想做一个单一的窗口窗体来显示我所有的服务器。凭借我的适当知识 - 我想以编程方式在我的 Windows 窗体中添加 AxMsRdpClient2AxMsTscAxNotSafeForScripting 控件。一开始我有一个 Form1,我在其中保存了我的凭据(IP、USER、PASS),并使用简单的 FOR 将 ActiveX Client 添加到我的 Form2 并连接到服务器。

这里我遇到了问题。让我们在下面查看我的代码。

从 Form1() 调用>

var btt = new RunRDP();
    for (int i = 0; i < 15; i++)
    {
        btt.PozitionMaster();
    } 

运行 RDP 类:

private static int _count;
private static int _row = 4;
private static int _colum = 4;
private static int _objId;

public void PozitionMaster()
    {
        if (_count != 6)
        {
            UInitializer(_objId++, _row, _colum);
            _row += 196;
            _count++;

        }
        else
        {
            _colum += 196;
            _row = 4;
            _count = 0;
        }
    }

UInitializer 函数:

public AxMsRdpClient2 rdpClient;

        public void UInitializer(int id, int x, int y)
        {
            Form2 ippo = new Form2();
            rdpClient = new AxMsRdpClient2();

            rdpClient.UserName = "username";
            rdpClient.Server = "192.168.0.100";
            rdpClient.Height = y;
            rdpClient.Width = x;

            rdpClient.BeginInit();
            rdpClient.CreateControl();


            if (ippo.InvokeRequired)
            {
                ippo.Invoke(new Action(() => ippo.Controls.Add(rdpClient)));
                ippo.Invoke(new Action(() => ippo.Update()));
            }
            else
            {
                ippo.Controls.Add(rdpClient);
            }

            //this.Controls.Add(rdpClient);

            rdpClient.AdvancedSettings2.RDPPort = 3389;
            rdpClient.AdvancedSettings2.ClearTextPassword = "hello_rdp";//     
            rdpClient.ColorDepth = 16;//     
            rdpClient.FullScreen = false;//     

            rdpClient.Connect();    
        }

得到那个错误:

    An unhandled exception of type 'System.Windows.Forms.AxHost.
InvalidActiveXStateException' occurred in AxInterop.MSTSCLib.dll

{"Exception of type 'System.Windows.Forms.AxHost+InvalidActiveXStateException' was thrown."}

最佳答案

InvalidActiveXStateException 在您开始使用 ActiveX 控件之前它的 native 窗口被创建时引发。直到您调用 ippo.Show(); 之后才会发生这种情况,将关键属性分配和方法调用移到该语句之后。

关于c# - RDP 客户端 - AxHost.InvalidActiveXStateException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27689826/

相关文章:

c# - 半透明窗体

delphi - 调整 MediaPlayer 的大小,无需播放按钮控件

winforms - 就像Form类一样为Winforms Control添加Load事件

c# - 可以使用 Visual Studio 2008 在 .Net C# 中创建与 Windows Mobile 兼容的 ActiveX 控件吗?

visual-studio-2008 - 将 VB 6.0 ActiveX 控件迁移到 MFC ActiveX 控件

c# - 如何使用条件从不同元素获取所有属性

c# - WebClient下载文件异步不起作用

c# - HiddenFor 助手的问题

c# - Microsoft Azure Functions Nuget 包阻止 NET 库打包到 Nuget 中

c# - 删除 Oracle 中不存在的记录是否被认为是错误的形式?