Welcome to MSDN Blogs Sign in | Join | Help

Writing a Desktop Sharing Application

 

Windows Desktop Sharing (WDS) API provides a rich set of API to share your complete desktop or just individual applications. Below is a step-by-step guide to write a sharing application. I have chosen C#/.Net to make things simpler and we will be using Visual Studio 2005 as the IDE.

Windows Desktop Sharing API Introduction:

Windows Desktop Sharing API allows a Windows Desktop Session to be shared across multiple viewers. More information on the API can be found on MSDN at http://msdn2.microsoft.com/en-us/library/aa373852.aspx. You could also refer to the Windows Desktop Sharing API Introduction blog. Windows Meeting Space & Remote Assistance use WDS API for collaboration and assistance scenarios. Similarly, you can write applications to achieve your sharing, collaboration, assistance, administration and deployment scenarios. 

Object Model of the API: API currently is published as an in-proc COM DLL (RdpEncom.dll) and is available in Vista. There are 2 primary objects that can be created:

  • 1. IRDPSRAPISharingSession - COM object that enables sharing of desktop
  • 2. IRDPSRAPIViewer - ActiveX control that can be embedded into a host window for viewing the sharing session.

Other objects can be created or queried from these 2 primary objects. There is also an event sink interface (IRDPSessionEvents) that the API would use to report events to the application. Applications should sink appropriate events as needed.

Desktop Sharing Application: Writing a desktop sharing application involves writing 2 different applications - one for sharing and the other for viewing.  These 2 applications can be written as one application working in different modes similar to Windows Meeting Space.

Sharer Application: You can choose to create either a console based application or windows based application. It is nice to have a GUI interface to control the viewers, start/stop sharing etc., so let's build a windows application. To create a sharer application you will need to first add a reference to RdpComApi 1.0 Type Library (rdpencom.dll in %windows%\system32 directory) to your project.

Starting the sharing Session: To start the sharer, first create an instance of RdpSession Class and then subscribe to the events published by the WDS API. For this you will need to declare an event handler of the corresponding delegate signature. Here is an example of declaring and defining an event handler:

private void OnAttendeeConnected(object pObjAttendee)

{

IRDPSRAPIAttendee pAttendee = pObjAttendee as IRDPSRAPIAttendee;

pAttendee.ControlLevel = CTRL_LEVEL.CTRL_LEVEL_VIEW;

LogTextBox.Text += ("Attendee Connected: " + pAttendee.RemoteName + Environment.NewLine);

}

 

Above is the event handler for the delegate:

void _IRDPSessionEvents_OnAttendeeConnectedEventHandler(object pAttendee);

 

This event is fired by the API when an attendee connects to the sharing session and will pass in the attendee instance to the event handler. Use that attendee instance to query information and also to set control level of the attendee. Control levels define the level of information that an attendee gets. In this implementation of event handler, attendee is given view control which means that attendee can only see the sharing session but cannot interact with it. We also print out some message to a text box so that the user who is running the sharing app can see the status of attendees connecting and disconnecting. Similarly we add event handlers for attendee disconnected and control level change requests.

After subscribing to the events that we are interested in, start the sharing session by calling Open() method on the RdpSession object. 

// Create a new RdpSession instance

m_pRdpSession = new RDPSession();

 

// Subscribe to events

m_pRdpSession.OnAttendeeConnected += new _IRDPSessionEvents_OnAttendeeConnectedEventHandler(OnAttendeeConnected);

m_pRdpSession.OnAttendeeDisconnected += new _IRDPSessionEvents_OnAttendeeDisconnectedEventHandler(OnAttendeeDisconnected);

m_pRdpSession.OnControlLevelChangeRequest += new _IRDPSessionEvents_OnControlLevelChangeRequestEventHandler(OnControlLevelChangeRequest);

 

// Start the Sharing Session

m_pRdpSession.Open();

LogTextBox.Text += "Presentation Started. Your Desktop is being shared." + Environment.NewLine;

 

