c# - 创建 RDP 连接时出现转换错误

标签 c# com casting

我正在尝试使用 Windows 表单创建一些 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 secure = (IMsTscNonScriptable) _rdpList[i].GetOcx());

失败并出现以下错误:

Unable to cast COM object of type 'System.__ComObject' to interface type 'MSTSCLib.IMsTscNonScriptable'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{C1E6743A-41C1-4A74-832A-0DD06C1C7A0E}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).

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

奇怪的是,如果我从 Windows 窗体事件(例如按钮单击)调用此方法,那么每次都工作正常,唯一的问题是当我从 WCF 服务调用此方法时发生。

请帮忙。

最佳答案

替换为您的代码:

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

与:

_rdpList[i].AdvancedSettings2.ClearTextPassword = pass;

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

关于c# - 创建 RDP 连接时出现转换错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10423779/

相关文章:

c++ - ->Release() 是在 COM 对象的析构函数上调用的吗?

java - 如何将从 Web 服务接收到的 Binary64 格式的图像转换为实际图像?

c - 强制转换指针的算术

c# - 将价格除以 3

c# - HTTP 请求和响应 C#

javascript - 如何使用 Jquery .load() 重新加载部分 View ?

用于连接两个 Bstr 字符串的 C++ 代码

c# - 预处理器指令

c# - .NET 互操作是来回复制数组数据,还是固定数组?

警告 “dereferencing type-punned pointer will break strict-aliasing rules” 的后果