private void TryVersion(RDPClientVersion clientVersion, MethodInvoker doApplyVersion)
        {
            try
            {
                if (lastDetectedVersion == RDPClientVersion.Unknown || lastDetectedVersion == clientVersion)
                {
                    doApplyVersion();
                    parent.Controls.Add((Control)client);                     // can throw a COMException

                    // Finish initialization - com object proxies
                    TrySetSecuredSettings(client.SecuredSettings, typeof(IMsTscSecuredSettings));
                    TrySetSecuredSettings(client.SecuredSettings2, typeof(IMsRdpClientSecuredSettings));

                    TrySetAdvancedSettings(client.AdvancedSettings9, typeof(MSTSCLib.IMsRdpClientAdvancedSettings8));
                    TrySetAdvancedSettings(client.AdvancedSettings8, typeof(MSTSCLib.IMsRdpClientAdvancedSettings7));
                    TrySetAdvancedSettings(client.AdvancedSettings7, typeof(MSTSCLib.IMsRdpClientAdvancedSettings6));
                    TrySetAdvancedSettings(client.AdvancedSettings6, typeof(MSTSCLib.IMsRdpClientAdvancedSettings5));
                    TrySetAdvancedSettings(client.AdvancedSettings5, typeof(MSTSCLib.IMsRdpClientAdvancedSettings4));
                    TrySetAdvancedSettings(client.AdvancedSettings3, typeof(MSTSCLib.IMsRdpClientAdvancedSettings2));
                    TrySetAdvancedSettings(client.AdvancedSettings2, typeof(MSTSCLib.IMsRdpClientAdvancedSettings));
                    TrySetAdvancedSettings(client.AdvancedSettings, typeof(MSTSCLib.IMsTscAdvancedSettings));

                    if (AdvancedSettings == null)
                    {
                        throw new NotSupportedException();
                    }

                    transportSettingsProxy = new InterfaceProxy <ITransportSettings>();
                    transportSettingsProxy.TargetInstance = client.TransportSettings2;
                    transportSettingsProxy.TargetType     = typeof(IMsRdpClientTransportSettings2);
                    TransportSettings = transportSettingsProxy.GetStrongTypedProxy();

                    host        = (AxHost)client;
                    host.Width  = host.Parent.ClientRectangle.Width;
                    host.Height = host.Parent.ClientRectangle.Height;

                    host.Visible = false;

                    lastDetectedVersion = clientVersion;
                }
            }
            catch (COMException ex)
            {
                if (parent.Controls.Contains((Control)client))
                {
                    parent.Controls.Remove((Control)client);                     // yes, this is required!
                }
                host   = null;
                client = null;
                System.Diagnostics.Debug.WriteLine(clientVersion + " failed to initialize: " + ex.Message);
                lastDetectedVersion = RDPClientVersion.Unknown;
            }
        }
        private void InitializeRdpComponent()
        {
            TryVersion(MsRdpClientVersion.MsClient80, () => { internalMsClient = new MsRDPClient80(); });
            TryVersion(MsRdpClientVersion.MsClient70, () => { internalMsClient = new MsRDPClient70(); });
            TryVersion(MsRdpClientVersion.MsClient61, () => { internalMsClient = new MsRDPClient61(); });
            TryVersion(MsRdpClientVersion.MsClient60, () => { internalMsClient = new MsRDPClient60(); });
            TryVersion(MsRdpClientVersion.MsClient50, () => { internalMsClient = new MsRDPClient50(); });

            if (lastDetectedVersion == MsRdpClientVersion.Unknown)
            {
                throw new NotSupportedException("MsRrdpClient could not be instanciated");
            }

            System.Diagnostics.Debug.WriteLine("AxRDPClient version instanciated: " + lastDetectedVersion);

            AxHost component = (AxHost)internalMsClient;

            component.Dock = DockStyle.Fill;
            Controls.Add(component);
        }
        private void CreateClient()
        {
            try
            {
                TryVersion(RDPClientVersion.MsRDPClient80, () => { client = new MsRDPClient80(); });
                TryVersion(RDPClientVersion.MsRDPClient70, () => { client = new MsRDPClient70(); });
                TryVersion(RDPClientVersion.MsRDPClient61, () => { client = new MsRDPClient61(); });
                TryVersion(RDPClientVersion.MsRDPClient60, () => { client = new MsRDPClient60(); });
                TryVersion(RDPClientVersion.MsRDPClient50, () => { client = new MsRDPClient50(); });

                if (client == null || lastDetectedVersion == RDPClientVersion.Unknown)
                {
                    throw new NotSupportedException("MsRrdpClient could not be instanciated");
                }

                System.Diagnostics.Debug.WriteLine("AxRDPClient version instanciated: " + lastDetectedVersion);

                RegisterEvents();
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.ToString());
            }
        }
 public MsAdvancedSettings(IMsRDPClient client)
 {
     this.client = client;
 }