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()
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(
234 ExWindowStationObjectType
,
239 if (!NT_SUCCESS(Status
))
240 SetLastNtError(Status
);
246 co_IntInitializeDesktopGraphics(VOID
)
249 UNICODE_STRING DriverName
= RTL_CONSTANT_STRING(L
"DISPLAY");
252 ScreenDeviceContext
= IntGdiCreateDC(&DriverName
, NULL
, NULL
, NULL
, FALSE
);
253 if (NULL
== ScreenDeviceContext
)
255 IntDestroyPrimarySurface();
258 GreSetDCOwner(ScreenDeviceContext
, GDI_OBJ_HMGR_PUBLIC
);
260 if (! IntCreatePrimarySurface())
265 hSystemBM
= NtGdiCreateCompatibleDC(ScreenDeviceContext
);
267 NtGdiSelectFont(hSystemBM
, NtGdiGetStockObject(SYSTEM_FONT
));
268 GreSetDCOwner(hSystemBM
, GDI_OBJ_HMGR_PUBLIC
);
270 /* Update the SERVERINFO */
271 gpsi
->aiSysMet
[SM_CXSCREEN
] = gppdevPrimary
->gdiinfo
.ulHorzRes
;
272 gpsi
->aiSysMet
[SM_CYSCREEN
] = gppdevPrimary
->gdiinfo
.ulVertRes
;
273 gpsi
->Planes
= NtGdiGetDeviceCaps(ScreenDeviceContext
, PLANES
);
274 gpsi
->BitsPixel
= NtGdiGetDeviceCaps(ScreenDeviceContext
, BITSPIXEL
);
275 gpsi
->BitCount
= gpsi
->Planes
* gpsi
->BitsPixel
;
276 gpsi
->dmLogPixels
= NtGdiGetDeviceCaps(ScreenDeviceContext
, LOGPIXELSY
);
277 if (NtGdiGetDeviceCaps(ScreenDeviceContext
, RASTERCAPS
) & RC_PALETTE
)
279 gpsi
->PUSIFlags
|= PUSIF_PALETTEDISPLAY
;
282 gpsi
->PUSIFlags
&= ~PUSIF_PALETTEDISPLAY
;
283 // Font is realized and this dc was previously set to internal DC_ATTR.
284 gpsi
->cxSysFontChar
= IntGetCharDimensions(hSystemBM
, &tmw
, (DWORD
*)&gpsi
->cySysFontChar
);
285 gpsi
->tmSysFont
= tmw
;
287 /* Put the pointer in the center of the screen */
288 gpsi
->ptCursor
.x
= gpsi
->aiSysMet
[SM_CXSCREEN
] / 2;
289 gpsi
->ptCursor
.y
= gpsi
->aiSysMet
[SM_CYSCREEN
] / 2;
292 UserAttachMonitor((HDEV
)gppdevPrimary
);
294 /* Setup the cursor */
295 co_IntLoadDefaultCursors();
297 /* Setup the icons */
303 /* Show the desktop */
304 pdesk
= IntGetActiveDesktop();
306 co_IntShowDesktop(pdesk
, gpsi
->aiSysMet
[SM_CXSCREEN
], gpsi
->aiSysMet
[SM_CYSCREEN
], TRUE
);
312 IntEndDesktopGraphics(VOID
)
314 if (NULL
!= ScreenDeviceContext
)
315 { // No need to allocate a new dcattr.
316 GreSetDCOwner(ScreenDeviceContext
, GDI_OBJ_HMGR_POWNED
);
317 GreDeleteObject(ScreenDeviceContext
);
318 ScreenDeviceContext
= NULL
;
320 IntHideDesktop(IntGetActiveDesktop());
321 IntDestroyPrimarySurface();
327 return ScreenDeviceContext
;
331 CheckWinstaAttributeAccess(ACCESS_MASK DesiredAccess
)
333 PPROCESSINFO ppi
= PsGetCurrentProcessWin32Process();
334 if ( gpidLogon
!= PsGetCurrentProcessId() )
336 if (!(ppi
->W32PF_flags
& W32PF_IOWINSTA
))
338 ERR("Requires Interactive Window Station\n");
339 EngSetLastError(ERROR_REQUIRES_INTERACTIVE_WINDOWSTATION
);
342 if (!RtlAreAllAccessesGranted(ppi
->amwinsta
, DesiredAccess
))
344 ERR("Access Denied\n");
345 EngSetLastError(ERROR_ACCESS_DENIED
);
353 /* PUBLIC FUNCTIONS ***********************************************************/
356 * NtUserCreateWindowStation
358 * Creates a new window station.
361 * lpszWindowStationName
362 * Pointer to a null-terminated string specifying the name of the
363 * window station to be created. Window station names are
364 * case-insensitive and cannot contain backslash characters (\).
365 * Only members of the Administrators group are allowed to specify a
369 * Requested type of access
372 * Security descriptor
374 * Unknown3, Unknown4, Unknown5
378 * If the function succeeds, the return value is a handle to the newly
379 * created window station. If the specified window station already
380 * exists, the function succeeds and returns a handle to the existing
381 * window station. If the function fails, the return value is NULL.
384 * Correct the prototype to match the Windows one (with 7 parameters
392 NtUserCreateWindowStation(
393 POBJECT_ATTRIBUTES ObjectAttributes
,
394 ACCESS_MASK dwDesiredAccess
,
401 UNICODE_STRING WindowStationName
;
402 PWINSTATION_OBJECT WindowStationObject
;
403 HWINSTA WindowStation
;
406 TRACE("NtUserCreateWindowStation called\n");
408 Status
= ObOpenObjectByName(
410 ExWindowStationObjectType
,
415 (PVOID
*)&WindowStation
);
417 if (NT_SUCCESS(Status
))
419 TRACE("NtUserCreateWindowStation opened window station %wZ\n", ObjectAttributes
->ObjectName
);
420 return (HWINSTA
)WindowStation
;
424 * No existing window station found, try to create new one
427 /* Capture window station name */
430 ProbeForRead( ObjectAttributes
, sizeof(OBJECT_ATTRIBUTES
), 1);
431 Status
= IntSafeCopyUnicodeStringTerminateNULL(&WindowStationName
, ObjectAttributes
->ObjectName
);
433 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
435 Status
=_SEH2_GetExceptionCode();
439 if (! NT_SUCCESS(Status
))
441 ERR("Failed reading capturing window station name\n");
442 SetLastNtError(Status
);
446 /* Create the window station object */
447 Status
= ObCreateObject(
449 ExWindowStationObjectType
,
453 sizeof(WINSTATION_OBJECT
),
456 (PVOID
*)&WindowStationObject
);
458 if (!NT_SUCCESS(Status
))
460 ERR("ObCreateObject failed for window station %wZ\n", &WindowStationName
);
461 ExFreePoolWithTag(WindowStationName
.Buffer
, TAG_STRING
);
462 SetLastNtError(STATUS_INSUFFICIENT_RESOURCES
);
466 Status
= ObInsertObject(
467 (PVOID
)WindowStationObject
,
472 (PVOID
*)&WindowStation
);
474 if (!NT_SUCCESS(Status
))
476 ERR("ObInsertObject failed for window station %wZ\n", &WindowStationName
);
477 ExFreePoolWithTag(WindowStationName
.Buffer
, TAG_STRING
);
478 SetLastNtError(STATUS_INSUFFICIENT_RESOURCES
);
479 ObDereferenceObject(WindowStationObject
);
483 /* Initialize the window station */
484 RtlZeroMemory(WindowStationObject
, sizeof(WINSTATION_OBJECT
));
486 InitializeListHead(&WindowStationObject
->DesktopListHead
);
487 Status
= RtlCreateAtomTable(37, &WindowStationObject
->AtomTable
);
488 WindowStationObject
->Name
= WindowStationName
;
489 WindowStationObject
->dwSessionId
= NtCurrentPeb()->SessionId
;
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(
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(
607 if (!NT_SUCCESS(Status
))
609 ERR("Validation of window station handle (%p) failed\n", hWinSta
);
613 ObDereferenceObject(Object
);
615 TRACE("Closing window station handle (%p)\n", hWinSta
);
617 Status
= ObCloseHandle(hWinSta
, UserMode
);
618 if (!NT_SUCCESS(Status
))
620 SetLastNtError(Status
);
628 * NtUserGetObjectInformation
630 * The NtUserGetObjectInformation function retrieves information about a
631 * window station or desktop object.
635 * Handle to the window station or desktop object for which to
636 * return information. This can be a handle of type HDESK or HWINSTA
637 * (for example, a handle returned by NtUserCreateWindowStation,
638 * NtUserOpenWindowStation, NtUserCreateDesktop, or NtUserOpenDesktop).
641 * Specifies the object information to be retrieved.
644 * Pointer to a buffer to receive the object information.
647 * Specifies the size, in bytes, of the buffer pointed to by the
651 * Pointer to a variable receiving the number of bytes required to
652 * store the requested information. If this variable's value is
653 * greater than the value of the nLength parameter when the function
654 * returns, the function returns FALSE, and none of the information
655 * is copied to the pvInfo buffer. If the value of the variable pointed
656 * to by lpnLengthNeeded is less than or equal to the value of nLength,
657 * the entire information block is copied.
660 * If the function succeeds, the return value is nonzero. If the function
661 * fails, the return value is zero.
668 NtUserGetObjectInformation(
673 PDWORD nLengthNeeded
)
675 PWINSTATION_OBJECT WinStaObject
;
676 PDESKTOP DesktopObject
= NULL
;
684 ProbeForWrite(nLengthNeeded
, sizeof(*nLengthNeeded
), 1);
685 ProbeForWrite(pvInformation
, nLength
, 1);
687 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
689 SetLastNtError(_SEH2_GetExceptionCode());
694 /* try windowstation */
695 TRACE("Trying to open window station %p\n", hObject
);
696 Status
= ObReferenceObjectByHandle(
699 ExWindowStationObjectType
,
701 (PVOID
*)&WinStaObject
,
704 if (Status
== STATUS_OBJECT_TYPE_MISMATCH
)
707 TRACE("Trying to open desktop %p\n", hObject
);
709 Status
= IntValidateDesktopHandle(
716 if (!NT_SUCCESS(Status
))
718 ERR("Failed: 0x%x\n", Status
);
722 TRACE("WinSta or Desktop opened!!\n");
728 Status
= STATUS_NOT_IMPLEMENTED
;
729 ERR("UOI_FLAGS unimplemented!\n");
733 if (WinStaObject
!= NULL
)
735 pvData
= WinStaObject
->Name
.Buffer
;
736 nDataSize
= WinStaObject
->Name
.Length
+ sizeof(WCHAR
);
737 Status
= STATUS_SUCCESS
;
739 else if (DesktopObject
!= NULL
)
741 pvData
= DesktopObject
->pDeskInfo
->szDesktopName
;
742 nDataSize
= (wcslen(DesktopObject
->pDeskInfo
->szDesktopName
) + 1) * sizeof(WCHAR
);
743 Status
= STATUS_SUCCESS
;
746 Status
= STATUS_INVALID_PARAMETER
;
750 if (WinStaObject
!= NULL
)
752 pvData
= L
"WindowStation";
753 nDataSize
= sizeof(L
"WindowStation");
754 Status
= STATUS_SUCCESS
;
756 else if (DesktopObject
!= NULL
)
759 nDataSize
= sizeof(L
"Desktop");
760 Status
= STATUS_SUCCESS
;
763 Status
= STATUS_INVALID_PARAMETER
;
767 Status
= STATUS_NOT_IMPLEMENTED
;
768 ERR("UOI_USER_SID unimplemented!\n");
772 Status
= STATUS_INVALID_PARAMETER
;
777 if (Status
== STATUS_SUCCESS
&& nLength
< nDataSize
)
778 Status
= STATUS_BUFFER_TOO_SMALL
;
783 *nLengthNeeded
= nDataSize
;
785 /* try to copy data to caller */
786 if (Status
== STATUS_SUCCESS
)
788 TRACE("Trying to copy data to caller (len = %lu, len needed = %lu)\n", nLength
, nDataSize
);
789 RtlCopyMemory(pvInformation
, pvData
, nDataSize
);
792 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
794 Status
= _SEH2_GetExceptionCode();
798 /* release objects */
799 if (WinStaObject
!= NULL
)
800 ObDereferenceObject(WinStaObject
);
801 if (DesktopObject
!= NULL
)
802 ObDereferenceObject(DesktopObject
);
804 if (!NT_SUCCESS(Status
))
806 SetLastNtError(Status
);
814 * NtUserSetObjectInformation
816 * The NtUserSetObjectInformation function sets information about a
817 * window station or desktop object.
821 * Handle to the window station or desktop object for which to set
822 * object information. This value can be a handle of type HDESK or
826 * Specifies the object information to be set.
829 * Pointer to a buffer containing the object information.
832 * Specifies the size, in bytes, of the information contained in the
833 * buffer pointed to by pvInfo.
836 * If the function succeeds, the return value is nonzero. If the function
837 * fails the return value is zero.
845 NtUserSetObjectInformation(
851 /* FIXME: ZwQueryObject */
852 /* FIXME: ZwSetInformationObject */
853 SetLastNtError(STATUS_UNSUCCESSFUL
);
861 UserGetProcessWindowStation(VOID
)
863 PPROCESSINFO ppi
= PsGetCurrentProcessWin32Process();
870 * NtUserGetProcessWindowStation
872 * Returns a handle to the current process window station.
875 * If the function succeeds, the return value is handle to the window
876 * station assigned to the current process. If the function fails, the
877 * return value is NULL.
884 NtUserGetProcessWindowStation(VOID
)
886 return UserGetProcessWindowStation();
890 UserSetProcessWindowStation(HWINSTA hWindowStation
)
895 OBJECT_HANDLE_INFORMATION ObjectHandleInfo
;
896 PWINSTATION_OBJECT NewWinSta
= NULL
, OldWinSta
;
898 ppi
= PsGetCurrentProcessWin32Process();
900 /* Reference the new window station */
901 if(hWindowStation
!=NULL
)
903 Status
= IntValidateWindowStationHandle( hWindowStation
,
908 if (!NT_SUCCESS(Status
))
910 TRACE("Validation of window station handle (%p) failed\n",
912 SetLastNtError(Status
);
917 OldWinSta
= ppi
->prpwinsta
;
918 hwinstaOld
= PsGetProcessWin32WindowStation(ppi
->peProcess
);
920 /* Dereference the previous window station */
921 if(OldWinSta
!= NULL
)
923 ObDereferenceObject(OldWinSta
);
926 /* Check if we have a stale handle (it should happen for console apps) */
927 if(hwinstaOld
!= ppi
->hwinsta
)
929 ObCloseHandle(hwinstaOld
, UserMode
);
933 * FIXME: Don't allow changing the window station if there are threads that are attached to desktops and own GUI objects.
936 PsSetProcessWindowStation(ppi
->peProcess
, hWindowStation
);
938 ppi
->prpwinsta
= NewWinSta
;
939 ppi
->hwinsta
= hWindowStation
;
940 ppi
->amwinsta
= hWindowStation
!= NULL
? ObjectHandleInfo
.GrantedAccess
: 0;
941 TRACE("WS : Granted Access 0x%08lx\n",ppi
->amwinsta
);
943 if (RtlAreAllAccessesGranted(ppi
->amwinsta
, WINSTA_READSCREEN
))
945 ppi
->W32PF_flags
|= W32PF_READSCREENACCESSGRANTED
;
949 ppi
->W32PF_flags
&= ~W32PF_READSCREENACCESSGRANTED
;
952 if (NewWinSta
&& !(NewWinSta
->Flags
& WSS_NOIO
) )
954 ppi
->W32PF_flags
|= W32PF_IOWINSTA
;
956 else // Might be closed if the handle is null.
958 ppi
->W32PF_flags
&= ~W32PF_IOWINSTA
;
964 * NtUserSetProcessWindowStation
966 * Assigns a window station to the current process.
970 * Handle to the window station.
980 NtUserSetProcessWindowStation(HWINSTA hWindowStation
)
984 UserEnterExclusive();
986 ret
= UserSetProcessWindowStation(hWindowStation
);
994 * NtUserLockWindowStation
996 * Locks switching desktops. Only the logon application is allowed to call this function.
1003 NtUserLockWindowStation(HWINSTA hWindowStation
)
1005 PWINSTATION_OBJECT Object
;
1008 TRACE("About to set process window station with handle (%p)\n",
1011 if (gpidLogon
!= PsGetCurrentProcessId())
1013 ERR("Unauthorized process attempted to lock the window station!\n");
1014 EngSetLastError(ERROR_ACCESS_DENIED
);
1018 Status
= IntValidateWindowStationHandle(
1024 if (!NT_SUCCESS(Status
))
1026 TRACE("Validation of window station handle (%p) failed\n",
1028 SetLastNtError(Status
);
1032 Object
->Flags
|= WSS_LOCKED
;
1034 ObDereferenceObject(Object
);
1039 * NtUserUnlockWindowStation
1041 * Unlocks switching desktops. Only the logon application is allowed to call this function.
1048 NtUserUnlockWindowStation(HWINSTA hWindowStation
)
1050 PWINSTATION_OBJECT Object
;
1054 TRACE("About to set process window station with handle (%p)\n",
1057 if (gpidLogon
!= PsGetCurrentProcessId())
1059 ERR("Unauthorized process attempted to unlock the window station!\n");
1060 EngSetLastError(ERROR_ACCESS_DENIED
);
1064 Status
= IntValidateWindowStationHandle(
1070 if (!NT_SUCCESS(Status
))
1072 TRACE("Validation of window station handle (%p) failed\n",
1074 SetLastNtError(Status
);
1078 Ret
= (Object
->Flags
& WSS_LOCKED
) == WSS_LOCKED
;
1079 Object
->Flags
&= ~WSS_LOCKED
;
1081 ObDereferenceObject(Object
);
1085 static NTSTATUS FASTCALL
1086 BuildWindowStationNameList(
1089 PULONG pRequiredSize
)
1091 OBJECT_ATTRIBUTES ObjectAttributes
;
1093 HANDLE DirectoryHandle
;
1094 char InitialBuffer
[256], *Buffer
;
1095 ULONG Context
, ReturnLength
, BufferSize
;
1097 POBJECT_DIRECTORY_INFORMATION DirEntry
;
1101 * Try to open the directory.
1103 InitializeObjectAttributes(
1105 &gustrWindowStationsDir
,
1106 OBJ_CASE_INSENSITIVE
,
1110 Status
= ZwOpenDirectoryObject(
1115 if (!NT_SUCCESS(Status
))
1120 /* First try to query the directory using a fixed-size buffer */
1123 Status
= ZwQueryDirectoryObject(DirectoryHandle
, InitialBuffer
, sizeof(InitialBuffer
),
1124 FALSE
, TRUE
, &Context
, &ReturnLength
);
1125 if (NT_SUCCESS(Status
))
1127 if (STATUS_NO_MORE_ENTRIES
== ZwQueryDirectoryObject(DirectoryHandle
, NULL
, 0, FALSE
,
1128 FALSE
, &Context
, NULL
))
1130 /* Our fixed-size buffer is large enough */
1131 Buffer
= InitialBuffer
;
1137 /* Need a larger buffer, check how large exactly */
1138 Status
= ZwQueryDirectoryObject(DirectoryHandle
, NULL
, 0, FALSE
, TRUE
, &Context
,
1140 if (!NT_SUCCESS(Status
))
1142 ERR("ZwQueryDirectoryObject failed\n");
1143 ObDereferenceObject(DirectoryHandle
);
1147 BufferSize
= ReturnLength
;
1148 Buffer
= ExAllocatePoolWithTag(PagedPool
, BufferSize
, TAG_WINSTA
);
1151 ObDereferenceObject(DirectoryHandle
);
1152 return STATUS_NO_MEMORY
;
1155 /* We should have a sufficiently large buffer now */
1157 Status
= ZwQueryDirectoryObject(DirectoryHandle
, Buffer
, BufferSize
,
1158 FALSE
, TRUE
, &Context
, &ReturnLength
);
1159 if (! NT_SUCCESS(Status
) ||
1160 STATUS_NO_MORE_ENTRIES
!= ZwQueryDirectoryObject(DirectoryHandle
, NULL
, 0, FALSE
,
1161 FALSE
, &Context
, NULL
))
1163 /* Something went wrong, maybe someone added a directory entry? Just give up. */
1164 ExFreePoolWithTag(Buffer
, TAG_WINSTA
);
1165 ObDereferenceObject(DirectoryHandle
);
1166 return NT_SUCCESS(Status
) ? STATUS_INTERNAL_ERROR
: Status
;
1170 ZwClose(DirectoryHandle
);
1173 * Count the required size of buffer.
1175 ReturnLength
= sizeof(DWORD
);
1177 for (DirEntry
= (POBJECT_DIRECTORY_INFORMATION
) Buffer
; 0 != DirEntry
->Name
.Length
;
1180 ReturnLength
+= DirEntry
->Name
.Length
+ sizeof(WCHAR
);
1183 TRACE("Required size: %lu Entry count: %lu\n", ReturnLength
, EntryCount
);
1184 if (NULL
!= pRequiredSize
)
1186 Status
= MmCopyToCaller(pRequiredSize
, &ReturnLength
, sizeof(ULONG
));
1187 if (! NT_SUCCESS(Status
))
1189 if (Buffer
!= InitialBuffer
)
1191 ExFreePoolWithTag(Buffer
, TAG_WINSTA
);
1193 return STATUS_BUFFER_TOO_SMALL
;
1198 * Check if the supplied buffer is large enough.
1200 if (dwSize
< ReturnLength
)
1202 if (Buffer
!= InitialBuffer
)
1204 ExFreePoolWithTag(Buffer
, TAG_WINSTA
);
1206 return STATUS_BUFFER_TOO_SMALL
;
1210 * Generate the resulting buffer contents.
1212 Status
= MmCopyToCaller(lpBuffer
, &EntryCount
, sizeof(DWORD
));
1213 if (! NT_SUCCESS(Status
))
1215 if (Buffer
!= InitialBuffer
)
1217 ExFreePoolWithTag(Buffer
, TAG_WINSTA
);
1221 lpBuffer
= (PVOID
) ((PCHAR
) lpBuffer
+ sizeof(DWORD
));
1224 for (DirEntry
= (POBJECT_DIRECTORY_INFORMATION
) Buffer
; 0 != DirEntry
->Name
.Length
;
1227 Status
= MmCopyToCaller(lpBuffer
, DirEntry
->Name
.Buffer
, DirEntry
->Name
.Length
);
1228 if (! NT_SUCCESS(Status
))
1230 if (Buffer
!= InitialBuffer
)
1232 ExFreePoolWithTag(Buffer
, TAG_WINSTA
);
1236 lpBuffer
= (PVOID
) ((PCHAR
) lpBuffer
+ DirEntry
->Name
.Length
);
1237 Status
= MmCopyToCaller(lpBuffer
, &NullWchar
, sizeof(WCHAR
));
1238 if (! NT_SUCCESS(Status
))
1240 if (Buffer
!= InitialBuffer
)
1242 ExFreePoolWithTag(Buffer
, TAG_WINSTA
);
1246 lpBuffer
= (PVOID
) ((PCHAR
) lpBuffer
+ sizeof(WCHAR
));
1252 if (Buffer
!= InitialBuffer
)
1254 ExFreePoolWithTag(Buffer
, TAG_WINSTA
);
1257 return STATUS_SUCCESS
;
1260 static NTSTATUS FASTCALL
1261 BuildDesktopNameList(
1262 HWINSTA hWindowStation
,
1265 PULONG pRequiredSize
)
1268 PWINSTATION_OBJECT WindowStation
;
1269 PLIST_ENTRY DesktopEntry
;
1270 PDESKTOP DesktopObject
;
1274 UNICODE_STRING DesktopName
;
1276 Status
= IntValidateWindowStationHandle(hWindowStation
,
1281 if (! NT_SUCCESS(Status
))
1287 * Count the required size of buffer.
1289 ReturnLength
= sizeof(DWORD
);
1291 for (DesktopEntry
= WindowStation
->DesktopListHead
.Flink
;
1292 DesktopEntry
!= &WindowStation
->DesktopListHead
;
1293 DesktopEntry
= DesktopEntry
->Flink
)
1295 DesktopObject
= CONTAINING_RECORD(DesktopEntry
, DESKTOP
, ListEntry
);
1296 RtlInitUnicodeString(&DesktopName
, DesktopObject
->pDeskInfo
->szDesktopName
);
1297 ReturnLength
+= DesktopName
.Length
+ sizeof(WCHAR
);
1300 TRACE("Required size: %lu Entry count: %lu\n", ReturnLength
, EntryCount
);
1301 if (NULL
!= pRequiredSize
)
1303 Status
= MmCopyToCaller(pRequiredSize
, &ReturnLength
, sizeof(ULONG
));
1304 if (! NT_SUCCESS(Status
))
1306 ObDereferenceObject(WindowStation
);
1307 return STATUS_BUFFER_TOO_SMALL
;
1312 * Check if the supplied buffer is large enough.
1314 if (dwSize
< ReturnLength
)
1316 ObDereferenceObject(WindowStation
);
1317 return STATUS_BUFFER_TOO_SMALL
;
1321 * Generate the resulting buffer contents.
1323 Status
= MmCopyToCaller(lpBuffer
, &EntryCount
, sizeof(DWORD
));
1324 if (! NT_SUCCESS(Status
))
1326 ObDereferenceObject(WindowStation
);
1329 lpBuffer
= (PVOID
) ((PCHAR
) lpBuffer
+ sizeof(DWORD
));
1332 for (DesktopEntry
= WindowStation
->DesktopListHead
.Flink
;
1333 DesktopEntry
!= &WindowStation
->DesktopListHead
;
1334 DesktopEntry
= DesktopEntry
->Flink
)
1336 DesktopObject
= CONTAINING_RECORD(DesktopEntry
, DESKTOP
, ListEntry
);
1337 RtlInitUnicodeString(&DesktopName
, DesktopObject
->pDeskInfo
->szDesktopName
);
1338 Status
= MmCopyToCaller(lpBuffer
, DesktopName
.Buffer
, DesktopName
.Length
);
1339 if (! NT_SUCCESS(Status
))
1341 ObDereferenceObject(WindowStation
);
1344 lpBuffer
= (PVOID
) ((PCHAR
)lpBuffer
+ DesktopName
.Length
);
1345 Status
= MmCopyToCaller(lpBuffer
, &NullWchar
, sizeof(WCHAR
));
1346 if (! NT_SUCCESS(Status
))
1348 ObDereferenceObject(WindowStation
);
1351 lpBuffer
= (PVOID
) ((PCHAR
) lpBuffer
+ sizeof(WCHAR
));
1355 * Clean up and return
1357 ObDereferenceObject(WindowStation
);
1358 return STATUS_SUCCESS
;
1362 * NtUserBuildNameList
1364 * Function used for enumeration of desktops or window stations.
1368 * For enumeration of window stations this parameter must be set to
1369 * zero. Otherwise it's handle for window station.
1372 * Size of buffer passed by caller.
1375 * Buffer passed by caller. If the function succeeds, the buffer is
1376 * filled with window station/desktop count (in first DWORD) and
1377 * NULL-terminated window station/desktop names.
1380 * If the function succeeds, this is the number of bytes copied.
1381 * Otherwise it's size of buffer needed for function to succeed.
1388 NtUserBuildNameList(
1389 HWINSTA hWindowStation
,
1392 PULONG pRequiredSize
)
1394 /* The WindowStation name list and desktop name list are build in completely
1395 different ways. Call the appropriate function */
1396 return NULL
== hWindowStation
? BuildWindowStationNameList(dwSize
, lpBuffer
, pRequiredSize
) :
1397 BuildDesktopNameList(hWindowStation
, dwSize
, lpBuffer
, pRequiredSize
);
1404 NtUserSetLogonNotifyWindow(HWND hWnd
)
1406 if (gpidLogon
!= PsGetCurrentProcessId())
1411 if (!IntIsWindow(hWnd
))
1423 NtUserLockWorkStation(VOID
)
1426 PTHREADINFO pti
= PsGetCurrentThreadWin32Thread();
1428 UserEnterExclusive();
1430 if (pti
->rpdesk
== IntGetActiveDesktop())
1432 ret
= UserPostMessage(hwndSAS
, WM_LOGONNOTIFY
, LN_LOCK_WORKSTATION
, 0);
1445 NtUserSetWindowStationUser(
1446 HWINSTA hWindowStation
,
1452 PWINSTATION_OBJECT WindowStation
= NULL
;
1455 UserEnterExclusive();
1457 if (gpidLogon
!= PsGetCurrentProcessId())
1459 EngSetLastError(ERROR_ACCESS_DENIED
);
1463 Status
= IntValidateWindowStationHandle(hWindowStation
,
1468 if (!NT_SUCCESS(Status
))
1473 if (WindowStation
->psidUser
)
1475 ExFreePoolWithTag(WindowStation
->psidUser
, USERTAG_SECURITY
);
1478 WindowStation
->psidUser
= ExAllocatePoolWithTag(PagedPool
, size
, USERTAG_SECURITY
);
1479 if (WindowStation
->psidUser
== NULL
)
1481 EngSetLastError(ERROR_OUTOFMEMORY
);
1487 ProbeForRead( psid
, size
, 1);
1488 ProbeForRead( pluid
, sizeof(LUID
), 1);
1490 RtlCopyMemory(WindowStation
->psidUser
, psid
, size
);
1491 WindowStation
->luidUser
= *pluid
;
1493 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
1495 Status
= _SEH2_GetExceptionCode();
1499 if (!NT_SUCCESS(Status
))
1501 ExFreePoolWithTag(WindowStation
->psidUser
, USERTAG_SECURITY
);
1502 WindowStation
->psidUser
= 0;
1509 if (WindowStation
) ObDereferenceObject(WindowStation
);