Click here to Skip to main content
15,867,704 members
Articles / Programming Languages / C++

Virtual Desktop: A Simple Desktop Management Tool

Rate me:
Please Sign up or sign in to vote.
4.83/5 (46 votes)
25 Jul 2008CPOL5 min read 229.3K   11.9K   143   69
This article gives you an overview of Windows Station, Windows Desktop and how to work with them. It also has a sample application (Virtual Desktop) demonstrating multiple desktop management.

VirtualDesktop/Virtual_Desktop.JPG

Introduction

A few months ago, one of my colleagues was using a tool that was managing multiple desktops. I was wondering how the tool could manage multiple desktops. I thought it would be just hiding-and-displaying the application windows for different desktops when a user switches between them. As a matter of curiosity, I started studying multiple desktops and ended up with this article.

As I went through the details, I found that I was wrong in thinking that the tool was just hiding-and-displaying the application windows for different desktops. Even the thought that the "Desktop" we see is just a directory was wrong. Let's see how it works.

Window Station and Window Desktop

Actually Windows uses Window Station and Desktop architecture to provide additional security. For the time being, we can refer to "Desktop" as a logical display surface, which contains User Interface Objects (GDI objects) and User Objects. A Window Station is a secure kernel object which contains a clipboard, an atom table, and one or more desktop objects. There exists a default window station "WinSta0", an interactive window station. "WinSta0" is the only interactive window station that can display a user interface or receive user input. All other window stations are non-interactive. By default, there exist three Desktops in an interactive window station : Default, WinLogon and ScreenSaver.

A Desktop is also a secure kernel object. When a new desktop is created, it is associated with the current window station of the calling process. There will always be only one desktop visible (Active) and ready to receive user inputs, provided the desktop is associated with an interactive window station i.e. WinSta0. This active desktop is called the Input Desktop.

As I specified, there exist three desktops in an interactive window station: Default, WinLogon and ScreenSaver. The screen prompting to press ALT + CTL + DEL to login is the WinLogon desktop. Whenever the user logs in, the Default desktop is created and made visible. This "Default" is the default active desktop for any logged in user. When you press ALT + CTL + DEL, you'll be switched back again to the WinLogon desktop. The ScreenSaver desktop will be made visible whenever the screen saver gets activated. As these desktop names are case-insensitive, there could be only one desktop of a specific name in a window station. But a desktop name can be repeated in different window stations. The purpose behind Window Station and Desktop objects is to provide top level isolation between processes.

About the Virtual Desktop (A Sample Application)

The Virtual Desktop Tool helps you to create and switch between different desktops. It even gives you an illusion of switching the application from one desktop to another. But this isn't true as any running application cannot be moved from one desktop to other. As I said, it's just an illusion. The application will be closed in the current desktop and will be launched on the specified desktop. This may cause loss of any unsaved data of the application and may not persist the state as it will be restarted.

The application is using hooks to display "Move To" menu options in the applications Control Panel (System Menu). For more details about the hooks, I would refer this article. I will give you an overview of different APIs used and classes built in the application. Rest of the code in the application is self explanatory and (what I think is) well documented.

Different APIs Used and Classes Built

GetProcessWindowStation

It retrieves the handle to the current Window Station of the calling process. The retrieved handle is used to enumerate all the desktops of the window station.

CreateDesktop

It creates a new Desktop with the specified name. This newly created desktop will be associated with the Window Station of the calling process and will be assigned to the calling thread.

OpenDesktop

It opens the specified Desktop. The handle returned by this function is used to switch between desktops.

CloseDesktop

It closes an open handle to Desktop.

SwitchDesktop

This makes the specified Desktop visible and ready to receive user inputs (i.e. makes it Active).

GetThreadDesktop

It retrieves the handle to the Desktop associated with the specified thread.

GetUserObjectInformation

It gives information about the Window Station and Desktop Objects.

The application has a class called CDesktopManager, in which all the desktop related activities are wrapped up. The application solution has a DLL Project, in which all the windows hook functionalities are wrapped up. This Event Hooker DLL is responsible for inserting menu options into the System Menu of the other applications.

Points of Interest

When we create a new desktop and switch to it, it shows nothing. The desktop will be blank(with just a wallpaper visible) -- that's because no process is running on that desktop. So, to avoid that, I launch an Explorer shell whenever I create a new desktop. I think I could add a feature to launch a new process/application on a specified desktop. And so I did in the current update. I have a few more features in mind. I'll keep updating whenever I complete them.

