Tuesday, November 03, 2009

Remote Desktop using C#.Net

Overview:
Remote Desktop Services is one of Microsoft Windows components to access a remote computer through 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 we 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.However, tab control is not in scope of this article. That gives me an idea for my next article :)

Microsoft Terminal Services Control

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.

This is how we do it

We will start by creating a windows application in the Visual Studio IDE.
Add reference to "Microsoft Terminal Services Control Type Library" from the COM tab. This will add MSTSCLib.dll to the project.





To add MSTSC to the toolbox, right click the toolbox and select "Choose Items…". Now add "Microsoft Terminal Services control from the COM tab.





Drag the newly added control from toolbox to the form.
Add 3 textbox and 2 button controls to the form.


Connect Button - Click Event:
Here is how we write the Connect button click event.

rdp.Server = txtServer.Text;
rdp.UserName = txtUserName.Text;

IMsTscNonScriptable secured = (IMsTscNonScriptable)rdp.GetOcx();
secured.ClearTextPassword = txtPassword.Text;
rdp.Connect();


Now assign the properties (Server, UserName) of RDP control with the textbox values.
Here's how easy 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;


Disconnect Button - Click Event:

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 (very clever huhJ)

if (rdp.Connected.ToString() == "1")
rdp.Disconnect();


Check the full source code here. Please leave your valuable comments and questions.
That's all folks! Happy Coding!

67 comments:

RRave said...

Hi,
I have read your blog, its cool and i think you are tamil.i would like to contact u. how can

tnx

夢想 said...

美國女孩做愛無碼影片美女老師做愛片下載美女與狗做愛美女做愛美女做愛片美女做愛貼圖美女做愛姿式美女做愛影片美媚做愛老婆做愛自拍這就叫做愛遊戲天堂做愛背著做愛胸部小做愛104色情小說123成人色情網18禁的色情遊戲a片色情a片色情網a片洪爺色情網a色情小說a色情影片av色情a片下載av色情dvdav免費色情片av免費色情影片av女優線上免費色情影片av女優色情影片av成人情色色情光碟圖庫有碼女優色情影片色情av女優介紹go2avaa色情免費看aa色情網站85論壇色情18禁色情網站18禁色情小說18禁地色情線上遊戲18禁地色情遊戲

BeeSpace said...

HI,
Please tell me, how you can use this for Smart Projects summary?
I need to do to connect to remote desktop to Pocket PC

糟糕啦 said...

來幫推 你個blog影d相真係好靚,係我至愛~ .........................

manojkumar (John) said...

I have downloaded this code and i tryed to run it.But i don't know what i have to give in Server Name:, and UserName:,Password: ?
What i have to give here?
I am using LAN.

Please respond me as soon as possible.It is very urgent to me.
If possible try to send C# code.

Advance Thanks.

manojkumar (John) said...

I have downloaded this code and i tryed to run it.But i don't know what i have to give in Server Name:, and UserName:,Password: ?
What i have to give here?
I am using LAN.

Please respond me as soon as possible.It is very urgent to me.
If possible try to send C# code.

Advance Thanks.

陽明山花季 said...

nice job! waiting for your new artical. ........................................

孝仁 said...

性愛巴士線上看sex性愛85cc性愛紅爺性愛本土美女性愛銀赫歐美av線上歐美av線上看歐美女自慰歐美成人女星歐美成人片女星歐美成人免費線上歐美成人情色歐美色情圖貼歐美免費成人電影歐美免費成人影片觀看歐美免費自拍歐美免費做愛片歐美免費情色影片模特兒平台標題樣?嫚雪兒免費小說影片avi影片a直播影片下?影片分享fuck影片成人片影片成人免費影片收尋成人小說 言情小說 情色自拍漫畫貼圖

惠蘋 said...

Gold will not buy everything...................................................

志冠少菁伶義 said...

He that travels far knows much.......................................................

ornit4u אורנית בן-דוד said...

I'm trying to achive exactly what you did in:
http://www.codeproject.com/KB/cs/RemoteDesktop_CSharpNET.aspx

After I have downloaded your code project I see he reference a AxMSTSCLib dll (which you have to create manually for your pc).


I have tried to create my own AxMSTSCLib dll following this link:
http://bytes.com/topic/c-sharp/answers/684780-axmstsclib-axmstscax-known-issue

The problem is that the dll differs from wahat actually needed (see my thread below)

As seen in the attached print-screen my dll doesn't contain an AxHost that is used to execute the command:


IMsTscNonScriptable secured = (IMsTscNonScriptable)rdp.GetOcx();

Any idea how to solve this?



photos attached:

http://drop.io/hidden/eopb4tphk8qekl/asset/bm8tZ2V0LW9jeC1qcGc%253D

http://drop.io/hidden/eopb4tphk8qekl/asset/Z2V0b2N4LTEtanBn

ornit4u אורנית בן-דוד said...
This comment has been removed by the author.
少菁 said...

I love readding, and thanks for your artical.........................................

惠IdellA_Fecteau1231蘋 said...

.有價值的東西,都要付出代價。- ..................................................

堯瑜 said...

逛到您的部落格讓我忍不住停下來!期待您的新文章!!........................................

雯俊 said...

好熱鬧喔 大家踴躍的留言 讓部落格更有活力........................................

JameyJ728_Beazley0 said...

志不立,天下無可成之事。.............................................

0612ConsuelaC_Hovey said...

A friend to everybody is a friend to nobody..............................................

桂竹桂竹 said...

向著星球長驅直進的人,反比踟躕在峽路上的人,更容易達到目的。......................................................

730A_ngelinaRabideau0 said...

廢話不多,祝你順心~^^ ........................................

Burma家銘ege said...

