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 WCHAR wstrWindowStationsDir
[MAX_PATH
];
53 OBJECT_ATTRIBUTES ObjectAttributes
;
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 swprintf(wstrWindowStationsDir
,
73 if (!RtlCreateUnicodeString(&gustrWindowStationsDir
, wstrWindowStationsDir
))
75 return STATUS_INSUFFICIENT_RESOURCES
;
79 InitializeObjectAttributes(&ObjectAttributes
,
80 &gustrWindowStationsDir
,
84 Status
= ZwCreateDirectoryObject(&hWinstaDir
, 0, &ObjectAttributes
);
85 if (!NT_SUCCESS(Status
))
87 ERR("Could not create %wZ directory (Status 0x%X)\n", &gustrWindowStationsDir
, Status
);
91 TRACE("Created directory %wZ for session %lu\n", &gustrWindowStationsDir
, Peb
->SessionId
);
96 /* OBJECT CALLBACKS **********************************************************/
100 IntWinStaObjectDelete(
101 _In_ PVOID Parameters
)
103 PWIN32_DELETEMETHOD_PARAMETERS DeleteParameters
= Parameters
;
104 PWINSTATION_OBJECT WinSta
= (PWINSTATION_OBJECT
)DeleteParameters
->Object
;
106 TRACE("Deleting window station (0x%p)\n", WinSta
);
108 WinSta
->Flags
|= WSS_DYING
;
110 UserEmptyClipboardData(WinSta
);
112 RtlDestroyAtomTable(WinSta
->AtomTable
);
114 RtlFreeUnicodeString(&WinSta
->Name
);
116 return STATUS_SUCCESS
;
121 IntWinStaObjectParse(
122 _In_ PVOID Parameters
)
124 PWIN32_PARSEMETHOD_PARAMETERS ParseParameters
= Parameters
;
125 PUNICODE_STRING RemainingName
= ParseParameters
->RemainingName
;
127 /* Assume we don't find anything */
128 *ParseParameters
->Object
= NULL
;
130 /* Check for an empty name */
131 if (!RemainingName
->Length
)
133 /* Make sure this is a window station, can't parse a desktop now */
134 if (ParseParameters
->ObjectType
!= ExWindowStationObjectType
)
137 return STATUS_OBJECT_TYPE_MISMATCH
;
140 /* Reference the window station and return */
141 ObReferenceObject(ParseParameters
->ParseObject
);
142 *ParseParameters
->Object
= ParseParameters
->ParseObject
;
143 return STATUS_SUCCESS
;
146 /* Check for leading slash */
147 if (RemainingName
->Buffer
[0] == OBJ_NAME_PATH_SEPARATOR
)
150 RemainingName
->Buffer
++;
151 RemainingName
->Length
-= sizeof(WCHAR
);
152 RemainingName
->MaximumLength
-= sizeof(WCHAR
);
155 /* Check if there is still a slash */
156 if (wcschr(RemainingName
->Buffer
, OBJ_NAME_PATH_SEPARATOR
))
158 /* In this case, fail */
159 return STATUS_OBJECT_PATH_INVALID
;
163 * Check if we are parsing a desktop.
165 if (ParseParameters
->ObjectType
== ExDesktopObjectType
)
167 /* Then call the desktop parse routine */
168 return IntDesktopObjectParse(ParseParameters
->ParseObject
,
169 ParseParameters
->ObjectType
,
170 ParseParameters
->AccessState
,
171 ParseParameters
->AccessMode
,
172 ParseParameters
->Attributes
,
173 ParseParameters
->CompleteName
,
175 ParseParameters
->Context
,
176 ParseParameters
->SecurityQos
,
177 ParseParameters
->Object
);
180 /* Should hopefully never get here */
181 return STATUS_OBJECT_TYPE_MISMATCH
;
187 _In_ PVOID Parameters
)
189 PWIN32_OKAYTOCLOSEMETHOD_PARAMETERS OkToCloseParameters
= Parameters
;
192 ppi
= PsGetCurrentProcessWin32Process();
194 if(ppi
&& (OkToCloseParameters
->Handle
== ppi
->hwinsta
))
196 return STATUS_ACCESS_DENIED
;
199 return STATUS_SUCCESS
;
202 /* PRIVATE FUNCTIONS **********************************************************/
205 * IntValidateWindowStationHandle
207 * Validates the window station handle.
210 * If the function succeeds, the handle remains referenced. If the
211 * fucntion fails, last error is set.
215 IntValidateWindowStationHandle(
216 HWINSTA WindowStation
,
217 KPROCESSOR_MODE AccessMode
,
218 ACCESS_MASK DesiredAccess
,
219 PWINSTATION_OBJECT
*Object
,
220 POBJECT_HANDLE_INFORMATION pObjectHandleInfo
)
224 if (WindowStation
== NULL
)
226 ERR("Invalid window station handle\n");
227 EngSetLastError(ERROR_INVALID_HANDLE
);
228 return STATUS_INVALID_HANDLE
;
231 Status
= ObReferenceObjectByHandle(WindowStation
,
233 ExWindowStationObjectType
,
238 if (!NT_SUCCESS(Status
))
239 SetLastNtError(Status
);
245 co_IntInitializeDesktopGraphics(VOID
)
248 UNICODE_STRING DriverName
= RTL_CONSTANT_STRING(L
"DISPLAY");
251 ScreenDeviceContext
= IntGdiCreateDC(&DriverName
, NULL
, NULL
, NULL
, FALSE
);
252 if (NULL
== ScreenDeviceContext
)
254 IntDestroyPrimarySurface();
257 GreSetDCOwner(ScreenDeviceContext
, GDI_OBJ_HMGR_PUBLIC
);
259 if (! IntCreatePrimarySurface())
264 hSystemBM
= NtGdiCreateCompatibleDC(ScreenDeviceContext
);
266 NtGdiSelectFont(hSystemBM
, NtGdiGetStockObject(SYSTEM_FONT
));
267 GreSetDCOwner(hSystemBM
, GDI_OBJ_HMGR_PUBLIC
);
269 /* Update the SERVERINFO */
270 gpsi
->aiSysMet
[SM_CXSCREEN
] = gppdevPrimary
->gdiinfo
.ulHorzRes
;
271 gpsi
->aiSysMet
[SM_CYSCREEN
] = gppdevPrimary
->gdiinfo
.ulVertRes
;
272 gpsi
->Planes
= NtGdiGetDeviceCaps(ScreenDeviceContext
, PLANES
);
273 gpsi
->BitsPixel
= NtGdiGetDeviceCaps(ScreenDeviceContext
, BITSPIXEL
);
274 gpsi
->BitCount
= gpsi
->Planes
* gpsi
->BitsPixel
;
275 gpsi
->dmLogPixels
= NtGdiGetDeviceCaps(ScreenDeviceContext
, LOGPIXELSY
);
276 if (NtGdiGetDeviceCaps(ScreenDeviceContext
, RASTERCAPS
) & RC_PALETTE
)
278 gpsi
->PUSIFlags
|= PUSIF_PALETTEDISPLAY
;
281 gpsi
->PUSIFlags
&= ~PUSIF_PALETTEDISPLAY
;
282 // Font is realized and this dc was previously set to internal DC_ATTR.
283 gpsi
->cxSysFontChar
= IntGetCharDimensions(hSystemBM
, &tmw
, (DWORD
*)&gpsi
->cySysFontChar
);
284 gpsi
->tmSysFont
= tmw
;
286 /* Put the pointer in the center of the screen */
287 gpsi
->ptCursor
.x
= gpsi
->aiSysMet
[SM_CXSCREEN
] / 2;
288 gpsi
->ptCursor
.y
= gpsi
->aiSysMet
[SM_CYSCREEN
] / 2;
291 UserAttachMonitor((HDEV
)gppdevPrimary
);
293 /* Setup the cursor */
294 co_IntLoadDefaultCursors();
296 /* Setup the icons */
302 /* Show the desktop */
303 pdesk
= IntGetActiveDesktop();
305 co_IntShowDesktop(pdesk
, gpsi
->aiSysMet
[SM_CXSCREEN
], gpsi
->aiSysMet
[SM_CYSCREEN
], TRUE
);
311 IntEndDesktopGraphics(VOID
)
313 if (NULL
!= ScreenDeviceContext
)
314 { // No need to allocate a new dcattr.
315 GreSetDCOwner(ScreenDeviceContext
, GDI_OBJ_HMGR_POWNED
);
316 GreDeleteObject(ScreenDeviceContext
);
317 ScreenDeviceContext
= NULL
;
319 IntHideDesktop(IntGetActiveDesktop());
320 IntDestroyPrimarySurface();
326 return ScreenDeviceContext
;
330 CheckWinstaAttributeAccess(ACCESS_MASK DesiredAccess
)
332 PPROCESSINFO ppi
= PsGetCurrentProcessWin32Process();
333 if ( gpidLogon
!= PsGetCurrentProcessId() )
335 if (!(ppi
->W32PF_flags
& W32PF_IOWINSTA
))
337 ERR("Requires Interactive Window Station\n");
338 EngSetLastError(ERROR_REQUIRES_INTERACTIVE_WINDOWSTATION
);
341 if (!RtlAreAllAccessesGranted(ppi
->amwinsta
, DesiredAccess
))
343 ERR("Access Denied\n");
344 EngSetLastError(ERROR_ACCESS_DENIED
);
352 /* PUBLIC FUNCTIONS ***********************************************************/
355 * NtUserCreateWindowStation
357 * Creates a new window station.
360 * lpszWindowStationName
361 * Pointer to a null-terminated string specifying the name of the
362 * window station to be created. Window station names are
363 * case-insensitive and cannot contain backslash characters (\).
364 * Only members of the Administrators group are allowed to specify a
368 * Requested type of access
371 * Security descriptor
373 * Unknown3, Unknown4, Unknown5
377 * If the function succeeds, the return value is a handle to the newly
378 * created window station. If the specified window station already
379 * exists, the function succeeds and returns a handle to the existing
380 * window station. If the function fails, the return value is NULL.
383 * Correct the prototype to match the Windows one (with 7 parameters
391 NtUserCreateWindowStation(
392 POBJECT_ATTRIBUTES ObjectAttributes
,
393 ACCESS_MASK dwDesiredAccess
,
400 UNICODE_STRING WindowStationName
;
401 PWINSTATION_OBJECT WindowStationObject
;
402 HWINSTA WindowStation
;
405 TRACE("NtUserCreateWindowStation called\n");
407 Status
= ObOpenObjectByName(ObjectAttributes
,
408 ExWindowStationObjectType
,
413 (PVOID
*)&WindowStation
);
415 if (NT_SUCCESS(Status
))
417 TRACE("NtUserCreateWindowStation opened window station %wZ\n", ObjectAttributes
->ObjectName
);
418 return (HWINSTA
)WindowStation
;
422 * No existing window station found, try to create new one
425 /* Capture window station name */
428 ProbeForRead( ObjectAttributes
, sizeof(OBJECT_ATTRIBUTES
), 1);
429 Status
= IntSafeCopyUnicodeStringTerminateNULL(&WindowStationName
, ObjectAttributes
->ObjectName
);
431 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
433 Status
=_SEH2_GetExceptionCode();
437 if (! NT_SUCCESS(Status
))
439 ERR("Failed reading capturing window station name\n");
440 SetLastNtError(Status
);
444 /* Create the window station object */
445 Status
= ObCreateObject(UserMode
,
446 ExWindowStationObjectType
,
450 sizeof(WINSTATION_OBJECT
),
453 (PVOID
*)&WindowStationObject
);
455 if (!NT_SUCCESS(Status
))
457 ERR("ObCreateObject failed with %lx for window station %wZ\n", Status
, &WindowStationName
);
458 ExFreePoolWithTag(WindowStationName
.Buffer
, TAG_STRING
);
459 SetLastNtError(STATUS_INSUFFICIENT_RESOURCES
);
463 /* Initialize the window station */
464 RtlZeroMemory(WindowStationObject
, sizeof(WINSTATION_OBJECT
));
466 InitializeListHead(&WindowStationObject
->DesktopListHead
);
467 WindowStationObject
->Name
= WindowStationName
;
468 WindowStationObject
->dwSessionId
= NtCurrentPeb()->SessionId
;
469 Status
= RtlCreateAtomTable(37, &WindowStationObject
->AtomTable
);
470 if (!NT_SUCCESS(Status
))
472 ERR("RtlCreateAtomTable failed with %lx for window station %wZ\n", Status
, &WindowStationName
);
473 ObDereferenceObject(WindowStationObject
);
474 SetLastNtError(STATUS_INSUFFICIENT_RESOURCES
);
478 Status
= ObInsertObject((PVOID
)WindowStationObject
,
483 (PVOID
*)&WindowStation
);
485 if (!NT_SUCCESS(Status
))
487 ERR("ObInsertObject failed with %lx for window station\n", Status
);
488 SetLastNtError(STATUS_INSUFFICIENT_RESOURCES
);
492 if (InputWindowStation
== NULL
)
494 ERR("Initializing input window station\n");
495 InputWindowStation
= WindowStationObject
;
497 WindowStationObject
->Flags
&= ~WSS_NOIO
;
503 WindowStationObject
->Flags
|= WSS_NOIO
;
506 TRACE("NtUserCreateWindowStation created object %p with name %wZ handle %p\n",
507 WindowStation
, &WindowStationObject
->Name
, WindowStation
);
508 return WindowStation
;
512 * NtUserOpenWindowStation
514 * Opens an existing window station.
517 * lpszWindowStationName
518 * Name of the existing window station.
521 * Requested type of access.
524 * If the function succeeds, the return value is the handle to the
525 * specified window station. If the function fails, the return value
529 * The returned handle can be closed with NtUserCloseWindowStation.
536 NtUserOpenWindowStation(
537 POBJECT_ATTRIBUTES ObjectAttributes
,
538 ACCESS_MASK dwDesiredAccess
)
543 Status
= ObOpenObjectByName(ObjectAttributes
,
544 ExWindowStationObjectType
,
551 if (!NT_SUCCESS(Status
))
553 ERR("NtUserOpenWindowStation failed\n");
554 SetLastNtError(Status
);
558 TRACE("Opened window station %wZ with handle %p\n", ObjectAttributes
->ObjectName
, hwinsta
);
564 * NtUserCloseWindowStation
566 * Closes a window station handle.
570 * Handle to the window station.
576 * The window station handle can be created with NtUserCreateWindowStation
577 * or NtUserOpenWindowStation. Attemps to close a handle to the window
578 * station assigned to the calling process will fail.
586 NtUserCloseWindowStation(
589 PWINSTATION_OBJECT Object
;
592 TRACE("NtUserCloseWindowStation called (%p)\n", hWinSta
);
594 if (hWinSta
== UserGetProcessWindowStation())
596 ERR("Attempted to close process window station\n");
600 Status
= IntValidateWindowStationHandle(hWinSta
,
606 if (!NT_SUCCESS(Status
))
608 ERR("Validation of window station handle (%p) failed\n", hWinSta
);
612 ObDereferenceObject(Object
);
614 TRACE("Closing window station handle (%p)\n", hWinSta
);
616 Status
= ObCloseHandle(hWinSta
, UserMode
);
617 if (!NT_SUCCESS(Status
))
619 SetLastNtError(Status
);
627 * NtUserGetObjectInformation
629 * The NtUserGetObjectInformation function retrieves information about a
630 * window station or desktop object.
634 * Handle to the window station or desktop object for which to
635 * return information. This can be a handle of type HDESK or HWINSTA
636 * (for example, a handle returned by NtUserCreateWindowStation,
637 * NtUserOpenWindowStation, NtUserCreateDesktop, or NtUserOpenDesktop).
640 * Specifies the object information to be retrieved.
643 * Pointer to a buffer to receive the object information.
646 * Specifies the size, in bytes, of the buffer pointed to by the
650 * Pointer to a variable receiving the number of bytes required to
651 * store the requested information. If this variable's value is
652 * greater than the value of the nLength parameter when the function
653 * returns, the function returns FALSE, and none of the information
654 * is copied to the pvInfo buffer. If the value of the variable pointed
655 * to by lpnLengthNeeded is less than or equal to the value of nLength,
656 * the entire information block is copied.
659 * If the function succeeds, the return value is nonzero. If the function
660 * fails, the return value is zero.
667 NtUserGetObjectInformation(
672 PDWORD nLengthNeeded
)
675 PWINSTATION_OBJECT WinStaObject
= NULL
;
676 PDESKTOP DesktopObject
= NULL
;
677 USEROBJECTFLAGS ObjectFlags
;
684 ProbeForWrite(nLengthNeeded
, sizeof(*nLengthNeeded
), 1);
685 ProbeForWrite(pvInformation
, nLength
, 1);
687 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
689 SetLastNtError(_SEH2_GetExceptionCode());
694 /* Try window station */
695 TRACE("Trying to open window station %p\n", hObject
);
696 Status
= ObReferenceObjectByHandle(hObject
,
698 ExWindowStationObjectType
,
700 (PVOID
*)&WinStaObject
,
703 if (Status
== STATUS_OBJECT_TYPE_MISMATCH
)
706 TRACE("Trying to open desktop %p\n", hObject
);
708 Status
= IntValidateDesktopHandle(hObject
,
714 if (!NT_SUCCESS(Status
))
716 ERR("Failed: 0x%x\n", Status
);
720 TRACE("WinSta or Desktop opened!!\n");
727 /* This is a default implementation that does almost nothing */
728 ObjectFlags
.fInherit
= FALSE
;
729 ObjectFlags
.fReserved
= FALSE
;
730 ObjectFlags
.dwFlags
= 0;
732 pvData
= &ObjectFlags
;
733 nDataSize
= sizeof(ObjectFlags
);
734 Status
= STATUS_SUCCESS
;
735 ERR("UOI_FLAGS unimplemented!\n");
741 if (WinStaObject
!= NULL
)
743 pvData
= WinStaObject
->Name
.Buffer
;
744 nDataSize
= WinStaObject
->Name
.Length
+ sizeof(WCHAR
);
745 Status
= STATUS_SUCCESS
;
747 else if (DesktopObject
!= NULL
)
749 pvData
= DesktopObject
->pDeskInfo
->szDesktopName
;
750 nDataSize
= (wcslen(DesktopObject
->pDeskInfo
->szDesktopName
) + 1) * sizeof(WCHAR
);
751 Status
= STATUS_SUCCESS
;
755 Status
= STATUS_INVALID_PARAMETER
;
762 if (WinStaObject
!= NULL
)
764 pvData
= L
"WindowStation";
765 nDataSize
= sizeof(L
"WindowStation");
766 Status
= STATUS_SUCCESS
;
768 else if (DesktopObject
!= NULL
)
771 nDataSize
= sizeof(L
"Desktop");
772 Status
= STATUS_SUCCESS
;
776 Status
= STATUS_INVALID_PARAMETER
;
782 Status
= STATUS_NOT_IMPLEMENTED
;
783 ERR("UOI_USER_SID unimplemented!\n");
787 Status
= STATUS_INVALID_PARAMETER
;
792 if ((Status
== STATUS_SUCCESS
) && (nLength
< nDataSize
))
793 Status
= STATUS_BUFFER_TOO_SMALL
;
798 *nLengthNeeded
= nDataSize
;
800 /* Try to copy data to caller */
801 if (Status
== STATUS_SUCCESS
)
803 TRACE("Trying to copy data to caller (len = %lu, len needed = %lu)\n", nLength
, nDataSize
);
804 RtlCopyMemory(pvInformation
, pvData
, nDataSize
);
807 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
809 Status
= _SEH2_GetExceptionCode();
813 /* Release objects */
814 if (DesktopObject
!= NULL
)
815 ObDereferenceObject(DesktopObject
);
816 if (WinStaObject
!= NULL
)
817 ObDereferenceObject(WinStaObject
);
819 if (!NT_SUCCESS(Status
))
821 SetLastNtError(Status
);
829 * NtUserSetObjectInformation
831 * The NtUserSetObjectInformation function sets information about a
832 * window station or desktop object.
836 * Handle to the window station or desktop object for which to set
837 * object information. This value can be a handle of type HDESK or
841 * Specifies the object information to be set.
844 * Pointer to a buffer containing the object information.
847 * Specifies the size, in bytes, of the information contained in the
848 * buffer pointed to by pvInfo.
851 * If the function succeeds, the return value is nonzero. If the function
852 * fails the return value is zero.
860 NtUserSetObjectInformation(
866 /* FIXME: ZwQueryObject */
867 /* FIXME: ZwSetInformationObject */
868 SetLastNtError(STATUS_UNSUCCESSFUL
);
876 UserGetProcessWindowStation(VOID
)
878 PPROCESSINFO ppi
= PsGetCurrentProcessWin32Process();
885 * NtUserGetProcessWindowStation
887 * Returns a handle to the current process window station.
890 * If the function succeeds, the return value is handle to the window
891 * station assigned to the current process. If the function fails, the
892 * return value is NULL.
899 NtUserGetProcessWindowStation(VOID
)
901 return UserGetProcessWindowStation();
905 UserSetProcessWindowStation(HWINSTA hWindowStation
)
910 OBJECT_HANDLE_INFORMATION ObjectHandleInfo
;
911 PWINSTATION_OBJECT NewWinSta
= NULL
, OldWinSta
;
913 ppi
= PsGetCurrentProcessWin32Process();
915 /* Reference the new window station */
916 if(hWindowStation
!=NULL
)
918 Status
= IntValidateWindowStationHandle(hWindowStation
,
923 if (!NT_SUCCESS(Status
))
925 TRACE("Validation of window station handle (%p) failed\n",
927 SetLastNtError(Status
);
932 OldWinSta
= ppi
->prpwinsta
;
933 hwinstaOld
= PsGetProcessWin32WindowStation(ppi
->peProcess
);
935 /* Dereference the previous window station */
936 if(OldWinSta
!= NULL
)
938 ObDereferenceObject(OldWinSta
);
941 /* Check if we have a stale handle (it should happen for console apps) */
942 if(hwinstaOld
!= ppi
->hwinsta
)
944 ObCloseHandle(hwinstaOld
, UserMode
);
948 * FIXME: Don't allow changing the window station if there are threads that are attached to desktops and own GUI objects.
951 PsSetProcessWindowStation(ppi
->peProcess
, hWindowStation
);
953 ppi
->prpwinsta
= NewWinSta
;
954 ppi
->hwinsta
= hWindowStation
;
955 ppi
->amwinsta
= hWindowStation
!= NULL
? ObjectHandleInfo
.GrantedAccess
: 0;
956 TRACE("WS : Granted Access 0x%08lx\n",ppi
->amwinsta
);
958 if (RtlAreAllAccessesGranted(ppi
->amwinsta
, WINSTA_READSCREEN
))
960 ppi
->W32PF_flags
|= W32PF_READSCREENACCESSGRANTED
;
964 ppi
->W32PF_flags
&= ~W32PF_READSCREENACCESSGRANTED
;
967 if (NewWinSta
&& !(NewWinSta
->Flags
& WSS_NOIO
) )
969 ppi
->W32PF_flags
|= W32PF_IOWINSTA
;
971 else // Might be closed if the handle is null.
973 ppi
->W32PF_flags
&= ~W32PF_IOWINSTA
;
979 * NtUserSetProcessWindowStation
981 * Assigns a window station to the current process.
985 * Handle to the window station.
995 NtUserSetProcessWindowStation(HWINSTA hWindowStation
)
999 UserEnterExclusive();
1001 ret
= UserSetProcessWindowStation(hWindowStation
);
1009 * NtUserLockWindowStation
1011 * Locks switching desktops. Only the logon application is allowed to call this function.
1018 NtUserLockWindowStation(HWINSTA hWindowStation
)
1020 PWINSTATION_OBJECT Object
;
1023 TRACE("About to set process window station with handle (%p)\n",
1026 if (gpidLogon
!= PsGetCurrentProcessId())
1028 ERR("Unauthorized process attempted to lock the window station!\n");
1029 EngSetLastError(ERROR_ACCESS_DENIED
);
1033 Status
= IntValidateWindowStationHandle(hWindowStation
,
1038 if (!NT_SUCCESS(Status
))
1040 TRACE("Validation of window station handle (%p) failed\n",
1042 SetLastNtError(Status
);
1046 Object
->Flags
|= WSS_LOCKED
;
1048 ObDereferenceObject(Object
);
1053 * NtUserUnlockWindowStation
1055 * Unlocks switching desktops. Only the logon application is allowed to call this function.
1062 NtUserUnlockWindowStation(HWINSTA hWindowStation
)
1064 PWINSTATION_OBJECT Object
;
1068 TRACE("About to set process window station with handle (%p)\n",
1071 if (gpidLogon
!= PsGetCurrentProcessId())
1073 ERR("Unauthorized process attempted to unlock the window station!\n");
1074 EngSetLastError(ERROR_ACCESS_DENIED
);
1078 Status
= IntValidateWindowStationHandle(hWindowStation
,
1083 if (!NT_SUCCESS(Status
))
1085 TRACE("Validation of window station handle (%p) failed\n",
1087 SetLastNtError(Status
);
1091 Ret
= (Object
->Flags
& WSS_LOCKED
) == WSS_LOCKED
;
1092 Object
->Flags
&= ~WSS_LOCKED
;
1094 ObDereferenceObject(Object
);
1098 static NTSTATUS FASTCALL
1099 BuildWindowStationNameList(
1102 PULONG pRequiredSize
)
1104 OBJECT_ATTRIBUTES ObjectAttributes
;
1106 HANDLE DirectoryHandle
;
1107 char InitialBuffer
[256], *Buffer
;
1108 ULONG Context
, ReturnLength
, BufferSize
;
1110 POBJECT_DIRECTORY_INFORMATION DirEntry
;
1114 * Try to open the directory.
1116 InitializeObjectAttributes(&ObjectAttributes
,
1117 &gustrWindowStationsDir
,
1118 OBJ_CASE_INSENSITIVE
| OBJ_KERNEL_HANDLE
,
1122 Status
= ZwOpenDirectoryObject(&DirectoryHandle
,
1126 if (!NT_SUCCESS(Status
))
1131 /* First try to query the directory using a fixed-size buffer */
1134 Status
= ZwQueryDirectoryObject(DirectoryHandle
,
1136 sizeof(InitialBuffer
),
1141 if (NT_SUCCESS(Status
))
1143 if (STATUS_NO_MORE_ENTRIES
== ZwQueryDirectoryObject(DirectoryHandle
, NULL
, 0, FALSE
,
1144 FALSE
, &Context
, NULL
))
1146 /* Our fixed-size buffer is large enough */
1147 Buffer
= InitialBuffer
;
1153 /* Need a larger buffer, check how large exactly */
1154 Status
= ZwQueryDirectoryObject(DirectoryHandle
, NULL
, 0, FALSE
, TRUE
, &Context
,
1156 if (!NT_SUCCESS(Status
))
1158 ERR("ZwQueryDirectoryObject failed\n");
1159 ZwClose(DirectoryHandle
);
1163 BufferSize
= ReturnLength
;
1164 Buffer
= ExAllocatePoolWithTag(PagedPool
, BufferSize
, TAG_WINSTA
);
1167 ZwClose(DirectoryHandle
);
1168 return STATUS_NO_MEMORY
;
1171 /* We should have a sufficiently large buffer now */
1173 Status
= ZwQueryDirectoryObject(DirectoryHandle
, Buffer
, BufferSize
,
1174 FALSE
, TRUE
, &Context
, &ReturnLength
);
1175 if (! NT_SUCCESS(Status
) ||
1176 STATUS_NO_MORE_ENTRIES
!= ZwQueryDirectoryObject(DirectoryHandle
, NULL
, 0, FALSE
,
1177 FALSE
, &Context
, NULL
))
1179 /* Something went wrong, maybe someone added a directory entry? Just give up. */
1180 ExFreePoolWithTag(Buffer
, TAG_WINSTA
);
1181 ZwClose(DirectoryHandle
);
1182 return NT_SUCCESS(Status
) ? STATUS_INTERNAL_ERROR
: Status
;
1186 ZwClose(DirectoryHandle
);
1189 * Count the required size of buffer.
1191 ReturnLength
= sizeof(DWORD
);
1193 for (DirEntry
= (POBJECT_DIRECTORY_INFORMATION
) Buffer
;
1194 0 != DirEntry
->Name
.Length
;
1197 ReturnLength
+= DirEntry
->Name
.Length
+ sizeof(WCHAR
);
1200 TRACE("Required size: %lu Entry count: %lu\n", ReturnLength
, EntryCount
);
1201 if (NULL
!= pRequiredSize
)
1203 Status
= MmCopyToCaller(pRequiredSize
, &ReturnLength
, sizeof(ULONG
));
1204 if (! NT_SUCCESS(Status
))
1206 if (Buffer
!= InitialBuffer
)
1208 ExFreePoolWithTag(Buffer
, TAG_WINSTA
);
1210 return STATUS_BUFFER_TOO_SMALL
;
1215 * Check if the supplied buffer is large enough.
1217 if (dwSize
< ReturnLength
)
1219 if (Buffer
!= InitialBuffer
)
1221 ExFreePoolWithTag(Buffer
, TAG_WINSTA
);
1223 return STATUS_BUFFER_TOO_SMALL
;
1227 * Generate the resulting buffer contents.
1229 Status
= MmCopyToCaller(lpBuffer
, &EntryCount
, sizeof(DWORD
));
1230 if (! NT_SUCCESS(Status
))
1232 if (Buffer
!= InitialBuffer
)
1234 ExFreePoolWithTag(Buffer
, TAG_WINSTA
);
1238 lpBuffer
= (PVOID
) ((PCHAR
) lpBuffer
+ sizeof(DWORD
));
1241 for (DirEntry
= (POBJECT_DIRECTORY_INFORMATION
) Buffer
;
1242 0 != DirEntry
->Name
.Length
;
1245 Status
= MmCopyToCaller(lpBuffer
, DirEntry
->Name
.Buffer
, DirEntry
->Name
.Length
);
1246 if (! NT_SUCCESS(Status
))
1248 if (Buffer
!= InitialBuffer
)
1250 ExFreePoolWithTag(Buffer
, TAG_WINSTA
);
1254 lpBuffer
= (PVOID
) ((PCHAR
) lpBuffer
+ DirEntry
->Name
.Length
);
1255 Status
= MmCopyToCaller(lpBuffer
, &NullWchar
, sizeof(WCHAR
));
1256 if (! NT_SUCCESS(Status
))
1258 if (Buffer
!= InitialBuffer
)
1260 ExFreePoolWithTag(Buffer
, TAG_WINSTA
);
1264 lpBuffer
= (PVOID
) ((PCHAR
) lpBuffer
+ sizeof(WCHAR
));
1270 if (Buffer
!= InitialBuffer
)
1272 ExFreePoolWithTag(Buffer
, TAG_WINSTA
);
1275 return STATUS_SUCCESS
;
1278 static NTSTATUS FASTCALL
1279 BuildDesktopNameList(
1280 HWINSTA hWindowStation
,
1283 PULONG pRequiredSize
)
1286 PWINSTATION_OBJECT WindowStation
;
1287 PLIST_ENTRY DesktopEntry
;
1288 PDESKTOP DesktopObject
;
1292 UNICODE_STRING DesktopName
;
1294 Status
= IntValidateWindowStationHandle(hWindowStation
,
1299 if (! NT_SUCCESS(Status
))
1305 * Count the required size of buffer.
1307 ReturnLength
= sizeof(DWORD
);
1309 for (DesktopEntry
= WindowStation
->DesktopListHead
.Flink
;
1310 DesktopEntry
!= &WindowStation
->DesktopListHead
;
1311 DesktopEntry
= DesktopEntry
->Flink
)
1313 DesktopObject
= CONTAINING_RECORD(DesktopEntry
, DESKTOP
, ListEntry
);
1314 RtlInitUnicodeString(&DesktopName
, DesktopObject
->pDeskInfo
->szDesktopName
);
1315 ReturnLength
+= DesktopName
.Length
+ sizeof(WCHAR
);
1318 TRACE("Required size: %lu Entry count: %lu\n", ReturnLength
, EntryCount
);
1319 if (NULL
!= pRequiredSize
)
1321 Status
= MmCopyToCaller(pRequiredSize
, &ReturnLength
, sizeof(ULONG
));
1322 if (! NT_SUCCESS(Status
))
1324 ObDereferenceObject(WindowStation
);
1325 return STATUS_BUFFER_TOO_SMALL
;
1330 * Check if the supplied buffer is large enough.
1332 if (dwSize
< ReturnLength
)
1334 ObDereferenceObject(WindowStation
);
1335 return STATUS_BUFFER_TOO_SMALL
;
1339 * Generate the resulting buffer contents.
1341 Status
= MmCopyToCaller(lpBuffer
, &EntryCount
, sizeof(DWORD
));
1342 if (! NT_SUCCESS(Status
))
1344 ObDereferenceObject(WindowStation
);
1347 lpBuffer
= (PVOID
) ((PCHAR
) lpBuffer
+ sizeof(DWORD
));
1350 for (DesktopEntry
= WindowStation
->DesktopListHead
.Flink
;
1351 DesktopEntry
!= &WindowStation
->DesktopListHead
;
1352 DesktopEntry
= DesktopEntry
->Flink
)
1354 DesktopObject
= CONTAINING_RECORD(DesktopEntry
, DESKTOP
, ListEntry
);
1355 RtlInitUnicodeString(&DesktopName
, DesktopObject
->pDeskInfo
->szDesktopName
);
1356 Status
= MmCopyToCaller(lpBuffer
, DesktopName
.Buffer
, DesktopName
.Length
);
1357 if (! NT_SUCCESS(Status
))
1359 ObDereferenceObject(WindowStation
);
1362 lpBuffer
= (PVOID
) ((PCHAR
)lpBuffer
+ DesktopName
.Length
);
1363 Status
= MmCopyToCaller(lpBuffer
, &NullWchar
, sizeof(WCHAR
));
1364 if (! NT_SUCCESS(Status
))
1366 ObDereferenceObject(WindowStation
);
1369 lpBuffer
= (PVOID
) ((PCHAR
) lpBuffer
+ sizeof(WCHAR
));
1373 * Clean up and return
1375 ObDereferenceObject(WindowStation
);
1376 return STATUS_SUCCESS
;
1380 * NtUserBuildNameList
1382 * Function used for enumeration of desktops or window stations.
1386 * For enumeration of window stations this parameter must be set to
1387 * zero. Otherwise it's handle for window station.
1390 * Size of buffer passed by caller.
1393 * Buffer passed by caller. If the function succeeds, the buffer is
1394 * filled with window station/desktop count (in first DWORD) and
1395 * NULL-terminated window station/desktop names.
1398 * If the function succeeds, this is the number of bytes copied.
1399 * Otherwise it's size of buffer needed for function to succeed.
1406 NtUserBuildNameList(
1407 HWINSTA hWindowStation
,
1410 PULONG pRequiredSize
)
1412 /* The WindowStation name list and desktop name list are build in completely
1413 different ways. Call the appropriate function */
1414 return NULL
== hWindowStation
? BuildWindowStationNameList(dwSize
, lpBuffer
, pRequiredSize
) :
1415 BuildDesktopNameList(hWindowStation
, dwSize
, lpBuffer
, pRequiredSize
);
1422 NtUserSetLogonNotifyWindow(HWND hWnd
)
1424 if (gpidLogon
!= PsGetCurrentProcessId())
1429 if (!IntIsWindow(hWnd
))
1441 NtUserLockWorkStation(VOID
)
1444 PTHREADINFO pti
= PsGetCurrentThreadWin32Thread();
1446 UserEnterExclusive();
1448 if (pti
->rpdesk
== IntGetActiveDesktop())
1450 ret
= UserPostMessage(hwndSAS
, WM_LOGONNOTIFY
, LN_LOCK_WORKSTATION
, 0);
1463 NtUserSetWindowStationUser(
1464 HWINSTA hWindowStation
,
1470 PWINSTATION_OBJECT WindowStation
= NULL
;
1473 UserEnterExclusive();
1475 if (gpidLogon
!= PsGetCurrentProcessId())
1477 EngSetLastError(ERROR_ACCESS_DENIED
);
1481 Status
= IntValidateWindowStationHandle(hWindowStation
,
1486 if (!NT_SUCCESS(Status
))
1491 if (WindowStation
->psidUser
)
1493 ExFreePoolWithTag(WindowStation
->psidUser
, USERTAG_SECURITY
);
1496 WindowStation
->psidUser
= ExAllocatePoolWithTag(PagedPool
, size
, USERTAG_SECURITY
);
1497 if (WindowStation
->psidUser
== NULL
)
1499 EngSetLastError(ERROR_OUTOFMEMORY
);
1505 ProbeForRead( psid
, size
, 1);
1506 ProbeForRead( pluid
, sizeof(LUID
), 1);
1508 RtlCopyMemory(WindowStation
->psidUser
, psid
, size
);
1509 WindowStation
->luidUser
= *pluid
;
1511 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
1513 Status
= _SEH2_GetExceptionCode();
1517 if (!NT_SUCCESS(Status
))
1519 ExFreePoolWithTag(WindowStation
->psidUser
, USERTAG_SECURITY
);
1520 WindowStation
->psidUser
= 0;
1527 if (WindowStation
) ObDereferenceObject(WindowStation
);