Click here to Skip to main content
12,396,461 members (31,804 online)
Click here to Skip to main content
Add your own
alternative version


50 bookmarked

Impersonate Linux's Multi-desktop Feature on Windows Platform

, 5 Mar 2009 CPOL
Rate this:
Please Sign up or sign in to vote.
This is a tiny tool to create multi virtual desktop, and allows you to switch between them.



The feature, multi desktops (workspace), is supported by KDE/GNOME on Linux, and it can also be found on Mac OSX.

This tiny tool can impersonate this feature on Windows platform.

Desktop Functions

There is a series of APIs to maintain desktops.

Function Description
CloseDesktop Closes an open handle to a desktop object.
CreateDesktop Creates a new desktop, associates it with the current window station of the calling process, and assigns it to the calling thread.
CreateDesktopEx Creates a new desktop, associates it with the current window station of the calling process, and assigns it to the calling thread.
EnumDesktops Enumerates all desktops associated with the current window station of the calling process.
EnumDesktopWindows Enumerates all top-level windows associated with the specified desktop.
GetThreadDesktop Retrieves a handle to the desktop assigned to the specified thread.
GetUserObjectInformation Gets information about a window station or desktop object.
GetUserObjectSecurity Gets security information for a window station or desktop object.
OpenDesktop Opens the specified desktop object.
OpenInputDesktop Opens the desktop that receives user input.
SetThreadDesktop Assigns the specified desktop to the calling thread.
SetUserObjectInformation Sets information about a window station or desktop object.
SetUserObjectSecurity Sets security information for a window station or desktop object.
SwitchDesktop Makes a desktop visible and activates it. This enables the desktop to receive input from the user.

How To Create a New Desktop 

The simple code below demonstrates how to create a new desktop.

// create the new desktop
stSecurityAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
stSecurityAttr.bInheritHandle = TRUE;
HDESK hDesktop = ::CreateDesktop( lpszDesktopName
	, 0
	, &stSecurityAttr
if( hDesktop == NULL )
	::MessageBox( m_hWnd, _T("Failed to create new desktop."), 
				_T("Error"), MB_ICONERROR | MB_OK);

// start the Windows Shell(explorer.exe) on the new created desktop
STARTUPINFO stStartInfo = {0};
stStartInfo.cb = sizeof(STARTUPINFO);
stStartInfo.lpDesktop = (LPTSTR)lpszDesktopName;
CString strCmdLine = _T("explorer.exe");
BOOL bRet = ::CreateProcess( NULL
	, strCmdLine.GetBuffer()
	, 0
	, &stStartInfo
	, &stProcInfo
if( !bRet )
	::MessageBox( m_hWnd, _T("Failed to launch the explorer.exe 
			in the new desktop."), _T("Error"), MB_ICONERROR | MB_OK);

// sleep for 3 seconds for the shell ready

// launch another instance on the new created shell
TCHAR tszBuffer[MAX_PATH] = {0};
GetModuleFileName( m_hInstance, tszBuffer, MAX_PATH);
bRet = ::CreateProcess( NULL
	, tszBuffer
	, 0
	, &stStartInfo
	, &stProcInfo


  • 5th March, 2009: Initial post


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


About the Author

Team Leader
China China
Jerry is from China. He was captivated by computer programming since 13 years old when first time played with Q-Basic.

  • Windows / Linux & C++
  • iOS & Obj-C
  • .Net & C#
  • Flex/Flash & ActionScript
  • HTML / CSS / Javascript
  • Gaming Server programming / video, audio processing / image & graphics

Contact: vcer(at)
Chinese Blog:

You may also be interested in...

Comments and Discussions

QuestionIE automatic exit in win7 with uac on, what's wrong? Pin
ocean_sky25-Apr-12 20:39
memberocean_sky25-Apr-12 20:39 
QuestionDesktop Screenshots ? Pin
User Test22-Dec-09 16:15
memberUser Test22-Dec-09 16:15 
AnswerRe: Desktop Screenshots ? Pin
User Test22-Dec-09 16:16
memberUser Test22-Dec-09 16:16 
GeneralGood & simple project Pin
Member 13002109-Apr-09 21:43
memberMember 13002109-Apr-09 21:43 
GeneralSysinternals Pin
Vahid_Nasiri5-Mar-09 10:13
memberVahid_Nasiri5-Mar-09 10:13 
GeneralImpressive Pin
Pablo Robert5-Mar-09 2:59
memberPablo Robert5-Mar-09 2:59 
GeneralRe: Impressive Pin
Andromeda Shun9-Mar-09 21:20
memberAndromeda Shun9-Mar-09 21:20 
GeneralRe: Impressive Pin
Pablo Robert10-Mar-09 3:10
memberPablo Robert10-Mar-09 3:10 
Thanks for the tip.

Looking at this script you told, I noticed he uses runas program. The same I mentioned in previous post.
The solution I'm looking for is some way to open an admin desktop to do some administrative tasks. It was really useful to have such an open source tool. And each time user wanted to switch to this desktop, asks for admin password.

I have downloaded your code and played with it. I was able to add that dialog which asks for user and password with CredUIPromptForCredentials. Figured out how to LogonUser. Tried to DuplicateTokenEx and ImpersonateLoggedOnUser to CreateProcessAsUser. But then I got 1314 error when trying to create explorer process.
Frown | :(
No privillege. Even if I launch the programa with admin account.

If I realize what is wrong I will surely let you know.

Best regards and thanks once again.
(I replied as you were the author. Apologize. Anyway, my message will be read by him the same way (I hope) Big Grin | :-D )

Pablo Robert
Programmer - Brazil

GeneralRe: Impressive Pin
Andromeda Shun10-Mar-09 21:35
memberAndromeda Shun10-Mar-09 21:35 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160721.1 | Last Updated 5 Mar 2009
Article Copyright 2009 by Jerry.Wang
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid