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.
391 IntCreateWindowStation(
392 OUT HWINSTA
* phWinSta
,
393 IN POBJECT_ATTRIBUTES ObjectAttributes
,
394 IN KPROCESSOR_MODE AccessMode
,
395 IN ACCESS_MASK dwDesiredAccess
,
403 HWINSTA WindowStation
;
404 PWINSTATION_OBJECT WindowStationObject
;
406 TRACE("IntCreateWindowStation called\n");
411 Status
= ObOpenObjectByName(ObjectAttributes
,
412 ExWindowStationObjectType
,
417 (PVOID
*)&WindowStation
);
418 if (NT_SUCCESS(Status
))
420 TRACE("IntCreateWindowStation opened window station %wZ\n",
421 ObjectAttributes
->ObjectName
);
422 *phWinSta
= WindowStation
;
427 * No existing window station found, try to create new one.
430 /* Create the window station object */
431 Status
= ObCreateObject(KernelMode
,
432 ExWindowStationObjectType
,
436 sizeof(WINSTATION_OBJECT
),
439 (PVOID
*)&WindowStationObject
);
440 if (!NT_SUCCESS(Status
))
442 ERR("ObCreateObject failed with %lx for window station %wZ\n",
443 Status
, ObjectAttributes
->ObjectName
);
444 SetLastNtError(Status
);
448 /* Initialize the window station */
449 RtlZeroMemory(WindowStationObject
, sizeof(WINSTATION_OBJECT
));
451 InitializeListHead(&WindowStationObject
->DesktopListHead
);
452 WindowStationObject
->Name
= *ObjectAttributes
->ObjectName
;
453 ObjectAttributes
->ObjectName
= NULL
; // FIXME! (see NtUserCreateWindowStation())
454 WindowStationObject
->dwSessionId
= NtCurrentPeb()->SessionId
;
455 Status
= RtlCreateAtomTable(37, &WindowStationObject
->AtomTable
);
456 if (!NT_SUCCESS(Status
))
458 ERR("RtlCreateAtomTable failed with %lx for window station %wZ\n", Status
, ObjectAttributes
->ObjectName
);
459 ObDereferenceObject(WindowStationObject
);
460 SetLastNtError(Status
);
464 Status
= ObInsertObject(WindowStationObject
,
469 (PVOID
*)&WindowStation
);
470 if (!NT_SUCCESS(Status
))
472 ERR("ObInsertObject failed with %lx for window station\n", Status
);
473 SetLastNtError(Status
);
477 // FIXME! TODO: Add this new window station to a linked list
479 if (InputWindowStation
== NULL
)
481 ERR("Initializing input window station\n");
482 InputWindowStation
= WindowStationObject
;
484 WindowStationObject
->Flags
&= ~WSS_NOIO
;
490 WindowStationObject
->Flags
|= WSS_NOIO
;
493 TRACE("IntCreateWindowStation created object 0x%p with name %wZ handle 0x%p\n",
494 WindowStationObject
, &WindowStationObject
->Name
, WindowStation
);
496 *phWinSta
= WindowStation
;
497 return STATUS_SUCCESS
;
502 NtUserCreateWindowStation(
503 IN POBJECT_ATTRIBUTES ObjectAttributes
,
504 IN ACCESS_MASK dwDesiredAccess
,
513 OBJECT_ATTRIBUTES LocalObjectAttributes
;
514 UNICODE_STRING WindowStationName
;
516 TRACE("NtUserCreateWindowStation called\n");
518 /* Capture window station name */
521 ProbeForRead(ObjectAttributes
, sizeof(OBJECT_ATTRIBUTES
), sizeof(ULONG
));
522 LocalObjectAttributes
= *ObjectAttributes
;
523 Status
= IntSafeCopyUnicodeStringTerminateNULL(&WindowStationName
,
524 LocalObjectAttributes
.ObjectName
);
525 LocalObjectAttributes
.ObjectName
= &WindowStationName
;
527 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
529 Status
=_SEH2_GetExceptionCode();
533 if (!NT_SUCCESS(Status
))
535 ERR("Failed reading or capturing window station name, Status 0x%08lx\n", Status
);
536 SetLastNtError(Status
);
540 // TODO: Capture and use the SecurityQualityOfService!
542 Status
= IntCreateWindowStation(&hWinSta
,
543 &LocalObjectAttributes
,
552 // FIXME! Because in some situations we store the allocated window station name
553 // inside the window station, we must not free it now! We know this fact when
554 // IntCreateWindowStation() sets LocalObjectAttributes.ObjectName to NULL.
555 // This hack must be removed once we just use the stored Ob name instead
556 // (in which case we will always free the allocated name here).
557 if (LocalObjectAttributes
.ObjectName
)
558 ExFreePoolWithTag(LocalObjectAttributes
.ObjectName
->Buffer
, TAG_STRING
);
560 if (NT_SUCCESS(Status
))
562 TRACE("NtUserCreateWindowStation created a window station with handle 0x%p\n", hWinSta
);
566 ASSERT(hWinSta
== NULL
);
567 TRACE("NtUserCreateWindowStation failed to create a window station!\n");
574 * NtUserOpenWindowStation
576 * Opens an existing window station.
579 * lpszWindowStationName
580 * Name of the existing window station.
583 * Requested type of access.
586 * If the function succeeds, the return value is the handle to the
587 * specified window station. If the function fails, the return value
591 * The returned handle can be closed with NtUserCloseWindowStation.
598 NtUserOpenWindowStation(
599 POBJECT_ATTRIBUTES ObjectAttributes
,
600 ACCESS_MASK dwDesiredAccess
)
605 Status
= ObOpenObjectByName(ObjectAttributes
,
606 ExWindowStationObjectType
,
613 if (!NT_SUCCESS(Status
))
615 ERR("NtUserOpenWindowStation failed\n");
616 SetLastNtError(Status
);
620 TRACE("Opened window station %wZ with handle %p\n", ObjectAttributes
->ObjectName
, hwinsta
);
626 * NtUserCloseWindowStation
628 * Closes a window station handle.
632 * Handle to the window station.
638 * The window station handle can be created with NtUserCreateWindowStation
639 * or NtUserOpenWindowStation. Attemps to close a handle to the window
640 * station assigned to the calling process will fail.
648 NtUserCloseWindowStation(
651 PWINSTATION_OBJECT Object
;
654 TRACE("NtUserCloseWindowStation called (%p)\n", hWinSta
);
656 if (hWinSta
== UserGetProcessWindowStation())
658 ERR("Attempted to close process window station\n");
662 Status
= IntValidateWindowStationHandle(hWinSta
,
668 if (!NT_SUCCESS(Status
))
670 ERR("Validation of window station handle (%p) failed\n", hWinSta
);
674 ObDereferenceObject(Object
);
676 TRACE("Closing window station handle (%p)\n", hWinSta
);
678 Status
= ObCloseHandle(hWinSta
, UserMode
);
679 if (!NT_SUCCESS(Status
))
681 SetLastNtError(Status
);
689 * NtUserGetObjectInformation
691 * The NtUserGetObjectInformation function retrieves information about a
692 * window station or desktop object.
696 * Handle to the window station or desktop object for which to
697 * return information. This can be a handle of type HDESK or HWINSTA
698 * (for example, a handle returned by NtUserCreateWindowStation,
699 * NtUserOpenWindowStation, NtUserCreateDesktop, or NtUserOpenDesktop).
702 * Specifies the object information to be retrieved.
705 * Pointer to a buffer to receive the object information.
708 * Specifies the size, in bytes, of the buffer pointed to by the
712 * Pointer to a variable receiving the number of bytes required to
713 * store the requested information. If this variable's value is
714 * greater than the value of the nLength parameter when the function
715 * returns, the function returns FALSE, and none of the information
716 * is copied to the pvInfo buffer. If the value of the variable pointed
717 * to by lpnLengthNeeded is less than or equal to the value of nLength,
718 * the entire information block is copied.
721 * If the function succeeds, the return value is nonzero. If the function
722 * fails, the return value is zero.
729 NtUserGetObjectInformation(
734 PDWORD nLengthNeeded
)
737 PWINSTATION_OBJECT WinStaObject
= NULL
;
738 PDESKTOP DesktopObject
= NULL
;
739 USEROBJECTFLAGS ObjectFlags
;
741 SIZE_T nDataSize
= 0;
746 ProbeForWrite(nLengthNeeded
, sizeof(*nLengthNeeded
), 1);
747 ProbeForWrite(pvInformation
, nLength
, 1);
749 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
751 SetLastNtError(_SEH2_GetExceptionCode());
756 /* Try window station */
757 TRACE("Trying to open window station %p\n", hObject
);
758 Status
= ObReferenceObjectByHandle(hObject
,
760 ExWindowStationObjectType
,
762 (PVOID
*)&WinStaObject
,
765 if (Status
== STATUS_OBJECT_TYPE_MISMATCH
)
768 TRACE("Trying to open desktop %p\n", hObject
);
770 Status
= IntValidateDesktopHandle(hObject
,
776 if (!NT_SUCCESS(Status
))
778 ERR("Failed: 0x%x\n", Status
);
782 TRACE("WinSta or Desktop opened!\n");
789 OBJECT_HANDLE_ATTRIBUTE_INFORMATION HandleInfo
;
792 ObjectFlags
.fReserved
= FALSE
;
794 /* Check whether this handle is inheritable */
795 Status
= ZwQueryObject(hObject
,
796 ObjectHandleFlagInformation
,
798 sizeof(OBJECT_HANDLE_ATTRIBUTE_INFORMATION
),
800 if (!NT_SUCCESS(Status
))
802 ERR("ZwQueryObject failed, Status 0x%08lx\n", Status
);
805 ObjectFlags
.fInherit
= HandleInfo
.Inherit
;
807 ObjectFlags
.dwFlags
= 0;
808 if (WinStaObject
!= NULL
)
810 if (!(WinStaObject
->Flags
& WSS_NOIO
))
811 ObjectFlags
.dwFlags
|= WSF_VISIBLE
;
813 else if (DesktopObject
!= NULL
)
815 FIXME("Setting DF_ALLOWOTHERACCOUNTHOOK is unimplemented.\n");
819 ERR("No associated WinStaObject nor DesktopObject!\n");
822 pvData
= &ObjectFlags
;
823 nDataSize
= sizeof(ObjectFlags
);
824 Status
= STATUS_SUCCESS
;
830 // FIXME: Use either ObQueryNameString() or read directly that name inside the Object section!
831 if (WinStaObject
!= NULL
)
833 pvData
= WinStaObject
->Name
.Buffer
;
834 nDataSize
= WinStaObject
->Name
.Length
+ sizeof(WCHAR
);
835 Status
= STATUS_SUCCESS
;
837 else if (DesktopObject
!= NULL
)
839 pvData
= DesktopObject
->pDeskInfo
->szDesktopName
;
840 nDataSize
= (wcslen(DesktopObject
->pDeskInfo
->szDesktopName
) + 1) * sizeof(WCHAR
);
841 Status
= STATUS_SUCCESS
;
845 Status
= STATUS_INVALID_PARAMETER
;
852 if (WinStaObject
!= NULL
)
854 pvData
= L
"WindowStation";
855 nDataSize
= sizeof(L
"WindowStation");
856 Status
= STATUS_SUCCESS
;
858 else if (DesktopObject
!= NULL
)
861 nDataSize
= sizeof(L
"Desktop");
862 Status
= STATUS_SUCCESS
;
866 Status
= STATUS_INVALID_PARAMETER
;
872 Status
= STATUS_NOT_IMPLEMENTED
;
873 ERR("UOI_USER_SID unimplemented!\n");
877 Status
= STATUS_INVALID_PARAMETER
;
882 if ((Status
== STATUS_SUCCESS
) && (nLength
< nDataSize
))
883 Status
= STATUS_BUFFER_TOO_SMALL
;
888 *nLengthNeeded
= nDataSize
;
890 /* Try to copy data to caller */
891 if (Status
== STATUS_SUCCESS
)
893 TRACE("Trying to copy data to caller (len = %lu, len needed = %lu)\n", nLength
, nDataSize
);
894 RtlCopyMemory(pvInformation
, pvData
, nDataSize
);
897 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
899 Status
= _SEH2_GetExceptionCode();
903 /* Release objects */
904 if (DesktopObject
!= NULL
)
905 ObDereferenceObject(DesktopObject
);
906 if (WinStaObject
!= NULL
)
907 ObDereferenceObject(WinStaObject
);
909 if (!NT_SUCCESS(Status
))
911 SetLastNtError(Status
);
919 * NtUserSetObjectInformation
921 * The NtUserSetObjectInformation function sets information about a
922 * window station or desktop object.
926 * Handle to the window station or desktop object for which to set
927 * object information. This value can be a handle of type HDESK or
931 * Specifies the object information to be set.
934 * Pointer to a buffer containing the object information.
937 * Specifies the size, in bytes, of the information contained in the
938 * buffer pointed to by pvInfo.
941 * If the function succeeds, the return value is nonzero. If the function
942 * fails the return value is zero.
950 NtUserSetObjectInformation(
956 /* FIXME: ZwQueryObject */
957 /* FIXME: ZwSetInformationObject */
958 SetLastNtError(STATUS_UNSUCCESSFUL
);
966 UserGetProcessWindowStation(VOID
)
968 PPROCESSINFO ppi
= PsGetCurrentProcessWin32Process();
975 * NtUserGetProcessWindowStation
977 * Returns a handle to the current process window station.
980 * If the function succeeds, the return value is handle to the window
981 * station assigned to the current process. If the function fails, the
982 * return value is NULL.
989 NtUserGetProcessWindowStation(VOID
)
991 return UserGetProcessWindowStation();
995 UserSetProcessWindowStation(HWINSTA hWindowStation
)
1000 OBJECT_HANDLE_INFORMATION ObjectHandleInfo
;
1001 PWINSTATION_OBJECT NewWinSta
= NULL
, OldWinSta
;
1003 ppi
= PsGetCurrentProcessWin32Process();
1005 /* Reference the new window station */
1006 if (hWindowStation
!= NULL
)
1008 Status
= IntValidateWindowStationHandle(hWindowStation
,
1013 if (!NT_SUCCESS(Status
))
1015 TRACE("Validation of window station handle (%p) failed\n",
1017 SetLastNtError(Status
);
1022 OldWinSta
= ppi
->prpwinsta
;
1023 hwinstaOld
= PsGetProcessWin32WindowStation(ppi
->peProcess
);
1025 /* Dereference the previous window station */
1026 if (OldWinSta
!= NULL
)
1028 ObDereferenceObject(OldWinSta
);
1031 /* Check if we have a stale handle (it should happen for console apps) */
1032 if (hwinstaOld
!= ppi
->hwinsta
)
1034 ObCloseHandle(hwinstaOld
, UserMode
);
1038 * FIXME: Don't allow changing the window station if there are threads that are attached to desktops and own GUI objects.
1041 PsSetProcessWindowStation(ppi
->peProcess
, hWindowStation
);
1043 ppi
->prpwinsta
= NewWinSta
;
1044 ppi
->hwinsta
= hWindowStation
;
1045 ppi
->amwinsta
= hWindowStation
!= NULL
? ObjectHandleInfo
.GrantedAccess
: 0;
1046 TRACE("WS : Granted Access 0x%08lx\n",ppi
->amwinsta
);
1048 if (RtlAreAllAccessesGranted(ppi
->amwinsta
, WINSTA_READSCREEN
))
1050 ppi
->W32PF_flags
|= W32PF_READSCREENACCESSGRANTED
;
1054 ppi
->W32PF_flags
&= ~W32PF_READSCREENACCESSGRANTED
;
1057 if (NewWinSta
&& !(NewWinSta
->Flags
& WSS_NOIO
))
1059 ppi
->W32PF_flags
|= W32PF_IOWINSTA
;
1061 else // Might be closed if the handle is null.
1063 ppi
->W32PF_flags
&= ~W32PF_IOWINSTA
;
1069 * NtUserSetProcessWindowStation
1071 * Assigns a window station to the current process.
1075 * Handle to the window station.
1085 NtUserSetProcessWindowStation(HWINSTA hWindowStation
)
1089 UserEnterExclusive();
1091 ret
= UserSetProcessWindowStation(hWindowStation
);
1099 * NtUserLockWindowStation
1101 * Locks switching desktops. Only the logon application is allowed to call this function.
1108 NtUserLockWindowStation(HWINSTA hWindowStation
)
1110 PWINSTATION_OBJECT Object
;
1113 TRACE("About to set process window station with handle (%p)\n",
1116 if (gpidLogon
!= PsGetCurrentProcessId())
1118 ERR("Unauthorized process attempted to lock the window station!\n");
1119 EngSetLastError(ERROR_ACCESS_DENIED
);
1123 Status
= IntValidateWindowStationHandle(hWindowStation
,
1128 if (!NT_SUCCESS(Status
))
1130 TRACE("Validation of window station handle (%p) failed\n",
1132 SetLastNtError(Status
);
1136 Object
->Flags
|= WSS_LOCKED
;
1138 ObDereferenceObject(Object
);
1143 * NtUserUnlockWindowStation
1145 * Unlocks switching desktops. Only the logon application is allowed to call this function.
1152 NtUserUnlockWindowStation(HWINSTA hWindowStation
)
1154 PWINSTATION_OBJECT Object
;
1158 TRACE("About to set process window station with handle (%p)\n",
1161 if (gpidLogon
!= PsGetCurrentProcessId())
1163 ERR("Unauthorized process attempted to unlock the window station!\n");
1164 EngSetLastError(ERROR_ACCESS_DENIED
);
1168 Status
= IntValidateWindowStationHandle(hWindowStation
,
1173 if (!NT_SUCCESS(Status
))
1175 TRACE("Validation of window station handle (%p) failed\n",
1177 SetLastNtError(Status
);
1181 Ret
= (Object
->Flags
& WSS_LOCKED
) == WSS_LOCKED
;
1182 Object
->Flags
&= ~WSS_LOCKED
;
1184 ObDereferenceObject(Object
);
1188 static NTSTATUS FASTCALL
1189 BuildWindowStationNameList(
1192 PULONG pRequiredSize
)
1194 OBJECT_ATTRIBUTES ObjectAttributes
;
1196 HANDLE DirectoryHandle
;
1197 char InitialBuffer
[256], *Buffer
;
1198 ULONG Context
, ReturnLength
, BufferSize
;
1200 POBJECT_DIRECTORY_INFORMATION DirEntry
;
1204 // FIXME: Fully wrong! Since, by calling NtUserCreateWindowStation
1205 // with judicious parameters one can create window stations elsewhere
1206 // than in Windows\WindowStations directory, Win32k definitely MUST
1207 // maintain a list of window stations it has created, and not rely
1208 // on the enumeration of Windows\WindowStations !!!
1212 * Try to open the directory.
1214 InitializeObjectAttributes(&ObjectAttributes
,
1215 &gustrWindowStationsDir
,
1216 OBJ_CASE_INSENSITIVE
| OBJ_KERNEL_HANDLE
,
1220 Status
= ZwOpenDirectoryObject(&DirectoryHandle
,
1224 if (!NT_SUCCESS(Status
))
1229 /* First try to query the directory using a fixed-size buffer */
1232 Status
= ZwQueryDirectoryObject(DirectoryHandle
,
1234 sizeof(InitialBuffer
),
1239 if (NT_SUCCESS(Status
))
1241 if (STATUS_NO_MORE_ENTRIES
== ZwQueryDirectoryObject(DirectoryHandle
, NULL
, 0, FALSE
,
1242 FALSE
, &Context
, NULL
))
1244 /* Our fixed-size buffer is large enough */
1245 Buffer
= InitialBuffer
;
1251 /* Need a larger buffer, check how large exactly */
1252 Status
= ZwQueryDirectoryObject(DirectoryHandle
, NULL
, 0, FALSE
, TRUE
, &Context
,
1254 if (!NT_SUCCESS(Status
))
1256 ERR("ZwQueryDirectoryObject failed\n");
1257 ZwClose(DirectoryHandle
);
1261 BufferSize
= ReturnLength
;
1262 Buffer
= ExAllocatePoolWithTag(PagedPool
, BufferSize
, TAG_WINSTA
);
1265 ZwClose(DirectoryHandle
);
1266 return STATUS_NO_MEMORY
;
1269 /* We should have a sufficiently large buffer now */
1271 Status
= ZwQueryDirectoryObject(DirectoryHandle
, Buffer
, BufferSize
,
1272 FALSE
, TRUE
, &Context
, &ReturnLength
);
1273 if (! NT_SUCCESS(Status
) ||
1274 STATUS_NO_MORE_ENTRIES
!= ZwQueryDirectoryObject(DirectoryHandle
, NULL
, 0, FALSE
,
1275 FALSE
, &Context
, NULL
))
1277 /* Something went wrong, maybe someone added a directory entry? Just give up. */
1278 ExFreePoolWithTag(Buffer
, TAG_WINSTA
);
1279 ZwClose(DirectoryHandle
);
1280 return NT_SUCCESS(Status
) ? STATUS_INTERNAL_ERROR
: Status
;
1284 ZwClose(DirectoryHandle
);
1287 * Count the required size of buffer.
1289 ReturnLength
= sizeof(DWORD
);
1291 for (DirEntry
= (POBJECT_DIRECTORY_INFORMATION
) Buffer
;
1292 0 != DirEntry
->Name
.Length
;
1295 ReturnLength
+= DirEntry
->Name
.Length
+ sizeof(WCHAR
);
1298 TRACE("Required size: %lu Entry count: %lu\n", ReturnLength
, EntryCount
);
1299 if (NULL
!= pRequiredSize
)
1301 Status
= MmCopyToCaller(pRequiredSize
, &ReturnLength
, sizeof(ULONG
));
1302 if (! NT_SUCCESS(Status
))
1304 if (Buffer
!= InitialBuffer
)
1306 ExFreePoolWithTag(Buffer
, TAG_WINSTA
);
1308 return STATUS_BUFFER_TOO_SMALL
;
1313 * Check if the supplied buffer is large enough.
1315 if (dwSize
< ReturnLength
)
1317 if (Buffer
!= InitialBuffer
)
1319 ExFreePoolWithTag(Buffer
, TAG_WINSTA
);
1321 return STATUS_BUFFER_TOO_SMALL
;
1325 * Generate the resulting buffer contents.
1327 Status
= MmCopyToCaller(lpBuffer
, &EntryCount
, sizeof(DWORD
));
1328 if (! NT_SUCCESS(Status
))
1330 if (Buffer
!= InitialBuffer
)
1332 ExFreePoolWithTag(Buffer
, TAG_WINSTA
);
1336 lpBuffer
= (PVOID
) ((PCHAR
) lpBuffer
+ sizeof(DWORD
));
1339 for (DirEntry
= (POBJECT_DIRECTORY_INFORMATION
) Buffer
;
1340 0 != DirEntry
->Name
.Length
;
1343 Status
= MmCopyToCaller(lpBuffer
, DirEntry
->Name
.Buffer
, DirEntry
->Name
.Length
);
1344 if (! NT_SUCCESS(Status
))
1346 if (Buffer
!= InitialBuffer
)
1348 ExFreePoolWithTag(Buffer
, TAG_WINSTA
);
1352 lpBuffer
= (PVOID
) ((PCHAR
) lpBuffer
+ DirEntry
->Name
.Length
);
1353 Status
= MmCopyToCaller(lpBuffer
, &NullWchar
, sizeof(WCHAR
));
1354 if (! NT_SUCCESS(Status
))
1356 if (Buffer
!= InitialBuffer
)
1358 ExFreePoolWithTag(Buffer
, TAG_WINSTA
);
1362 lpBuffer
= (PVOID
) ((PCHAR
) lpBuffer
+ sizeof(WCHAR
));
1368 if (Buffer
!= InitialBuffer
)
1370 ExFreePoolWithTag(Buffer
, TAG_WINSTA
);
1373 return STATUS_SUCCESS
;
1376 static NTSTATUS FASTCALL
1377 BuildDesktopNameList(
1378 HWINSTA hWindowStation
,
1381 PULONG pRequiredSize
)
1384 PWINSTATION_OBJECT WindowStation
;
1385 PLIST_ENTRY DesktopEntry
;
1386 PDESKTOP DesktopObject
;
1390 UNICODE_STRING DesktopName
;
1392 Status
= IntValidateWindowStationHandle(hWindowStation
,
1397 if (! NT_SUCCESS(Status
))
1403 * Count the required size of buffer.
1405 ReturnLength
= sizeof(DWORD
);
1407 for (DesktopEntry
= WindowStation
->DesktopListHead
.Flink
;
1408 DesktopEntry
!= &WindowStation
->DesktopListHead
;
1409 DesktopEntry
= DesktopEntry
->Flink
)
1411 DesktopObject
= CONTAINING_RECORD(DesktopEntry
, DESKTOP
, ListEntry
);
1412 RtlInitUnicodeString(&DesktopName
, DesktopObject
->pDeskInfo
->szDesktopName
);
1413 ReturnLength
+= DesktopName
.Length
+ sizeof(WCHAR
);
1416 TRACE("Required size: %lu Entry count: %lu\n", ReturnLength
, EntryCount
);
1417 if (NULL
!= pRequiredSize
)
1419 Status
= MmCopyToCaller(pRequiredSize
, &ReturnLength
, sizeof(ULONG
));
1420 if (! NT_SUCCESS(Status
))
1422 ObDereferenceObject(WindowStation
);
1423 return STATUS_BUFFER_TOO_SMALL
;
1428 * Check if the supplied buffer is large enough.
1430 if (dwSize
< ReturnLength
)
1432 ObDereferenceObject(WindowStation
);
1433 return STATUS_BUFFER_TOO_SMALL
;
1437 * Generate the resulting buffer contents.
1439 Status
= MmCopyToCaller(lpBuffer
, &EntryCount
, sizeof(DWORD
));
1440 if (! NT_SUCCESS(Status
))
1442 ObDereferenceObject(WindowStation
);
1445 lpBuffer
= (PVOID
) ((PCHAR
) lpBuffer
+ sizeof(DWORD
));
1448 for (DesktopEntry
= WindowStation
->DesktopListHead
.Flink
;
1449 DesktopEntry
!= &WindowStation
->DesktopListHead
;
1450 DesktopEntry
= DesktopEntry
->Flink
)
1452 DesktopObject
= CONTAINING_RECORD(DesktopEntry
, DESKTOP
, ListEntry
);
1453 RtlInitUnicodeString(&DesktopName
, DesktopObject
->pDeskInfo
->szDesktopName
);
1454 Status
= MmCopyToCaller(lpBuffer
, DesktopName
.Buffer
, DesktopName
.Length
);
1455 if (! NT_SUCCESS(Status
))
1457 ObDereferenceObject(WindowStation
);
1460 lpBuffer
= (PVOID
) ((PCHAR
)lpBuffer
+ DesktopName
.Length
);
1461 Status
= MmCopyToCaller(lpBuffer
, &NullWchar
, sizeof(WCHAR
));
1462 if (! NT_SUCCESS(Status
))
1464 ObDereferenceObject(WindowStation
);
1467 lpBuffer
= (PVOID
) ((PCHAR
) lpBuffer
+ sizeof(WCHAR
));
1471 * Clean up and return
1473 ObDereferenceObject(WindowStation
);
1474 return STATUS_SUCCESS
;
1478 * NtUserBuildNameList
1480 * Function used for enumeration of desktops or window stations.
1484 * For enumeration of window stations this parameter must be set to
1485 * zero. Otherwise it's handle for window station.
1488 * Size of buffer passed by caller.
1491 * Buffer passed by caller. If the function succeeds, the buffer is
1492 * filled with window station/desktop count (in first DWORD) and
1493 * NULL-terminated window station/desktop names.
1496 * If the function succeeds, this is the number of bytes copied.
1497 * Otherwise it's size of buffer needed for function to succeed.
1504 NtUserBuildNameList(
1505 HWINSTA hWindowStation
,
1508 PULONG pRequiredSize
)
1510 /* The WindowStation name list and desktop name list are build in completely
1511 different ways. Call the appropriate function */
1512 return NULL
== hWindowStation
? BuildWindowStationNameList(dwSize
, lpBuffer
, pRequiredSize
) :
1513 BuildDesktopNameList(hWindowStation
, dwSize
, lpBuffer
, pRequiredSize
);
1520 NtUserSetLogonNotifyWindow(HWND hWnd
)
1522 if (gpidLogon
!= PsGetCurrentProcessId())
1527 if (!IntIsWindow(hWnd
))
1539 NtUserLockWorkStation(VOID
)
1542 PTHREADINFO pti
= PsGetCurrentThreadWin32Thread();
1544 UserEnterExclusive();
1546 if (pti
->rpdesk
== IntGetActiveDesktop())
1548 ret
= UserPostMessage(hwndSAS
, WM_LOGONNOTIFY
, LN_LOCK_WORKSTATION
, 0);
1562 NtUserSetWindowStationUser(
1563 IN HWINSTA hWindowStation
,
1565 IN PSID psid OPTIONAL
,
1570 PWINSTATION_OBJECT WindowStation
= NULL
;
1573 UserEnterExclusive();
1575 if (gpidLogon
!= PsGetCurrentProcessId())
1577 EngSetLastError(ERROR_ACCESS_DENIED
);
1581 /* Validate the window station */
1582 Status
= IntValidateWindowStationHandle(hWindowStation
,
1587 if (!NT_SUCCESS(Status
))
1592 /* Capture the user LUID */
1595 ProbeForRead(pluid
, sizeof(LUID
), 1);
1598 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
1600 Status
= _SEH2_GetExceptionCode();
1601 _SEH2_YIELD(goto Leave
);
1605 /* Reset the window station user LUID */
1606 RtlZeroMemory(&WindowStation
->luidUser
, sizeof(LUID
));
1608 /* Reset the window station user SID */
1609 if (WindowStation
->psidUser
)
1611 ExFreePoolWithTag(WindowStation
->psidUser
, USERTAG_SECURITY
);
1612 WindowStation
->psidUser
= NULL
;
1615 /* Copy the new user SID if one has been provided */
1618 WindowStation
->psidUser
= ExAllocatePoolWithTag(PagedPool
, size
, USERTAG_SECURITY
);
1619 if (WindowStation
->psidUser
== NULL
)
1621 EngSetLastError(ERROR_OUTOFMEMORY
);
1625 Status
= STATUS_SUCCESS
;
1628 ProbeForRead(psid
, size
, 1);
1629 RtlCopyMemory(WindowStation
->psidUser
, psid
, size
);
1631 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
1633 Status
= _SEH2_GetExceptionCode();
1637 if (!NT_SUCCESS(Status
))
1639 ExFreePoolWithTag(WindowStation
->psidUser
, USERTAG_SECURITY
);
1640 WindowStation
->psidUser
= NULL
;
1645 /* Copy the new user LUID */
1646 WindowStation
->luidUser
= luidUser
;
1652 ObDereferenceObject(WindowStation
);