Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 24 additions & 13 deletions win32ss/user/ntuser/desktop.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,18 @@ IntFreeDesktopHeap(IN PDESKTOP pdesk);
#ifdef _WIN64
DWORD gdwDesktopSectionSize = 20 * 1024; // 20 MB (Windows 7 style)
#else
DWORD gdwDesktopSectionSize = 3 * 1024; // 3 MB (Windows 2003 style)
DWORD gdwDesktopSectionSize = 3 * 1024; // 3 MB (Windows 2003 style)
#endif
DWORD gdwNOIOSectionSize = 128;
DWORD gdwWinlogonSectionSize = 128;

/* Currently active desktop */
PDESKTOP gpdeskInputDesktop = NULL;
PDESKTOP gpdeskInputDesktop = NULL; ///< Currently active desktop.
HDC ScreenDeviceContext = NULL;
PTHREADINFO gptiDesktopThread = NULL;
HCURSOR gDesktopCursor = NULL;
PKEVENT gpDesktopThreadStartedEvent = NULL;
PKEVENT gpDesktopSwitchEvent = NULL; ///< WinSta0_DesktopSwitch event.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there something depending on this name?
If not, then it might make sense to indicate that it is a legacy thing.

Copy link
Contributor Author

@HBelusca HBelusca Sep 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand your question.
But yes, "WinSta0_DesktopSwitch" is the expected name of the event created, so that apps can open it to wait on i, and this still exists in windows, so I don't see why it's a "legacy thing".
(See the links in the pr description.)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

gpDesktopSwitchEvent is the name I was referring to.

Copy link
Contributor Author

@HBelusca HBelusca Oct 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

gpDesktopSwitchEvent is the name I was referring to.

gpDesktopSwitchEvent is the object pointer variable name (like all the others above it), which is used in the code.
The comment next to it, indicates for the reader which exposed (in the NT object namespace) global named event this object pointer refers to; that is, "\BasedNamedObjects\WinSta0_DesktopSwitch".

HANDLE ghDesktopSwitchEvent = NULL; ///< WinSta0_DesktopSwitch handle in CSRSS process.

/* OBJECT CALLBACKS **********************************************************/

Expand Down Expand Up @@ -1246,8 +1247,8 @@ IntResolveDesktop(
* Validates the desktop handle.
*
* Remarks
* If the function succeeds, the handle remains referenced. If the
* fucntion fails, last error is set.
* If the function succeeds, the handle remains referenced.
* If the function fails, last error is set.
*/

NTSTATUS FASTCALL
Expand Down Expand Up @@ -2982,14 +2983,14 @@ NtUserSwitchDesktop(HDESK hdesk)
if (!NT_SUCCESS(Status))
{
ERR("Validation of desktop handle 0x%p failed\n", hdesk);
goto Exit; // Return FALSE
goto Exit;
}

if (PsGetCurrentProcessSessionId() != pdesk->rpwinstaParent->dwSessionId)
{
ObDereferenceObject(pdesk);
ERR("NtUserSwitchDesktop called for a desktop of a different session\n");
goto Exit; // Return FALSE
goto Exit;
}

if (pdesk == gpdeskInputDesktop)
Expand All @@ -3001,22 +3002,22 @@ NtUserSwitchDesktop(HDESK hdesk)
}

/*
* Don't allow applications switch the desktop if it's locked, unless the caller
* is the logon application itself
* Don't allow applications switch the desktop if it's locked,
* unless the caller is the logon application itself.
*/
if ((pdesk->rpwinstaParent->Flags & WSS_LOCKED) &&
gpidLogon != PsGetCurrentProcessId())
{
ObDereferenceObject(pdesk);
ERR("Switching desktop 0x%p denied because the window station is locked!\n", hdesk);
goto Exit; // Return FALSE
goto Exit;
}

if (pdesk->rpwinstaParent != InputWindowStation)
{
ObDereferenceObject(pdesk);
ERR("Switching desktop 0x%p denied because desktop doesn't belong to the interactive winsta!\n", hdesk);
goto Exit; // Return FALSE
goto Exit;
}

/* FIXME: Fail if the process is associated with a secured
Expand All @@ -3037,15 +3038,25 @@ NtUserSwitchDesktop(HDESK hdesk)
IntHideDesktop(gpdeskInputDesktop);
}

/* Set the active desktop in the desktop's window station. */
/* Set the active desktop in the desktop's window station */
InputWindowStation->ActiveDesktop = pdesk;

/* Set the global state. */
/* Set the global state */
gpdeskInputDesktop = pdesk;

/* Show the new desktop window */
co_IntShowDesktop(pdesk, UserGetSystemMetrics(SM_CXSCREEN), UserGetSystemMetrics(SM_CYSCREEN), bRedrawDesktop);

// TODO: Request hard-error popups to be re-spawn to the new desktop.

/* Notify waiters that a desktop has been switched on the
* interactive window station: pulse the legacy (NT 3.5+)
* event and send the desktop-switch notification (Vista+) */
KePulseEvent(gpDesktopSwitchEvent, EVENT_INCREMENT, FALSE);
#if (_WIN32_WINNT >= _WIN32_WINNT_VISTA)
IntNotifyWinEvent(EVENT_SYSTEM_DESKTOPSWITCH, NULL, OBJID_WINDOW, CHILDID_SELF, 0);
#endif

TRACE("SwitchDesktop gpdeskInputDesktop 0x%p\n", gpdeskInputDesktop);
ObDereferenceObject(pdesk);

Expand Down
2 changes: 2 additions & 0 deletions win32ss/user/ntuser/desktop.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ extern HDC ScreenDeviceContext;
extern PTHREADINFO gptiForeground;
extern PTHREADINFO gptiDesktopThread;
extern PKEVENT gpDesktopThreadStartedEvent;
extern PKEVENT gpDesktopSwitchEvent;
extern HANDLE ghDesktopSwitchEvent;

typedef struct _SHELL_HOOK_WINDOW
{
Expand Down
Loading
Loading