Skip to main content

Windows Application to connect to Remote Desktop in the Network

Remote Desktop Services is one of Microsoft Windows components to access a remote computer through the network. Only the user interface of the application is presented at the client. Any input is redirected over to the remote computer over the network.

At work, we use Remote Desktop a great deal. It allows us to login to a remote server to perform health checks, deploy applications, troubleshoot problems, etc. We also use remote desktop often when we do WFH (work from home).
Why do you want to write a .NET application to do this when you have the MS Terminal Services client available from OS? Well, consider if you want to work on 3 different application servers at the same time and want to toggle between these 3 servers quite often. With the MSTSC, we will be running 3 different clients for the 3 servers and it is difficult to manage the working environment. In .NET, you can develop an application with tab control to load remote desktop sessions in different tabs in one window.

We will be using AxMSTSCLib an ActiveX component in our program to connect to the remote computer. It’s not that hard to build a remote desktop application in .NET. Microsoft has a “Microsoft RDP client control” ActiveX control that we will be using in our application.

Steps To be Followed:

We will start by creating a Windows application in the Visual Studio IDE.
Add a reference to “Microsoft Terminal Services Control Type Library” from the COM tab. This will addMSTSCLib.dll to the project.
Sample Image - maximum width is 600 pixels
To add MSTSC to the toolbox, right click the toolbox and select “Choose Items…”. Now add “Microsoft Terminal Services control from the COM tab.
Sample Image - maximum width is 600 pixels
Drag the newly added control from toolbox to the form.
Add 3 textbox and 2 button controls to the form:
Sample Image - maximum width is 600 pixels

Write the following lines of code on click event of Connect button:

rdp.Server = txtServer.Text;
rdp.UserName = txtUserName.Text;
IMsTscNonScriptable secured = (IMsTscNonScriptable)rdp.GetOcx();
secured.ClearTextPassword = txtPassword.Text;
rdp.Connect();

Now assign the properties (ServerUserName) of RDP control with the textbox values.
Here’s how easy it is to login to remote machine. However there is one catch, there is no direct method in RDP control through which you can pass the username and password to login to the remote desktop.
Due to security reasons, you have to implement an interface (IMsTscNonScriptable) to cast it separately.
IMsTscNonScriptable secured = IMsTscNonScriptable)rdp.GetOcx();
secured.ClearTextPassword = txtPassword.Text;

To disconnect from the remote desktop session, we just need to call the Disconnect() method.
Before disconnecting, we want to ensure that the connection is still available. We don't want to disconnect if it is already disconnected.
if (rdp.Connected.ToString() == "1")
 rdp.Disconnect();

i hope this small implementation will help you...

Regards,
Gajanan

Comments

Popular posts from this blog

Creating package in Oracle Database using Toad For Oracle

What are Packages in Oracle Database A package is  a group   of procedures, functions,  variables   and  SQL statements   created as a single unit. It is used to store together related objects. A package has two parts, Package  Specification  and Package Body.

Resolving 'Setup Account Privileges' error while installing SQL Server

A new installation of Microsoft SQL Server 2012 or Microsoft SQL Server 2008 R2 fails You see the following error message when you try to install a new instance of SQL Server 2012 or SQL Server 2008 R2: Rule "Setup account privileges" failed.

Creating Oracle stored Procedures using TOAD for Oracle

In a database management system, a  stored procedure  is a set of Structured Query Language (SQL) statements with an assigned name that's stored in the database in compiled form so that it can be shared by a number of programs. The use of  stored procedures  can be helpful in controlling  access to data, preserving  data integrity  and  improving  productivity.