Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ C Windows Win7
Hi, there,
 
Any one could help me to have a look at this which acted as below:
Environment:
>>1. win7 32bits.
>>2. UAC ON
 
I prepared a little test program(called SwitchDesktop) which is do and only do switch default desktop to another desktop (e.g. mytest default desktop.)
 
Things seems very dramatically what happenned next.
1. UAC OFF, switched to another desktop, everything works fine and perfect nice.
2. UAC ON, switched to another desktop, everything works fine except explorer could not start up as normal, and even not as the default desktop, which works perfect well on winxp. and everything works perfect nice when I switched back to default desktop.
3. Right Clicked and choose run as administrator, everything works fine and ok.
 
well, I tried sysinternalsuits's desktops.exe, that works fine no matter UAC on or off, IE could start up as normally as the default desktop.
 
So, I wonder how does Desktops.exe realise switch desktop?
I checked security, SwitchDesktop performs the same with Desktops. I was thought that things may goes wrong frome the process of explorer.exe for that which is the parent process of iexplorer.exe. but I got confused after I did not start explorer.exe and switched desktop only with uac off and start iexplorer.exe with taskmgr.exe --> file -> run->iexplorer.exe

here's my code:
#include "stdafx.h"
#include "resource.h"

#include "ShellApi.h"

 
#include <string.h>
#include <stdio.h>
#include <string.h>

HINSTANCE hInst = NULL;
 
HDESK hDesktopCurrent;
HDESK hDesktopLlx;
HDESK hOriginalInput;
 
DWORD g_pid = 0;
LONG APIENTRY WndProc(
    HWND hWnd,
    UINT message,      // type of message
    WPARAM wParam,     // additional information
    LPARAM lParam)     // additional information
{
	int wmId, wmEvent;
	PAINTSTRUCT ps;
	HDC hdc;
 
	switch (message) 
	{
		case WM_COMMAND:
			wmId    = LOWORD(wParam); 
			wmEvent = HIWORD(wParam); 
			// Parse the menu selections:
			switch (wmId) {
				case IDM_ABOUT:
				//DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About);
				break;
				case IDM_EXIT:
				DestroyWindow(hWnd);
				break;
				default:
				return DefWindowProc(hWnd, message, wParam, lParam);
			}
			break;
		case WM_PAINT:
			hdc = BeginPaint(hWnd, &ps);
			// TODO: Add any drawing code here...
			RECT rt;
			GetClientRect(hWnd, &rt);
			//DrawText(hdc, szHello, strlen(szHello), &rt, DT_CENTER);
			EndPaint(hWnd, &ps);
			break;
			/*
			case WM_DESTROY:
			PostQuitMessage(0);
			break;
			//*/
		case WM_LBUTTONDOWN:
			break;
		case WM_HOTKEY:
			if(7777 == wParam)
			{
				PostQuitMessage(0);
			}
			else if(7778 == wParam)
			{
				SwitchDesktop(hOriginalInput);
			}
			else if(7779 == wParam)
			{
				SwitchDesktop(hOriginalInput);
			}
			break;
		case WM_QUIT:
		case WM_DESTROY:
			SwitchDesktop(hOriginalInput);
			return DefWindowProc(hWnd, message, wParam, lParam);
		default:
			return DefWindowProc(hWnd, message, wParam, lParam);
	}
	return 0;
}
 
void StartMyExplore(void)
{
	STARTUPINFO sui;         // Process startup info
	PROCESS_INFORMATION pi;  // info returned from CreateProcess
	SECURITY_ATTRIBUTES attr;
 
	ZeroMemory ((PVOID)&sui, sizeof(sui));
 
	sui.cb = sizeof (sui);
	sui.lpDesktop = _T("my_new_desktop_1");
 
	 attr.nLength = sizeof(attr);
	 attr.lpSecurityDescriptor = NULL;
	 attr.bInheritHandle = true;
 
	CreateProcess (NULL,   // image name
		"C:\\Windows\\Explorer.exe", // command line
		&attr,   // process security attributes
		NULL,   // thread security attributes
		true,   // inherit handles
		0,
		NULL,   // environment block
		NULL,   // current directory
		&sui,   // STARTUPINFO
		&pi);   // PROCESS_INFORMATION
}
 