Creating an Invitation: Create an invitation that can be sent to attendees so that they can use the invitation to connect to the sharing session. The invitation can be sent to the attendees using any mechanism desired. For example, Remote assistance allows to save the invitation and send it by email or to send through IM. Windows Meeting space uses "People Near Me" feature to identify and then send the invitations. In this implementation, let's take a simple approach and save it to a file. Here is the corresponding code:

// Create invitation.

IRDPSRAPIInvitation pInvitation = m_pRdpSession.Invitations.CreateInvitation("WinPresenter","PresentationGroup","",5);

string invitationString = pInvitation.ConnectionString;

// Save Connection String to File

WriteToFile(invitationString);

 

Stopping the Sharing Session: To stop the sharing session, call close method on the RdpSession class instance. Here is the code:

m_pRdpSession.Close();

LogTextBox.Text += "Presentation Stopped." + Environment.NewLine;

Marshal.ReleaseComObject(m_pRdpSession);

m_pRdpSession = null;

 

You may notice that I am calling ReleaseComObject. Reason being that once Close() is called then that RdpSession class instance cannot be used anymore. We will need to create another instance of RdpSession class to restart sharing.

Viewer Application: You will need to create a windows application and then add RdpViewer Class as an ActiveX control. Adding ActiveX controls to Windows Forms is a good resource to learn about adding ActiveX controls in Visual Studio IDE. Visual Studio will create an instance of AxRdpViewer and you can resize the ActiveX control to fit to your needs on the Windows Form. You can also choose to create this ActiveX control dynamically and place it in the host Form.

 

Connecting to the Sharing Session: First subscribe to the events that you are interested in. The easiest way in Visual Studio to add event handlers to an event is to click on the Events lightning bolt in the property browser of ActiveX control, then double-click on any events you wish to handle. An empty method signature and the appropriate event hooking and unhooking code are then emitted for you.  Subscribe to OnConnectionEstablished, OnConnectionTerminated, OnConnectionFailed and OnError events at a minimum so that you can report status and errors, if there are any. Here is some code for OnError event handler method:

private void OnError(object sender, _IRDPSessionEvents_OnErrorEvent e)

{

int ErrorCode = (int)e.errorInfo;

LogTextBox.Text += ("Error 0x" + ErrorCode.ToString("X") + Environment.NewLine);

}

To connect to the Sharing Session, you will need an invitation. Since we saved the invitation created in the Sharing Application as a file, let's just read the file into a string. Then call Connect method with the invitation file. Please note that we are passing empty string as password. Since we created the invitation on the sharing application with empty string as the password this would work. This is not advised if you are writing a commercial application because anyone can connect to your Sharing Session if they can get hold of the invitation.

Here is the code to connect to the Sharing Session:

string ConnectionString = ReadFromFile();

if (ConnectionString != null)

{

pRdpViewer.Connect(ConnectionString, "Viewer1", "");

}

Once the connection is succesfully established with sharing session, API will fire OnConnectionEstablished event or OnConnectionFailed event if the connection fails.

 

Disconnecting from the Sharing Session: To disconnect from the sharing session, call Disconnect method on the AxRdpViewer class instance. Here is the code:

pRdpViewer.Disconnect();

 

API will fire OnConnectionTerminated event once the viewer is disconnected from the sharing session.

Wrapping up:

This is all you need to do to share your desktop. Obviously you will need to do much more if you want to have control on who is connected, display a nice GUI for attendee information, kick attendees etc. This will give you a good start and explore the API provided by WDS API to achieve your other scenarios.

Sample Application:

WinPresenter is a sample Desktop Sharing Suite of applications encompassing both Sharing and Viewing application. You should be able to find source code including the snippets above. I left out error checking, printing messages to user etc., so please modify it to your needs.

Usage of Sample Applications:

Sharer Application: Run winsharer.exe. Invitation file will be saved to the current directory with file name inv.xml

Viewer application: Run winviewer.exe %path to invitation file%. Either specify the directory where you have run the winsharer or copy inv.xml to some other location and specify the path. If there is no path specified it will try to read inv.xml from the current directory.

