Remote Desktop and High DPI

Feb 12, 2019  

Thanks to Chris K.’s blog post I finally found a nice solution to open an RDP connection to a remote host from my HiDPI laptop. Basically, the solution is to copy mstsc.exe and set its AppCompatFlag to make it DPI unaware:

1cd C:\Windows\System32
2copy mstsc.exe mstsc2.exe
3copy en-us\mstsc.exe.mui en-us\mstsc2.exe.mui
4reg add "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers" \
5  /t REG_SZ /v "C:\Windows\System32\mstsc2.exe" /d "~ DPIUNAWARE" /f

Interesting side-effects

When running the modified instance of the remote desktop client mstsc2.exe with one of my servers, I could no longer execute one of my .NET command line applications. It crashed mysteriously very early in its startup code.

After investigating, I found that setting the width of the console could indeed throw an exception:

1// Can throw ArgumentOutOfRangeException
2System.Console.SetWindowSize (width, System.Console.WindowHeight);

Hack, works only when using an English locale

In order to solve this issue, I added a hack in order to limit the width of the console to what the exception reports; in its error message, it says maximum window size of 160 (for instance). It’s a hack, but it works in my environment.

 1try
 2{
 3    System.Console.SetWindowSize (width, System.Console.WindowHeight);
 4}
 5catch (System.ArgumentOutOfRangeException ex)
 6{
 7    var find = "maximum window size of ";
 8    var message = ex.Message;
 9    var pos = message.IndexOf (find);
10
11    if (pos < 0) throw;
12
13    message = message.Substring (pos + find.Length);
14    pos = message.IndexOf (' ');
15
16    if (pos < 0) throw;
17
18    message = message.Substring (0, pos);
19
20    width = int.Parse (message, System.Globalization.CultureInfo.InvariantCulture);
21    System.Console.SetWindowSize (width, System.Console.WindowHeight);
22}