Thursday, May 22, 2014

Remote Desktop application with RDP protocol

        Skype, Team viewer and Lync are some of well-known desktop sharing applications that are widely using in novel world. This kind of applications enable users to login to remote computer and use it form any where.

         Most of these application developers provide API s that can be used to integrate those facilities in to our own applications.The problem is all the feature are inflexible due to the terms and conditions. 

      RDP, which is introduced by Microsoft can be used to create windows applications which can share desktop among users. It is more flexible and easy to use rather than other APIs .

RDP stand for the term of Remote desktop protocol .Remote Desktop Protocol is a property protocol developed by Microsoft, which provides a user with a graphical interface to connect to another computer over a network connection.
In order to configure RDP with the project it should have some additional references to both rdpcomplib and Axrdpcomplib. both these dll files enable you to use RDP in your program. 

** to import rdpcomplib   you need to go the "Add references" menu by right clicking the project. rdpcomplib is categorized under com libraries. so you can select the co and tick the rdpcomplib. in addition you need to search for windows terminal services library and tick it too.

** to import Axrdpcomplib you should use "RDP viewer class" in your Form. So you need to add it to the tools box by right click on the tool box. From the popup menu, you can select "Choose Items" and search for the  "RDPviewer class" which is categorized under Com items.

Following source code shows you how to write the server side of the application.       
     
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using RDPCOMAPILib;
using AxMSTSCLib;
using System.Runtime.InteropServices;

namespace TCP_to_RDP_Converter
{
    public partial class Form1 : Form
    {
        public static RDPSession currentSession = null;
        public static void createSession()
        {
            currentSession = new RDPSession();
        }

        public static void Connect(RDPSession session)
        {
            session.OnAttendeeConnected += Incoming;
            session.Open();
        }

        public static void Disconnect(RDPSession session)
        {
            session.Close();
        }

        public static string getConnectionString(RDPSession session, String authString, 
            string group, string password, int clientLimit)
        {
            IRDPSRAPIInvitation invitation =
                session.Invitations.CreateInvitation
                (authString, group, password, clientLimit);
                        return invitation.ConnectionString;
        }

        private static void Incoming(object Guest)
        {
            IRDPSRAPIAttendee MyGuest = (IRDPSRAPIAttendee)Guest;
            MyGuest.ControlLevel = CTRL_LEVEL.CTRL_LEVEL_INTERACTIVE;
        }

        /// <summary>
        /// Handle the form items
        /// </summary>
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            createSession();
            Connect(currentSession);
            textConnectionString.Text = getConnectionString(currentSession,
                "Test","Group","",5);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Disconnect(currentSession);
        }


    }
}
Following source code shows the client application that is using above server.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using RDPCOMAPILib;
using AxRDPCOMAPILib;

namespace Simple_RDP_Client
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        public static void Connect(string invitation, AxRDPViewer display, string userName, string password)
        {
            display.Connect(invitation, userName, password);
        }

        public static void disconnect(AxRDPViewer display)
        {
            display.Disconnect();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                Connect(textConnectionString.Text, this.axRDPViewer, "", "");
            }
            catch (Exception)
            {
                MessageBox.Show("Unable to connect to the Server");
            }
        }
    }
}

Source of the project can be downloaded from here