Published Friday, March 23, 2007 8:37 PM by termserv
Attachment(s): WinPresenterFinal.zip

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# re: Writing a Desktop Sharing Application

Hi,

I tried your application sample. I am getting 6 desktops in the ActiveX control and thus am not able to control it effectively and I can see 6 desktops and 6 mouse cursors. I checked in my friend's machine too and I find the same problem - 6 desktops !

Do you have any idea what is happening ? I checked the code, the code seems to be ok.

Would be happy if you could help me.

Thanks,

C.C.Chakkaradeep

Tuesday, May 08, 2007 12:32 AM by Chakkaradeep

# re: Writing a Desktop Sharing Application

Are you running the winsharer.exe and winviewer.exe on the same machine? If yes, then you may see multiple desktops one inside the other recursively.

Wednesday, May 16, 2007 6:40 PM by Seenu

# re: Writing a Desktop Sharing Application

Initially we did that mistake of running the sharer and viewer in the same machine and later we did run separately and found actually what was happening :D

Its working perfect with two different machines.

Thanks !

Thursday, May 17, 2007 6:13 PM by C.C.Chakkaradeep

# re: Writing a Desktop Sharing Application

Hi,

What is the easiest way to make RDPViewer full screen other than using the size property

Thanks

Friday, May 25, 2007 1:33 AM by C.C.Chakkaradeep

# re: Writing a Desktop Sharing Application

Hi,

Is there a way to control the resolution of the shared desktop using RDPViewer ?

P.S

===

I am not able to locate any help other than this blog and MSDN and thus I am posting my queries here. I am working in Microsoft Imagine Cup Project where I use this Remote Desktop Sharing API. I would appreciate any help i get thru this blog..thanks :)

Monday, May 28, 2007 1:35 AM by C.C.Chakkaradeep

# re: Writing a Desktop Sharing Application

Chakkaradeep, you should be able to switch to full screen by pressing Ctrl+Alt+Break when the viewer control has focus. I dont have the app off hand to verify but this key combination should work. Press the same keys to switch to the original window mode. Also you can set SmartSizing property (IRDPSRAPIViewer) to True to see the full screen smart sized in the window.

Monday, May 28, 2007 3:22 AM by Seenu

# re: Writing a Desktop Sharing Application

There is no API to set the resolution of the sharing desktop from the viewer control. However, you can change the desktop resolution as you normally do from the RDPViewer itself by right clicking on the desktop and going to display properties. OnSharedDesktopSettingsChanged is fired to the viewer app. You can then adjust the viewer control window size, if needed.

Monday, May 28, 2007 3:30 AM by Seenu

# re: Writing a Desktop Sharing Application

Hi Seenu,

Thanks for the reply :), I will check with the Full Screen view

Thanks

Monday, May 28, 2007 5:38 AM by Chakkaradeep

# re: Writing a Desktop Sharing Application

I tried setting the property SmartSize as,

rdpViewer.SmartSizing=true, but I get exception caught there and a long list of exception message pasted here -- http://pastebin.ca/515749

Do i need to set this smartsizing property only after I connect to a desktop ?

Thanks

Monday, May 28, 2007 6:50 AM by Chakkaradeep

# re: Writing a Desktop Sharing Application

Yes you need to set the SmartSizing property after you connect in Vista. I couldn't find the actual error code in the pasted text so I am not sure if this was the reason it failed.

Tuesday, May 29, 2007 6:37 PM by srneerud

# re: Writing a Desktop Sharing Application

SmartSizing works good ;) , Yes , we need to enable it once we establish the connection. The full screen (Ctrl+Alt+Break) is not working, anyways, I will test again and would confirm whether its working or not..

Thursday, May 31, 2007 1:15 PM by Chakkaradeep

# re: Writing a Desktop Sharing Application

Hi,

I always get InvalidActiveXStateException if I try to use rdpViewer.Connect function in a constructor, but If I invoke Connect function by a Button Click, say Connect Button, it works. Is this a bug or is that how its meant to work ?

Friday, June 01, 2007 11:50 AM by C.C.Chakkaradeep

# re: Writing a Desktop Sharing Application

