2

我正在尝试使用 Windows 窗体 anf 创建一些 RDP 客户端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 服务调用此方法时会出现唯一的问题。

请帮忙。

4

2 回答 2

2

在您的代码中替换:

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

和:

_rdpList[i].AdvancedSettings2.ClearTextPassword = pass;

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

于 2016-08-07T14:02:10.467 回答
1

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

一些关于动态的文档

您的代码如下所示:

dynamic secured = _rdpList[i].GetOcx());
于 2012-05-03T01:10:29.307 回答