Issue with providing load balancing information using AxMSTSCLib

Lukas Peschel 10 Reputation points
2023-01-12T11:16:50.74+00:00

I am trying to establish a remote desktop connection via a remote desktop session broker in my C# application using the 'AxInterop.MSTSCLib' where I encounter problems with the transfer of the load balancing information.

Basically, I create a new object and set the basic parameters. A connection directly to a single Remote Desktop (RD) server is already possible with this:

var rd = new AxMsRdpClient11NotSafeForScripting();

rd.Server = "target-server.domain.local";
rd.UserName = "user";
var secured = (MSTSCLib.IMsTscNonScriptable)rd.GetOcx();
secured.ClearTextPassword = "password";

The RD environment I want to connect to consists of multiple RD services collections, each managing one or more RD Servers. For new RD connections, RD clients connect to an RD broker server, which redirects the clients according to the individual load balancing information they provide.

To provide this load balancing info, using mstsc.exe and an .rdp file I provide the setting 'loadbalanceinfo:s:tsv://MS Terminal Services Plugin.1.99999' where '99999' is the name of the targeted RD Collection which works fine. To implement this in MSTSCLib, I have to set it with 'rd.AdvancedSettings9.LoadBalanceInfo'.

However, as this post on the Microsoft Technet forum also describes, the load balancing info string must first be re-encoded.

var lbinfo = "loadbalanceinfo:s:tsv://MS Terminal Services Plugin.1.99999";

if (lbinfo.Length % 2 == 1) lbinfo += " ";
lbinfo += "\r\n";
var b = Encoding.UTF8.GetBytes(lbinfo);
var final = Encoding.Unicode.GetString(b);

rd.AdvancedSettings9.LoadBalanceInfo = final;

According to the last comment in the Technet post, the number of bytes must be even, so a space (U+0020) is added if nessesary.

With 'rd.Connect()' the connection is established and my client reaches the connection broker. In the first step my connection request appears in the logs of the broker server:

[TerminalServices-SessionBroker (Operational)]
The Remote Desktop Connection Broker has received a connection request for user {user}.
Notes in the RDP file (TSV URL) = 'tsv://MS Terminal Services Plugin.1.99999 ' //two empty spaces after the 99999 Original application = NULL
The call comes from the redirection server = broker.domain.local
The redirector is configured as a virtual machine redirector.

Note the two spaces at the end of the TSV URL that gets added by the used code because the bytes count would be uneven otherwise. The only difference to mstsc.exe is that there is only one space (which is the new line break we also add I guess) displayed.

In the next step the error occurs and our connection attempt fails. It says 'The farm specified for the connection does not exist.' The OnDisconnected event is triggered and provides me the information: 'DiscReason: 3' and 'ExtendedDisconnectReason: 1040'.

My guess: Because of the space we add, the name of the RD collection can't be assigned correctly anymore. If we compare the passed information (loadbalanceinfo:s:tsv://MS Terminal Services Plugin.1.99999) on byte level, we also see that this added space is the only difference:

mstsc.exe 

74 73 76 3a 2f 2f 4d 53 20 54 65 72 6d 69 6e 61 6c 20 53 65 72 76 69 63 65 73 20 50 6c 75 67 69 6e 2e 31 2e 39 39 39 39 39 0d 0a

AxMSTSCLib: 

74 73 76 3a 2f 2f 4d 53 20 54 65 72 6d 69 6e 61 6c 20 53 65 72 76 69 63 65 73 20 50 6c 75 67 69 6e 2e 31 2e 39 39 39 39 39 20 0d 0a //Note the third last byte, the U+0020, which we added to achieve an even byte count.

If I do not add the space and the number of bytes (43) is thus not even, I see in WireShark that the TPKT packet is not even sent anymore. The broker server, according to its logs, also takes no notice of the connection attempt. The OnDisconnected event provides me the information:: 'DiscReason: 4' and 'ExtendedDisconnectReason: exDiscReasonNoInfo'. So it seems that the AxMSTSCLib already assumes that the byte number is even.

Unfortunately, I can't figure out how mstsc.exe manages to successfully transmit the same odd string. On byte level I work supposedly identically.

I have uploaded the entire byte contents of the TPKT packages here for better comparison: https://pastebin.com/tLtfWHiP

I am grateful for any ideas.

Thanks and regards

Lukas P.

Remote Desktop
Remote Desktop
A Microsoft app that connects remotely to computers and to virtual apps and desktops.
4,612 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,040 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Torbjørn Svae 0 Reputation points
    2024-06-20T00:58:09.04+00:00

    I experienced the same issue, an extra space was added when I looked in eventviewer. I don't really know how to explain it:

    My original collection names were
    Desktop-2022-Prod with alias Desktop-2022-Pro (working)
    Desktop-2022-Prod-2 with alias Desktop-2022-Pro0 (failed as in the initial post here)

    My initial though was that the alias was too long, it seems that all of the collections I have is max 16 characters, except the one that was shortened but added a 0 at the end causing it to have 17 characters. I changed the alias to:
    Desktop-2022-2-P
    and now it works as normal. I don't know if the collection alias length has to be an even number or if its in my case that it was too long because of the 0 at the end, but it worked.

    If your alias is just 99999 then it can't be because it is too long, but the length of the alias is not an even number. I've yet to try this some more.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.