Skip to content

Commit

Permalink
fix MsRdpEx init code and add logging
Browse files Browse the repository at this point in the history
  • Loading branch information
StefanKoell committed Apr 9, 2024
1 parent fc72c4d commit cb52b3f
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -1,20 +1,12 @@
namespace RoyalApps.Community.Rdp.WinForms.Configuration;
using System;

namespace RoyalApps.Community.Rdp.WinForms.Configuration;

/// <summary>
/// The remote desktop client configuration.
/// </summary>
public class RdpClientConfiguration
{
/// <summary>
/// The client version to use. By default (value is 0) the highest available client will be used.
/// </summary>
public int ClientVersion { get; set; }

/// <summary>
/// If true, Microsoft's Remote Desktop Client is used (when installed) instead of the legacy MSTSC ActiveX control.
/// </summary>
public bool UseMsRdc { get; set; }

/// <summary>
/// Specifies the name of the server to which the current control is connected.
/// The new server name. This parameter can be a DNS name or IP address.
Expand All @@ -33,6 +25,11 @@ public class RdpClientConfiguration
/// </seealso>
public int Port { get; set; } = 3389;

/// <summary>
/// The client version to use. By default (value is 0) the highest available client will be used.
/// </summary>
public int ClientVersion { get; set; }

/// <summary>
/// Specifies the names of virtual channel client DLLs to be loaded. Virtual channel client DLLs are also referred to as Plug-in DLLs.
/// Comma-separated list of the names of the virtual channel client DLLs to be loaded. The DLL names must contain only alphanumeric characters.
Expand All @@ -41,6 +38,31 @@ public class RdpClientConfiguration
/// <cref>https://docs.microsoft.com/en-us/windows/win32/termserv/imstscadvancedsettings-plugindlls</cref>
/// </seealso>
public string? PluginDlls { get; set; }

/// <summary>
/// If true, Microsoft's Remote Desktop Client is used (when installed) instead of the legacy MSTSC ActiveX control.
/// </summary>
public bool UseMsRdc { get; set; }

/// <summary>
/// If set, the Microsoft Remote Desktop Client files (rdclientax.dll) will be searched here at first.
/// </summary>
public string? MsRdcPath { get; set; }

/// <summary>
/// If true, a detailed log file will be written to the file system (see: LogFilePath)
/// </summary>
public bool LogEnabled { get; set; }

/// <summary>
/// The following log levels are available: TRACE, DEBUG, INFO, WARN, ERROR, FATAL, OFF
/// </summary>
public string LogLevel { get; set; } = "TRACE";

/// <summary>
/// The file path to the log file when LogEnabled is set to true.
/// </summary>
public string LogFilePath { get; set; } = Environment.ExpandEnvironmentVariables(@"%TEMP%\MsRdpEx.log");

/// <summary>
/// The credential configuration used to log on to the remote desktop session.
Expand Down
32 changes: 22 additions & 10 deletions src/RoyalApps.Community.Rdp.WinForms/Controls/RdpControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@ public class RdpControl : UserControl
private Size _previousClientSize = Size.Empty;

private readonly Timer _timerResizeInProgress;
private readonly HashSet<string> _rdClientSearchPaths = new()
{
private readonly HashSet<string> _rdClientSearchPaths =
[
@"%ProgramFiles%\Remote Desktop",
@"%ProgramFiles(x86)%\Remote Desktop",
@"%ProgramFiles(ARM)%\Remote Desktop",
@"%LocalAppData%\Apps\Remote Desktop"
};
];

/// <summary>
/// Access to the RDP client and their events, methods and properties.
Expand Down Expand Up @@ -541,6 +541,10 @@ private void CreateRdpClient()
{
RdpClient = RdpClientFactory.Create(RdpConfiguration.ClientVersion);

Environment.SetEnvironmentVariable("MSRDPEX_LOG_ENABLED", RdpConfiguration.LogEnabled ? "1" : "0");
Environment.SetEnvironmentVariable("MSRDPEX_LOG_LEVEL", RdpConfiguration.LogLevel);
Environment.SetEnvironmentVariable("MSRDPEX_LOG_FILE_PATH", RdpConfiguration.LogFilePath);

var control = (Control) RdpClient;
control.Dock = DockStyle.Fill;
Controls.Add(control);
Expand All @@ -550,14 +554,21 @@ private void CreateRdpClient()
if (RdpConfiguration.UseMsRdc)
{
string? msRdcAxLibrary = null;
foreach (var path in _rdClientSearchPaths)

if (!string.IsNullOrWhiteSpace(RdpConfiguration.MsRdcPath) && File.Exists(RdpConfiguration.MsRdcPath))
msRdcAxLibrary = RdpConfiguration.MsRdcPath;

if (string.IsNullOrWhiteSpace(msRdcAxLibrary))
{
var searchPath = Path.Combine(Environment.ExpandEnvironmentVariables(path), "rdclientax.dll");
Logger.LogDebug("Searching file {SearchPath}", searchPath);
if (!Path.Exists(searchPath))
continue;
msRdcAxLibrary = searchPath;
break;
foreach (var path in _rdClientSearchPaths)
{
var searchPath = Path.Combine(Environment.ExpandEnvironmentVariables(path), "rdclientax.dll");
Logger.LogDebug("Searching file {SearchPath}", searchPath);
if (!Path.Exists(searchPath))
continue;
msRdcAxLibrary = searchPath;
break;
}
}

if (string.IsNullOrWhiteSpace(msRdcAxLibrary))
Expand All @@ -568,6 +579,7 @@ private void CreateRdpClient()
else
{
Environment.SetEnvironmentVariable("MSRDPEX_RDCLIENTAX_DLL", msRdcAxLibrary);
Environment.SetEnvironmentVariable("MSRDPEX_AXNAME", "msrdc");
Logger.LogInformation("Microsoft Remote Desktop Client will be used");

RdpClient.AxName = "msrdc";
Expand Down

0 comments on commit cb52b3f

Please sign in to comment.