與人相處多微笑,個人獨處多沈思。 ............................................................

王周宏儒 said...

Constant dripping wears away the stone. 滴水穿石!加油!..................................................

胤珠胤珠 said...

It takes all kinds to make a world..................................................................

曉薇 said...

來幫推 你個blog影d相真係好靚,係我至愛~.................................................................

婷珊 said...

河水永遠是相同的,可是每一剎那又都是新的。......................................................................

江婷 said...

在莫非定律中有項笨蛋定律:「一個組織中的笨蛋,恆大於等於三分之二。」......................................................................

郁雯郁雯 said...

It takes all kinds to make a world.............................................................

Nanung said...

hi, this is great article. i'm really interested.
my question is, should we configure something on the server so that client can monitor it? can this application used for client-client monitoring (not always client-server monitoring)? i need to learn this remote desktop because now i have to develop an educational application which teacher computer can monitor student's computer. any idea? thank you very much.

王名仁 said...

向著星球長驅直進的人,反比踟躕在峽路上的人,更容易達到目的。............................................................

雲亨雲亨雲亨 said...

People throw stones only at trees with fruit on them.............................................................

子生子生 said...

這麼用心的經營你的文章, 當然值得我們留連拜訪的!............................................................

LupeConstan治士 said...

多謝美味的心靈雞湯......................................................................

茹希茹希 said...

喜歡看大家的文章,祝你順心~^^ ............................................................

寧年 said...

問聲好~~ 祝福您的blog愈來愈熱鬧!............................................................

潘凱花潘凱花 said...

這麼優的部落格,不踩一下不開心............................................................

怡潔怡潔 said...

一時的錯誤不算什麼,錯而不改才是一生中永遠且最大的錯誤............................................................

曾法幸 said...

愛情是一種發明,需要不斷改良。只是,這種發明和其他發明不一樣,它沒有專利權,隨時會被人搶走。............................................................

曹彥均曹彥均 said...

好的blog值得一推再推,要愈來愈好哦!!!!............................................................

小袁璇 said...

人生中最好的禮物就是屬於自己的一部份..................................................

國林維 said...

一棵樹除非在春天開了花,否則難望在秋天結果。..................................................

家唐銘 said...

量力而為,別勉強了,Cut your coat according to your cloth.............................................................

陳劉嘉韋蓉宣 said...

百發百中不是一試就成的。..................................................

偉曹琬 said...

Judge not of men and things at first sight................................................

偉曹琬 said...

河水永遠是相同的,可是每一剎那又都是新的。..................................................

法榮陳法榮陳法榮陳 said...

及時行樂-快樂不需要理由............................................................

麗王王珠 said...

I love readding, and thanks for your artical.............................................................

翊翊翊翊張瑜翊翊翊 said...

當最困難的時候,也就是離成功不遠的時候。..................................................

孫邦柔 said...

好看耶~一定每天有空給你支持..................................................

灏群 said...

你的部落格很棒,我期待更新喔............................................................

Vish said...

hey i got a bit confusion...what to do on client side for accessing it after i have filled the fields username password sever name etc. with the client's credentials.

Nazmul Hossain said...

I have downloaded this code and i tryed to run it.But i don't know what i have to give in Server Name:, and UserName:,Password: ?
What i have to give here?
I am using LAN.

Please respond me as soon as possible.It is very urgent to me.
If possible try to send C# code.

Nazmul Hossain said...

Can anyone tell me about Server Name, and UserName,Password of this code?plz help me.....

waqas said...

client ip contains the ip of the pc u want to connect with on LAN
user name must contain th comp name
and password contains the password of the comp. u want to access means the remote one

Self said...

i using this code but i can;t connect with remote desktop. and no any error msg nthing to dipsly plz sugeest me .. asap. its urgent for me..

javacafe said...

I am getting "connection cannot proceed authentication not enabled error while trying to connect to the server"
kindly advice
me

javacafe said...

Hi I am getting the below error
" the connection cannot proceed because authentication is not enabled "

kindly advice me

Daniel Dinu said...

Hi there , I need your help if you are kind. please tell me a way to contact you! thanks

משה ומוריה אברמוביץ said...

Hi I really need your help see http://stackoverflow.com/questions/10423779/casting-error-while-creating-rdp-connection

NarmadhaMano K said...

How to add audio files in a folder within my c# windows application.If i will create exe file ,it must run on another system.Please send me the coding.

SlavaNetDeveloper said...

good blog :) I also write articles about .NET developing

SlavaNetDeveloper said...

good blog :) I also write articles about .NET developing

User said...

rdp.Disconnect() only closes the remote window? But this is not correct logoff.
With logoff over MS RDP will data to be used for a new user, such as saved the Desktop directory on your hard drive!
How can I logoff with object rdp? The calling of the method Disconnect () is not enough!

User said...

rdp.Disconnect() only closes the remote window? But this is not correct logoff.
With logoff over MS RDP will data to be used for a new user, such as saved the Desktop directory on your hard drive!
How can I logoff with object rdp? The calling of the method Disconnect () is not enough!

User said...

rdp.Disconnect() only closes the remote window? But this is not correct logoff.
With logoff over MS RDP will data to be used for a new user, such as saved the Desktop directory on your hard drive!
How can I logoff with object rdp? The calling of the method Disconnect () is not enough!

User said...

Where is the method Logout()?

The Method Disconnect() is NOT Logout()!

Shraban Kumar Bhoi said...

what is servername,user name and password,pls explain which servername,which password and which username we have to provide

khalid sabtan said...

Dear Thiagarajan.A
your article is good but it did not reached it target ... we are the hangury programmer ,time is important to all of us would you kindley reveal the complete code
khalid sabtan
from saudi arabia