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

26 comments:

  1. but how can client side get connection string from server ??

    ReplyDelete
    Replies
    1. Sines this is very basic application which only had purpose to give some sense to start with, i didn't implement it in here. To can do this sharing using many ways, including following two, that i prefer.
      1. (The easy way). share the generated connection string on a cloud location that can be accessed by the client application
      2. (The Hard way) . Use TCP to share the connection string. In this case client needs to send a TCP request to the TCP listener at the server side. if request is acceptable listener replies with the connection string

      I think this will make you some sense.

      Delete
    2. can u upload this project without using any Api's or libraries ?

      Delete
    3. What did u mean by that? Can u please elaborate..

      Delete
    4. you had uploaded this program by using Rdp libraries i'm asking that can you make this project without using this libraries ?

      Delete
    5. Rdp is some kind of a closed source technology that has copy right authority. Only way that we can use it is using these libraries.. So we cant implement it without those.. If you need to write open source remote desktop application use vnc instead. It is an alternative to RDP that uses UDP

      Delete
  2. What library (DLL) contains AxRDPCOMAPILib?

    ReplyDelete
  3. How the connectionstring is formed? I´m geting two results from diferents computers:
    1st not working:
    *E**A KH="hide=" ID="2"/**C**T ID="1" SID="hide"**L P="17223" N="hide"/**L P="17224" N="192.168.0.16"/**/T**/C**/E*

    2st working:
    *E**A KH="hide=" ID="2"/**C**T ID="1" SID="hide"**L P="36420" N="hide"/**L P="36421" N="192.168.1.36"/**L P="38513" N="189.12.11.133"/**/T**/C**/E*

    how can i force the invite return me the correct connectionstring?

    thanks for any help...

    ReplyDelete
  4. Hey .. I have connected a several monitors with my main PC ..
    The question is can it connect my main PC to attached monitors ???
    Or the connection string generated is same for all client monitors ???

    ReplyDelete
    Replies
    1. The connection will be to the computer, you can define the the area that will be shared, something like session.SetDesktopSharedRect(0, 0, System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width, System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height);

      Delete
  5. This comment has been removed by the author.

    ReplyDelete
  6. I´m triyng to change the colordepth but not sucessul yet, can you show some example explaing how to change this propertie?

    ReplyDelete
    Replies
    1. thank you man , I'll be very grateful, i already tryed that:
      public static void Connect()
      {
      currentSession.OnAttendeeConnected += Incoming;
      currentSession.SetDesktopSharedRect(0, 0, System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width, System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height);
      currentSession.colordepth = (int)8;
      currentSession.Open();
      }

      Delete
    2. but not worked yet. i´m trying other things, if i got ill let you know too.!!!1 :-)

      Delete
  7. Hello,

    Do it work like team viewer. I mean we can remote control one pc from other pc.

    Thanks in advance
    Ankita

    ReplyDelete
  8. You can add features as your wish.. Team Viewer don't use RDP.. But another protocol that can do the same... and also they have a advance method of NAT traversing technology that includes client server communication mechanism that uses clients as servers, which is tricky.

    ReplyDelete
  9. how can i change the connection string ?

    ReplyDelete
  10. hey Sandaruwan, can you make a exempla using reverse connection?

    ReplyDelete
    Replies
    1. Hi Ronald,

      Actually i'm really busy with my Software Engineering works.. Less time for do my research works.. Some how i'll try to make an example.

      Keep in touch

      Delete
  11. hey Sandaruwan, can you make a exempla using reverse connection?

    ReplyDelete
  12. hi , i need you help , when i open RDP Client project and open design mode of form , there i did not see the Server name textbox where use server address.

    ReplyDelete