This is how it is meant to work. ActiveX control will not be fully initialized to be able to call Connect in constructor

Friday, June 01, 2007 11:46 PM by Seenu

# re: Writing a Desktop Sharing Application

Hi,

I am not able to take control of the desktop using the same way how WinPresenter is doing so.

The scenario is below,

1) I connect to Desktop

2) I set SmartSiziing Property to true

3) RequestControl(RDPCOMAPILib.CTRL_LEVEL.CTRL_LEVEL_INTERACTIVE)

I have put a MessageBox in the Server side, I do get the Messagebox popup when the ControlLevelChange request occurs and it also displays the Requested Level in MessageBox, but I dont get the control. Any idea why its happening?

Sunday, June 03, 2007 1:06 AM by C.C.Chakkaradeep

# re: Writing a Desktop Sharing Application

And yes, it was working earlier and suddenly stopped working :(

Sunday, June 03, 2007 1:07 AM by C.C.Chakkaradeep

# re: Writing a Desktop Sharing Application

Hi,

I tested several times,I am not able to take control of the desktop all the times. Out of 20 times I tried, I got only once full control and 5 times partial - mouse would moved a bit and then it stop, and rest failure.

Any idea or suggestions?

Monday, June 04, 2007 6:19 AM by C.C.Chakkaradeep

# re: Writing a Desktop Sharing Application

Hi,

Is it possible to work behind a firewall ? ..If I try to connect a computer into a network without true IP I need to create a NAT in router for it for each computer on network.

Is there a way where the "Sharer Application" connect to "Viewer application" like a "invert connection", so can "Sharer" can be behind a firewall.

Monday, June 04, 2007 6:34 AM by Alexnaldo Santos

# re: Writing a Desktop Sharing Application

Ok, Its weird, If I take Control of the desktop(by  clicking a button) and then click on the ActiveX control, minimize that window, and then restore,I get control of the remote desktop...we managed to show our demo using this trick...any idea why its happening like this?

Tuesday, June 05, 2007 12:57 AM by C.C.Chakkaradeep

# re: Writing a Desktop Sharing Application

Chakkaradeep, please make sure that you set the appropriate control level in the OnControlLevelChangeRequest event handler in sharer application. Also when the viewer actually gets the requested control, API will fire "OnAttendeeUpdate" event on both sharer and viewer. Once you get this event you can check if the viewer\attendee indeed has the requeted control.

Tuesday, June 05, 2007 1:32 AM by srneerud

# re: Writing a Desktop Sharing Application

Alexnaldo, yes you can reverse connect to viewer from the sharer. Please look at ConnectToClient API of IRDPSRAPISharingSession  and StartReverseConnectListener of IRDPSRAPIViewer interface.

Tuesday, June 05, 2007 1:35 AM by srneerud

# re: Writing a Desktop Sharing Application

I'm also having problems with setting the control level. I've modified the sample WinPresenter application to share only two single windows instead of the whole desktop. As Chakkaradeep I can only get interactive control maybe one out of 10-20 tries.

Tuesday, June 05, 2007 2:34 AM by paal_a

# re: Writing a Desktop Sharing Application

Thanks. I saw the correct API.

Now, Is it possible to record all RDP traffic ? We need to create a "image logger" to record the session and playback again, like a "session_xyz.avi".

As far as I know there is no publically available RDP documentation, so is very hard to interact with it.

Tuesday, June 05, 2007 8:43 AM by Alexnaldo Santos

# re: Writing a Desktop Sharing Application

Here is my Code - http://pastebin.ca/540462

I think I have everything. The problem is, if i Minimize or move the window from the current focus and then bring back the window (where this activex contol is) , i can take control of the desktop.

Tuesday, June 05, 2007 12:13 PM by C.C.Chakkaradeep

# re: Writing a Desktop Sharing Application

Alexnaldo, there is no API to record all RDP traffic. Please contact the Terminal Server newsgroups for RDP Documentation.

Tuesday, June 05, 2007 1:34 PM by srneerud

# re: Writing a Desktop Sharing Application

Chakkaradeep, I dont see OnAttendeeUpdate event being handled in the Viewer application. Until the viewer gets this event, viewer wont have the control requested for. In this handler you can check the control level of the viewer. From what you describe, it seems that ActiveX control doesnt seem to have focus until you minimize and maximize the application. It could be a bug in setting the focus to ActiveX control in your application.

Tuesday, June 05, 2007 1:51 PM by srneerud

# re: Writing a Desktop Sharing Application

Ok, I will look into it and reply :)

