Click here to Skip to main content
13,353,124 members (63,565 online)
Click here to Skip to main content
Add your own
alternative version


23 bookmarked
Posted 19 Dec 2004

Terminal Services Listening Port Utility in Managed C++

, 19 Dec 2004
Rate this:
Please Sign up or sign in to vote.
Utility allowing convenient modification of a Terminal Servers listening port from 3389 to something else.


Illustration 1: Terminal Services Listening Port Utility GUI


Terminal Services and Remote Desktop both use the RDP protocol and a default port of 3389. Remote Desktop included in Windows XP is a watered down version of Terminal Services, and as such, it only allows for a single user session at a time. In contrast, Terminal Services available in Windows Server 2000 and Windows Server 2003 can be used in two distinctive modes:

  • Remote Administration - allowing up to two administrative users to login;
  • Application Server Mode - allows n amount of users to process applications on the server.

The Terminal Services Configuration and Management utilities provided by Microsoft as MMC snap-ins do not provide any TCP listening port options. The only way to change a Terminal Server’s default port from 3389 is via the Windows registry.

The Terminal Services Listening Port Utility hopes to showcase a simple managed C++ application which allows convenient modification of a Terminal Servers listening port on supported platforms.

Supported platforms

  • Windows 2000 Server
  • Windows 2000 Advanced Server
  • Windows XP Professional
  • Windows Server 2003

    [Windows XP Home only ships with the RDP Client and is not supported.]


Having multiple live Terminal Servers at my command, I started to search for means of making them less obvious over the networks. Changing the default Terminal Server port from 3389 to something else seemed like a good place to start. This, in effect, hides a Terminal Server and at least, in theory, should improve security. Sure, Terminal Servers can still be discovered over a network, but only if one was to probe each port physically for a TS response.

Code Outline

The Terminal Services Listening Port Utility retrieves the Terminal Server TCP port value for the default network interface from the registry. The user is presented with the ability to specify a new port value, and if the new value represents a valid port, then the user may store this new value into the Windows registry.

Registry Operations

Registry access is performed via the Microsoft::Win32::RegistryKey class which represents a key level node in the Windows registry. This class is a Registry Encapsulation and is straightforward to use.

// Obtain Terminal Services Port from registry
private: System::Void MainForm_Load(System::Object * sender, 
                                      System::EventArgs * e)
    // Highest port possible is 65534 which is 5 characters
    txtPortNumber->MaxLength = 5;

    regKey = Registry->LocalMachine->
                   "\\Terminal Server\\WinStations\\RDP-Tcp", 
        true );

    if(regKey != 0)    
        txtPortNumber->Text = 
        MessageBox::Show("Terminal Server not found in Registry");
        btnSet->Enabled = false;

// Write New Port to Registry
private: System::Void btnSet_Click(System::Object * sender, 
                                     System::EventArgs * e)
             "A Port must be >= 1 and <= 65534");
        errorProvider->SetError(txtPortNumber, "");

        if(regKey != 0)
            // store as REG_DWORD
            MessageBox::Show(String::Format("Port saved as {0}", 
              txtPortNumber->Text), "Success", 
              MessageBoxButtons::OK, MessageBoxIcon::Information);

Validation Routines

The Terminal Services Listening Port Utility implements the following two validation algorithms to restrict input and reduce user errors:

// determine whether the string qualifies as a port, 
// a valid port is an integer in the range of 1-65534 
private : bool IsValidPort(System::String * txtPort) 
    // we already know txtPort is numeric 
    // bit slow I suppose
    Int32 iPort = Int32::Parse(txtPort); 
    if (iPort < 1 || iPort > 65534) 
        return false; 
        return true; 
// determine whether the keystroke is an integer of the range 0-9 
// used in relation with the KeyPress event and Handled Property 
private : bool IsValidInt(System::String * ch) 
    String * format = "0123456789"; 

        return true; 
        return false; 

private: System::Void txtPortNumber_KeyPress(System::Object *  sender,
    System::Windows::Forms::KeyPressEventArgs *  e)
    // Stops the character from being entered
    // into the control since it is non-integer.
    if(!IsValidInt(e->KeyChar.ToString()) )
        e->Handled = true;

Client Configuration

Don’t forget to configure your Terminal Services client or Remote Desktop client to connect to the new port instead of the default port 3389. Firewalls or other NAT related appliances may have to be reconfigured for either pass through or port forwarding of any new ports.

For example, to instruct the Remote Desktop client utility to connect using the new port, simply append a colon followed by the non-standard port number to the computer address field.

Illustration 2: XP Remote Desktop Client using non standard port

Related Resources


  • 12/17/04: Original submission
  • 12/20/04: Updated resources


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Alexander Kent
Kentdome LLC
United States United States
Biography in progress Wink | ;-)

You may also be interested in...


Comments and Discussions

Questionsending data to an RDP session. Pin
Member 210890318-Aug-08 23:20
memberMember 210890318-Aug-08 23:20 
GeneralRemote Desktop Connection Pin
Pikesh4-Aug-05 0:01
memberPikesh4-Aug-05 0:01 
GeneralRemote Server Port Change Pin
andrewmatrwq20-Dec-04 0:43
memberandrewmatrwq20-Dec-04 0:43 
GeneralRe: Remote Server Port Change Pin
Alexander Kent31-Dec-04 18:07
memberAlexander Kent31-Dec-04 18:07 
QuestionRegedit? Pin
stano20-Dec-04 0:02
memberstano20-Dec-04 0:02 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.180111.1 | Last Updated 20 Dec 2004
Article Copyright 2004 by Alexander Kent
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid