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 *******************************************************************/
18 * The currently active window station. This is the
19 * only one interactive window station on the system.
21 PWINSTATION_OBJECT InputWindowStation
= NULL
;
23 /* Winlogon SAS window */
26 /* Full path to WindowStations directory */
27 UNICODE_STRING gustrWindowStationsDir
;
29 /* INITIALIZATION FUNCTIONS ****************************************************/
34 InitWindowStationImpl(VOID
)
36 GENERIC_MAPPING IntWindowStationMapping
= { WINSTA_READ
,
41 /* Set Winsta Object Attributes */
42 ExWindowStationObjectType
->TypeInfo
.DefaultNonPagedPoolCharge
= sizeof(WINSTATION_OBJECT
);
43 ExWindowStationObjectType
->TypeInfo
.GenericMapping
= IntWindowStationMapping
;
44 ExWindowStationObjectType
->TypeInfo
.ValidAccessMask
= WINSTA_ACCESS_ALL
;
46 return STATUS_SUCCESS
;
51 UserCreateWinstaDirectory(VOID
)
55 OBJECT_ATTRIBUTES ObjectAttributes
;
57 WCHAR wstrWindowStationsDir
[MAX_PATH
];
59 /* Create the WindowStations directory and cache its path for later use */
61 if(Peb
->SessionId
== 0)
63 if (!RtlCreateUnicodeString(&gustrWindowStationsDir
, WINSTA_OBJ_DIR
))
65 return STATUS_INSUFFICIENT_RESOURCES
;
70 Status
= RtlStringCbPrintfW(wstrWindowStationsDir
,
71 sizeof(wstrWindowStationsDir
),
76 if (!NT_SUCCESS(Status
))
79 if (!RtlCreateUnicodeString(&gustrWindowStationsDir
, wstrWindowStationsDir
))
81 return STATUS_INSUFFICIENT_RESOURCES
;
85 InitializeObjectAttributes(&ObjectAttributes
,
86 &gustrWindowStationsDir
,
90 Status
= ZwCreateDirectoryObject(&hWinstaDir
, DIRECTORY_CREATE_OBJECT
, &ObjectAttributes
);
91 if (!NT_SUCCESS(Status
))
93 ERR("Could not create %wZ directory (Status 0x%X)\n", &gustrWindowStationsDir
, Status
);
97 TRACE("Created directory %wZ for session %lu\n", &gustrWindowStationsDir
, Peb
->SessionId
);
102 /* OBJECT CALLBACKS ***********************************************************/
106 IntWinStaObjectDelete(
107 _In_ PVOID Parameters
)
109 PWIN32_DELETEMETHOD_PARAMETERS DeleteParameters
= Parameters
;
110 PWINSTATION_OBJECT WinSta
= (PWINSTATION_OBJECT
)DeleteParameters
->Object
;
112 TRACE("Deleting window station 0x%p\n", WinSta
);
114 if (WinSta
== InputWindowStation
)
116 ERR("WARNING: Deleting the interactive window station '%wZ'!\n",
117 &(OBJECT_HEADER_TO_NAME_INFO(OBJECT_TO_OBJECT_HEADER(InputWindowStation
))->Name
));
119 /* Only Winlogon can close and delete the interactive window station */
120 ASSERT(gpidLogon
== PsGetCurrentProcessId());
122 InputWindowStation
= NULL
;
125 WinSta
->Flags
|= WSS_DYING
;
127 UserEmptyClipboardData(WinSta
);
129 RtlDestroyAtomTable(WinSta
->AtomTable
);
131 return STATUS_SUCCESS
;
136 IntWinStaObjectParse(
137 _In_ PVOID Parameters
)
139 PWIN32_PARSEMETHOD_PARAMETERS ParseParameters
= Parameters
;
140 PUNICODE_STRING RemainingName
= ParseParameters
->RemainingName
;
142 /* Assume we don't find anything */
143 *ParseParameters
->Object
= NULL
;
145 /* Check for an empty name */
146 if (!RemainingName
->Length
)
148 /* Make sure this is a window station, can't parse a desktop now */
149 if (ParseParameters
->ObjectType
!= ExWindowStationObjectType
)
152 return STATUS_OBJECT_TYPE_MISMATCH
;
155 /* Reference the window station and return */
156 ObReferenceObject(ParseParameters
->ParseObject
);
157 *ParseParameters
->Object
= ParseParameters
->ParseObject
;
158 return STATUS_SUCCESS
;
161 /* Check for leading slash */
162 if (RemainingName
->Buffer
[0] == OBJ_NAME_PATH_SEPARATOR
)
165 RemainingName
->Buffer
++;
166 RemainingName
->Length
-= sizeof(WCHAR
);
167 RemainingName
->MaximumLength
-= sizeof(WCHAR
);
170 /* Check if there is still a slash */
171 if (wcschr(RemainingName
->Buffer
, OBJ_NAME_PATH_SEPARATOR
))
173 /* In this case, fail */
174 return STATUS_OBJECT_PATH_INVALID
;
178 * Check if we are parsing a desktop.
180 if (ParseParameters
->ObjectType
== ExDesktopObjectType
)
182 /* Then call the desktop parse routine */
183 return IntDesktopObjectParse(ParseParameters
->ParseObject
,
184 ParseParameters
->ObjectType
,
185 ParseParameters
->AccessState
,
186 ParseParameters
->AccessMode
,
187 ParseParameters
->Attributes
,
188 ParseParameters
->CompleteName
,
190 ParseParameters
->Context
,
191 ParseParameters
->SecurityQos
,
192 ParseParameters
->Object
);
195 /* Should hopefully never get here */
196 return STATUS_OBJECT_TYPE_MISMATCH
;
202 _In_ PVOID Parameters
)
204 PWIN32_OKAYTOCLOSEMETHOD_PARAMETERS OkToCloseParameters
= Parameters
;
207 ppi
= PsGetCurrentProcessWin32Process();
209 if (ppi
&& (OkToCloseParameters
->Handle
== ppi
->hwinsta
))
211 return STATUS_ACCESS_DENIED
;
214 return STATUS_SUCCESS
;
217 /* PRIVATE FUNCTIONS **********************************************************/
220 * IntValidateWindowStationHandle
222 * Validates the window station handle.
225 * If the function succeeds, the handle remains referenced. If the
226 * fucntion fails, last error is set.
230 IntValidateWindowStationHandle(
231 HWINSTA WindowStation
,
232 KPROCESSOR_MODE AccessMode
,
233 ACCESS_MASK DesiredAccess
,
234 PWINSTATION_OBJECT
*Object
,
235 POBJECT_HANDLE_INFORMATION pObjectHandleInfo
)
239 if (WindowStation
== NULL
)
241 ERR("Invalid window station handle\n");
242 EngSetLastError(ERROR_INVALID_HANDLE
);
243 return STATUS_INVALID_HANDLE
;
246 Status
= ObReferenceObjectByHandle(WindowStation
,
248 ExWindowStationObjectType
,
253 if (!NT_SUCCESS(Status
))
254 SetLastNtError(Status
);
260 co_IntInitializeDesktopGraphics(VOID
)
263 UNICODE_STRING DriverName
= RTL_CONSTANT_STRING(L
"DISPLAY");
266 ScreenDeviceContext
= IntGdiCreateDC(&DriverName
, NULL
, NULL
, NULL
, FALSE
);
267 if (NULL
== ScreenDeviceContext
)
269 IntDestroyPrimarySurface();
272 GreSetDCOwner(ScreenDeviceContext
, GDI_OBJ_HMGR_PUBLIC
);
274 if (!IntCreatePrimarySurface())
279 hSystemBM
= NtGdiCreateCompatibleDC(ScreenDeviceContext
);
281 NtGdiSelectFont(hSystemBM
, NtGdiGetStockObject(SYSTEM_FONT
));
282 GreSetDCOwner(hSystemBM
, GDI_OBJ_HMGR_PUBLIC
);
284 /* Update the system metrics */
287 /* Set new size of the monitor */
288 UserUpdateMonitorSize((HDEV
)gppdevPrimary
);
290 /* Update the SERVERINFO */
291 gpsi
->aiSysMet
[SM_CXSCREEN
] = gppdevPrimary
->gdiinfo
.ulHorzRes
;
292 gpsi
->aiSysMet
[SM_CYSCREEN
] = gppdevPrimary
->gdiinfo
.ulVertRes
;
293 gpsi
->Planes
= NtGdiGetDeviceCaps(ScreenDeviceContext
, PLANES
);
294 gpsi
->BitsPixel
= NtGdiGetDeviceCaps(ScreenDeviceContext
, BITSPIXEL
);
295 gpsi
->BitCount
= gpsi
->Planes
* gpsi
->BitsPixel
;
296 gpsi
->dmLogPixels
= NtGdiGetDeviceCaps(ScreenDeviceContext
, LOGPIXELSY
);
297 if (NtGdiGetDeviceCaps(ScreenDeviceContext
, RASTERCAPS
) & RC_PALETTE
)
299 gpsi
->PUSIFlags
|= PUSIF_PALETTEDISPLAY
;
303 gpsi
->PUSIFlags
&= ~PUSIF_PALETTEDISPLAY
;
305 // Font is realized and this dc was previously set to internal DC_ATTR.
306 gpsi
->cxSysFontChar
= IntGetCharDimensions(hSystemBM
, &tmw
, (DWORD
*)&gpsi
->cySysFontChar
);
307 gpsi
->tmSysFont
= tmw
;
309 /* Put the pointer in the center of the screen */
310 gpsi
->ptCursor
.x
= gpsi
->aiSysMet
[SM_CXSCREEN
] / 2;
311 gpsi
->ptCursor
.y
= gpsi
->aiSysMet
[SM_CYSCREEN
] / 2;
314 UserAttachMonitor((HDEV
)gppdevPrimary
);
316 /* Setup the cursor */
317 co_IntLoadDefaultCursors();
319 /* Setup the icons */
325 /* Show the desktop */
326 pdesk
= IntGetActiveDesktop();
328 co_IntShowDesktop(pdesk
, gpsi
->aiSysMet
[SM_CXSCREEN
], gpsi
->aiSysMet
[SM_CYSCREEN
], TRUE
);
334 IntEndDesktopGraphics(VOID
)
336 if (NULL
!= ScreenDeviceContext
)
337 { // No need to allocate a new dcattr.
338 GreSetDCOwner(ScreenDeviceContext
, GDI_OBJ_HMGR_POWNED
);
339 GreDeleteObject(ScreenDeviceContext
);
340 ScreenDeviceContext
= NULL
;
342 IntHideDesktop(IntGetActiveDesktop());
343 IntDestroyPrimarySurface();
349 return ScreenDeviceContext
;
353 CheckWinstaAttributeAccess(ACCESS_MASK DesiredAccess
)
355 PPROCESSINFO ppi
= PsGetCurrentProcessWin32Process();
356 if ( gpidLogon
!= PsGetCurrentProcessId() )
358 if (!(ppi
->W32PF_flags
& W32PF_IOWINSTA
))
360 ERR("Requires Interactive Window Station\n");
361 EngSetLastError(ERROR_REQUIRES_INTERACTIVE_WINDOWSTATION
);
364 if (!RtlAreAllAccessesGranted(ppi
->amwinsta
, DesiredAccess
))
366 ERR("Access Denied\n");
367 EngSetLastError(ERROR_ACCESS_DENIED
);
375 /* PUBLIC FUNCTIONS ***********************************************************/
378 * NtUserCreateWindowStation
380 * Creates a new window station.
383 * lpszWindowStationName
384 * Pointer to a null-terminated string specifying the name of the
385 * window station to be created. Window station names are
386 * case-insensitive and cannot contain backslash characters (\).
387 * Only members of the Administrators group are allowed to specify a
391 * Requested type of access
394 * Security descriptor
396 * Unknown3, Unknown4, Unknown5, Unknown6
400 * If the function succeeds, the return value is a handle to the newly
401 * created window station. If the specified window station already
402 * exists, the function succeeds and returns a handle to the existing
403 * window station. If the function fails, the return value is NULL.
411 IntCreateWindowStation(
412 OUT HWINSTA
* phWinSta
,
413 IN POBJECT_ATTRIBUTES ObjectAttributes
,
414 IN KPROCESSOR_MODE AccessMode
,
415 IN KPROCESSOR_MODE OwnerMode
,
416 IN ACCESS_MASK dwDesiredAccess
,
425 PWINSTATION_OBJECT WindowStation
;
427 TRACE("IntCreateWindowStation called\n");
432 Status
= ObOpenObjectByName(ObjectAttributes
,
433 ExWindowStationObjectType
,
439 if (NT_SUCCESS(Status
))
441 TRACE("IntCreateWindowStation opened window station '%wZ'\n",
442 ObjectAttributes
->ObjectName
);
448 * No existing window station found, try to create a new one.
451 /* Create the window station object */
452 Status
= ObCreateObject(AccessMode
,
453 ExWindowStationObjectType
,
457 sizeof(WINSTATION_OBJECT
),
460 (PVOID
*)&WindowStation
);
461 if (!NT_SUCCESS(Status
))
463 ERR("ObCreateObject failed for window station '%wZ', Status 0x%08lx\n",
464 ObjectAttributes
->ObjectName
, Status
);
465 SetLastNtError(Status
);
469 /* Initialize the window station */
470 RtlZeroMemory(WindowStation
, sizeof(WINSTATION_OBJECT
));
472 InitializeListHead(&WindowStation
->DesktopListHead
);
473 WindowStation
->dwSessionId
= NtCurrentPeb()->SessionId
;
474 Status
= RtlCreateAtomTable(37, &WindowStation
->AtomTable
);
475 if (!NT_SUCCESS(Status
))
477 ERR("RtlCreateAtomTable failed for window station '%wZ', Status 0x%08lx\n",
478 ObjectAttributes
->ObjectName
, Status
);
479 ObDereferenceObject(WindowStation
);
480 SetLastNtError(Status
);
484 Status
= ObInsertObject(WindowStation
,
490 if (!NT_SUCCESS(Status
))
492 ERR("ObInsertObject failed for window station, Status 0x%08lx\n", Status
);
493 SetLastNtError(Status
);
497 // FIXME! TODO: Add this new window station to a linked list
499 if (InputWindowStation
== NULL
)
501 ERR("Initializing input window station\n");
503 /* Only Winlogon can create the interactive window station */
504 ASSERT(gpidLogon
== PsGetCurrentProcessId());
506 InputWindowStation
= WindowStation
;
507 WindowStation
->Flags
&= ~WSS_NOIO
;
511 UserCreateSystemThread(ST_DESKTOP_THREAD
);
512 UserCreateSystemThread(ST_RIT
);
514 /* Desktop functions require the desktop thread running so wait for it to initialize */
516 KeWaitForSingleObject(gpDesktopThreadStartedEvent
,
525 WindowStation
->Flags
|= WSS_NOIO
;
528 TRACE("IntCreateWindowStation created window station '%wZ' object 0x%p handle 0x%p\n",
529 ObjectAttributes
->ObjectName
, WindowStation
, hWinSta
);
532 EngSetLastError(ERROR_SUCCESS
);
534 return STATUS_SUCCESS
;
538 FreeUserModeWindowStationName(
539 IN OUT PUNICODE_STRING WindowStationName
,
540 IN PUNICODE_STRING TebStaticUnicodeString
,
541 IN OUT POBJECT_ATTRIBUTES UserModeObjectAttributes OPTIONAL
,
542 IN POBJECT_ATTRIBUTES LocalObjectAttributes OPTIONAL
)
546 /* Try to restore the user's UserModeObjectAttributes */
547 if (UserModeObjectAttributes
&& LocalObjectAttributes
)
551 ProbeForWrite(UserModeObjectAttributes
, sizeof(OBJECT_ATTRIBUTES
), sizeof(ULONG
));
552 *UserModeObjectAttributes
= *LocalObjectAttributes
;
554 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
561 /* Free the user-mode memory */
562 if (WindowStationName
&& (WindowStationName
!= TebStaticUnicodeString
))
564 ZwFreeVirtualMemory(ZwCurrentProcess(),
565 (PVOID
*)&WindowStationName
,
572 BuildUserModeWindowStationName(
573 IN OUT POBJECT_ATTRIBUTES UserModeObjectAttributes
,
574 IN OUT POBJECT_ATTRIBUTES LocalObjectAttributes
,
575 OUT PUNICODE_STRING
* WindowStationName
,
576 OUT PUNICODE_STRING
* TebStaticUnicodeString
)
585 *WindowStationName
= NULL
;
586 *TebStaticUnicodeString
= NULL
;
588 /* Retrieve the current process LUID */
589 Status
= GetProcessLuid(NULL
, NULL
, &CallerLuid
);
590 if (!NT_SUCCESS(Status
))
592 ERR("Failed to retrieve the caller LUID, Status 0x%08lx\n", Status
);
596 /* Compute the needed string size */
597 MemSize
= _scwprintf(L
"%wZ\\Service-0x%x-%x$",
598 &gustrWindowStationsDir
,
601 MemSize
= MemSize
* sizeof(WCHAR
) + sizeof(UNICODE_NULL
);
602 if (MemSize
> MAXUSHORT
)
604 ERR("Window station name length is too long.\n");
605 return STATUS_NAME_TOO_LONG
;
607 StrSize
= (USHORT
)MemSize
;
610 * Check whether it's short enough so that we can use the static buffer
611 * in the TEB. Otherwise continue with virtual memory allocation.
613 Teb
= NtCurrentTeb();
614 if (Teb
&& (StrSize
<= sizeof(Teb
->StaticUnicodeBuffer
)))
616 /* We can use the TEB's static unicode string */
617 ASSERT(Teb
->StaticUnicodeString
.Buffer
== Teb
->StaticUnicodeBuffer
);
618 ASSERT(Teb
->StaticUnicodeString
.MaximumLength
== sizeof(Teb
->StaticUnicodeBuffer
));
620 /* Remember the TEB's static unicode string address for later */
621 *TebStaticUnicodeString
= &Teb
->StaticUnicodeString
;
623 *WindowStationName
= *TebStaticUnicodeString
;
624 (*WindowStationName
)->Length
= 0;
628 /* The TEB's static unicode string is too small, allocate some user-mode virtual memory */
629 MemSize
+= ALIGN_UP(sizeof(UNICODE_STRING
), sizeof(PVOID
));
631 /* Allocate the memory in user-mode */
632 Status
= ZwAllocateVirtualMemory(ZwCurrentProcess(),
633 (PVOID
*)WindowStationName
,
638 if (!NT_SUCCESS(Status
))
640 ERR("ZwAllocateVirtualMemory() failed, Status 0x%08lx\n", Status
);
644 RtlInitEmptyUnicodeString(*WindowStationName
,
645 (PWCHAR
)((ULONG_PTR
)*WindowStationName
+
646 ALIGN_UP(sizeof(UNICODE_STRING
), sizeof(PVOID
))),
650 /* Build a valid window station name from the LUID */
651 Status
= RtlStringCbPrintfW((*WindowStationName
)->Buffer
,
652 (*WindowStationName
)->MaximumLength
,
653 L
"%wZ\\Service-0x%x-%x$",
654 &gustrWindowStationsDir
,
657 if (!NT_SUCCESS(Status
))
659 ERR("Impossible to build a valid window station name, Status 0x%08lx\n", Status
);
662 (*WindowStationName
)->Length
= (USHORT
)(wcslen((*WindowStationName
)->Buffer
) * sizeof(WCHAR
));
664 /* Try to update the user's UserModeObjectAttributes */
667 ProbeForWrite(UserModeObjectAttributes
, sizeof(OBJECT_ATTRIBUTES
), sizeof(ULONG
));
668 *LocalObjectAttributes
= *UserModeObjectAttributes
;
670 UserModeObjectAttributes
->ObjectName
= *WindowStationName
;
671 UserModeObjectAttributes
->RootDirectory
= NULL
;
673 Status
= STATUS_SUCCESS
;
675 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
677 Status
= _SEH2_GetExceptionCode();
682 if (!NT_SUCCESS(Status
))
684 /* Release the window station name */
685 FreeUserModeWindowStationName(*WindowStationName
,
686 *TebStaticUnicodeString
,
695 NtUserCreateWindowStation(
696 IN POBJECT_ATTRIBUTES ObjectAttributes
,
697 IN ACCESS_MASK dwDesiredAccess
,
704 NTSTATUS Status
= STATUS_SUCCESS
;
705 HWINSTA hWinSta
= NULL
;
706 OBJECT_ATTRIBUTES LocalObjectAttributes
;
707 PUNICODE_STRING WindowStationName
= NULL
;
708 PUNICODE_STRING TebStaticUnicodeString
= NULL
;
709 KPROCESSOR_MODE OwnerMode
= UserMode
;
711 TRACE("NtUserCreateWindowStation called\n");
713 /* Capture the object attributes and the window station name */
716 ProbeForRead(ObjectAttributes
, sizeof(OBJECT_ATTRIBUTES
), sizeof(ULONG
));
717 LocalObjectAttributes
= *ObjectAttributes
;
718 if (LocalObjectAttributes
.Length
!= sizeof(OBJECT_ATTRIBUTES
))
720 ERR("Invalid ObjectAttributes length!\n");
721 Status
= STATUS_INVALID_PARAMETER
;
726 * Check whether the caller provided a window station name together
727 * with a RootDirectory handle.
729 * If the caller did not provide a window station name, build a new one
730 * based on the logon session identifier for the calling process.
731 * The new name is allocated in user-mode, as the rest of ObjectAttributes
732 * already is, so that the validation performed by the Object Manager
733 * can be done adequately.
735 if ((LocalObjectAttributes
.ObjectName
== NULL
||
736 LocalObjectAttributes
.ObjectName
->Buffer
== NULL
||
737 LocalObjectAttributes
.ObjectName
->Length
== 0 ||
738 LocalObjectAttributes
.ObjectName
->Buffer
[0] == UNICODE_NULL
)
740 LocalObjectAttributes.RootDirectory == NULL */)
742 /* No, build the new window station name */
743 Status
= BuildUserModeWindowStationName(ObjectAttributes
,
744 &LocalObjectAttributes
,
746 &TebStaticUnicodeString
);
747 if (!NT_SUCCESS(Status
))
749 ERR("BuildUserModeWindowStationName() failed, Status 0x%08lx\n", Status
);
752 OwnerMode
= KernelMode
;
755 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
757 Status
=_SEH2_GetExceptionCode();
758 ERR("ObjectAttributes capture failed, Status 0x%08lx\n", Status
);
762 if (!NT_SUCCESS(Status
))
764 SetLastNtError(Status
);
768 UserEnterExclusive();
770 /* Create the window station */
771 Status
= IntCreateWindowStation(&hWinSta
,
783 if (NT_SUCCESS(Status
))
785 TRACE("NtUserCreateWindowStation created window station '%wZ' with handle 0x%p\n",
786 ObjectAttributes
->ObjectName
, hWinSta
);
790 ASSERT(hWinSta
== NULL
);
791 ERR("NtUserCreateWindowStation failed to create window station '%wZ', Status 0x%08lx\n",
792 ObjectAttributes
->ObjectName
, Status
);
795 /* Try to restore the user's ObjectAttributes and release the window station name */
796 FreeUserModeWindowStationName(WindowStationName
,
797 TebStaticUnicodeString
,
798 (OwnerMode
== KernelMode
? ObjectAttributes
: NULL
),
799 &LocalObjectAttributes
);
801 if (!NT_SUCCESS(Status
))
803 ASSERT(hWinSta
== NULL
);
804 SetLastNtError(Status
);
811 * NtUserOpenWindowStation
813 * Opens an existing window station.
816 * lpszWindowStationName
817 * Name of the existing window station.
820 * Requested type of access.
823 * If the function succeeds, the return value is the handle to the
824 * specified window station. If the function fails, the return value
828 * The returned handle can be closed with NtUserCloseWindowStation.
836 NtUserOpenWindowStation(
837 IN POBJECT_ATTRIBUTES ObjectAttributes
,
838 IN ACCESS_MASK dwDesiredAccess
)
840 NTSTATUS Status
= STATUS_SUCCESS
;
841 HWINSTA hWinSta
= NULL
;
842 OBJECT_ATTRIBUTES LocalObjectAttributes
;
843 PUNICODE_STRING WindowStationName
= NULL
;
844 PUNICODE_STRING TebStaticUnicodeString
= NULL
;
845 KPROCESSOR_MODE OwnerMode
= UserMode
;
847 TRACE("NtUserOpenWindowStation called\n");
849 /* Capture the object attributes and the window station name */
852 ProbeForRead(ObjectAttributes
, sizeof(OBJECT_ATTRIBUTES
), sizeof(ULONG
));
853 LocalObjectAttributes
= *ObjectAttributes
;
854 if (LocalObjectAttributes
.Length
!= sizeof(OBJECT_ATTRIBUTES
))
856 ERR("Invalid ObjectAttributes length!\n");
857 Status
= STATUS_INVALID_PARAMETER
;
862 * Check whether the caller did not provide a window station name,
863 * or provided the special "Service-0x00000000-00000000$" name.
865 * NOTE: On Windows, the special "Service-0x00000000-00000000$" string
866 * is used instead of an empty name (observed when API-monitoring
867 * OpenWindowStation() called with an empty window station name).
869 if ((LocalObjectAttributes
.ObjectName
== NULL
||
870 LocalObjectAttributes
.ObjectName
->Buffer
== NULL
||
871 LocalObjectAttributes
.ObjectName
->Length
== 0 ||
872 LocalObjectAttributes
.ObjectName
->Buffer
[0] == UNICODE_NULL
)
874 LocalObjectAttributes.RootDirectory == NULL */)
876 /* No, remember that for later */
877 LocalObjectAttributes
.ObjectName
= NULL
;
879 if (LocalObjectAttributes
.ObjectName
&&
880 LocalObjectAttributes
.ObjectName
->Length
==
881 sizeof(L
"Service-0x00000000-00000000$") - sizeof(UNICODE_NULL
) &&
882 _wcsnicmp(LocalObjectAttributes
.ObjectName
->Buffer
,
883 L
"Service-0x00000000-00000000$",
884 LocalObjectAttributes
.ObjectName
->Length
/ sizeof(WCHAR
)) == 0)
886 /* No, remember that for later */
887 LocalObjectAttributes
.ObjectName
= NULL
;
891 * If the caller did not provide a window station name, build a new one
892 * based on the logon session identifier for the calling process.
893 * The new name is allocated in user-mode, as the rest of ObjectAttributes
894 * already is, so that the validation performed by the Object Manager
895 * can be done adequately.
897 if (!LocalObjectAttributes
.ObjectName
)
899 /* No, build the new window station name */
900 Status
= BuildUserModeWindowStationName(ObjectAttributes
,
901 &LocalObjectAttributes
,
903 &TebStaticUnicodeString
);
904 if (!NT_SUCCESS(Status
))
906 ERR("BuildUserModeWindowStationName() failed, Status 0x%08lx\n", Status
);
909 OwnerMode
= KernelMode
;
912 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
914 Status
=_SEH2_GetExceptionCode();
915 ERR("ObjectAttributes capture failed, Status 0x%08lx\n", Status
);
919 if (!NT_SUCCESS(Status
))
921 SetLastNtError(Status
);
925 /* Open the window station */
926 Status
= ObOpenObjectByName(ObjectAttributes
,
927 ExWindowStationObjectType
,
933 if (NT_SUCCESS(Status
))
935 TRACE("NtUserOpenWindowStation opened window station '%wZ' with handle 0x%p\n",
936 ObjectAttributes
->ObjectName
, hWinSta
);
940 ASSERT(hWinSta
== NULL
);
941 ERR("NtUserOpenWindowStation failed to open window station '%wZ', Status 0x%08lx\n",
942 ObjectAttributes
->ObjectName
, Status
);
945 /* Try to restore the user's ObjectAttributes and release the window station name */
946 FreeUserModeWindowStationName(WindowStationName
,
947 TebStaticUnicodeString
,
948 (OwnerMode
== KernelMode
? ObjectAttributes
: NULL
),
949 &LocalObjectAttributes
);
951 if (!NT_SUCCESS(Status
))
953 ASSERT(hWinSta
== NULL
);
954 SetLastNtError(Status
);
961 * NtUserCloseWindowStation
963 * Closes a window station handle.
967 * Handle to the window station.
973 * The window station handle can be created with NtUserCreateWindowStation
974 * or NtUserOpenWindowStation. Attempts to close a handle to the window
975 * station assigned to the calling process will fail.
983 NtUserCloseWindowStation(
986 PWINSTATION_OBJECT Object
;
989 TRACE("NtUserCloseWindowStation called (%p)\n", hWinSta
);
991 if (hWinSta
== UserGetProcessWindowStation())
993 ERR("Attempted to close process window station\n");
997 Status
= IntValidateWindowStationHandle(hWinSta
,
1002 if (!NT_SUCCESS(Status
))
1004 ERR("Validation of window station handle (%p) failed\n", hWinSta
);
1008 ObDereferenceObject(Object
);
1010 TRACE("Closing window station handle (%p)\n", hWinSta
);
1012 Status
= ObCloseHandle(hWinSta
, UserMode
);
1013 if (!NT_SUCCESS(Status
))
1015 SetLastNtError(Status
);
1023 * NtUserGetObjectInformation
1025 * The NtUserGetObjectInformation function retrieves information about a
1026 * window station or desktop object.
1030 * Handle to the window station or desktop object for which to
1031 * return information. This can be a handle of type HDESK or HWINSTA
1032 * (for example, a handle returned by NtUserCreateWindowStation,
1033 * NtUserOpenWindowStation, NtUserCreateDesktop, or NtUserOpenDesktop).
1036 * Specifies the object information to be retrieved.
1039 * Pointer to a buffer to receive the object information.
1042 * Specifies the size, in bytes, of the buffer pointed to by the
1046 * Pointer to a variable receiving the number of bytes required to
1047 * store the requested information. If this variable's value is
1048 * greater than the value of the nLength parameter when the function
1049 * returns, the function returns FALSE, and none of the information
1050 * is copied to the pvInfo buffer. If the value of the variable pointed
1051 * to by lpnLengthNeeded is less than or equal to the value of nLength,
1052 * the entire information block is copied.
1055 * If the function succeeds, the return value is nonzero. If the function
1056 * fails, the return value is zero.
1063 NtUserGetObjectInformation(
1066 PVOID pvInformation
,
1068 PDWORD nLengthNeeded
)
1071 PWINSTATION_OBJECT WinStaObject
= NULL
;
1072 PDESKTOP DesktopObject
= NULL
;
1073 POBJECT_HEADER ObjectHeader
;
1074 POBJECT_HEADER_NAME_INFO NameInfo
;
1075 OBJECT_HANDLE_INFORMATION HandleInfo
;
1076 USEROBJECTFLAGS ObjectFlags
;
1077 PUNICODE_STRING pStrNameU
= NULL
;
1078 PVOID pvData
= NULL
;
1079 SIZE_T nDataSize
= 0;
1084 ProbeForWrite(nLengthNeeded
, sizeof(*nLengthNeeded
), 1);
1085 ProbeForWrite(pvInformation
, nLength
, 1);
1087 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
1089 SetLastNtError(_SEH2_GetExceptionCode());
1094 /* Try window station */
1095 TRACE("Trying to open window station 0x%p\n", hObject
);
1096 Status
= ObReferenceObjectByHandle(hObject
,
1098 ExWindowStationObjectType
,
1100 (PVOID
*)&WinStaObject
,
1103 if (Status
== STATUS_OBJECT_TYPE_MISMATCH
)
1106 TRACE("Trying to open desktop %p\n", hObject
);
1107 WinStaObject
= NULL
;
1108 Status
= IntValidateDesktopHandle(hObject
,
1114 if (!NT_SUCCESS(Status
))
1116 ERR("Failed: 0x%x\n", Status
);
1120 TRACE("WinSta or Desktop opened!\n");
1127 ObjectFlags
.fReserved
= FALSE
;
1128 ObjectFlags
.fInherit
= !!(HandleInfo
.HandleAttributes
& OBJ_INHERIT
);
1130 ObjectFlags
.dwFlags
= 0;
1131 if (WinStaObject
!= NULL
)
1133 if (!(WinStaObject
->Flags
& WSS_NOIO
))
1134 ObjectFlags
.dwFlags
|= WSF_VISIBLE
;
1136 else if (DesktopObject
!= NULL
)
1138 FIXME("Setting DF_ALLOWOTHERACCOUNTHOOK is unimplemented.\n");
1142 ERR("No associated WinStaObject nor DesktopObject!\n");
1145 pvData
= &ObjectFlags
;
1146 nDataSize
= sizeof(ObjectFlags
);
1147 Status
= STATUS_SUCCESS
;
1153 if (WinStaObject
!= NULL
)
1155 ObjectHeader
= OBJECT_TO_OBJECT_HEADER(WinStaObject
);
1156 NameInfo
= OBJECT_HEADER_TO_NAME_INFO(ObjectHeader
);
1158 if (NameInfo
&& (NameInfo
->Name
.Length
> 0))
1160 /* Named window station */
1161 pStrNameU
= &NameInfo
->Name
;
1162 nDataSize
= pStrNameU
->Length
+ sizeof(UNICODE_NULL
);
1166 /* Unnamed window station (should never happen!) */
1169 nDataSize
= sizeof(UNICODE_NULL
);
1171 Status
= STATUS_SUCCESS
;
1173 else if (DesktopObject
!= NULL
)
1175 pvData
= DesktopObject
->pDeskInfo
->szDesktopName
;
1176 nDataSize
= (wcslen(DesktopObject
->pDeskInfo
->szDesktopName
) + 1) * sizeof(WCHAR
);
1177 Status
= STATUS_SUCCESS
;
1181 Status
= STATUS_INVALID_PARAMETER
;
1188 if (WinStaObject
!= NULL
)
1190 ObjectHeader
= OBJECT_TO_OBJECT_HEADER(WinStaObject
);
1191 pStrNameU
= &ObjectHeader
->Type
->Name
;
1192 nDataSize
= pStrNameU
->Length
+ sizeof(UNICODE_NULL
);
1193 Status
= STATUS_SUCCESS
;
1195 else if (DesktopObject
!= NULL
)
1197 ObjectHeader
= OBJECT_TO_OBJECT_HEADER(DesktopObject
);
1198 pStrNameU
= &ObjectHeader
->Type
->Name
;
1199 nDataSize
= pStrNameU
->Length
+ sizeof(UNICODE_NULL
);
1200 Status
= STATUS_SUCCESS
;
1204 Status
= STATUS_INVALID_PARAMETER
;
1210 Status
= STATUS_NOT_IMPLEMENTED
;
1211 ERR("UOI_USER_SID unimplemented!\n");
1215 Status
= STATUS_INVALID_PARAMETER
;
1220 if ((Status
== STATUS_SUCCESS
) && (nLength
< nDataSize
))
1221 Status
= STATUS_BUFFER_TOO_SMALL
;
1226 *nLengthNeeded
= nDataSize
;
1228 /* Try to copy data to caller */
1229 if (Status
== STATUS_SUCCESS
&& (nDataSize
> 0))
1231 TRACE("Trying to copy data to caller (len = %lu, len needed = %lu)\n", nLength
, nDataSize
);
1235 RtlCopyMemory(pvInformation
, pvData
, nDataSize
);
1239 /* Copy and NULL-terminate the string */
1240 RtlCopyMemory(pvInformation
, pStrNameU
->Buffer
, pStrNameU
->Length
);
1241 ((PWCHAR
)pvInformation
)[pStrNameU
->Length
/ sizeof(WCHAR
)] = UNICODE_NULL
;
1245 /* Zero the memory */
1246 RtlZeroMemory(pvInformation
, nDataSize
);
1250 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
1252 Status
= _SEH2_GetExceptionCode();
1256 /* Release objects */
1257 if (DesktopObject
!= NULL
)
1258 ObDereferenceObject(DesktopObject
);
1259 if (WinStaObject
!= NULL
)
1260 ObDereferenceObject(WinStaObject
);
1262 if (!NT_SUCCESS(Status
))
1264 SetLastNtError(Status
);
1272 * NtUserSetObjectInformation
1274 * The NtUserSetObjectInformation function sets information about a
1275 * window station or desktop object.
1279 * Handle to the window station or desktop object for which to set
1280 * object information. This value can be a handle of type HDESK or
1284 * Specifies the object information to be set.
1287 * Pointer to a buffer containing the object information.
1290 * Specifies the size, in bytes, of the information contained in the
1291 * buffer pointed to by pvInfo.
1294 * If the function succeeds, the return value is nonzero. If the function
1295 * fails the return value is zero.
1303 NtUserSetObjectInformation(
1306 PVOID pvInformation
,
1309 /* FIXME: ZwQueryObject */
1310 /* FIXME: ZwSetInformationObject */
1311 SetLastNtError(STATUS_UNSUCCESSFUL
);
1317 UserGetProcessWindowStation(VOID
)
1319 PPROCESSINFO ppi
= PsGetCurrentProcessWin32Process();
1321 return ppi
->hwinsta
;
1326 * NtUserGetProcessWindowStation
1328 * Returns a handle to the current process window station.
1331 * If the function succeeds, the return value is handle to the window
1332 * station assigned to the current process. If the function fails, the
1333 * return value is NULL.
1340 NtUserGetProcessWindowStation(VOID
)
1342 return UserGetProcessWindowStation();
1346 UserSetProcessWindowStation(HWINSTA hWindowStation
)
1350 OBJECT_HANDLE_INFORMATION ObjectHandleInfo
;
1351 PWINSTATION_OBJECT NewWinSta
= NULL
, OldWinSta
;
1352 HWINSTA hCacheWinSta
;
1354 ppi
= PsGetCurrentProcessWin32Process();
1356 /* Reference the new window station */
1357 if (hWindowStation
!= NULL
)
1359 Status
= IntValidateWindowStationHandle(hWindowStation
,
1364 if (!NT_SUCCESS(Status
))
1366 TRACE("Validation of window station handle 0x%p failed\n", hWindowStation
);
1367 SetLastNtError(Status
);
1372 OldWinSta
= ppi
->prpwinsta
;
1373 hCacheWinSta
= PsGetProcessWin32WindowStation(ppi
->peProcess
);
1375 /* Dereference the previous window station */
1376 if (OldWinSta
!= NULL
)
1378 ObDereferenceObject(OldWinSta
);
1382 * FIXME: Don't allow changing the window station if there are threads that are attached to desktops and own GUI objects?
1385 /* Close the cached EPROCESS window station handle if needed */
1386 if (hCacheWinSta
!= NULL
)
1388 /* Reference the window station */
1389 Status
= ObReferenceObjectByHandle(hCacheWinSta
,
1391 ExWindowStationObjectType
,
1395 if (!NT_SUCCESS(Status
))
1397 ERR("Failed to reference the inherited window station, Status 0x%08lx\n", Status
);
1398 /* We failed, reset the cache */
1399 hCacheWinSta
= NULL
;
1400 PsSetProcessWindowStation(ppi
->peProcess
, hCacheWinSta
);
1405 * Close the old handle and reset the cache only
1406 * if we are setting a different window station.
1408 if (NewWinSta
!= OldWinSta
)
1410 ObCloseHandle(hCacheWinSta
, UserMode
);
1411 hCacheWinSta
= NULL
;
1412 PsSetProcessWindowStation(ppi
->peProcess
, hCacheWinSta
);
1415 /* Dereference the window station */
1416 ObDereferenceObject(OldWinSta
);
1420 /* Duplicate and save a new cached EPROCESS window station handle */
1421 if ((hCacheWinSta
== NULL
) && (hWindowStation
!= NULL
))
1423 Status
= ZwDuplicateObject(ZwCurrentProcess(),
1426 (PHANDLE
)&hCacheWinSta
,
1429 DUPLICATE_SAME_ACCESS
);
1430 if (!NT_SUCCESS(Status
))
1432 ERR("UserSetProcessWindowStation: Failed to duplicate the window station handle, Status 0x%08lx\n", Status
);
1436 PsSetProcessWindowStation(ppi
->peProcess
, hCacheWinSta
);
1440 ppi
->prpwinsta
= NewWinSta
;
1441 ppi
->hwinsta
= hWindowStation
;
1442 ppi
->amwinsta
= hWindowStation
!= NULL
? ObjectHandleInfo
.GrantedAccess
: 0;
1443 TRACE("WS : Granted Access 0x%08lx\n",ppi
->amwinsta
);
1445 if (RtlAreAllAccessesGranted(ppi
->amwinsta
, WINSTA_READSCREEN
))
1447 ppi
->W32PF_flags
|= W32PF_READSCREENACCESSGRANTED
;
1451 ppi
->W32PF_flags
&= ~W32PF_READSCREENACCESSGRANTED
;
1454 if (NewWinSta
&& !(NewWinSta
->Flags
& WSS_NOIO
))
1456 ppi
->W32PF_flags
|= W32PF_IOWINSTA
;
1458 else /* Might be closed if the handle is NULL */
1460 ppi
->W32PF_flags
&= ~W32PF_IOWINSTA
;
1466 * NtUserSetProcessWindowStation
1468 * Assigns a window station to the current process.
1472 * Handle to the window station.
1482 NtUserSetProcessWindowStation(HWINSTA hWindowStation
)
1486 UserEnterExclusive();
1488 ret
= UserSetProcessWindowStation(hWindowStation
);
1496 * NtUserLockWindowStation
1498 * Locks switching desktops. Only the logon application is allowed to call this function.
1505 NtUserLockWindowStation(HWINSTA hWindowStation
)
1507 PWINSTATION_OBJECT Object
;
1510 TRACE("About to set process window station with handle (%p)\n",
1513 if (gpidLogon
!= PsGetCurrentProcessId())
1515 ERR("Unauthorized process attempted to lock the window station!\n");
1516 EngSetLastError(ERROR_ACCESS_DENIED
);
1520 Status
= IntValidateWindowStationHandle(hWindowStation
,
1525 if (!NT_SUCCESS(Status
))
1527 TRACE("Validation of window station handle (%p) failed\n",
1529 SetLastNtError(Status
);
1533 Object
->Flags
|= WSS_LOCKED
;
1535 ObDereferenceObject(Object
);
1540 * NtUserUnlockWindowStation
1542 * Unlocks switching desktops. Only the logon application is allowed to call this function.
1549 NtUserUnlockWindowStation(HWINSTA hWindowStation
)
1551 PWINSTATION_OBJECT Object
;
1555 TRACE("About to set process window station with handle (%p)\n",
1558 if (gpidLogon
!= PsGetCurrentProcessId())
1560 ERR("Unauthorized process attempted to unlock the window station!\n");
1561 EngSetLastError(ERROR_ACCESS_DENIED
);
1565 Status
= IntValidateWindowStationHandle(hWindowStation
,
1570 if (!NT_SUCCESS(Status
))
1572 TRACE("Validation of window station handle (%p) failed\n",
1574 SetLastNtError(Status
);
1578 Ret
= (Object
->Flags
& WSS_LOCKED
) == WSS_LOCKED
;
1579 Object
->Flags
&= ~WSS_LOCKED
;
1581 ObDereferenceObject(Object
);
1585 static NTSTATUS FASTCALL
1586 BuildWindowStationNameList(
1589 PULONG pRequiredSize
)
1591 OBJECT_ATTRIBUTES ObjectAttributes
;
1593 HANDLE DirectoryHandle
;
1594 char InitialBuffer
[256], *Buffer
;
1595 ULONG Context
, ReturnLength
, BufferSize
;
1597 POBJECT_DIRECTORY_INFORMATION DirEntry
;
1601 // FIXME: Fully wrong! Since, by calling NtUserCreateWindowStation
1602 // with judicious parameters one can create window stations elsewhere
1603 // than in Windows\WindowStations directory, Win32k definitely MUST
1604 // maintain a list of window stations it has created, and not rely
1605 // on the enumeration of Windows\WindowStations !!!
1609 * Try to open the directory.
1611 InitializeObjectAttributes(&ObjectAttributes
,
1612 &gustrWindowStationsDir
,
1613 OBJ_CASE_INSENSITIVE
| OBJ_KERNEL_HANDLE
,
1617 Status
= ZwOpenDirectoryObject(&DirectoryHandle
,
1621 if (!NT_SUCCESS(Status
))
1626 /* First try to query the directory using a fixed-size buffer */
1629 Status
= ZwQueryDirectoryObject(DirectoryHandle
,
1631 sizeof(InitialBuffer
),
1636 if (NT_SUCCESS(Status
))
1638 if (STATUS_NO_MORE_ENTRIES
== ZwQueryDirectoryObject(DirectoryHandle
, NULL
, 0, FALSE
,
1639 FALSE
, &Context
, NULL
))
1641 /* Our fixed-size buffer is large enough */
1642 Buffer
= InitialBuffer
;
1648 /* Need a larger buffer, check how large exactly */
1649 Status
= ZwQueryDirectoryObject(DirectoryHandle
, NULL
, 0, FALSE
, TRUE
, &Context
,
1651 if (!NT_SUCCESS(Status
))
1653 ERR("ZwQueryDirectoryObject failed\n");
1654 ZwClose(DirectoryHandle
);
1658 BufferSize
= ReturnLength
;
1659 Buffer
= ExAllocatePoolWithTag(PagedPool
, BufferSize
, TAG_WINSTA
);
1662 ZwClose(DirectoryHandle
);
1663 return STATUS_NO_MEMORY
;
1666 /* We should have a sufficiently large buffer now */
1668 Status
= ZwQueryDirectoryObject(DirectoryHandle
, Buffer
, BufferSize
,
1669 FALSE
, TRUE
, &Context
, &ReturnLength
);
1670 if (! NT_SUCCESS(Status
) ||
1671 STATUS_NO_MORE_ENTRIES
!= ZwQueryDirectoryObject(DirectoryHandle
, NULL
, 0, FALSE
,
1672 FALSE
, &Context
, NULL
))
1674 /* Something went wrong, maybe someone added a directory entry? Just give up. */
1675 ExFreePoolWithTag(Buffer
, TAG_WINSTA
);
1676 ZwClose(DirectoryHandle
);
1677 return NT_SUCCESS(Status
) ? STATUS_INTERNAL_ERROR
: Status
;
1681 ZwClose(DirectoryHandle
);
1684 * Count the required size of buffer.
1686 ReturnLength
= sizeof(DWORD
);
1688 for (DirEntry
= (POBJECT_DIRECTORY_INFORMATION
) Buffer
;
1689 0 != DirEntry
->Name
.Length
;
1692 ReturnLength
+= DirEntry
->Name
.Length
+ sizeof(WCHAR
);
1695 TRACE("Required size: %lu Entry count: %lu\n", ReturnLength
, EntryCount
);
1696 if (NULL
!= pRequiredSize
)
1698 Status
= MmCopyToCaller(pRequiredSize
, &ReturnLength
, sizeof(ULONG
));
1699 if (! NT_SUCCESS(Status
))
1701 if (Buffer
!= InitialBuffer
)
1703 ExFreePoolWithTag(Buffer
, TAG_WINSTA
);
1705 return STATUS_BUFFER_TOO_SMALL
;
1710 * Check if the supplied buffer is large enough.
1712 if (dwSize
< ReturnLength
)
1714 if (Buffer
!= InitialBuffer
)
1716 ExFreePoolWithTag(Buffer
, TAG_WINSTA
);
1718 return STATUS_BUFFER_TOO_SMALL
;
1722 * Generate the resulting buffer contents.
1724 Status
= MmCopyToCaller(lpBuffer
, &EntryCount
, sizeof(DWORD
));
1725 if (! NT_SUCCESS(Status
))
1727 if (Buffer
!= InitialBuffer
)
1729 ExFreePoolWithTag(Buffer
, TAG_WINSTA
);
1733 lpBuffer
= (PVOID
) ((PCHAR
) lpBuffer
+ sizeof(DWORD
));
1736 for (DirEntry
= (POBJECT_DIRECTORY_INFORMATION
) Buffer
;
1737 0 != DirEntry
->Name
.Length
;
1740 Status
= MmCopyToCaller(lpBuffer
, DirEntry
->Name
.Buffer
, DirEntry
->Name
.Length
);
1741 if (! NT_SUCCESS(Status
))
1743 if (Buffer
!= InitialBuffer
)
1745 ExFreePoolWithTag(Buffer
, TAG_WINSTA
);
1749 lpBuffer
= (PVOID
) ((PCHAR
) lpBuffer
+ DirEntry
->Name
.Length
);
1750 Status
= MmCopyToCaller(lpBuffer
, &NullWchar
, sizeof(WCHAR
));
1751 if (! NT_SUCCESS(Status
))
1753 if (Buffer
!= InitialBuffer
)
1755 ExFreePoolWithTag(Buffer
, TAG_WINSTA
);
1759 lpBuffer
= (PVOID
) ((PCHAR
) lpBuffer
+ sizeof(WCHAR
));
1765 if (Buffer
!= InitialBuffer
)
1767 ExFreePoolWithTag(Buffer
, TAG_WINSTA
);
1770 return STATUS_SUCCESS
;
1773 static NTSTATUS FASTCALL
1774 BuildDesktopNameList(
1775 HWINSTA hWindowStation
,
1778 PULONG pRequiredSize
)
1781 PWINSTATION_OBJECT WindowStation
;
1782 PLIST_ENTRY DesktopEntry
;
1783 PDESKTOP DesktopObject
;
1787 UNICODE_STRING DesktopName
;
1789 Status
= IntValidateWindowStationHandle(hWindowStation
,
1794 if (! NT_SUCCESS(Status
))
1800 * Count the required size of buffer.
1802 ReturnLength
= sizeof(DWORD
);
1804 for (DesktopEntry
= WindowStation
->DesktopListHead
.Flink
;
1805 DesktopEntry
!= &WindowStation
->DesktopListHead
;
1806 DesktopEntry
= DesktopEntry
->Flink
)
1808 DesktopObject
= CONTAINING_RECORD(DesktopEntry
, DESKTOP
, ListEntry
);
1809 RtlInitUnicodeString(&DesktopName
, DesktopObject
->pDeskInfo
->szDesktopName
);
1810 ReturnLength
+= DesktopName
.Length
+ sizeof(WCHAR
);
1813 TRACE("Required size: %lu Entry count: %lu\n", ReturnLength
, EntryCount
);
1814 if (NULL
!= pRequiredSize
)
1816 Status
= MmCopyToCaller(pRequiredSize
, &ReturnLength
, sizeof(ULONG
));
1817 if (! NT_SUCCESS(Status
))
1819 ObDereferenceObject(WindowStation
);
1820 return STATUS_BUFFER_TOO_SMALL
;
1825 * Check if the supplied buffer is large enough.
1827 if (dwSize
< ReturnLength
)
1829 ObDereferenceObject(WindowStation
);
1830 return STATUS_BUFFER_TOO_SMALL
;
1834 * Generate the resulting buffer contents.
1836 Status
= MmCopyToCaller(lpBuffer
, &EntryCount
, sizeof(DWORD
));
1837 if (! NT_SUCCESS(Status
))
1839 ObDereferenceObject(WindowStation
);
1842 lpBuffer
= (PVOID
) ((PCHAR
) lpBuffer
+ sizeof(DWORD
));
1845 for (DesktopEntry
= WindowStation
->DesktopListHead
.Flink
;
1846 DesktopEntry
!= &WindowStation
->DesktopListHead
;
1847 DesktopEntry
= DesktopEntry
->Flink
)
1849 DesktopObject
= CONTAINING_RECORD(DesktopEntry
, DESKTOP
, ListEntry
);
1850 RtlInitUnicodeString(&DesktopName
, DesktopObject
->pDeskInfo
->szDesktopName
);
1851 Status
= MmCopyToCaller(lpBuffer
, DesktopName
.Buffer
, DesktopName
.Length
);
1852 if (! NT_SUCCESS(Status
))
1854 ObDereferenceObject(WindowStation
);
1857 lpBuffer
= (PVOID
) ((PCHAR
)lpBuffer
+ DesktopName
.Length
);
1858 Status
= MmCopyToCaller(lpBuffer
, &NullWchar
, sizeof(WCHAR
));
1859 if (! NT_SUCCESS(Status
))
1861 ObDereferenceObject(WindowStation
);
1864 lpBuffer
= (PVOID
) ((PCHAR
) lpBuffer
+ sizeof(WCHAR
));
1868 * Clean up and return
1870 ObDereferenceObject(WindowStation
);
1871 return STATUS_SUCCESS
;
1875 * NtUserBuildNameList
1877 * Function used for enumeration of desktops or window stations.
1881 * For enumeration of window stations this parameter must be set to
1882 * zero. Otherwise it's handle for window station.
1885 * Size of buffer passed by caller.
1888 * Buffer passed by caller. If the function succeeds, the buffer is
1889 * filled with window station/desktop count (in first DWORD) and
1890 * NULL-terminated window station/desktop names.
1893 * If the function succeeds, this is the number of bytes copied.
1894 * Otherwise it's size of buffer needed for function to succeed.
1901 NtUserBuildNameList(
1902 HWINSTA hWindowStation
,
1905 PULONG pRequiredSize
)
1907 /* The WindowStation name list and desktop name list are build in completely
1908 different ways. Call the appropriate function */
1909 return NULL
== hWindowStation
? BuildWindowStationNameList(dwSize
, lpBuffer
, pRequiredSize
) :
1910 BuildDesktopNameList(hWindowStation
, dwSize
, lpBuffer
, pRequiredSize
);
1917 NtUserSetLogonNotifyWindow(HWND hWnd
)
1919 if (gpidLogon
!= PsGetCurrentProcessId())
1924 if (!IntIsWindow(hWnd
))
1936 NtUserLockWorkStation(VOID
)
1939 PTHREADINFO pti
= PsGetCurrentThreadWin32Thread();
1941 UserEnterExclusive();
1943 if (pti
->rpdesk
== IntGetActiveDesktop())
1945 ret
= UserPostMessage(hwndSAS
, WM_LOGONNOTIFY
, LN_LOCK_WORKSTATION
, 0);
1959 NtUserSetWindowStationUser(
1960 IN HWINSTA hWindowStation
,
1962 IN PSID psid OPTIONAL
,
1967 PWINSTATION_OBJECT WindowStation
= NULL
;
1970 UserEnterExclusive();
1972 if (gpidLogon
!= PsGetCurrentProcessId())
1974 EngSetLastError(ERROR_ACCESS_DENIED
);
1978 /* Validate the window station */
1979 Status
= IntValidateWindowStationHandle(hWindowStation
,
1984 if (!NT_SUCCESS(Status
))
1989 /* Capture the user LUID */
1992 ProbeForRead(pluid
, sizeof(LUID
), 1);
1995 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
1997 Status
= _SEH2_GetExceptionCode();
1998 _SEH2_YIELD(goto Leave
);
2002 /* Reset the window station user LUID */
2003 RtlZeroMemory(&WindowStation
->luidUser
, sizeof(LUID
));
2005 /* Reset the window station user SID */
2006 if (WindowStation
->psidUser
)
2008 ExFreePoolWithTag(WindowStation
->psidUser
, USERTAG_SECURITY
);
2009 WindowStation
->psidUser
= NULL
;
2012 /* Copy the new user SID if one has been provided */
2015 WindowStation
->psidUser
= ExAllocatePoolWithTag(PagedPool
, size
, USERTAG_SECURITY
);
2016 if (WindowStation
->psidUser
== NULL
)
2018 EngSetLastError(ERROR_OUTOFMEMORY
);
2022 Status
= STATUS_SUCCESS
;
2025 ProbeForRead(psid
, size
, 1);
2026 RtlCopyMemory(WindowStation
->psidUser
, psid
, size
);
2028 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
2030 Status
= _SEH2_GetExceptionCode();
2034 if (!NT_SUCCESS(Status
))
2036 ExFreePoolWithTag(WindowStation
->psidUser
, USERTAG_SECURITY
);
2037 WindowStation
->psidUser
= NULL
;
2042 /* Copy the new user LUID */
2043 WindowStation
->luidUser
= luidUser
;
2049 ObDereferenceObject(WindowStation
);