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 SERVERINFO */
285 gpsi
->aiSysMet
[SM_CXSCREEN
] = gppdevPrimary
->gdiinfo
.ulHorzRes
;
286 gpsi
->aiSysMet
[SM_CYSCREEN
] = gppdevPrimary
->gdiinfo
.ulVertRes
;
287 gpsi
->Planes
= NtGdiGetDeviceCaps(ScreenDeviceContext
, PLANES
);
288 gpsi
->BitsPixel
= NtGdiGetDeviceCaps(ScreenDeviceContext
, BITSPIXEL
);
289 gpsi
->BitCount
= gpsi
->Planes
* gpsi
->BitsPixel
;
290 gpsi
->dmLogPixels
= NtGdiGetDeviceCaps(ScreenDeviceContext
, LOGPIXELSY
);
291 if (NtGdiGetDeviceCaps(ScreenDeviceContext
, RASTERCAPS
) & RC_PALETTE
)
293 gpsi
->PUSIFlags
|= PUSIF_PALETTEDISPLAY
;
296 gpsi
->PUSIFlags
&= ~PUSIF_PALETTEDISPLAY
;
297 // Font is realized and this dc was previously set to internal DC_ATTR.
298 gpsi
->cxSysFontChar
= IntGetCharDimensions(hSystemBM
, &tmw
, (DWORD
*)&gpsi
->cySysFontChar
);
299 gpsi
->tmSysFont
= tmw
;
301 /* Put the pointer in the center of the screen */
302 gpsi
->ptCursor
.x
= gpsi
->aiSysMet
[SM_CXSCREEN
] / 2;
303 gpsi
->ptCursor
.y
= gpsi
->aiSysMet
[SM_CYSCREEN
] / 2;
306 UserAttachMonitor((HDEV
)gppdevPrimary
);
308 /* Setup the cursor */
309 co_IntLoadDefaultCursors();
311 /* Setup the icons */
317 /* Show the desktop */
318 pdesk
= IntGetActiveDesktop();
320 co_IntShowDesktop(pdesk
, gpsi
->aiSysMet
[SM_CXSCREEN
], gpsi
->aiSysMet
[SM_CYSCREEN
], TRUE
);
326 IntEndDesktopGraphics(VOID
)
328 if (NULL
!= ScreenDeviceContext
)
329 { // No need to allocate a new dcattr.
330 GreSetDCOwner(ScreenDeviceContext
, GDI_OBJ_HMGR_POWNED
);
331 GreDeleteObject(ScreenDeviceContext
);
332 ScreenDeviceContext
= NULL
;
334 IntHideDesktop(IntGetActiveDesktop());
335 IntDestroyPrimarySurface();
341 return ScreenDeviceContext
;
345 CheckWinstaAttributeAccess(ACCESS_MASK DesiredAccess
)
347 PPROCESSINFO ppi
= PsGetCurrentProcessWin32Process();
348 if ( gpidLogon
!= PsGetCurrentProcessId() )
350 if (!(ppi
->W32PF_flags
& W32PF_IOWINSTA
))
352 ERR("Requires Interactive Window Station\n");
353 EngSetLastError(ERROR_REQUIRES_INTERACTIVE_WINDOWSTATION
);
356 if (!RtlAreAllAccessesGranted(ppi
->amwinsta
, DesiredAccess
))
358 ERR("Access Denied\n");
359 EngSetLastError(ERROR_ACCESS_DENIED
);
367 /* PUBLIC FUNCTIONS ***********************************************************/
370 * NtUserCreateWindowStation
372 * Creates a new window station.
375 * lpszWindowStationName
376 * Pointer to a null-terminated string specifying the name of the
377 * window station to be created. Window station names are
378 * case-insensitive and cannot contain backslash characters (\).
379 * Only members of the Administrators group are allowed to specify a
383 * Requested type of access
386 * Security descriptor
388 * Unknown3, Unknown4, Unknown5, Unknown6
392 * If the function succeeds, the return value is a handle to the newly
393 * created window station. If the specified window station already
394 * exists, the function succeeds and returns a handle to the existing
395 * window station. If the function fails, the return value is NULL.
403 IntCreateWindowStation(
404 OUT HWINSTA
* phWinSta
,
405 IN POBJECT_ATTRIBUTES ObjectAttributes
,
406 IN KPROCESSOR_MODE AccessMode
,
407 IN KPROCESSOR_MODE OwnerMode
,
408 IN ACCESS_MASK dwDesiredAccess
,
417 PWINSTATION_OBJECT WindowStation
;
419 TRACE("IntCreateWindowStation called\n");
424 Status
= ObOpenObjectByName(ObjectAttributes
,
425 ExWindowStationObjectType
,
431 if (NT_SUCCESS(Status
))
433 TRACE("IntCreateWindowStation opened window station '%wZ'\n",
434 ObjectAttributes
->ObjectName
);
440 * No existing window station found, try to create a new one.
443 /* Create the window station object */
444 Status
= ObCreateObject(AccessMode
,
445 ExWindowStationObjectType
,
449 sizeof(WINSTATION_OBJECT
),
452 (PVOID
*)&WindowStation
);
453 if (!NT_SUCCESS(Status
))
455 ERR("ObCreateObject failed for window station '%wZ', Status 0x%08lx\n",
456 ObjectAttributes
->ObjectName
, Status
);
457 SetLastNtError(Status
);
461 /* Initialize the window station */
462 RtlZeroMemory(WindowStation
, sizeof(WINSTATION_OBJECT
));
464 InitializeListHead(&WindowStation
->DesktopListHead
);
465 WindowStation
->dwSessionId
= NtCurrentPeb()->SessionId
;
466 Status
= RtlCreateAtomTable(37, &WindowStation
->AtomTable
);
467 if (!NT_SUCCESS(Status
))
469 ERR("RtlCreateAtomTable failed for window station '%wZ', Status 0x%08lx\n",
470 ObjectAttributes
->ObjectName
, Status
);
471 ObDereferenceObject(WindowStation
);
472 SetLastNtError(Status
);
476 Status
= ObInsertObject(WindowStation
,
482 if (!NT_SUCCESS(Status
))
484 ERR("ObInsertObject failed for window station, Status 0x%08lx\n", Status
);
485 SetLastNtError(Status
);
489 // FIXME! TODO: Add this new window station to a linked list
491 if (InputWindowStation
== NULL
)
493 ERR("Initializing input window station\n");
495 /* Only Winlogon can create the interactive window station */
496 ASSERT(gpidLogon
== PsGetCurrentProcessId());
498 InputWindowStation
= WindowStation
;
499 WindowStation
->Flags
&= ~WSS_NOIO
;
503 UserCreateSystemThread(ST_DESKTOP_THREAD
);
504 UserCreateSystemThread(ST_RIT
);
506 /* Desktop functions require the desktop thread running so wait for it to initialize */
508 KeWaitForSingleObject(gpDesktopThreadStartedEvent
,
517 WindowStation
->Flags
|= WSS_NOIO
;
520 TRACE("IntCreateWindowStation created window station '%wZ' object 0x%p handle 0x%p\n",
521 ObjectAttributes
->ObjectName
, WindowStation
, hWinSta
);
524 EngSetLastError(ERROR_SUCCESS
);
526 return STATUS_SUCCESS
;
530 FreeUserModeWindowStationName(
531 IN OUT PUNICODE_STRING WindowStationName
,
532 IN PUNICODE_STRING TebStaticUnicodeString
,
533 IN OUT POBJECT_ATTRIBUTES UserModeObjectAttributes OPTIONAL
,
534 IN POBJECT_ATTRIBUTES LocalObjectAttributes OPTIONAL
)
538 /* Try to restore the user's UserModeObjectAttributes */
539 if (UserModeObjectAttributes
&& LocalObjectAttributes
)
543 ProbeForWrite(UserModeObjectAttributes
, sizeof(OBJECT_ATTRIBUTES
), sizeof(ULONG
));
544 *UserModeObjectAttributes
= *LocalObjectAttributes
;
546 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
553 /* Free the user-mode memory */
554 if (WindowStationName
&& (WindowStationName
!= TebStaticUnicodeString
))
556 ZwFreeVirtualMemory(ZwCurrentProcess(),
557 (PVOID
*)&WindowStationName
,
564 BuildUserModeWindowStationName(
565 IN OUT POBJECT_ATTRIBUTES UserModeObjectAttributes
,
566 IN OUT POBJECT_ATTRIBUTES LocalObjectAttributes
,
567 OUT PUNICODE_STRING
* WindowStationName
,
568 OUT PUNICODE_STRING
* TebStaticUnicodeString
)
577 *WindowStationName
= NULL
;
578 *TebStaticUnicodeString
= NULL
;
580 /* Retrieve the current process LUID */
581 Status
= GetProcessLuid(NULL
, NULL
, &CallerLuid
);
582 if (!NT_SUCCESS(Status
))
584 ERR("Failed to retrieve the caller LUID, Status 0x%08lx\n", Status
);
588 /* Compute the needed string size */
589 MemSize
= _scwprintf(L
"%wZ\\Service-0x%x-%x$",
590 &gustrWindowStationsDir
,
593 MemSize
= MemSize
* sizeof(WCHAR
) + sizeof(UNICODE_NULL
);
594 if (MemSize
> MAXUSHORT
)
596 ERR("Window station name length is too long.\n");
597 return STATUS_NAME_TOO_LONG
;
599 StrSize
= (USHORT
)MemSize
;
602 * Check whether it's short enough so that we can use the static buffer
603 * in the TEB. Otherwise continue with virtual memory allocation.
605 Teb
= NtCurrentTeb();
606 if (Teb
&& (StrSize
<= sizeof(Teb
->StaticUnicodeBuffer
)))
608 /* We can use the TEB's static unicode string */
609 ASSERT(Teb
->StaticUnicodeString
.Buffer
== Teb
->StaticUnicodeBuffer
);
610 ASSERT(Teb
->StaticUnicodeString
.MaximumLength
== sizeof(Teb
->StaticUnicodeBuffer
));
612 /* Remember the TEB's static unicode string address for later */
613 *TebStaticUnicodeString
= &Teb
->StaticUnicodeString
;
615 *WindowStationName
= *TebStaticUnicodeString
;
616 (*WindowStationName
)->Length
= 0;
620 /* The TEB's static unicode string is too small, allocate some user-mode virtual memory */
621 MemSize
+= ALIGN_UP(sizeof(UNICODE_STRING
), sizeof(PVOID
));
623 /* Allocate the memory in user-mode */
624 Status
= ZwAllocateVirtualMemory(ZwCurrentProcess(),
625 (PVOID
*)WindowStationName
,
630 if (!NT_SUCCESS(Status
))
632 ERR("ZwAllocateVirtualMemory() failed, Status 0x%08lx\n", Status
);
636 RtlInitEmptyUnicodeString(*WindowStationName
,
637 (PWCHAR
)((ULONG_PTR
)*WindowStationName
+
638 ALIGN_UP(sizeof(UNICODE_STRING
), sizeof(PVOID
))),
642 /* Build a valid window station name from the LUID */
643 Status
= RtlStringCbPrintfW((*WindowStationName
)->Buffer
,
644 (*WindowStationName
)->MaximumLength
,
645 L
"%wZ\\Service-0x%x-%x$",
646 &gustrWindowStationsDir
,
649 if (!NT_SUCCESS(Status
))
651 ERR("Impossible to build a valid window station name, Status 0x%08lx\n", Status
);
654 (*WindowStationName
)->Length
= (USHORT
)(wcslen((*WindowStationName
)->Buffer
) * sizeof(WCHAR
));
656 /* Try to update the user's UserModeObjectAttributes */
659 ProbeForWrite(UserModeObjectAttributes
, sizeof(OBJECT_ATTRIBUTES
), sizeof(ULONG
));
660 *LocalObjectAttributes
= *UserModeObjectAttributes
;
662 UserModeObjectAttributes
->ObjectName
= *WindowStationName
;
663 UserModeObjectAttributes
->RootDirectory
= NULL
;
665 Status
= STATUS_SUCCESS
;
667 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
669 Status
= _SEH2_GetExceptionCode();
674 if (!NT_SUCCESS(Status
))
676 /* Release the window station name */
677 FreeUserModeWindowStationName(*WindowStationName
,
678 *TebStaticUnicodeString
,
687 NtUserCreateWindowStation(
688 IN POBJECT_ATTRIBUTES ObjectAttributes
,
689 IN ACCESS_MASK dwDesiredAccess
,
696 NTSTATUS Status
= STATUS_SUCCESS
;
697 HWINSTA hWinSta
= NULL
;
698 OBJECT_ATTRIBUTES LocalObjectAttributes
;
699 PUNICODE_STRING WindowStationName
= NULL
;
700 PUNICODE_STRING TebStaticUnicodeString
= NULL
;
701 KPROCESSOR_MODE OwnerMode
= UserMode
;
703 TRACE("NtUserCreateWindowStation called\n");
705 /* Capture the object attributes and the window station name */
708 ProbeForRead(ObjectAttributes
, sizeof(OBJECT_ATTRIBUTES
), sizeof(ULONG
));
709 LocalObjectAttributes
= *ObjectAttributes
;
710 if (LocalObjectAttributes
.Length
!= sizeof(OBJECT_ATTRIBUTES
))
712 ERR("Invalid ObjectAttributes length!\n");
713 Status
= STATUS_INVALID_PARAMETER
;
718 * Check whether the caller provided a window station name together
719 * with a RootDirectory handle.
721 * If the caller did not provide a window station name, build a new one
722 * based on the logon session identifier for the calling process.
723 * The new name is allocated in user-mode, as the rest of ObjectAttributes
724 * already is, so that the validation performed by the Object Manager
725 * can be done adequately.
727 if ((LocalObjectAttributes
.ObjectName
== NULL
||
728 LocalObjectAttributes
.ObjectName
->Buffer
== NULL
||
729 LocalObjectAttributes
.ObjectName
->Length
== 0 ||
730 LocalObjectAttributes
.ObjectName
->Buffer
[0] == UNICODE_NULL
)
732 LocalObjectAttributes.RootDirectory == NULL */)
734 /* No, build the new window station name */
735 Status
= BuildUserModeWindowStationName(ObjectAttributes
,
736 &LocalObjectAttributes
,
738 &TebStaticUnicodeString
);
739 if (!NT_SUCCESS(Status
))
741 ERR("BuildUserModeWindowStationName() failed, Status 0x%08lx\n", Status
);
744 OwnerMode
= KernelMode
;
747 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
749 Status
=_SEH2_GetExceptionCode();
750 ERR("ObjectAttributes capture failed, Status 0x%08lx\n", Status
);
754 if (!NT_SUCCESS(Status
))
756 SetLastNtError(Status
);
760 UserEnterExclusive();
762 /* Create the window station */
763 Status
= IntCreateWindowStation(&hWinSta
,
775 if (NT_SUCCESS(Status
))
777 TRACE("NtUserCreateWindowStation created window station '%wZ' with handle 0x%p\n",
778 ObjectAttributes
->ObjectName
, hWinSta
);
782 ASSERT(hWinSta
== NULL
);
783 ERR("NtUserCreateWindowStation failed to create window station '%wZ', Status 0x%08lx\n",
784 ObjectAttributes
->ObjectName
, Status
);
787 /* Try to restore the user's ObjectAttributes and release the window station name */
788 FreeUserModeWindowStationName(WindowStationName
,
789 TebStaticUnicodeString
,
790 (OwnerMode
== KernelMode
? ObjectAttributes
: NULL
),
791 &LocalObjectAttributes
);
793 if (!NT_SUCCESS(Status
))
795 ASSERT(hWinSta
== NULL
);
796 SetLastNtError(Status
);
803 * NtUserOpenWindowStation
805 * Opens an existing window station.
808 * lpszWindowStationName
809 * Name of the existing window station.
812 * Requested type of access.
815 * If the function succeeds, the return value is the handle to the
816 * specified window station. If the function fails, the return value
820 * The returned handle can be closed with NtUserCloseWindowStation.
828 NtUserOpenWindowStation(
829 IN POBJECT_ATTRIBUTES ObjectAttributes
,
830 IN ACCESS_MASK dwDesiredAccess
)
832 NTSTATUS Status
= STATUS_SUCCESS
;
833 HWINSTA hWinSta
= NULL
;
834 OBJECT_ATTRIBUTES LocalObjectAttributes
;
835 PUNICODE_STRING WindowStationName
= NULL
;
836 PUNICODE_STRING TebStaticUnicodeString
= NULL
;
837 KPROCESSOR_MODE OwnerMode
= UserMode
;
839 TRACE("NtUserOpenWindowStation called\n");
841 /* Capture the object attributes and the window station name */
844 ProbeForRead(ObjectAttributes
, sizeof(OBJECT_ATTRIBUTES
), sizeof(ULONG
));
845 LocalObjectAttributes
= *ObjectAttributes
;
846 if (LocalObjectAttributes
.Length
!= sizeof(OBJECT_ATTRIBUTES
))
848 ERR("Invalid ObjectAttributes length!\n");
849 Status
= STATUS_INVALID_PARAMETER
;
854 * Check whether the caller did not provide a window station name,
855 * or provided the special "Service-0x00000000-00000000$" name.
857 * NOTE: On Windows, the special "Service-0x00000000-00000000$" string
858 * is used instead of an empty name (observed when API-monitoring
859 * OpenWindowStation() called with an empty window station name).
861 if ((LocalObjectAttributes
.ObjectName
== NULL
||
862 LocalObjectAttributes
.ObjectName
->Buffer
== NULL
||
863 LocalObjectAttributes
.ObjectName
->Length
== 0 ||
864 LocalObjectAttributes
.ObjectName
->Buffer
[0] == UNICODE_NULL
)
866 LocalObjectAttributes.RootDirectory == NULL */)
868 /* No, remember that for later */
869 LocalObjectAttributes
.ObjectName
= NULL
;
871 if (LocalObjectAttributes
.ObjectName
&&
872 LocalObjectAttributes
.ObjectName
->Length
==
873 sizeof(L
"Service-0x00000000-00000000$") - sizeof(UNICODE_NULL
) &&
874 _wcsnicmp(LocalObjectAttributes
.ObjectName
->Buffer
,
875 L
"Service-0x00000000-00000000$",
876 LocalObjectAttributes
.ObjectName
->Length
/ sizeof(WCHAR
)) == 0)
878 /* No, remember that for later */
879 LocalObjectAttributes
.ObjectName
= NULL
;
883 * If the caller did not provide a window station name, build a new one
884 * based on the logon session identifier for the calling process.
885 * The new name is allocated in user-mode, as the rest of ObjectAttributes
886 * already is, so that the validation performed by the Object Manager
887 * can be done adequately.
889 if (!LocalObjectAttributes
.ObjectName
)
891 /* No, build the new window station name */
892 Status
= BuildUserModeWindowStationName(ObjectAttributes
,
893 &LocalObjectAttributes
,
895 &TebStaticUnicodeString
);
896 if (!NT_SUCCESS(Status
))
898 ERR("BuildUserModeWindowStationName() failed, Status 0x%08lx\n", Status
);
901 OwnerMode
= KernelMode
;
904 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
906 Status
=_SEH2_GetExceptionCode();
907 ERR("ObjectAttributes capture failed, Status 0x%08lx\n", Status
);
911 if (!NT_SUCCESS(Status
))
913 SetLastNtError(Status
);
917 /* Open the window station */
918 Status
= ObOpenObjectByName(ObjectAttributes
,
919 ExWindowStationObjectType
,
925 if (NT_SUCCESS(Status
))
927 TRACE("NtUserOpenWindowStation opened window station '%wZ' with handle 0x%p\n",
928 ObjectAttributes
->ObjectName
, hWinSta
);
932 ASSERT(hWinSta
== NULL
);
933 ERR("NtUserOpenWindowStation failed to open window station '%wZ', Status 0x%08lx\n",
934 ObjectAttributes
->ObjectName
, Status
);
937 /* Try to restore the user's ObjectAttributes and release the window station name */
938 FreeUserModeWindowStationName(WindowStationName
,
939 TebStaticUnicodeString
,
940 (OwnerMode
== KernelMode
? ObjectAttributes
: NULL
),
941 &LocalObjectAttributes
);
943 if (!NT_SUCCESS(Status
))
945 ASSERT(hWinSta
== NULL
);
946 SetLastNtError(Status
);
953 * NtUserCloseWindowStation
955 * Closes a window station handle.
959 * Handle to the window station.
965 * The window station handle can be created with NtUserCreateWindowStation
966 * or NtUserOpenWindowStation. Attempts to close a handle to the window
967 * station assigned to the calling process will fail.
975 NtUserCloseWindowStation(
978 PWINSTATION_OBJECT Object
;
981 TRACE("NtUserCloseWindowStation called (%p)\n", hWinSta
);
983 if (hWinSta
== UserGetProcessWindowStation())
985 ERR("Attempted to close process window station\n");
989 Status
= IntValidateWindowStationHandle(hWinSta
,
994 if (!NT_SUCCESS(Status
))
996 ERR("Validation of window station handle (%p) failed\n", hWinSta
);
1000 ObDereferenceObject(Object
);
1002 TRACE("Closing window station handle (%p)\n", hWinSta
);
1004 Status
= ObCloseHandle(hWinSta
, UserMode
);
1005 if (!NT_SUCCESS(Status
))
1007 SetLastNtError(Status
);
1015 * NtUserGetObjectInformation
1017 * The NtUserGetObjectInformation function retrieves information about a
1018 * window station or desktop object.
1022 * Handle to the window station or desktop object for which to
1023 * return information. This can be a handle of type HDESK or HWINSTA
1024 * (for example, a handle returned by NtUserCreateWindowStation,
1025 * NtUserOpenWindowStation, NtUserCreateDesktop, or NtUserOpenDesktop).
1028 * Specifies the object information to be retrieved.
1031 * Pointer to a buffer to receive the object information.
1034 * Specifies the size, in bytes, of the buffer pointed to by the
1038 * Pointer to a variable receiving the number of bytes required to
1039 * store the requested information. If this variable's value is
1040 * greater than the value of the nLength parameter when the function
1041 * returns, the function returns FALSE, and none of the information
1042 * is copied to the pvInfo buffer. If the value of the variable pointed
1043 * to by lpnLengthNeeded is less than or equal to the value of nLength,
1044 * the entire information block is copied.
1047 * If the function succeeds, the return value is nonzero. If the function
1048 * fails, the return value is zero.
1055 NtUserGetObjectInformation(
1058 PVOID pvInformation
,
1060 PDWORD nLengthNeeded
)
1063 PWINSTATION_OBJECT WinStaObject
= NULL
;
1064 PDESKTOP DesktopObject
= NULL
;
1065 POBJECT_HEADER ObjectHeader
;
1066 POBJECT_HEADER_NAME_INFO NameInfo
;
1067 OBJECT_HANDLE_INFORMATION HandleInfo
;
1068 USEROBJECTFLAGS ObjectFlags
;
1069 PUNICODE_STRING pStrNameU
= NULL
;
1070 PVOID pvData
= NULL
;
1071 SIZE_T nDataSize
= 0;
1076 ProbeForWrite(nLengthNeeded
, sizeof(*nLengthNeeded
), 1);
1077 ProbeForWrite(pvInformation
, nLength
, 1);
1079 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
1081 SetLastNtError(_SEH2_GetExceptionCode());
1086 /* Try window station */
1087 TRACE("Trying to open window station 0x%p\n", hObject
);
1088 Status
= ObReferenceObjectByHandle(hObject
,
1090 ExWindowStationObjectType
,
1092 (PVOID
*)&WinStaObject
,
1095 if (Status
== STATUS_OBJECT_TYPE_MISMATCH
)
1098 TRACE("Trying to open desktop %p\n", hObject
);
1099 WinStaObject
= NULL
;
1100 Status
= IntValidateDesktopHandle(hObject
,
1106 if (!NT_SUCCESS(Status
))
1108 ERR("Failed: 0x%x\n", Status
);
1112 TRACE("WinSta or Desktop opened!\n");
1119 ObjectFlags
.fReserved
= FALSE
;
1120 ObjectFlags
.fInherit
= !!(HandleInfo
.HandleAttributes
& OBJ_INHERIT
);
1122 ObjectFlags
.dwFlags
= 0;
1123 if (WinStaObject
!= NULL
)
1125 if (!(WinStaObject
->Flags
& WSS_NOIO
))
1126 ObjectFlags
.dwFlags
|= WSF_VISIBLE
;
1128 else if (DesktopObject
!= NULL
)
1130 FIXME("Setting DF_ALLOWOTHERACCOUNTHOOK is unimplemented.\n");
1134 ERR("No associated WinStaObject nor DesktopObject!\n");
1137 pvData
= &ObjectFlags
;
1138 nDataSize
= sizeof(ObjectFlags
);
1139 Status
= STATUS_SUCCESS
;
1145 if (WinStaObject
!= NULL
)
1147 ObjectHeader
= OBJECT_TO_OBJECT_HEADER(WinStaObject
);
1148 NameInfo
= OBJECT_HEADER_TO_NAME_INFO(ObjectHeader
);
1150 if (NameInfo
&& (NameInfo
->Name
.Length
> 0))
1152 /* Named window station */
1153 pStrNameU
= &NameInfo
->Name
;
1154 nDataSize
= pStrNameU
->Length
+ sizeof(UNICODE_NULL
);
1158 /* Unnamed window station (should never happen!) */
1161 nDataSize
= sizeof(UNICODE_NULL
);
1163 Status
= STATUS_SUCCESS
;
1165 else if (DesktopObject
!= NULL
)
1167 pvData
= DesktopObject
->pDeskInfo
->szDesktopName
;
1168 nDataSize
= (wcslen(DesktopObject
->pDeskInfo
->szDesktopName
) + 1) * sizeof(WCHAR
);
1169 Status
= STATUS_SUCCESS
;
1173 Status
= STATUS_INVALID_PARAMETER
;
1180 if (WinStaObject
!= NULL
)
1182 ObjectHeader
= OBJECT_TO_OBJECT_HEADER(WinStaObject
);
1183 pStrNameU
= &ObjectHeader
->Type
->Name
;
1184 nDataSize
= pStrNameU
->Length
+ sizeof(UNICODE_NULL
);
1185 Status
= STATUS_SUCCESS
;
1187 else if (DesktopObject
!= NULL
)
1189 ObjectHeader
= OBJECT_TO_OBJECT_HEADER(DesktopObject
);
1190 pStrNameU
= &ObjectHeader
->Type
->Name
;
1191 nDataSize
= pStrNameU
->Length
+ sizeof(UNICODE_NULL
);
1192 Status
= STATUS_SUCCESS
;
1196 Status
= STATUS_INVALID_PARAMETER
;
1202 Status
= STATUS_NOT_IMPLEMENTED
;
1203 ERR("UOI_USER_SID unimplemented!\n");
1207 Status
= STATUS_INVALID_PARAMETER
;
1212 if ((Status
== STATUS_SUCCESS
) && (nLength
< nDataSize
))
1213 Status
= STATUS_BUFFER_TOO_SMALL
;
1218 *nLengthNeeded
= nDataSize
;
1220 /* Try to copy data to caller */
1221 if (Status
== STATUS_SUCCESS
&& (nDataSize
> 0))
1223 TRACE("Trying to copy data to caller (len = %lu, len needed = %lu)\n", nLength
, nDataSize
);
1227 RtlCopyMemory(pvInformation
, pvData
, nDataSize
);
1231 /* Copy and NULL-terminate the string */
1232 RtlCopyMemory(pvInformation
, pStrNameU
->Buffer
, pStrNameU
->Length
);
1233 ((PWCHAR
)pvInformation
)[pStrNameU
->Length
/ sizeof(WCHAR
)] = UNICODE_NULL
;
1237 /* Zero the memory */
1238 RtlZeroMemory(pvInformation
, nDataSize
);
1242 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
1244 Status
= _SEH2_GetExceptionCode();
1248 /* Release objects */
1249 if (DesktopObject
!= NULL
)
1250 ObDereferenceObject(DesktopObject
);
1251 if (WinStaObject
!= NULL
)
1252 ObDereferenceObject(WinStaObject
);
1254 if (!NT_SUCCESS(Status
))
1256 SetLastNtError(Status
);
1264 * NtUserSetObjectInformation
1266 * The NtUserSetObjectInformation function sets information about a
1267 * window station or desktop object.
1271 * Handle to the window station or desktop object for which to set
1272 * object information. This value can be a handle of type HDESK or
1276 * Specifies the object information to be set.
1279 * Pointer to a buffer containing the object information.
1282 * Specifies the size, in bytes, of the information contained in the
1283 * buffer pointed to by pvInfo.
1286 * If the function succeeds, the return value is nonzero. If the function
1287 * fails the return value is zero.
1295 NtUserSetObjectInformation(
1298 PVOID pvInformation
,
1301 /* FIXME: ZwQueryObject */
1302 /* FIXME: ZwSetInformationObject */
1303 SetLastNtError(STATUS_UNSUCCESSFUL
);
1309 UserGetProcessWindowStation(VOID
)
1311 PPROCESSINFO ppi
= PsGetCurrentProcessWin32Process();
1313 return ppi
->hwinsta
;
1318 * NtUserGetProcessWindowStation
1320 * Returns a handle to the current process window station.
1323 * If the function succeeds, the return value is handle to the window
1324 * station assigned to the current process. If the function fails, the
1325 * return value is NULL.
1332 NtUserGetProcessWindowStation(VOID
)
1334 return UserGetProcessWindowStation();
1338 UserSetProcessWindowStation(HWINSTA hWindowStation
)
1342 OBJECT_HANDLE_INFORMATION ObjectHandleInfo
;
1343 PWINSTATION_OBJECT NewWinSta
= NULL
, OldWinSta
;
1344 HWINSTA hCacheWinSta
;
1346 ppi
= PsGetCurrentProcessWin32Process();
1348 /* Reference the new window station */
1349 if (hWindowStation
!= NULL
)
1351 Status
= IntValidateWindowStationHandle(hWindowStation
,
1356 if (!NT_SUCCESS(Status
))
1358 TRACE("Validation of window station handle 0x%p failed\n", hWindowStation
);
1359 SetLastNtError(Status
);
1364 OldWinSta
= ppi
->prpwinsta
;
1365 hCacheWinSta
= PsGetProcessWin32WindowStation(ppi
->peProcess
);
1367 /* Dereference the previous window station */
1368 if (OldWinSta
!= NULL
)
1370 ObDereferenceObject(OldWinSta
);
1374 * FIXME: Don't allow changing the window station if there are threads that are attached to desktops and own GUI objects?
1377 /* Close the cached EPROCESS window station handle if needed */
1378 if (hCacheWinSta
!= NULL
)
1380 /* Reference the window station */
1381 Status
= ObReferenceObjectByHandle(hCacheWinSta
,
1383 ExWindowStationObjectType
,
1387 if (!NT_SUCCESS(Status
))
1389 ERR("Failed to reference the inherited window station, Status 0x%08lx\n", Status
);
1390 /* We failed, reset the cache */
1391 hCacheWinSta
= NULL
;
1392 PsSetProcessWindowStation(ppi
->peProcess
, hCacheWinSta
);
1397 * Close the old handle and reset the cache only
1398 * if we are setting a different window station.
1400 if (NewWinSta
!= OldWinSta
)
1402 ObCloseHandle(hCacheWinSta
, UserMode
);
1403 hCacheWinSta
= NULL
;
1404 PsSetProcessWindowStation(ppi
->peProcess
, hCacheWinSta
);
1407 /* Dereference the window station */
1408 ObDereferenceObject(OldWinSta
);
1412 /* Duplicate and save a new cached EPROCESS window station handle */
1413 if ((hCacheWinSta
== NULL
) && (hWindowStation
!= NULL
))
1415 Status
= ZwDuplicateObject(ZwCurrentProcess(),
1418 (PHANDLE
)&hCacheWinSta
,
1421 DUPLICATE_SAME_ACCESS
);
1422 if (!NT_SUCCESS(Status
))
1424 ERR("UserSetProcessWindowStation: Failed to duplicate the window station handle, Status 0x%08lx\n", Status
);
1428 PsSetProcessWindowStation(ppi
->peProcess
, hCacheWinSta
);
1432 ppi
->prpwinsta
= NewWinSta
;
1433 ppi
->hwinsta
= hWindowStation
;
1434 ppi
->amwinsta
= hWindowStation
!= NULL
? ObjectHandleInfo
.GrantedAccess
: 0;
1435 TRACE("WS : Granted Access 0x%08lx\n",ppi
->amwinsta
);
1437 if (RtlAreAllAccessesGranted(ppi
->amwinsta
, WINSTA_READSCREEN
))
1439 ppi
->W32PF_flags
|= W32PF_READSCREENACCESSGRANTED
;
1443 ppi
->W32PF_flags
&= ~W32PF_READSCREENACCESSGRANTED
;
1446 if (NewWinSta
&& !(NewWinSta
->Flags
& WSS_NOIO
))
1448 ppi
->W32PF_flags
|= W32PF_IOWINSTA
;
1450 else /* Might be closed if the handle is NULL */
1452 ppi
->W32PF_flags
&= ~W32PF_IOWINSTA
;
1458 * NtUserSetProcessWindowStation
1460 * Assigns a window station to the current process.
1464 * Handle to the window station.
1474 NtUserSetProcessWindowStation(HWINSTA hWindowStation
)
1478 UserEnterExclusive();
1480 ret
= UserSetProcessWindowStation(hWindowStation
);
1488 * NtUserLockWindowStation
1490 * Locks switching desktops. Only the logon application is allowed to call this function.
1497 NtUserLockWindowStation(HWINSTA hWindowStation
)
1499 PWINSTATION_OBJECT Object
;
1502 TRACE("About to set process window station with handle (%p)\n",
1505 if (gpidLogon
!= PsGetCurrentProcessId())
1507 ERR("Unauthorized process attempted to lock the window station!\n");
1508 EngSetLastError(ERROR_ACCESS_DENIED
);
1512 Status
= IntValidateWindowStationHandle(hWindowStation
,
1517 if (!NT_SUCCESS(Status
))
1519 TRACE("Validation of window station handle (%p) failed\n",
1521 SetLastNtError(Status
);
1525 Object
->Flags
|= WSS_LOCKED
;
1527 ObDereferenceObject(Object
);
1532 * NtUserUnlockWindowStation
1534 * Unlocks switching desktops. Only the logon application is allowed to call this function.
1541 NtUserUnlockWindowStation(HWINSTA hWindowStation
)
1543 PWINSTATION_OBJECT Object
;
1547 TRACE("About to set process window station with handle (%p)\n",
1550 if (gpidLogon
!= PsGetCurrentProcessId())
1552 ERR("Unauthorized process attempted to unlock the window station!\n");
1553 EngSetLastError(ERROR_ACCESS_DENIED
);
1557 Status
= IntValidateWindowStationHandle(hWindowStation
,
1562 if (!NT_SUCCESS(Status
))
1564 TRACE("Validation of window station handle (%p) failed\n",
1566 SetLastNtError(Status
);
1570 Ret
= (Object
->Flags
& WSS_LOCKED
) == WSS_LOCKED
;
1571 Object
->Flags
&= ~WSS_LOCKED
;
1573 ObDereferenceObject(Object
);
1577 static NTSTATUS FASTCALL
1578 BuildWindowStationNameList(
1581 PULONG pRequiredSize
)
1583 OBJECT_ATTRIBUTES ObjectAttributes
;
1585 HANDLE DirectoryHandle
;
1586 char InitialBuffer
[256], *Buffer
;
1587 ULONG Context
, ReturnLength
, BufferSize
;
1589 POBJECT_DIRECTORY_INFORMATION DirEntry
;
1593 // FIXME: Fully wrong! Since, by calling NtUserCreateWindowStation
1594 // with judicious parameters one can create window stations elsewhere
1595 // than in Windows\WindowStations directory, Win32k definitely MUST
1596 // maintain a list of window stations it has created, and not rely
1597 // on the enumeration of Windows\WindowStations !!!
1601 * Try to open the directory.
1603 InitializeObjectAttributes(&ObjectAttributes
,
1604 &gustrWindowStationsDir
,
1605 OBJ_CASE_INSENSITIVE
| OBJ_KERNEL_HANDLE
,
1609 Status
= ZwOpenDirectoryObject(&DirectoryHandle
,
1613 if (!NT_SUCCESS(Status
))
1618 /* First try to query the directory using a fixed-size buffer */
1621 Status
= ZwQueryDirectoryObject(DirectoryHandle
,
1623 sizeof(InitialBuffer
),
1628 if (NT_SUCCESS(Status
))
1630 if (STATUS_NO_MORE_ENTRIES
== ZwQueryDirectoryObject(DirectoryHandle
, NULL
, 0, FALSE
,
1631 FALSE
, &Context
, NULL
))
1633 /* Our fixed-size buffer is large enough */
1634 Buffer
= InitialBuffer
;
1640 /* Need a larger buffer, check how large exactly */
1641 Status
= ZwQueryDirectoryObject(DirectoryHandle
, NULL
, 0, FALSE
, TRUE
, &Context
,
1643 if (!NT_SUCCESS(Status
))
1645 ERR("ZwQueryDirectoryObject failed\n");
1646 ZwClose(DirectoryHandle
);
1650 BufferSize
= ReturnLength
;
1651 Buffer
= ExAllocatePoolWithTag(PagedPool
, BufferSize
, TAG_WINSTA
);
1654 ZwClose(DirectoryHandle
);
1655 return STATUS_NO_MEMORY
;
1658 /* We should have a sufficiently large buffer now */
1660 Status
= ZwQueryDirectoryObject(DirectoryHandle
, Buffer
, BufferSize
,
1661 FALSE
, TRUE
, &Context
, &ReturnLength
);
1662 if (! NT_SUCCESS(Status
) ||
1663 STATUS_NO_MORE_ENTRIES
!= ZwQueryDirectoryObject(DirectoryHandle
, NULL
, 0, FALSE
,
1664 FALSE
, &Context
, NULL
))
1666 /* Something went wrong, maybe someone added a directory entry? Just give up. */
1667 ExFreePoolWithTag(Buffer
, TAG_WINSTA
);
1668 ZwClose(DirectoryHandle
);
1669 return NT_SUCCESS(Status
) ? STATUS_INTERNAL_ERROR
: Status
;
1673 ZwClose(DirectoryHandle
);
1676 * Count the required size of buffer.
1678 ReturnLength
= sizeof(DWORD
);
1680 for (DirEntry
= (POBJECT_DIRECTORY_INFORMATION
) Buffer
;
1681 0 != DirEntry
->Name
.Length
;
1684 ReturnLength
+= DirEntry
->Name
.Length
+ sizeof(WCHAR
);
1687 TRACE("Required size: %lu Entry count: %lu\n", ReturnLength
, EntryCount
);
1688 if (NULL
!= pRequiredSize
)
1690 Status
= MmCopyToCaller(pRequiredSize
, &ReturnLength
, sizeof(ULONG
));
1691 if (! NT_SUCCESS(Status
))
1693 if (Buffer
!= InitialBuffer
)
1695 ExFreePoolWithTag(Buffer
, TAG_WINSTA
);
1697 return STATUS_BUFFER_TOO_SMALL
;
1702 * Check if the supplied buffer is large enough.
1704 if (dwSize
< ReturnLength
)
1706 if (Buffer
!= InitialBuffer
)
1708 ExFreePoolWithTag(Buffer
, TAG_WINSTA
);
1710 return STATUS_BUFFER_TOO_SMALL
;
1714 * Generate the resulting buffer contents.
1716 Status
= MmCopyToCaller(lpBuffer
, &EntryCount
, sizeof(DWORD
));
1717 if (! NT_SUCCESS(Status
))
1719 if (Buffer
!= InitialBuffer
)
1721 ExFreePoolWithTag(Buffer
, TAG_WINSTA
);
1725 lpBuffer
= (PVOID
) ((PCHAR
) lpBuffer
+ sizeof(DWORD
));
1728 for (DirEntry
= (POBJECT_DIRECTORY_INFORMATION
) Buffer
;
1729 0 != DirEntry
->Name
.Length
;
1732 Status
= MmCopyToCaller(lpBuffer
, DirEntry
->Name
.Buffer
, DirEntry
->Name
.Length
);
1733 if (! NT_SUCCESS(Status
))
1735 if (Buffer
!= InitialBuffer
)
1737 ExFreePoolWithTag(Buffer
, TAG_WINSTA
);
1741 lpBuffer
= (PVOID
) ((PCHAR
) lpBuffer
+ DirEntry
->Name
.Length
);
1742 Status
= MmCopyToCaller(lpBuffer
, &NullWchar
, sizeof(WCHAR
));
1743 if (! NT_SUCCESS(Status
))
1745 if (Buffer
!= InitialBuffer
)
1747 ExFreePoolWithTag(Buffer
, TAG_WINSTA
);
1751 lpBuffer
= (PVOID
) ((PCHAR
) lpBuffer
+ sizeof(WCHAR
));
1757 if (Buffer
!= InitialBuffer
)
1759 ExFreePoolWithTag(Buffer
, TAG_WINSTA
);
1762 return STATUS_SUCCESS
;
1765 static NTSTATUS FASTCALL
1766 BuildDesktopNameList(
1767 HWINSTA hWindowStation
,
1770 PULONG pRequiredSize
)
1773 PWINSTATION_OBJECT WindowStation
;
1774 PLIST_ENTRY DesktopEntry
;
1775 PDESKTOP DesktopObject
;
1779 UNICODE_STRING DesktopName
;
1781 Status
= IntValidateWindowStationHandle(hWindowStation
,
1786 if (! NT_SUCCESS(Status
))
1792 * Count the required size of buffer.
1794 ReturnLength
= sizeof(DWORD
);
1796 for (DesktopEntry
= WindowStation
->DesktopListHead
.Flink
;
1797 DesktopEntry
!= &WindowStation
->DesktopListHead
;
1798 DesktopEntry
= DesktopEntry
->Flink
)
1800 DesktopObject
= CONTAINING_RECORD(DesktopEntry
, DESKTOP
, ListEntry
);
1801 RtlInitUnicodeString(&DesktopName
, DesktopObject
->pDeskInfo
->szDesktopName
);
1802 ReturnLength
+= DesktopName
.Length
+ sizeof(WCHAR
);
1805 TRACE("Required size: %lu Entry count: %lu\n", ReturnLength
, EntryCount
);
1806 if (NULL
!= pRequiredSize
)
1808 Status
= MmCopyToCaller(pRequiredSize
, &ReturnLength
, sizeof(ULONG
));
1809 if (! NT_SUCCESS(Status
))
1811 ObDereferenceObject(WindowStation
);
1812 return STATUS_BUFFER_TOO_SMALL
;
1817 * Check if the supplied buffer is large enough.
1819 if (dwSize
< ReturnLength
)
1821 ObDereferenceObject(WindowStation
);
1822 return STATUS_BUFFER_TOO_SMALL
;
1826 * Generate the resulting buffer contents.
1828 Status
= MmCopyToCaller(lpBuffer
, &EntryCount
, sizeof(DWORD
));
1829 if (! NT_SUCCESS(Status
))
1831 ObDereferenceObject(WindowStation
);
1834 lpBuffer
= (PVOID
) ((PCHAR
) lpBuffer
+ sizeof(DWORD
));
1837 for (DesktopEntry
= WindowStation
->DesktopListHead
.Flink
;
1838 DesktopEntry
!= &WindowStation
->DesktopListHead
;
1839 DesktopEntry
= DesktopEntry
->Flink
)
1841 DesktopObject
= CONTAINING_RECORD(DesktopEntry
, DESKTOP
, ListEntry
);
1842 RtlInitUnicodeString(&DesktopName
, DesktopObject
->pDeskInfo
->szDesktopName
);
1843 Status
= MmCopyToCaller(lpBuffer
, DesktopName
.Buffer
, DesktopName
.Length
);
1844 if (! NT_SUCCESS(Status
))
1846 ObDereferenceObject(WindowStation
);
1849 lpBuffer
= (PVOID
) ((PCHAR
)lpBuffer
+ DesktopName
.Length
);
1850 Status
= MmCopyToCaller(lpBuffer
, &NullWchar
, sizeof(WCHAR
));
1851 if (! NT_SUCCESS(Status
))
1853 ObDereferenceObject(WindowStation
);
1856 lpBuffer
= (PVOID
) ((PCHAR
) lpBuffer
+ sizeof(WCHAR
));
1860 * Clean up and return
1862 ObDereferenceObject(WindowStation
);
1863 return STATUS_SUCCESS
;
1867 * NtUserBuildNameList
1869 * Function used for enumeration of desktops or window stations.
1873 * For enumeration of window stations this parameter must be set to
1874 * zero. Otherwise it's handle for window station.
1877 * Size of buffer passed by caller.
1880 * Buffer passed by caller. If the function succeeds, the buffer is
1881 * filled with window station/desktop count (in first DWORD) and
1882 * NULL-terminated window station/desktop names.
1885 * If the function succeeds, this is the number of bytes copied.
1886 * Otherwise it's size of buffer needed for function to succeed.
1893 NtUserBuildNameList(
1894 HWINSTA hWindowStation
,
1897 PULONG pRequiredSize
)
1899 /* The WindowStation name list and desktop name list are build in completely
1900 different ways. Call the appropriate function */
1901 return NULL
== hWindowStation
? BuildWindowStationNameList(dwSize
, lpBuffer
, pRequiredSize
) :
1902 BuildDesktopNameList(hWindowStation
, dwSize
, lpBuffer
, pRequiredSize
);
1909 NtUserSetLogonNotifyWindow(HWND hWnd
)
1911 if (gpidLogon
!= PsGetCurrentProcessId())
1916 if (!IntIsWindow(hWnd
))
1928 NtUserLockWorkStation(VOID
)
1931 PTHREADINFO pti
= PsGetCurrentThreadWin32Thread();
1933 UserEnterExclusive();
1935 if (pti
->rpdesk
== IntGetActiveDesktop())
1937 ret
= UserPostMessage(hwndSAS
, WM_LOGONNOTIFY
, LN_LOCK_WORKSTATION
, 0);
1951 NtUserSetWindowStationUser(
1952 IN HWINSTA hWindowStation
,
1954 IN PSID psid OPTIONAL
,
1959 PWINSTATION_OBJECT WindowStation
= NULL
;
1962 UserEnterExclusive();
1964 if (gpidLogon
!= PsGetCurrentProcessId())
1966 EngSetLastError(ERROR_ACCESS_DENIED
);
1970 /* Validate the window station */
1971 Status
= IntValidateWindowStationHandle(hWindowStation
,
1976 if (!NT_SUCCESS(Status
))
1981 /* Capture the user LUID */
1984 ProbeForRead(pluid
, sizeof(LUID
), 1);
1987 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
1989 Status
= _SEH2_GetExceptionCode();
1990 _SEH2_YIELD(goto Leave
);
1994 /* Reset the window station user LUID */
1995 RtlZeroMemory(&WindowStation
->luidUser
, sizeof(LUID
));
1997 /* Reset the window station user SID */
1998 if (WindowStation
->psidUser
)
2000 ExFreePoolWithTag(WindowStation
->psidUser
, USERTAG_SECURITY
);
2001 WindowStation
->psidUser
= NULL
;
2004 /* Copy the new user SID if one has been provided */
2007 WindowStation
->psidUser
= ExAllocatePoolWithTag(PagedPool
, size
, USERTAG_SECURITY
);
2008 if (WindowStation
->psidUser
== NULL
)
2010 EngSetLastError(ERROR_OUTOFMEMORY
);
2014 Status
= STATUS_SUCCESS
;
2017 ProbeForRead(psid
, size
, 1);
2018 RtlCopyMemory(WindowStation
->psidUser
, psid
, size
);
2020 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
2022 Status
= _SEH2_GetExceptionCode();
2026 if (!NT_SUCCESS(Status
))
2028 ExFreePoolWithTag(WindowStation
->psidUser
, USERTAG_SECURITY
);
2029 WindowStation
->psidUser
= NULL
;
2034 /* Copy the new user LUID */
2035 WindowStation
->luidUser
= luidUser
;
2041 ObDereferenceObject(WindowStation
);