From 09308813a3d38a8c745b4e9d15dc511aa38f6f77 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20H=C3=A4fele?= Date: Fri, 29 Nov 2024 22:46:28 +0100 Subject: [PATCH] Update RdpClientFactory to actually create a instance of the native control --- .../Controls/RdpClientFactory.cs | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/src/RoyalApps.Community.Rdp.WinForms/Controls/RdpClientFactory.cs b/src/RoyalApps.Community.Rdp.WinForms/Controls/RdpClientFactory.cs index 2224be5..863de7f 100644 --- a/src/RoyalApps.Community.Rdp.WinForms/Controls/RdpClientFactory.cs +++ b/src/RoyalApps.Community.Rdp.WinForms/Controls/RdpClientFactory.cs @@ -1,4 +1,8 @@ using System; +using System.Reflection; +using System.Runtime.InteropServices; +using System.Windows.Forms; +using AxMSTSCLib; using RoyalApps.Community.Rdp.WinForms.Interfaces; namespace RoyalApps.Community.Rdp.WinForms.Controls; @@ -37,14 +41,22 @@ public static IRdpClient Create(int rdpClientVersion = 0) throw new Exception("Failed to create RDP client instance.", exception); } - private static bool Create(out IRdpClient? rdpClient, out Exception? exception) where T : IRdpClient + private static bool Create(out IRdpClient? rdpClient, out Exception? exception) where T : AxHostEx, IRdpClient { rdpClient = null; exception = null; try { - rdpClient = Activator.CreateInstance(); + // Only calling the constructor isn't enough, as the native RDP control will be created at a later point + var client = Activator.CreateInstance(); + + // Read the clsid from the AxHost, and manually create an instance of the RDP control + var clsid = (Guid)typeof(AxHost).GetField("_clsid", BindingFlags.Instance | BindingFlags.NonPublic)!.GetValue(client)!; + var instance = client.RdpCreateInstance(clsid); + Marshal.ReleaseComObject(instance); + + rdpClient = client; return true; } catch (Exception ex) @@ -53,4 +65,4 @@ private static bool Create(out IRdpClient? rdpClient, out Exception? exceptio return false; } } -} \ No newline at end of file +}