Wednesday, June 06, 2007 3:03 AM by Chakkaradeep

# re: Writing a Desktop Sharing Application

Is there an event to grab the Click event on the control ? Did i miss it or its not implemented ?

Saturday, June 09, 2007 7:40 PM by Chakkaradeep

# re: Writing a Desktop Sharing Application

Is possible to uses RDP libraries of Windows Vista in the Windows XP?

I need this features but in the Windows XP.

Monday, June 11, 2007 5:32 PM by Alexnaldo Santos

# re: Writing a Desktop Sharing Application

No there is no event to grab the Click event.

Tuesday, June 12, 2007 6:37 PM by srneerud

# re: Writing a Desktop Sharing Application

Alexnaldo, WDS Api components are not compatible with XP. This is a new feature in Windows Vista.

Tuesday, June 12, 2007 6:38 PM by srneerud

# re: Writing a Desktop Sharing Application

Hello...

I'm new to c# (I use to work with c++ but 10 years ago and I left it) so I'm a novice now.

I would like a little assistance on this.

I tried to run the app (WinPresenterFinal.zip) in windows xp but it gave me an error... (

basically... winxp doesn't have the dll (RdpEncom.dll), so I grab it from a windows vista installation and copied it to c:\windows\system32

I tried to register the dll but it gave me an error. (regsvr32 <file>)

can't register the file...

can't run the app..

another thing.. I'm using VS2005.

Thank you

Monday, June 25, 2007 8:23 PM by mams

# re: Writing a Desktop Sharing Application

mams, rdpencom.dll will not work in Windows XP. It is supported only in Windows Vista and up operating systems.

Wednesday, June 27, 2007 9:30 PM by srneerud

# re: Writing a Desktop Sharing Application

Hi all,

Thanks to the Microsoft Team who helped me here to build my application. I was building for New Zealand Imagine Cup and my team won second place in the New Zealand finals :)...thanks to all :)

Sunday, July 08, 2007 5:37 PM by Chakkaradeep

# re: Writing a Desktop Sharing Application

mams, you cant run this application in Windows XP and its meant only for Windows Vista. Please read all the comments to get familiar with this application.

Thanks

Saturday, July 21, 2007 6:24 AM by Chakkaradeep

# re: Writing a Desktop Sharing Application

Hi,

  I wanted to develop similar application for windows 2000, where I need functionalities like Remote Desktop Sharing (Entire desktop sharing)and selected Application sharing. Can u throw light to proceed further.

Thanks for the co-operation.

Thursday, September 13, 2007 6:54 AM by David

# re: Writing a Desktop Sharing Application

Hi

what are the apis that will be used for WDS in windows xp

Thursday, September 27, 2007 12:58 AM by Raj

# re: Writing a Desktop Sharing Application

WDS API is only available Vista & up. Its not available downlevel (Windows XP, Windows 2000 etc.,).

Thursday, September 27, 2007 2:27 PM by srneerud

# re: Writing a Desktop Sharing Application

Hi

when im sending date using virtual channels from sharer to viewer, viewer is receiving the data(onChannelReceiver event is firing),but when im sending data from Viewer to sharer, sharer is not receiving tha data (OnchannelReceive event of sharer is not firing)

Rajendra

Thursday, October 04, 2007 1:20 AM by Rajendra

# Is there way to create an remote assistance invitatation in XP ?

Is there way to create an remote assistance invitatation in XP ?

Friday, October 19, 2007 10:26 AM by Dioman

# re: Writing a Desktop Sharing Application

how can get this thing in winxp sp2