Revision History

  • 17 Nov 2007 : Initial release
  • 27 Dec 2007 : The application was exiting once it switches the desktop. It wasn't programmed to stay in memory any longer after desktop switch. But in this release the application launches a new instance of its own after desktop switch and then exits. This makes users feel that the application is always there for them in all desktops. This change has been made after reading one of the comments posted by a reader.
  • 1 Jun 2008 : The feature of launching application is been incorporated. Also few bugs I found have been fixed.
  • 25 July 2008 : Modified the code to encorporate following features:
    • Removed the bug of hooking my own application. :D
    • Added featue of shortcut-keys to easily navigate between the desktops.
    • Displaying the current desktop name in the application icon tooltip [so you don't get confused on which desktop you are !].
    • Tested on Vista also.

If you find a bug in the application or any feature to be embeded, please let me know, so that I can work on it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
India India
Hello All !
This is Mallinath S. Karkanti, from India. I'm working as a Software Developer in one of the Middle Scale Company... !

Comments and Discussions

 
QuestionTo create interactive window station under other than system account... Pin
masaniparesh29-Nov-07 3:02
masaniparesh29-Nov-07 3:02 
Hi,

Could you please tell me if it is possible to create interactive window station under other than system account?

My problem is i am creating interactive process from windows service which is running under domain account. So if i could create interactive window then only interactive process will get created.

Let me tell you something more that microsoft providing interactive service only under system account. So if you know is there any way to create interactive service under other than system account then it will really a helpful to me.

Thanks in advance,
Paresh
AnswerRe: To create interactive window station under other than system account... Pin
Malli_S30-Nov-07 3:24
Malli_S30-Nov-07 3:24 
GeneralRe: To create interactive window station under other than system account... Pin
masaniparesh30-Nov-07 20:30
masaniparesh30-Nov-07 20:30 
GeneralRe: To create interactive window station under other than system account... Pin
SNI30-Oct-08 23:16
SNI30-Oct-08 23:16 
GeneralRe: To create interactive window station under other than system account... Pin
masaniparesh31-Oct-08 0:54
masaniparesh31-Oct-08 0:54 
GeneralRe: To create interactive window station under other than system account... Pin
masaniparesh31-Oct-08 0:44
masaniparesh31-Oct-08 0:44 
GeneralRe: To create interactive window station under other than system account... Pin
Malli_S3-Nov-08 2:34
Malli_S3-Nov-08 2:34 
GeneralRe: To create interactive window station under other than system account... Pin
Member 1166861321-May-15 21:17
Member 1166861321-May-15 21:17 
GeneralRe: To create interactive window station under other than system account... Pin
Malli_S28-Jul-15 3:42
Malli_S28-Jul-15 3:42 
QuestionDoes it work on Vista? Pin
dan 1228-Nov-07 13:57
dan 1228-Nov-07 13:57 
AnswerRe: Does it work on Vista? Pin
Malli_S28-Nov-07 18:18
Malli_S28-Nov-07 18:18 
GeneralRe: Does it work on Vista? Pin
blphillips30-Nov-07 4:40
blphillips30-Nov-07 4:40 
GeneralRe: Does it work on Vista? Pin
Malli_S10-Jan-08 19:04
Malli_S10-Jan-08 19:04 
GeneralRe: Does it work on Vista? [modified] Pin
Barry Etter1-Feb-08 13:22
Barry Etter1-Feb-08 13:22 
GeneralRe: Does it work on Vista? Pin
Malli_S5-Feb-08 20:41
Malli_S5-Feb-08 20:41 
GeneralRe: Does it work on Vista? Pin
Michał Zalewski31-Jul-08 12:05
Michał Zalewski31-Jul-08 12:05 
GeneralRe: Does it work on Vista? Pin
Malli_S31-Jul-08 23:10
Malli_S31-Jul-08 23:10 
GeneralRe: Does it work on Vista? Pin
Michał Zalewski1-Aug-08 2:01
Michał Zalewski1-Aug-08 2:01 
GeneralRe: Does it work on Vista? Pin
blphillips25-Aug-08 8:20
blphillips25-Aug-08 8:20 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.