2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS Win32k subsystem
4 * PURPOSE: Window stations
5 * FILE: win32ss/user/ntuser/winsta.c
6 * PROGRAMER: Casper S. Hornstrup (chorns@users.sourceforge.net)
7 * TODO: The process window station is created on
8 * the first USER32/GDI32 call not related
9 * to window station/desktop handling
13 DBG_DEFAULT_CHANNEL(UserWinsta
);
15 /* GLOBALS *******************************************************************/
17 /* Currently active window station */
18 PWINSTATION_OBJECT InputWindowStation
= NULL
;
20 /* Winlogon SAS window */
23 /* Full path to WindowStations directory */
24 UNICODE_STRING gustrWindowStationsDir
;
26 /* INITIALIZATION FUNCTIONS ****************************************************/
31 InitWindowStationImpl(VOID
)
33 GENERIC_MAPPING IntWindowStationMapping
= { WINSTA_READ
,
38 /* Set Winsta Object Attributes */
39 ExWindowStationObjectType
->TypeInfo
.DefaultNonPagedPoolCharge
= sizeof(WINSTATION_OBJECT
);
40 ExWindowStationObjectType
->TypeInfo
.GenericMapping
= IntWindowStationMapping
;
41 ExWindowStationObjectType
->TypeInfo
.ValidAccessMask
= WINSTA_ACCESS_ALL
;
43 return STATUS_SUCCESS
;
48 UserCreateWinstaDirectory(VOID
)
52 OBJECT_ATTRIBUTES ObjectAttributes
;
54 WCHAR wstrWindowStationsDir
[MAX_PATH
];
56 /* Create the WindowStations directory and cache its path for later use */
58 if(Peb
->SessionId
== 0)
60 if (!RtlCreateUnicodeString(&gustrWindowStationsDir
, WINSTA_OBJ_DIR
))
62 return STATUS_INSUFFICIENT_RESOURCES
;
67 Status
= RtlStringCbPrintfW(wstrWindowStationsDir
,
68 sizeof(wstrWindowStationsDir
),
73 if (!NT_SUCCESS(Status
))
76 if (!RtlCreateUnicodeString(&gustrWindowStationsDir
, wstrWindowStationsDir
))
78 return STATUS_INSUFFICIENT_RESOURCES
;
82 InitializeObjectAttributes(&ObjectAttributes
,
83 &gustrWindowStationsDir
,
87 Status
= ZwCreateDirectoryObject(&hWinstaDir
, DIRECTORY_CREATE_OBJECT
, &ObjectAttributes
);
88 if (!NT_SUCCESS(Status
))
90 ERR("Could not create %wZ directory (Status 0x%X)\n", &gustrWindowStationsDir
, Status
);
94 TRACE("Created directory %wZ for session %lu\n", &gustrWindowStationsDir
, Peb
->SessionId
);
99 /* OBJECT CALLBACKS **********************************************************/
103 IntWinStaObjectDelete(
104 _In_ PVOID Parameters
)
106 PWIN32_DELETEMETHOD_PARAMETERS DeleteParameters
= Parameters
;
107 PWINSTATION_OBJECT WinSta
= (PWINSTATION_OBJECT
)DeleteParameters
->Object
;
109 TRACE("Deleting window station (0x%p)\n", WinSta
);
111 WinSta
->Flags
|= WSS_DYING
;
113 UserEmptyClipboardData(WinSta
);
115 RtlDestroyAtomTable(WinSta
->AtomTable
);
117 RtlFreeUnicodeString(&WinSta
->Name
);
119 return STATUS_SUCCESS
;
124 IntWinStaObjectParse(
125 _In_ PVOID Parameters
)
127 PWIN32_PARSEMETHOD_PARAMETERS ParseParameters
= Parameters
;
128 PUNICODE_STRING RemainingName
= ParseParameters
->RemainingName
;
130 /* Assume we don't find anything */
131 *ParseParameters
->Object
= NULL
;
133 /* Check for an empty name */
134 if (!RemainingName
->Length
)
136 /* Make sure this is a window station, can't parse a desktop now */
137 if (ParseParameters
->ObjectType
!= ExWindowStationObjectType
)
140 return STATUS_OBJECT_TYPE_MISMATCH
;
143 /* Reference the window station and return */
144 ObReferenceObject(ParseParameters
->ParseObject
);
145 *ParseParameters
->Object
= ParseParameters
->ParseObject
;
146 return STATUS_SUCCESS
;
149 /* Check for leading slash */
150 if (RemainingName
->Buffer
[0] == OBJ_NAME_PATH_SEPARATOR
)
153 RemainingName
->Buffer
++;
154 RemainingName
->Length
-= sizeof(WCHAR
);
155 RemainingName
->MaximumLength
-= sizeof(WCHAR
);
158 /* Check if there is still a slash */
159 if (wcschr(RemainingName
->Buffer
, OBJ_NAME_PATH_SEPARATOR
))
161 /* In this case, fail */
162 return STATUS_OBJECT_PATH_INVALID
;
166 * Check if we are parsing a desktop.
168 if (ParseParameters
->ObjectType
== ExDesktopObjectType
)
170 /* Then call the desktop parse routine */
171 return IntDesktopObjectParse(ParseParameters
->ParseObject
,
172 ParseParameters
->ObjectType
,
173 ParseParameters
->AccessState
,
174 ParseParameters
->AccessMode
,
175 ParseParameters
->Attributes
,
176 ParseParameters
->CompleteName
,
178 ParseParameters
->Context
,
179 ParseParameters
->SecurityQos
,
180 ParseParameters
->Object
);
183 /* Should hopefully never get here */
184 return STATUS_OBJECT_TYPE_MISMATCH
;
190 _In_ PVOID Parameters
)
192 PWIN32_OKAYTOCLOSEMETHOD_PARAMETERS OkToCloseParameters
= Parameters
;
195 ppi
= PsGetCurrentProcessWin32Process();
197 if(ppi
&& (OkToCloseParameters
->Handle
== ppi
->hwinsta
))
199 return STATUS_ACCESS_DENIED
;
202 return STATUS_SUCCESS
;
205 /* PRIVATE FUNCTIONS **********************************************************/
208 * IntValidateWindowStationHandle
210 * Validates the window station handle.
213 * If the function succeeds, the handle remains referenced. If the
214 * fucntion fails, last error is set.
218 IntValidateWindowStationHandle(
219 HWINSTA WindowStation
,
220 KPROCESSOR_MODE AccessMode
,
221 ACCESS_MASK DesiredAccess
,
222 PWINSTATION_OBJECT
*Object
,
223 POBJECT_HANDLE_INFORMATION pObjectHandleInfo
)
227 if (WindowStation
== NULL
)
229 ERR("Invalid window station handle\n");
230 EngSetLastError(ERROR_INVALID_HANDLE
);
231 return STATUS_INVALID_HANDLE
;
234 Status
= ObReferenceObjectByHandle(WindowStation
,
236 ExWindowStationObjectType
,
241 if (!NT_SUCCESS(Status
))
242 SetLastNtError(Status
);
248 co_IntInitializeDesktopGraphics(VOID
)
251 UNICODE_STRING DriverName
= RTL_CONSTANT_STRING(L
"DISPLAY");
254 ScreenDeviceContext
= IntGdiCreateDC(&DriverName
, NULL
, NULL
, NULL
, FALSE
);
255 if (NULL
== ScreenDeviceContext
)
257 IntDestroyPrimarySurface();
260 GreSetDCOwner(ScreenDeviceContext
, GDI_OBJ_HMGR_PUBLIC
);
262 if (! IntCreatePrimarySurface())
267 hSystemBM
= NtGdiCreateCompatibleDC(ScreenDeviceContext
);
269 NtGdiSelectFont(hSystemBM
, NtGdiGetStockObject(SYSTEM_FONT
));
270 GreSetDCOwner(hSystemBM
, GDI_OBJ_HMGR_PUBLIC
);
272 /* Update the SERVERINFO */
273 gpsi
->aiSysMet
[SM_CXSCREEN
] = gppdevPrimary
->gdiinfo
.ulHorzRes
;
274 gpsi
->aiSysMet
[SM_CYSCREEN
] = gppdevPrimary
->gdiinfo
.ulVertRes
;
275 gpsi
->Planes
= NtGdiGetDeviceCaps(ScreenDeviceContext
, PLANES
);
276 gpsi
->BitsPixel
= NtGdiGetDeviceCaps(ScreenDeviceContext
, BITSPIXEL
);
277 gpsi
->BitCount
= gpsi
->Planes
* gpsi
->BitsPixel
;
278 gpsi
->dmLogPixels
= NtGdiGetDeviceCaps(ScreenDeviceContext
, LOGPIXELSY
);
279 if (NtGdiGetDeviceCaps(ScreenDeviceContext
, RASTERCAPS
) & RC_PALETTE
)
281 gpsi
->PUSIFlags
|= PUSIF_PALETTEDISPLAY
;
284 gpsi
->PUSIFlags
&= ~PUSIF_PALETTEDISPLAY
;
285 // Font is realized and this dc was previously set to internal DC_ATTR.
286 gpsi
->cxSysFontChar
= IntGetCharDimensions(hSystemBM
, &tmw
, (DWORD
*)&gpsi
->cySysFontChar
);
287 gpsi
->tmSysFont
= tmw
;
289 /* Put the pointer in the center of the screen */
290 gpsi
->ptCursor
.x
= gpsi
->aiSysMet
[SM_CXSCREEN
] / 2;
291 gpsi
->ptCursor
.y
= gpsi
->aiSysMet
[SM_CYSCREEN
] / 2;
294 UserAttachMonitor((HDEV
)gppdevPrimary
);
296 /* Setup the cursor */
297 co_IntLoadDefaultCursors();
299 /* Setup the icons */
305 /* Show the desktop */
306 pdesk
= IntGetActiveDesktop();
308 co_IntShowDesktop(pdesk
, gpsi
->aiSysMet
[SM_CXSCREEN
], gpsi
->aiSysMet
[SM_CYSCREEN
], TRUE
);
314 IntEndDesktopGraphics(VOID
)
316 if (NULL
!= ScreenDeviceContext
)
317 { // No need to allocate a new dcattr.
318 GreSetDCOwner(ScreenDeviceContext
, GDI_OBJ_HMGR_POWNED
);
319 GreDeleteObject(ScreenDeviceContext
);
320 ScreenDeviceContext
= NULL
;
322 IntHideDesktop(IntGetActiveDesktop());
323 IntDestroyPrimarySurface();
329 return ScreenDeviceContext
;
333 CheckWinstaAttributeAccess(ACCESS_MASK DesiredAccess
)
335 PPROCESSINFO ppi
= PsGetCurrentProcessWin32Process();
336 if ( gpidLogon
!= PsGetCurrentProcessId() )
338 if (!(ppi
->W32PF_flags
& W32PF_IOWINSTA
))
340 ERR("Requires Interactive Window Station\n");
341 EngSetLastError(ERROR_REQUIRES_INTERACTIVE_WINDOWSTATION
);
344 if (!RtlAreAllAccessesGranted(ppi
->amwinsta
, DesiredAccess
))
346 ERR("Access Denied\n");
347 EngSetLastError(ERROR_ACCESS_DENIED
);
355 /* PUBLIC FUNCTIONS ***********************************************************/
358 * NtUserCreateWindowStation
360 * Creates a new window station.
363 * lpszWindowStationName
364 * Pointer to a null-terminated string specifying the name of the
365 * window station to be created. Window station names are
366 * case-insensitive and cannot contain backslash characters (\).
367 * Only members of the Administrators group are allowed to specify a
371 * Requested type of access
374 * Security descriptor
376 * Unknown3, Unknown4, Unknown5, Unknown6
380 * If the function succeeds, the return value is a handle to the newly
381 * created window station. If the specified window station already
382 * exists, the function succeeds and returns a handle to the existing
383 * window station. If the function fails, the return value is NULL.
390 NtUserCreateWindowStation(
391 POBJECT_ATTRIBUTES ObjectAttributes
,
392 ACCESS_MASK dwDesiredAccess
,
399 UNICODE_STRING WindowStationName
;
400 PWINSTATION_OBJECT WindowStationObject
;
401 HWINSTA WindowStation
;
404 TRACE("NtUserCreateWindowStation called\n");
406 Status
= ObOpenObjectByName(ObjectAttributes
,
407 ExWindowStationObjectType
,
412 (PVOID
*)&WindowStation
);
414 if (NT_SUCCESS(Status
))
416 TRACE("NtUserCreateWindowStation opened window station %wZ\n", ObjectAttributes
->ObjectName
);
417 return (HWINSTA
)WindowStation
;
421 * No existing window station found, try to create new one
424 /* Capture window station name */
427 ProbeForRead( ObjectAttributes
, sizeof(OBJECT_ATTRIBUTES
), 1);
428 Status
= IntSafeCopyUnicodeStringTerminateNULL(&WindowStationName
, ObjectAttributes
->ObjectName
);
430 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
432 Status
=_SEH2_GetExceptionCode();
436 if (! NT_SUCCESS(Status
))
438 ERR("Failed reading capturing window station name\n");
439 SetLastNtError(Status
);
443 /* Create the window station object */
444 Status
= ObCreateObject(UserMode
,
445 ExWindowStationObjectType
,
449 sizeof(WINSTATION_OBJECT
),
452 (PVOID
*)&WindowStationObject
);
454 if (!NT_SUCCESS(Status
))
456 ERR("ObCreateObject failed with %lx for window station %wZ\n", Status
, &WindowStationName
);
457 ExFreePoolWithTag(WindowStationName
.Buffer
, TAG_STRING
);
458 SetLastNtError(STATUS_INSUFFICIENT_RESOURCES
);
462 /* Initialize the window station */
463 RtlZeroMemory(WindowStationObject
, sizeof(WINSTATION_OBJECT
));
465 InitializeListHead(&WindowStationObject
->DesktopListHead
);
466 WindowStationObject
->Name
= WindowStationName
;
467 WindowStationObject
->dwSessionId
= NtCurrentPeb()->SessionId
;
468 Status
= RtlCreateAtomTable(37, &WindowStationObject
->AtomTable
);
469 if (!NT_SUCCESS(Status
))
471 ERR("RtlCreateAtomTable failed with %lx for window station %wZ\n", Status
, &WindowStationName
);
472 ObDereferenceObject(WindowStationObject
);
473 SetLastNtError(STATUS_INSUFFICIENT_RESOURCES
);
477 Status
= ObInsertObject((PVOID
)WindowStationObject
,
482 (PVOID
*)&WindowStation
);
484 if (!NT_SUCCESS(Status
))
486 ERR("ObInsertObject failed with %lx for window station\n", Status
);
487 SetLastNtError(STATUS_INSUFFICIENT_RESOURCES
);
491 if (InputWindowStation
== NULL
)
493 ERR("Initializing input window station\n");
494 InputWindowStation
= WindowStationObject
;
496 WindowStationObject
->Flags
&= ~WSS_NOIO
;
502 WindowStationObject
->Flags
|= WSS_NOIO
;
505 TRACE("NtUserCreateWindowStation created object %p with name %wZ handle %p\n",
506 WindowStation
, &WindowStationObject
->Name
, WindowStation
);
507 return WindowStation
;
511 * NtUserOpenWindowStation
513 * Opens an existing window station.
516 * lpszWindowStationName
517 * Name of the existing window station.
520 * Requested type of access.
523 * If the function succeeds, the return value is the handle to the
524 * specified window station. If the function fails, the return value
528 * The returned handle can be closed with NtUserCloseWindowStation.
535 NtUserOpenWindowStation(
536 POBJECT_ATTRIBUTES ObjectAttributes
,
537 ACCESS_MASK dwDesiredAccess
)
542 Status
= ObOpenObjectByName(ObjectAttributes
,
543 ExWindowStationObjectType
,
550 if (!NT_SUCCESS(Status
))
552 ERR("NtUserOpenWindowStation failed\n");
553 SetLastNtError(Status
);
557 TRACE("Opened window station %wZ with handle %p\n", ObjectAttributes
->ObjectName
, hwinsta
);
563 * NtUserCloseWindowStation
565 * Closes a window station handle.
569 * Handle to the window station.
575 * The window station handle can be created with NtUserCreateWindowStation
576 * or NtUserOpenWindowStation. Attemps to close a handle to the window
577 * station assigned to the calling process will fail.
585 NtUserCloseWindowStation(
588 PWINSTATION_OBJECT Object
;
591 TRACE("NtUserCloseWindowStation called (%p)\n", hWinSta
);
593 if (hWinSta
== UserGetProcessWindowStation())
595 ERR("Attempted to close process window station\n");
599 Status
= IntValidateWindowStationHandle(hWinSta
,
605 if (!NT_SUCCESS(Status
))
607 ERR("Validation of window station handle (%p) failed\n", hWinSta
);
611 ObDereferenceObject(Object
);
613 TRACE("Closing window station handle (%p)\n", hWinSta
);
615 Status
= ObCloseHandle(hWinSta
, UserMode
);
616 if (!NT_SUCCESS(Status
))
618 SetLastNtError(Status
);
626 * NtUserGetObjectInformation
628 * The NtUserGetObjectInformation function retrieves information about a
629 * window station or desktop object.
633 * Handle to the window station or desktop object for which to
634 * return information. This can be a handle of type HDESK or HWINSTA
635 * (for example, a handle returned by NtUserCreateWindowStation,
636 * NtUserOpenWindowStation, NtUserCreateDesktop, or NtUserOpenDesktop).
639 * Specifies the object information to be retrieved.
642 * Pointer to a buffer to receive the object information.
645 * Specifies the size, in bytes, of the buffer pointed to by the
649 * Pointer to a variable receiving the number of bytes required to
650 * store the requested information. If this variable's value is
651 * greater than the value of the nLength parameter when the function
652 * returns, the function returns FALSE, and none of the information
653 * is copied to the pvInfo buffer. If the value of the variable pointed
654 * to by lpnLengthNeeded is less than or equal to the value of nLength,
655 * the entire information block is copied.
658 * If the function succeeds, the return value is nonzero. If the function
659 * fails, the return value is zero.
666 NtUserGetObjectInformation(
671 PDWORD nLengthNeeded
)
674 PWINSTATION_OBJECT WinStaObject
= NULL
;
675 PDESKTOP DesktopObject
= NULL
;
676 USEROBJECTFLAGS ObjectFlags
;
678 SIZE_T nDataSize
= 0;
683 ProbeForWrite(nLengthNeeded
, sizeof(*nLengthNeeded
), 1);
684 ProbeForWrite(pvInformation
, nLength
, 1);
686 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
688 SetLastNtError(_SEH2_GetExceptionCode());
693 /* Try window station */
694 TRACE("Trying to open window station %p\n", hObject
);
695 Status
= ObReferenceObjectByHandle(hObject
,
697 ExWindowStationObjectType
,
699 (PVOID
*)&WinStaObject
,
702 if (Status
== STATUS_OBJECT_TYPE_MISMATCH
)
705 TRACE("Trying to open desktop %p\n", hObject
);
707 Status
= IntValidateDesktopHandle(hObject
,
713 if (!NT_SUCCESS(Status
))
715 ERR("Failed: 0x%x\n", Status
);
719 TRACE("WinSta or Desktop opened!\n");
726 OBJECT_HANDLE_ATTRIBUTE_INFORMATION HandleInfo
;
729 ObjectFlags
.fReserved
= FALSE
;
731 /* Check whether this handle is inheritable */
732 Status
= ZwQueryObject(hObject
,
733 ObjectHandleFlagInformation
,
735 sizeof(OBJECT_HANDLE_ATTRIBUTE_INFORMATION
),
737 if (!NT_SUCCESS(Status
))
739 ERR("ZwQueryObject failed, Status 0x%08lx\n", Status
);
742 ObjectFlags
.fInherit
= HandleInfo
.Inherit
;
744 ObjectFlags
.dwFlags
= 0;
745 if (WinStaObject
!= NULL
)
747 if (!(WinStaObject
->Flags
& WSS_NOIO
))
748 ObjectFlags
.dwFlags
|= WSF_VISIBLE
;
750 else if (DesktopObject
!= NULL
)
752 FIXME("Setting DF_ALLOWOTHERACCOUNTHOOK is unimplemented.\n");
756 ERR("No associated WinStaObject nor DesktopObject!\n");
759 pvData
= &ObjectFlags
;
760 nDataSize
= sizeof(ObjectFlags
);
761 Status
= STATUS_SUCCESS
;
767 if (WinStaObject
!= NULL
)
769 pvData
= WinStaObject
->Name
.Buffer
;
770 nDataSize
= WinStaObject
->Name
.Length
+ sizeof(WCHAR
);
771 Status
= STATUS_SUCCESS
;
773 else if (DesktopObject
!= NULL
)
775 pvData
= DesktopObject
->pDeskInfo
->szDesktopName
;
776 nDataSize
= (wcslen(DesktopObject
->pDeskInfo
->szDesktopName
) + 1) * sizeof(WCHAR
);
777 Status
= STATUS_SUCCESS
;
781 Status
= STATUS_INVALID_PARAMETER
;
788 if (WinStaObject
!= NULL
)
790 pvData
= L
"WindowStation";
791 nDataSize
= sizeof(L
"WindowStation");
792 Status
= STATUS_SUCCESS
;
794 else if (DesktopObject
!= NULL
)
797 nDataSize
= sizeof(L
"Desktop");
798 Status
= STATUS_SUCCESS
;
802 Status
= STATUS_INVALID_PARAMETER
;
808 Status
= STATUS_NOT_IMPLEMENTED
;
809 ERR("UOI_USER_SID unimplemented!\n");
813 Status
= STATUS_INVALID_PARAMETER
;
818 if ((Status
== STATUS_SUCCESS
) && (nLength
< nDataSize
))
819 Status
= STATUS_BUFFER_TOO_SMALL
;
824 *nLengthNeeded
= nDataSize
;
826 /* Try to copy data to caller */
827 if (Status
== STATUS_SUCCESS
)
829 TRACE("Trying to copy data to caller (len = %lu, len needed = %lu)\n", nLength
, nDataSize
);
830 RtlCopyMemory(pvInformation
, pvData
, nDataSize
);
833 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
835 Status
= _SEH2_GetExceptionCode();
839 /* Release objects */
840 if (DesktopObject
!= NULL
)
841 ObDereferenceObject(DesktopObject
);
842 if (WinStaObject
!= NULL
)
843 ObDereferenceObject(WinStaObject
);
845 if (!NT_SUCCESS(Status
))
847 SetLastNtError(Status
);
855 * NtUserSetObjectInformation
857 * The NtUserSetObjectInformation function sets information about a
858 * window station or desktop object.
862 * Handle to the window station or desktop object for which to set
863 * object information. This value can be a handle of type HDESK or
867 * Specifies the object information to be set.
870 * Pointer to a buffer containing the object information.
873 * Specifies the size, in bytes, of the information contained in the
874 * buffer pointed to by pvInfo.
877 * If the function succeeds, the return value is nonzero. If the function
878 * fails the return value is zero.
886 NtUserSetObjectInformation(
892 /* FIXME: ZwQueryObject */
893 /* FIXME: ZwSetInformationObject */
894 SetLastNtError(STATUS_UNSUCCESSFUL
);
902 UserGetProcessWindowStation(VOID
)
904 PPROCESSINFO ppi
= PsGetCurrentProcessWin32Process();
911 * NtUserGetProcessWindowStation
913 * Returns a handle to the current process window station.
916 * If the function succeeds, the return value is handle to the window
917 * station assigned to the current process. If the function fails, the
918 * return value is NULL.
925 NtUserGetProcessWindowStation(VOID
)
927 return UserGetProcessWindowStation();
931 UserSetProcessWindowStation(HWINSTA hWindowStation
)
936 OBJECT_HANDLE_INFORMATION ObjectHandleInfo
;
937 PWINSTATION_OBJECT NewWinSta
= NULL
, OldWinSta
;
939 ppi
= PsGetCurrentProcessWin32Process();
941 /* Reference the new window station */
942 if (hWindowStation
!= NULL
)
944 Status
= IntValidateWindowStationHandle(hWindowStation
,
949 if (!NT_SUCCESS(Status
))
951 TRACE("Validation of window station handle (%p) failed\n",
953 SetLastNtError(Status
);
958 OldWinSta
= ppi
->prpwinsta
;
959 hwinstaOld
= PsGetProcessWin32WindowStation(ppi
->peProcess
);
961 /* Dereference the previous window station */
962 if (OldWinSta
!= NULL
)
964 ObDereferenceObject(OldWinSta
);
967 /* Check if we have a stale handle (it should happen for console apps) */
968 if (hwinstaOld
!= ppi
->hwinsta
)
970 ObCloseHandle(hwinstaOld
, UserMode
);
974 * FIXME: Don't allow changing the window station if there are threads that are attached to desktops and own GUI objects.
977 PsSetProcessWindowStation(ppi
->peProcess
, hWindowStation
);
979 ppi
->prpwinsta
= NewWinSta
;
980 ppi
->hwinsta
= hWindowStation
;
981 ppi
->amwinsta
= hWindowStation
!= NULL
? ObjectHandleInfo
.GrantedAccess
: 0;
982 TRACE("WS : Granted Access 0x%08lx\n",ppi
->amwinsta
);
984 if (RtlAreAllAccessesGranted(ppi
->amwinsta
, WINSTA_READSCREEN
))
986 ppi
->W32PF_flags
|= W32PF_READSCREENACCESSGRANTED
;
990 ppi
->W32PF_flags
&= ~W32PF_READSCREENACCESSGRANTED
;
993 if (NewWinSta
&& !(NewWinSta
->Flags
& WSS_NOIO
))
995 ppi
->W32PF_flags
|= W32PF_IOWINSTA
;
997 else // Might be closed if the handle is null.
999 ppi
->W32PF_flags
&= ~W32PF_IOWINSTA
;
1005 * NtUserSetProcessWindowStation
1007 * Assigns a window station to the current process.
1011 * Handle to the window station.
1021 NtUserSetProcessWindowStation(HWINSTA hWindowStation
)
1025 UserEnterExclusive();
1027 ret
= UserSetProcessWindowStation(hWindowStation
);
1035 * NtUserLockWindowStation
1037 * Locks switching desktops. Only the logon application is allowed to call this function.
1044 NtUserLockWindowStation(HWINSTA hWindowStation
)
1046 PWINSTATION_OBJECT Object
;
1049 TRACE("About to set process window station with handle (%p)\n",
1052 if (gpidLogon
!= PsGetCurrentProcessId())
1054 ERR("Unauthorized process attempted to lock the window station!\n");
1055 EngSetLastError(ERROR_ACCESS_DENIED
);
1059 Status
= IntValidateWindowStationHandle(hWindowStation
,
1064 if (!NT_SUCCESS(Status
))
1066 TRACE("Validation of window station handle (%p) failed\n",
1068 SetLastNtError(Status
);
1072 Object
->Flags
|= WSS_LOCKED
;
1074 ObDereferenceObject(Object
);
1079 * NtUserUnlockWindowStation
1081 * Unlocks switching desktops. Only the logon application is allowed to call this function.
1088 NtUserUnlockWindowStation(HWINSTA hWindowStation
)
1090 PWINSTATION_OBJECT Object
;
1094 TRACE("About to set process window station with handle (%p)\n",
1097 if (gpidLogon
!= PsGetCurrentProcessId())
1099 ERR("Unauthorized process attempted to unlock the window station!\n");
1100 EngSetLastError(ERROR_ACCESS_DENIED
);
1104 Status
= IntValidateWindowStationHandle(hWindowStation
,
1109 if (!NT_SUCCESS(Status
))
1111 TRACE("Validation of window station handle (%p) failed\n",
1113 SetLastNtError(Status
);
1117 Ret
= (Object
->Flags
& WSS_LOCKED
) == WSS_LOCKED
;
1118 Object
->Flags
&= ~WSS_LOCKED
;
1120 ObDereferenceObject(Object
);
1124 static NTSTATUS FASTCALL
1125 BuildWindowStationNameList(
1128 PULONG pRequiredSize
)
1130 OBJECT_ATTRIBUTES ObjectAttributes
;
1132 HANDLE DirectoryHandle
;
1133 char InitialBuffer
[256], *Buffer
;
1134 ULONG Context
, ReturnLength
, BufferSize
;
1136 POBJECT_DIRECTORY_INFORMATION DirEntry
;
1140 * Try to open the directory.
1142 InitializeObjectAttributes(&ObjectAttributes
,
1143 &gustrWindowStationsDir
,
1144 OBJ_CASE_INSENSITIVE
| OBJ_KERNEL_HANDLE
,
1148 Status
= ZwOpenDirectoryObject(&DirectoryHandle
,
1152 if (!NT_SUCCESS(Status
))
1157 /* First try to query the directory using a fixed-size buffer */
1160 Status
= ZwQueryDirectoryObject(DirectoryHandle
,
1162 sizeof(InitialBuffer
),
1167 if (NT_SUCCESS(Status
))
1169 if (STATUS_NO_MORE_ENTRIES
== ZwQueryDirectoryObject(DirectoryHandle
, NULL
, 0, FALSE
,
1170 FALSE
, &Context
, NULL
))
1172 /* Our fixed-size buffer is large enough */
1173 Buffer
= InitialBuffer
;
1179 /* Need a larger buffer, check how large exactly */
1180 Status
= ZwQueryDirectoryObject(DirectoryHandle
, NULL
, 0, FALSE
, TRUE
, &Context
,
1182 if (!NT_SUCCESS(Status
))
1184 ERR("ZwQueryDirectoryObject failed\n");
1185 ZwClose(DirectoryHandle
);
1189 BufferSize
= ReturnLength
;
1190 Buffer
= ExAllocatePoolWithTag(PagedPool
, BufferSize
, TAG_WINSTA
);
1193 ZwClose(DirectoryHandle
);
1194 return STATUS_NO_MEMORY
;
1197 /* We should have a sufficiently large buffer now */
1199 Status
= ZwQueryDirectoryObject(DirectoryHandle
, Buffer
, BufferSize
,
1200 FALSE
, TRUE
, &Context
, &ReturnLength
);
1201 if (! NT_SUCCESS(Status
) ||
1202 STATUS_NO_MORE_ENTRIES
!= ZwQueryDirectoryObject(DirectoryHandle
, NULL
, 0, FALSE
,
1203 FALSE
, &Context
, NULL
))
1205 /* Something went wrong, maybe someone added a directory entry? Just give up. */
1206 ExFreePoolWithTag(Buffer
, TAG_WINSTA
);
1207 ZwClose(DirectoryHandle
);
1208 return NT_SUCCESS(Status
) ? STATUS_INTERNAL_ERROR
: Status
;
1212 ZwClose(DirectoryHandle
);
1215 * Count the required size of buffer.
1217 ReturnLength
= sizeof(DWORD
);
1219 for (DirEntry
= (POBJECT_DIRECTORY_INFORMATION
) Buffer
;
1220 0 != DirEntry
->Name
.Length
;
1223 ReturnLength
+= DirEntry
->Name
.Length
+ sizeof(WCHAR
);
1226 TRACE("Required size: %lu Entry count: %lu\n", ReturnLength
, EntryCount
);
1227 if (NULL
!= pRequiredSize
)
1229 Status
= MmCopyToCaller(pRequiredSize
, &ReturnLength
, sizeof(ULONG
));
1230 if (! NT_SUCCESS(Status
))
1232 if (Buffer
!= InitialBuffer
)
1234 ExFreePoolWithTag(Buffer
, TAG_WINSTA
);
1236 return STATUS_BUFFER_TOO_SMALL
;
1241 * Check if the supplied buffer is large enough.
1243 if (dwSize
< ReturnLength
)
1245 if (Buffer
!= InitialBuffer
)
1247 ExFreePoolWithTag(Buffer
, TAG_WINSTA
);
1249 return STATUS_BUFFER_TOO_SMALL
;
1253 * Generate the resulting buffer contents.
1255 Status
= MmCopyToCaller(lpBuffer
, &EntryCount
, sizeof(DWORD
));
1256 if (! NT_SUCCESS(Status
))
1258 if (Buffer
!= InitialBuffer
)
1260 ExFreePoolWithTag(Buffer
, TAG_WINSTA
);
1264 lpBuffer
= (PVOID
) ((PCHAR
) lpBuffer
+ sizeof(DWORD
));
1267 for (DirEntry
= (POBJECT_DIRECTORY_INFORMATION
) Buffer
;
1268 0 != DirEntry
->Name
.Length
;
1271 Status
= MmCopyToCaller(lpBuffer
, DirEntry
->Name
.Buffer
, DirEntry
->Name
.Length
);
1272 if (! NT_SUCCESS(Status
))
1274 if (Buffer
!= InitialBuffer
)
1276 ExFreePoolWithTag(Buffer
, TAG_WINSTA
);
1280 lpBuffer
= (PVOID
) ((PCHAR
) lpBuffer
+ DirEntry
->Name
.Length
);
1281 Status
= MmCopyToCaller(lpBuffer
, &NullWchar
, sizeof(WCHAR
));
1282 if (! NT_SUCCESS(Status
))
1284 if (Buffer
!= InitialBuffer
)
1286 ExFreePoolWithTag(Buffer
, TAG_WINSTA
);
1290 lpBuffer
= (PVOID
) ((PCHAR
) lpBuffer
+ sizeof(WCHAR
));
1296 if (Buffer
!= InitialBuffer
)
1298 ExFreePoolWithTag(Buffer
, TAG_WINSTA
);
1301 return STATUS_SUCCESS
;
1304 static NTSTATUS FASTCALL
1305 BuildDesktopNameList(
1306 HWINSTA hWindowStation
,
1309 PULONG pRequiredSize
)
1312 PWINSTATION_OBJECT WindowStation
;
1313 PLIST_ENTRY DesktopEntry
;
1314 PDESKTOP DesktopObject
;
1318 UNICODE_STRING DesktopName
;
1320 Status
= IntValidateWindowStationHandle(hWindowStation
,
1325 if (! NT_SUCCESS(Status
))
1331 * Count the required size of buffer.
1333 ReturnLength
= sizeof(DWORD
);
1335 for (DesktopEntry
= WindowStation
->DesktopListHead
.Flink
;
1336 DesktopEntry
!= &WindowStation
->DesktopListHead
;
1337 DesktopEntry
= DesktopEntry
->Flink
)
1339 DesktopObject
= CONTAINING_RECORD(DesktopEntry
, DESKTOP
, ListEntry
);
1340 RtlInitUnicodeString(&DesktopName
, DesktopObject
->pDeskInfo
->szDesktopName
);
1341 ReturnLength
+= DesktopName
.Length
+ sizeof(WCHAR
);
1344 TRACE("Required size: %lu Entry count: %lu\n", ReturnLength
, EntryCount
);
1345 if (NULL
!= pRequiredSize
)
1347 Status
= MmCopyToCaller(pRequiredSize
, &ReturnLength
, sizeof(ULONG
));
1348 if (! NT_SUCCESS(Status
))
1350 ObDereferenceObject(WindowStation
);
1351 return STATUS_BUFFER_TOO_SMALL
;
1356 * Check if the supplied buffer is large enough.
1358 if (dwSize
< ReturnLength
)
1360 ObDereferenceObject(WindowStation
);
1361 return STATUS_BUFFER_TOO_SMALL
;
1365 * Generate the resulting buffer contents.
1367 Status
= MmCopyToCaller(lpBuffer
, &EntryCount
, sizeof(DWORD
));
1368 if (! NT_SUCCESS(Status
))
1370 ObDereferenceObject(WindowStation
);
1373 lpBuffer
= (PVOID
) ((PCHAR
) lpBuffer
+ sizeof(DWORD
));
1376 for (DesktopEntry
= WindowStation
->DesktopListHead
.Flink
;
1377 DesktopEntry
!= &WindowStation
->DesktopListHead
;
1378 DesktopEntry
= DesktopEntry
->Flink
)
1380 DesktopObject
= CONTAINING_RECORD(DesktopEntry
, DESKTOP
, ListEntry
);
1381 RtlInitUnicodeString(&DesktopName
, DesktopObject
->pDeskInfo
->szDesktopName
);
1382 Status
= MmCopyToCaller(lpBuffer
, DesktopName
.Buffer
, DesktopName
.Length
);
1383 if (! NT_SUCCESS(Status
))
1385 ObDereferenceObject(WindowStation
);
1388 lpBuffer
= (PVOID
) ((PCHAR
)lpBuffer
+ DesktopName
.Length
);
1389 Status
= MmCopyToCaller(lpBuffer
, &NullWchar
, sizeof(WCHAR
));
1390 if (! NT_SUCCESS(Status
))
1392 ObDereferenceObject(WindowStation
);
1395 lpBuffer
= (PVOID
) ((PCHAR
) lpBuffer
+ sizeof(WCHAR
));
1399 * Clean up and return
1401 ObDereferenceObject(WindowStation
);
1402 return STATUS_SUCCESS
;
1406 * NtUserBuildNameList
1408 * Function used for enumeration of desktops or window stations.
1412 * For enumeration of window stations this parameter must be set to
1413 * zero. Otherwise it's handle for window station.
1416 * Size of buffer passed by caller.
1419 * Buffer passed by caller. If the function succeeds, the buffer is
1420 * filled with window station/desktop count (in first DWORD) and
1421 * NULL-terminated window station/desktop names.
1424 * If the function succeeds, this is the number of bytes copied.
1425 * Otherwise it's size of buffer needed for function to succeed.
1432 NtUserBuildNameList(
1433 HWINSTA hWindowStation
,
1436 PULONG pRequiredSize
)
1438 /* The WindowStation name list and desktop name list are build in completely
1439 different ways. Call the appropriate function */
1440 return NULL
== hWindowStation
? BuildWindowStationNameList(dwSize
, lpBuffer
, pRequiredSize
) :
1441 BuildDesktopNameList(hWindowStation
, dwSize
, lpBuffer
, pRequiredSize
);
1448 NtUserSetLogonNotifyWindow(HWND hWnd
)
1450 if (gpidLogon
!= PsGetCurrentProcessId())
1455 if (!IntIsWindow(hWnd
))
1467 NtUserLockWorkStation(VOID
)
1470 PTHREADINFO pti
= PsGetCurrentThreadWin32Thread();
1472 UserEnterExclusive();
1474 if (pti
->rpdesk
== IntGetActiveDesktop())
1476 ret
= UserPostMessage(hwndSAS
, WM_LOGONNOTIFY
, LN_LOCK_WORKSTATION
, 0);
1490 NtUserSetWindowStationUser(
1491 IN HWINSTA hWindowStation
,
1493 IN PSID psid OPTIONAL
,
1498 PWINSTATION_OBJECT WindowStation
= NULL
;
1501 UserEnterExclusive();
1503 if (gpidLogon
!= PsGetCurrentProcessId())
1505 EngSetLastError(ERROR_ACCESS_DENIED
);
1509 /* Validate the window station */
1510 Status
= IntValidateWindowStationHandle(hWindowStation
,
1515 if (!NT_SUCCESS(Status
))
1520 /* Capture the user LUID */
1523 ProbeForRead(pluid
, sizeof(LUID
), 1);
1526 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
1528 Status
= _SEH2_GetExceptionCode();
1529 _SEH2_YIELD(goto Leave
);
1533 /* Reset the window station user LUID */
1534 RtlZeroMemory(&WindowStation
->luidUser
, sizeof(LUID
));
1536 /* Reset the window station user SID */
1537 if (WindowStation
->psidUser
)
1539 ExFreePoolWithTag(WindowStation
->psidUser
, USERTAG_SECURITY
);
1540 WindowStation
->psidUser
= NULL
;
1543 /* Copy the new user SID if one has been provided */
1546 WindowStation
->psidUser
= ExAllocatePoolWithTag(PagedPool
, size
, USERTAG_SECURITY
);
1547 if (WindowStation
->psidUser
== NULL
)
1549 EngSetLastError(ERROR_OUTOFMEMORY
);
1553 Status
= STATUS_SUCCESS
;
1556 ProbeForRead(psid
, size
, 1);
1557 RtlCopyMemory(WindowStation
->psidUser
, psid
, size
);
1559 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
1561 Status
= _SEH2_GetExceptionCode();
1565 if (!NT_SUCCESS(Status
))
1567 ExFreePoolWithTag(WindowStation
->psidUser
, USERTAG_SECURITY
);
1568 WindowStation
->psidUser
= NULL
;
1573 /* Copy the new user LUID */
1574 WindowStation
->luidUser
= luidUser
;
1580 ObDereferenceObject(WindowStation
);