简体   繁体   English

远程桌面ActiveX控件

[英]Remote Desktop ActiveX control

I've got a C# application that I've been using for years to script remote desktop connections. 我有一个C#应用程序,我多年来一直用它来编写远程桌面连接的脚本。 It's always been built on the AxMsRdpClient3 (note the 3, which I guess is some kind of version number). 它始终建立在AxMsRdpClient3上(注意3,我猜这是某种版本号)。 There are features in AxMsRdpClient8 (version 8) that I want to be able to use, but as I understand it, this requires Remote Desktop version 8 to be installed. 我希望能够使用AxMsRdpClient8(版本8)中的功能,但据我了解,这需要安装远程桌面版本8。 Not all users have that installed (or even can install it on Windows XP/Vista), though. 但并非所有用户都安装了这些用户(甚至可以安装在Windows XP / Vista上)。

So as Sheng Jiang suggested, I am creating the control at runtime now and I have code that looks like this: 正如盛江建议的那样,我现在正在运行时创建控件,我的代码如下所示:

try
{
    AxMsRdpClient8 rdp8 = new AxMsRdpClient8();
    rdp8.BeginInit();
    //  set some properties here
    rdp8.EndInit(); // throws Exception on machines without version 8 installed
}
catch (Exception ex)
{
    AxMsRdpClient3 rdp3 = new AxMsRdpClient3();
    rdp3.BeginInit();
    //  set some properties here
    rdp3.EndInit();
}

As expected, rdp8.EndInit() throws an exception on machines that do not have Remote Desktop version 8 installed. 正如预期的那样,rdp8.EndInit()会在未安装远程桌面版本8的计算机上引发异常。 The problem is that after we try to create the AxMSRDPClient8, the rdp3.EndInit() fails as well (class not registered) on older machines. 问题是,在我们尝试创建AxMSRDPClient8之后,rdp3.EndInit()也在旧机器上失败(类未注册)。 If I don't attempt to create the AxMSRDPClient8 first, the AxMSRDPClient3 initializes and works correctly. 如果我不首先尝试创建AxMSRDPClient8,则AxMSRDPClient3初始化并正常工作。

each version of RDP activeX has a different clsid . 每个版本的RDP activeX都有不同的clsid You need to detect the OS version and create an activex at runtime with the class id corresponding to the lowest OS version you plan to support . 您需要检测操作系统版本并在运行时创建一个activex,其类ID对应于您计划支持的最低操作系统版本

If your code depends on late binding, better rewrite your code to work with IMsRdpClient* and IMsRdpClientNonScriptable*. 如果您的代码依赖于后期绑定,则可以更好地重写代码以使用IMsRdpClient *和IMsRdpClientNonScriptable *。 For example MsRdpClient8NotSafeForScripting supports the following interfaces: 例如,MsRdpClient8NotSafeForScripting支持以下接口:

  • up to version 8 of IMsRdpClient* 最高版本8的IMsRdpClient *
  • up to version 5 of IMsRdpClientNonScriptable 最多为IMsRdpClientNonScriptable的第5版
  • IMsTscNonScriptable IMsTscNonScriptable
  • IMsRdpPreferredRedirectionInfo IMsRdpPreferredRedirectionInfo
  • IMsRdpExtendedSettings IMsRdpExtendedSettings

MsRdpClient3NotSafeForScripting supports MsRdpClient3NotSafeForScripting支持

  • version 2 of IMsRdpClient* IMsRdpClient版本2 *
  • IMsRdpClientNonScriptable IMsRdpClientNonScriptable
  • IMsTscNonScriptable. IMsTscNonScriptable。

if you want to detect whether your activex support a particular interface version, just cast the ActiveX's instance to the interface. 如果要检测您的activex是否支持特定的接口版本,只需将ActiveX的实例强制转换为接口即可。 when a cast fails you know the interface is not supported. 当转换失败时,您知道不支持该接口。

protected void CreateRdpActiveX()
{
    try
    {
        string clsid=GetRdpActiveXClsIdByOSVersion();
        Type type = Type.GetTypeFromCLSID(clsid, true);
        this.axRdp = new AxHost (type.GUID.ToString());
        ((ISupportInitialize)(axRdp)).BeginInit();
        SuspendLayout();
        this.panel1.Controls.Add(axRdp);     
        ((ISupportInitialize)(axRdp)).EndInit();
        ResumeLayout(false);
        var msRdpClient8 = axRdp.GetOcx() as IMsRdpClient8;
        if(msRdpClient8!=null)
        {
             var advancedSettings9 =msRdpClient8.AdvancedSettings9 as IMsRdpClientAdvancedSettings8;
             if(advancedSettings9!=null) 
                 advancedSettings9.BandwidthDetection=true;

        }
    }
    catch (System.Exception ex)
    {
        System.Console.WriteLine(ex.Message);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM