Skip to content

Commit

Permalink
Merge pull request #10 from haefele/patch-1
Browse files Browse the repository at this point in the history
Update RdpClientFactory to actually create a instance of the native control
  • Loading branch information
StefanKoell authored Dec 2, 2024
2 parents 7b3a583 + 0930881 commit ba79314
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions src/RoyalApps.Community.Rdp.WinForms/Controls/RdpClientFactory.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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<T>(out IRdpClient? rdpClient, out Exception? exception) where T : IRdpClient
private static bool Create<T>(out IRdpClient? rdpClient, out Exception? exception) where T : AxHostEx, IRdpClient
{
rdpClient = null;
exception = null;

try
{
rdpClient = Activator.CreateInstance<T>();
// Only calling the constructor isn't enough, as the native RDP control will be created at a later point
var client = Activator.CreateInstance<T>();

// 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)
Expand All @@ -53,4 +65,4 @@ private static bool Create<T>(out IRdpClient? rdpClient, out Exception? exceptio
return false;
}
}
}
}

0 comments on commit ba79314

Please sign in to comment.