Click here to Skip to main content
12,633,623 members (32,094 online)
Click here to Skip to main content
Add your own
alternative version

Stats

26.8K views
1.2K downloads
50 bookmarked
Posted

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.

2.jpg

Introduction

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.

FunctionDescription
CloseDesktopCloses an open handle to a desktop object.
CreateDesktopCreates a new desktop, associates it with the current window station of the calling process, and assigns it to the calling thread.
CreateDesktopExCreates a new desktop, associates it with the current window station of the calling process, and assigns it to the calling thread.
EnumDesktopsEnumerates all desktops associated with the current window station of the calling process.
EnumDesktopWindowsEnumerates all top-level windows associated with the specified desktop.
GetThreadDesktopRetrieves a handle to the desktop assigned to the specified thread.
GetUserObjectInformationGets information about a window station or desktop object.
GetUserObjectSecurityGets security information for a window station or desktop object.
OpenDesktopOpens the specified desktop object.
OpenInputDesktopOpens the desktop that receives user input.
SetThreadDesktopAssigns the specified desktop to the calling thread.
SetUserObjectInformationSets information about a window station or desktop object.
SetUserObjectSecuritySets security information for a window station or desktop object.
SwitchDesktopMakes 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
SECURITY_ATTRIBUTES stSecurityAttr = {sizeof(SECURITY_ATTRIBUTES), 0, TRUE};
stSecurityAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
stSecurityAttr.bInheritHandle = TRUE;
HDESK hDesktop = ::CreateDesktop( lpszDesktopName
	, NULL
	, NULL
	, 0
	, GENERIC_ALL
	, &stSecurityAttr
	);
if( hDesktop == NULL )
{
	::MessageBox( m_hWnd, _T("Failed to create new desktop."), 
				_T("Error"), MB_ICONERROR | MB_OK);
	return;
}

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

// sleep for 3 seconds for the shell ready
Sleep(3000);

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

History

  • 5th March, 2009: Initial post

License

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

Share

About the Author

Jerry.Wang
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)qq.com
Chinese Blog: http://blog.csdn.net/wangjia184

You may also be interested in...

Pro
Pro

Comments and Discussions

 
QuestionIE automatic exit in win7 with uac on, what's wrong? Pin
ocean_sky25-Apr-12 21:39
memberocean_sky25-Apr-12 21:39 
QuestionDesktop Screenshots ? Pin
User Test22-Dec-09 17:15
memberUser Test22-Dec-09 17:15 
AnswerRe: Desktop Screenshots ? Pin
User Test22-Dec-09 17:16
memberUser Test22-Dec-09 17:16 
GeneralGood & simple project Pin
Member 13002109-Apr-09 22:43
memberMember 13002109-Apr-09 22:43 
GeneralSysinternals Pin
Vahid_Nasiri5-Mar-09 11:13
memberVahid_Nasiri5-Mar-09 11:13 
GeneralImpressive Pin
Pablo Robert5-Mar-09 3:59
memberPablo Robert5-Mar-09 3:59 
I really enjoyed it. Smile | :)

I was thinking about a nice feature I would appreciate to be implemented:

Sometimes I need to open a new desktop with a admin account.
The task of kill explorer, 'runas' command to open another instance with privileged account, then revert back to user explorer it's annoying. Just to access the documents and setting\administrator\ folders. I know I could just logoff and login, but in a station that cannot be turned off/logged off it's really a pain in the ***.

When logged in a user account, the application could show that screen that ask for user and password and impersonate using that information.

Anyway, thanks a lot for this great piece of software. My 5!!!

Best Regards
Big Grin | :-D

Pablo Robert
Programmer - Brazil

GeneralRe: Impressive Pin
Andromeda Shun9-Mar-09 22:20
memberAndromeda Shun9-Mar-09 22:20 
GeneralRe: Impressive Pin
Pablo Robert10-Mar-09 4:10
memberPablo Robert10-Mar-09 4:10 
GeneralRe: Impressive Pin
Andromeda Shun10-Mar-09 22:35
memberAndromeda Shun10-Mar-09 22: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.161208.2 | Last Updated 5 Mar 2009
Article Copyright 2009 by Jerry.Wang
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid