【问题标题】:casting error while creating RDP connection创建 RDP 连接时出现转换错误
【发布时间】:2023-03-15 10:49:02
【问题描述】:

我正在尝试使用 Windows 形式创建一些 RDP 客户端 anf AxMsTscAxNotSafeForScripting

我有以下方法:

_rdpList = new List<AxMsTscAxNotSafeForScripting>();


public bool addRDP(string ip, string username, string pass)
    {
        for (int i = 0; i < number ; i++)
        {

            if (_rdpList[i].Connected.ToString() != "1")
            {
                try
                {
                    _rdpList[i].Server = ip;
                    _rdpList[i].UserName = username;



                    IMsTscNonScriptable secured = (IMsTscNonScriptable) _rdpList[i].GetOcx());
                    secured.ClearTextPassword = pass;
                    _rdpList[i].Connect();
                    _picList[int.Parse(_rdpList[i].Name)].ImageLocation = greenPath;
                    return true;

                }
                catch (Exception Ex)
                {
                    MessageBox.Show("Error Connecting", "Error connecting to remote desktop " + ip + " Error:  " + Ex.Message, MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

我从线程调用此方法,当它尝试执行时:IMsTscNonScriptable secured = (IMsTscNonScriptable) _rdpList[i].GetOcx());

它失败并出现以下错误:

无法将“System.__ComObject”类型的 COM 对象转换为接口类型“MSTSCLib.IMsTscNonScriptable”。此操作失败,因为 IID 为“{C1E6743A-41C1-4A74-832A-0DD06C1C7A0E}”的接口的 COM 组件上的 QueryInterface 调用因以下错误而失败:不支持此类接口(来自 HRESULT 的异常:0x80004002 (E_NOINTERFACE)) .

我整天都在研究它,但我无法理解这个问题?

奇怪的是,如果我从 Windows 窗体事件(例如按钮单击)调用此方法,每项工作都很好,当我从 WCF 服务调用此方法时会出现唯一的问题。

请帮忙。

【问题讨论】:

    标签: c# com casting


    【解决方案1】:

    在您的代码中替换:

    IMsTscNonScriptable secured = (IMsTscNonScriptable) _rdpList[i].GetOcx());
    secured.ClearTextPassword = pass;
    

    与:

    _rdpList[i].AdvancedSettings2.ClearTextPassword = pass;
    

    注意:如果 AdvancedSettings2 在对象 _rdpList[i] 上不可用,请删除对 MSTSCLib 的引用并重新添加。

    【讨论】:

      【解决方案2】:

      可能有很多解决方案,但我认为最简单的方法是在这里使用dynamic。据我了解,这是他们创建 dynamic 关键字的部分原因。只要你知道接口,那么你就可以使用它而不必担心投射。您将失去智能感知,但省去了在 COM 上拔头发的麻烦

      Some documentation on dynamic

      您的代码如下所示:

      dynamic secured = _rdpList[i].GetOcx());
      

      【讨论】:

      • 我尝试完全按照您的建议并在尝试执行 secured.ClearTextPassword = pass; 后使用动态 BUT 行我收到此错误消息 'System.__ComObject' does not contain a definition for 'ClearTextPassword'
      猜你喜欢
      • 2018-02-23
      • 1970-01-01
      • 1970-01-01
      • 2018-12-12
      • 1970-01-01
      • 2018-01-20
      • 1970-01-01
      • 2019-02-14
      • 2016-05-30
      相关资源
      最近更新 更多