Tuesday, December 04, 2007 5:19 AM by ddd

# re: Writing a Desktop Sharing Application

Can anyone tell me how "teamviewer" has done this in comparison to the model described above.  That product is working for 2000/xp already.  

Friday, December 14, 2007 11:30 AM by dantetrevino

# App Sharing between XP and Vista machines ?

I have this application using App Sharing (RTC 1.3) in XP but it cannot run in Vista because Vista does not support RTC..I can update the App to use Desktop Sharing API but it won't run in XP from what I read here. Then I guess I need two versions, one with RTC for XP and another with Desktop Sharing API for Vista.

Then, How can do App Sharing between a XP and a Vista machine ?? I would appreciate your orientation guys !

Thanks,

Jaime.

Friday, December 14, 2007 11:46 AM by Jaime Santana

# re: Writing a Desktop Sharing Application

ddd, this API is not supported on XP.

Friday, December 14, 2007 10:47 PM by srneerud

# re: Writing a Desktop Sharing Application

Jaime, I dont think you can do interop between WDS API and RTC API.

Friday, December 14, 2007 10:51 PM by srneerud

# re: Writing a Desktop Sharing Application

Can anyone tell me what infrastructure one requires for creating Desktop Sharing Application in XP?

I have seen many querries on this but no replies. Please throw some light on this.

Monday, December 31, 2007 4:25 AM by Harry

# re: Writing a Desktop Sharing Application

Thanks a lot for such a nice information on the WDS.

How can i share particular application using this API.

Wednesday, January 02, 2008 12:19 AM by Harish

# re: Writing a Desktop Sharing Application

Harry, you can probably look at NetMeeting API or RTC API to acheive Desktop Sharing on XP.

Wednesday, January 02, 2008 12:54 AM by srneerud

# re: Writing a Desktop Sharing Application

Harish, look at IRDPSRAPIApplicationFilter interface. This link http://msdn2.microsoft.com/en-us/library/aa373267(VS.85).aspx should have details on this interface. To enable application sharing, you need to set "Enabled" property on this interface. Also available application and/or windows can be enumerated using the "Applications" and "Windows" API respectively.

Wednesday, January 02, 2008 12:59 AM by srneerud

# re: Writing a Desktop Sharing Application

srneerud,

If I can not interop between RDS API and RTC API. Do you know how can I have a XP and a Vista do App Sharing between them ??

Tuesday, January 22, 2008 2:00 PM by Jaime Santana

# re: Writing a Desktop Sharing Application

Hey guys - I am interested in a particular project which a customer has been asking me to investigate.

What i want to do is to develop a terminal server program which can be installed on windows XP.

Tha aim of this program would be allow me to connect to my windows XP machine from any RDP client application running on any OS, whilst supporting multiple users at one time.

Any guidelines on where i can start on this?

Saturday, March 01, 2008 1:44 AM by robrek

# re: Writing a Desktop Sharing Application

I am sometimes not able to control, it just beeps when i click on activex. Seeing other discussions, it was faced by Chakkaradeep also i think, its the same issue.

I have OnAttendeeUpdate handler both in viewer and sharer, viewer always fires, sharer never fires.

Any inputs?

Monday, April 14, 2008 10:29 AM by sam

# re: Writing a Desktop Sharing Application

hmm. setfocus() did the trick.

Monday, April 14, 2008 11:27 AM by sam

# re: Writing a Desktop Sharing Application

Is there any way to add click event.

I am displaying few sessions in a form (thumbnail / small size session-viewer) and I want to open a bigger window when user clicks on the thumbnail.

Wednesday, April 30, 2008 10:06 AM by amit

# re: Writing a Desktop Sharing Application

Can somebody give me help on virtual channels? I'm trying to transfer data from sharer to viewer via on of this channels. I have created channel on sharer's side, permitted access for viewer-attendee and implemented OnChannelDataReceived event for viewer. Sharer is sending data, but receive trigger on viewer side isn't firing.

Thursday, July 10, 2008 8:00 AM by LIX

Leave a Comment

(required) 
required 
(required) 
 
Page view tracker