int CALLBACK WinMain( HINSTANCE hInstance,
      HINSTANCE hPrevInstance,
      LPSTR lpCmdLine,
      int nCmdShow)
{
	WNDCLASS wc;
	SECURITY_ATTRIBUTES attr;
	wc.style = CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW;
	wc.lpfnWndProc = WndProc;
	wc.cbClsExtra = 0;
	wc.cbWndExtra = 0;
	wc.hInstance = hInstance;
	wc.hIcon = LoadIcon (NULL, "IDI_SETTHREADDESKTOP");
	wc.hCursor = LoadCursor (NULL, IDC_ARROW);
	wc.hbrBackground = (HBRUSH)GetStockObject (WHITE_BRUSH);
	wc.lpszMenuName = NULL;
	wc.lpszClassName = "newdesktopclass";
 
	if(!RegisterClass(&wc)) {
		return TRUE;
	}
 
	// save original ...
	hDesktopCurrent = NULL;
	hOriginalInput = NULL;
	hDesktopCurrent = GetThreadDesktop(GetCurrentThreadId());
	hOriginalInput = OpenInputDesktop(0, FALSE, DESKTOP_SWITCHDESKTOP);
 
	hDesktopLlx = NULL;
 
	attr.nLength = sizeof(attr);
	attr.lpSecurityDescriptor = NULL;
	attr.bInheritHandle = true;
	 
	hDesktopLlx = CreateDesktopEx(
				"my_new_desktop_1", 
				NULL,
				NULL,
				0,
				GENERIC_ALL,
				&attr,
				0,
				NULL);
	if(hDesktopLlx == NULL) {
		MessageBox(NULL, "CreateDesktopEx failed", NULL, MB_OK);
		return 0;
	}
 
	SwitchDesktop(hDesktopLlx);
	SetThreadDesktop(hDesktopLlx);
 

	HWND hWnd = NULL;
	hWnd = CreateWindow ("newdesktopclass",
		"hello, new window!",
		WS_OVERLAPPEDWINDOW,
		CW_USEDEFAULT, 
		0, 
		CW_USEDEFAULT, 
		0, 
		NULL, 
		NULL, 
		hInstance, 
		NULL
		);
	if(NULL == hWnd) {
		return TRUE;
	}
 
	//register hotkey for exit this desktop or switch to another desktop
	//ShowWindow(hWnd, SW_SHOW);
	//UpdateWindow(hWnd);
	if(!RegisterHotKey(hWnd, 7777, MOD_CONTROL, 'Q'))
	{//exit process
		return TRUE;
	}
 
	if(!RegisterHotKey(hWnd, 7778, MOD_CONTROL | MOD_SHIFT, 'Q'))
	{//switch to new desktop
		return TRUE;
	}
	if(!RegisterHotKey(hWnd, 7779, MOD_CONTROL | MOD_SHIFT, 'W'))
	{//switch to original desktop
		return TRUE;
	}
 
	StartMyExplore();
 
	MSG msg;
	while (GetMessage(&msg, NULL,  0, 0)) {
		TranslateMessage(&msg);// Translates virtual key codes
		DispatchMessage(&msg); // Dispatches message to window
	}
 
	SwitchDesktop(hOriginalInput);
	SetThreadDesktop(hDesktopCurrent);
 
	return TRUE;
}
 

Posted 23-Apr-12 15:50pm
Edited 24-Apr-12 14:39pm
v2

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



Advertise | Privacy | Mobile
Web02 | 2.8.141015.1 | Last Updated 24 Apr 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100