2 * ReactOS W32 Subsystem
3 * Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003 ReactOS Team
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 * COPYRIGHT: See COPYING in the top level directory
20 * PROJECT: ReactOS kernel
21 * PURPOSE: Window stations
22 * FILE: subsys/win32k/ntuser/winsta.c
23 * PROGRAMER: Casper S. Hornstrup (chorns@users.sourceforge.net)
25 * 06-06-2001 CSH Created
26 * NOTES: Exported functions set the Win32 last error value
27 * on errors. The value can be retrieved with the Win32
28 * function GetLastError().
29 * TODO: The process window station is created on
30 * the first USER32/GDI32 call not related
31 * to window station/desktop handling
34 /* INCLUDES ******************************************************************/
41 /* GLOBALS *******************************************************************/
43 /* Currently active window station */
44 PWINSTATION_OBJECT InputWindowStation
= NULL
;
46 /* INITALIZATION FUNCTIONS ****************************************************/
48 static GENERIC_MAPPING IntWindowStationMapping
=
50 STANDARD_RIGHTS_READ
| WINSTA_ENUMDESKTOPS
| WINSTA_ENUMERATE
| WINSTA_READATTRIBUTES
| WINSTA_READSCREEN
,
51 STANDARD_RIGHTS_WRITE
| WINSTA_ACCESSCLIPBOARD
| WINSTA_CREATEDESKTOP
| WINSTA_WRITEATTRIBUTES
,
52 STANDARD_RIGHTS_EXECUTE
| WINSTA_ACCESSGLOBALATOMS
| WINSTA_EXITWINDOWS
,
53 STANDARD_RIGHTS_REQUIRED
| WINSTA_ACCESSCLIPBOARD
| WINSTA_ACCESSGLOBALATOMS
| WINSTA_CREATEDESKTOP
|
54 WINSTA_ENUMDESKTOPS
| WINSTA_ENUMERATE
| WINSTA_EXITWINDOWS
|
55 WINSTA_READATTRIBUTES
| WINSTA_READSCREEN
| WINSTA_WRITEATTRIBUTES
59 InitWindowStationImpl(VOID
)
61 OBJECT_ATTRIBUTES ObjectAttributes
;
62 HANDLE WindowStationsDirectory
;
63 UNICODE_STRING UnicodeString
;
67 * Create the '\Windows\WindowStations' directory
70 RtlInitUnicodeString(&UnicodeString
, WINSTA_ROOT_NAME
);
71 InitializeObjectAttributes(&ObjectAttributes
, &UnicodeString
,
73 Status
= ZwCreateDirectoryObject(&WindowStationsDirectory
, 0,
75 if (!NT_SUCCESS(Status
))
77 DPRINT("Could not create \\Windows\\WindowStations directory "
78 "(Status 0x%X)\n", Status
);
82 /* Set Winsta Object Attributes */
83 ExWindowStationObjectType
->TypeInfo
.DefaultNonPagedPoolCharge
= sizeof(WINSTATION_OBJECT
);
84 ExWindowStationObjectType
->TypeInfo
.GenericMapping
= IntWindowStationMapping
;
86 return STATUS_SUCCESS
;
90 CleanupWindowStationImpl(VOID
)
92 return STATUS_SUCCESS
;
95 /* OBJECT CALLBACKS **********************************************************/
98 IntWinStaObjectDelete(PWIN32_DELETEMETHOD_PARAMETERS Parameters
)
100 PWINSTATION_OBJECT WinSta
= (PWINSTATION_OBJECT
)Parameters
->Object
;
102 DPRINT("Deleting window station (0x%X)\n", WinSta
);
104 RtlDestroyAtomTable(WinSta
->AtomTable
);
106 RtlFreeUnicodeString(&WinSta
->Name
);
111 IntWinStaObjectParse(PWIN32_PARSEMETHOD_PARAMETERS Parameters
)
113 PUNICODE_STRING RemainingName
= Parameters
->RemainingName
;
115 /* Assume we don't find anything */
116 *Parameters
->Object
= NULL
;
118 /* Check for an empty name */
119 if (!RemainingName
->Length
)
121 /* Make sure this is a window station, can't parse a desktop now */
122 if (Parameters
->ObjectType
!= ExWindowStationObjectType
)
125 return STATUS_OBJECT_TYPE_MISMATCH
;
128 /* Reference the window station and return */
129 ObReferenceObject(Parameters
->ParseObject
);
130 *Parameters
->Object
= Parameters
->ParseObject
;
131 return STATUS_SUCCESS
;
134 /* Check for leading slash */
135 if (RemainingName
->Buffer
[0] == OBJ_NAME_PATH_SEPARATOR
)
138 RemainingName
->Buffer
++;
139 RemainingName
->Length
-= sizeof(WCHAR
);
140 RemainingName
->MaximumLength
-= sizeof(WCHAR
);
143 /* Check if there is still a slash */
144 if (wcschr(RemainingName
->Buffer
, OBJ_NAME_PATH_SEPARATOR
))
146 /* In this case, fail */
147 return STATUS_OBJECT_PATH_INVALID
;
151 * Check if we are parsing a desktop.
153 if (Parameters
->ObjectType
== ExDesktopObjectType
)
155 /* Then call the desktop parse routine */
156 return IntDesktopObjectParse(Parameters
->ParseObject
,
157 Parameters
->ObjectType
,
158 Parameters
->AccessState
,
159 Parameters
->AccessMode
,
160 Parameters
->Attributes
,
161 Parameters
->CompleteName
,
164 Parameters
->SecurityQos
,
168 /* Should hopefully never get here */
169 return STATUS_OBJECT_TYPE_MISMATCH
;
172 /* PRIVATE FUNCTIONS **********************************************************/
175 * IntGetFullWindowStationName
177 * Get a full window station object name from a name specified in
178 * NtUserCreateWindowStation, NtUserOpenWindowStation, NtUserCreateDesktop
179 * or NtUserOpenDesktop.
182 * TRUE on success, FALSE on failure.
186 IntGetFullWindowStationName(
187 OUT PUNICODE_STRING FullName
,
188 IN PUNICODE_STRING WinStaName
,
189 IN OPTIONAL PUNICODE_STRING DesktopName
)
193 FullName
->Length
= WINSTA_ROOT_NAME_LENGTH
* sizeof(WCHAR
);
194 if (WinStaName
!= NULL
)
195 FullName
->Length
+= WinStaName
->Length
+ sizeof(WCHAR
);
196 if (DesktopName
!= NULL
)
197 FullName
->Length
+= DesktopName
->Length
+ sizeof(WCHAR
);
198 FullName
->MaximumLength
= FullName
->Length
;
199 FullName
->Buffer
= ExAllocatePoolWithTag(PagedPool
, FullName
->Length
, TAG_STRING
);
200 if (FullName
->Buffer
== NULL
)
205 Buffer
= FullName
->Buffer
;
206 memcpy(Buffer
, WINSTA_ROOT_NAME
, WINSTA_ROOT_NAME_LENGTH
* sizeof(WCHAR
));
207 Buffer
+= WINSTA_ROOT_NAME_LENGTH
;
208 if (WinStaName
!= NULL
)
210 memcpy(Buffer
, L
"\\", sizeof(WCHAR
));
212 memcpy(Buffer
, WinStaName
->Buffer
, WinStaName
->Length
);
214 if (DesktopName
!= NULL
)
216 Buffer
+= WinStaName
->Length
/ sizeof(WCHAR
);
217 memcpy(Buffer
, L
"\\", sizeof(WCHAR
));
219 memcpy(Buffer
, DesktopName
->Buffer
, DesktopName
->Length
);
227 * IntValidateWindowStationHandle
229 * Validates the window station handle.
232 * If the function succeeds, the handle remains referenced. If the
233 * fucntion fails, last error is set.
237 IntValidateWindowStationHandle(
238 HWINSTA WindowStation
,
239 KPROCESSOR_MODE AccessMode
,
240 ACCESS_MASK DesiredAccess
,
241 PWINSTATION_OBJECT
*Object
)
245 if (WindowStation
== NULL
)
247 // DPRINT1("Invalid window station handle\n");
248 SetLastWin32Error(ERROR_INVALID_HANDLE
);
249 return STATUS_INVALID_HANDLE
;
252 Status
= ObReferenceObjectByHandle(
255 ExWindowStationObjectType
,
260 if (!NT_SUCCESS(Status
))
261 SetLastNtError(Status
);
267 IntGetWindowStationObject(PWINSTATION_OBJECT Object
)
271 Status
= ObReferenceObjectByPointer(
274 ExWindowStationObjectType
,
277 return NT_SUCCESS(Status
);
281 co_IntInitializeDesktopGraphics(VOID
)
283 UNICODE_STRING DriverName
;
284 if (! IntCreatePrimarySurface())
288 RtlInitUnicodeString(&DriverName
, L
"DISPLAY");
289 ScreenDeviceContext
= IntGdiCreateDC(&DriverName
, NULL
, NULL
, NULL
, FALSE
);
290 if (NULL
== ScreenDeviceContext
)
292 IntDestroyPrimarySurface();
295 DC_SetOwnership(ScreenDeviceContext
, NULL
);
297 UserAcquireOrReleaseInputOwnership(FALSE
);
299 /* Setup the cursor */
300 co_IntLoadDefaultCursors();
306 IntEndDesktopGraphics(VOID
)
308 UserAcquireOrReleaseInputOwnership(TRUE
);
309 if (NULL
!= ScreenDeviceContext
)
311 DC_SetOwnership(ScreenDeviceContext
, PsGetCurrentProcess());
312 NtGdiDeleteObjectApp(ScreenDeviceContext
);
313 ScreenDeviceContext
= NULL
;
315 IntHideDesktop(IntGetActiveDesktop());
316 IntDestroyPrimarySurface();
322 return ScreenDeviceContext
;
325 /* PUBLIC FUNCTIONS ***********************************************************/
328 * NtUserCreateWindowStation
330 * Creates a new window station.
333 * lpszWindowStationName
334 * Pointer to a null-terminated string specifying the name of the
335 * window station to be created. Window station names are
336 * case-insensitive and cannot contain backslash characters (\).
337 * Only members of the Administrators group are allowed to specify a
341 * Requested type of access
344 * Security descriptor
346 * Unknown3, Unknown4, Unknown5
350 * If the function succeeds, the return value is a handle to the newly
351 * created window station. If the specified window station already
352 * exists, the function succeeds and returns a handle to the existing
353 * window station. If the function fails, the return value is NULL.
356 * Correct the prototype to match the Windows one (with 7 parameters
364 NtUserCreateWindowStation(
365 PUNICODE_STRING lpszWindowStationName
,
366 ACCESS_MASK dwDesiredAccess
,
367 LPSECURITY_ATTRIBUTES lpSecurity
,
372 PSYSTEM_CURSORINFO CurInfo
;
373 UNICODE_STRING WindowStationName
;
374 UNICODE_STRING FullWindowStationName
;
375 PWINSTATION_OBJECT WindowStationObject
;
376 HWINSTA WindowStation
;
377 OBJECT_ATTRIBUTES ObjectAttributes
;
381 * Generate full window station name
383 Status
= ProbeAndCaptureUnicodeString(&WindowStationName
,
385 lpszWindowStationName
);
386 if (!NT_SUCCESS(Status
))
388 DPRINT1("Failed to capture window station name (status 0x%08x)\n",
390 SetLastNtError(Status
);
393 if (!IntGetFullWindowStationName(&FullWindowStationName
,
397 ReleaseCapturedUnicodeString(&WindowStationName
, UserMode
);
398 SetLastNtError(STATUS_INSUFFICIENT_RESOURCES
);
403 * Try to open already existing window station
406 DPRINT("Trying to open window station (%wZ)\n", &FullWindowStationName
);
408 /* Initialize ObjectAttributes for the window station object */
409 InitializeObjectAttributes(
411 &FullWindowStationName
,
416 Status
= ObOpenObjectByName(
418 ExWindowStationObjectType
,
423 (PVOID
*)&WindowStation
);
425 if (NT_SUCCESS(Status
))
427 DPRINT("Successfully opened window station (%wZ)\n",
428 FullWindowStationName
);
429 ExFreePool(FullWindowStationName
.Buffer
);
430 ReleaseCapturedUnicodeString(&WindowStationName
, UserMode
);
431 return (HWINSTA
)WindowStation
;
435 * No existing window station found, try to create new one
438 DPRINT("Creating window station (%wZ)\n", &FullWindowStationName
);
440 Status
= ObCreateObject(
442 ExWindowStationObjectType
,
446 sizeof(WINSTATION_OBJECT
),
449 (PVOID
*)&WindowStationObject
);
451 if (!NT_SUCCESS(Status
))
453 DPRINT1("Failed creating window station (%wZ)\n", &FullWindowStationName
);
454 ExFreePool(FullWindowStationName
.Buffer
);
455 ReleaseCapturedUnicodeString(&WindowStationName
, UserMode
);
456 SetLastNtError(STATUS_INSUFFICIENT_RESOURCES
);
460 KeInitializeSpinLock(&WindowStationObject
->Lock
);
462 InitializeListHead(&WindowStationObject
->DesktopListHead
);
464 WindowStationObject
->AtomTable
= NULL
;
465 Status
= RtlCreateAtomTable(37, &WindowStationObject
->AtomTable
);
466 WindowStationObject
->SystemMenuTemplate
= (HANDLE
)0;
468 WindowStationObject
->Name
= WindowStationName
;
470 Status
= ObInsertObject(
471 (PVOID
)WindowStationObject
,
473 STANDARD_RIGHTS_REQUIRED
,
476 (PVOID
*)&WindowStation
);
478 if (!NT_SUCCESS(Status
))
480 DPRINT1("Failed creating window station (%wZ)\n", &FullWindowStationName
);
481 ExFreePool(FullWindowStationName
.Buffer
);
482 ExFreePool(WindowStationName
.Buffer
);
483 SetLastNtError(STATUS_INSUFFICIENT_RESOURCES
);
484 ObDereferenceObject(WindowStationObject
);
489 * Initialize the new window station object
492 if(!(CurInfo
= ExAllocatePool(PagedPool
, sizeof(SYSTEM_CURSORINFO
))))
494 ExFreePool(FullWindowStationName
.Buffer
);
495 /* FIXME - Delete window station object */
496 ObDereferenceObject(WindowStationObject
);
497 SetLastNtError(STATUS_INSUFFICIENT_RESOURCES
);
501 CurInfo
->Enabled
= FALSE
;
502 CurInfo
->ButtonsDown
= 0;
503 CurInfo
->CursorClipInfo
.IsClipped
= FALSE
;
504 CurInfo
->LastBtnDown
= 0;
505 CurInfo
->CurrentCursorObject
= NULL
;
506 CurInfo
->ShowingCursor
= 0;
508 /* FIXME: Obtain the following information from the registry */
509 CurInfo
->SwapButtons
= FALSE
;
510 CurInfo
->DblClickSpeed
= 500;
511 CurInfo
->DblClickWidth
= 4;
512 CurInfo
->DblClickHeight
= 4;
514 WindowStationObject
->SystemCursor
= CurInfo
;
516 if (!IntSetupCurIconHandles(WindowStationObject
))
518 DPRINT1("Setting up the Cursor/Icon Handle table failed!\n");
519 /* FIXME: Complain more loudly? */
520 ExFreePool(FullWindowStationName
.Buffer
);
523 DPRINT("Window station successfully created (%wZ)\n", FullWindowStationName
);
524 ExFreePool(FullWindowStationName
.Buffer
);
525 return WindowStation
;
529 * NtUserOpenWindowStation
531 * Opens an existing window station.
534 * lpszWindowStationName
535 * Name of the existing window station.
538 * Requested type of access.
541 * If the function succeeds, the return value is the handle to the
542 * specified window station. If the function fails, the return value
546 * The returned handle can be closed with NtUserCloseWindowStation.
553 NtUserOpenWindowStation(
554 PUNICODE_STRING lpszWindowStationName
,
555 ACCESS_MASK dwDesiredAccess
)
557 UNICODE_STRING WindowStationName
;
558 HWINSTA WindowStation
;
559 OBJECT_ATTRIBUTES ObjectAttributes
;
563 * Generate full window station name
566 if (!IntGetFullWindowStationName(&WindowStationName
, lpszWindowStationName
,
569 SetLastNtError(STATUS_INSUFFICIENT_RESOURCES
);
573 DPRINT("Trying to open window station (%wZ)\n", &WindowStationName
);
575 /* Initialize ObjectAttributes for the window station object */
576 InitializeObjectAttributes(
583 Status
= ObOpenObjectByName(
585 ExWindowStationObjectType
,
590 (PVOID
*)&WindowStation
);
592 if (!NT_SUCCESS(Status
))
594 SetLastNtError(Status
);
595 ExFreePool(WindowStationName
.Buffer
);
599 DPRINT("Successfully opened window station (%wZ)\n", &WindowStationName
);
600 ExFreePool(WindowStationName
.Buffer
);
602 return WindowStation
;
606 * NtUserCloseWindowStation
608 * Closes a window station handle.
612 * Handle to the window station.
618 * The window station handle can be created with NtUserCreateWindowStation
619 * or NtUserOpenWindowStation. Attemps to close a handle to the window
620 * station assigned to the calling process will fail.
628 NtUserCloseWindowStation(
631 PWINSTATION_OBJECT Object
;
634 DPRINT("About to close window station handle (0x%X)\n", hWinSta
);
636 Status
= IntValidateWindowStationHandle(
642 if (!NT_SUCCESS(Status
))
644 DPRINT("Validation of window station handle (0x%X) failed\n", hWinSta
);
649 /* FIXME - free the cursor information when actually deleting the object!! */
650 ASSERT(Object
->SystemCursor
);
651 ExFreePool(Object
->SystemCursor
);
654 ObDereferenceObject(Object
);
656 DPRINT("Closing window station handle (0x%X)\n", hWinSta
);
658 Status
= ZwClose(hWinSta
);
659 if (!NT_SUCCESS(Status
))
661 SetLastNtError(Status
);
669 * NtUserGetObjectInformation
671 * The NtUserGetObjectInformation function retrieves information about a
672 * window station or desktop object.
676 * Handle to the window station or desktop object for which to
677 * return information. This can be a handle of type HDESK or HWINSTA
678 * (for example, a handle returned by NtUserCreateWindowStation,
679 * NtUserOpenWindowStation, NtUserCreateDesktop, or NtUserOpenDesktop).
682 * Specifies the object information to be retrieved.
685 * Pointer to a buffer to receive the object information.
688 * Specifies the size, in bytes, of the buffer pointed to by the
692 * Pointer to a variable receiving the number of bytes required to
693 * store the requested information. If this variable's value is
694 * greater than the value of the nLength parameter when the function
695 * returns, the function returns FALSE, and none of the information
696 * is copied to the pvInfo buffer. If the value of the variable pointed
697 * to by lpnLengthNeeded is less than or equal to the value of nLength,
698 * the entire information block is copied.
701 * If the function succeeds, the return value is nonzero. If the function
702 * fails, the return value is zero.
709 NtUserGetObjectInformation(
714 PDWORD nLengthNeeded
)
716 PWINSTATION_OBJECT WinStaObject
= NULL
;
717 PDESKTOP_OBJECT DesktopObject
= NULL
;
722 /* try windowstation */
723 DPRINT("Trying to open window station 0x%x\n", hObject
);
724 Status
= IntValidateWindowStationHandle(
726 UserMode
,/*ExGetPreviousMode(),*/
727 GENERIC_READ
, /* FIXME: is this ok? */
731 if (!NT_SUCCESS(Status
) && Status
!= STATUS_OBJECT_TYPE_MISMATCH
)
733 DPRINT("Failed: 0x%x\n", Status
);
734 SetLastNtError(Status
);
738 if (Status
== STATUS_OBJECT_TYPE_MISMATCH
)
741 DPRINT("Trying to open desktop 0x%x\n", hObject
);
742 Status
= IntValidateDesktopHandle(
744 UserMode
,/*ExGetPreviousMode(),*/
745 GENERIC_READ
, /* FIXME: is this ok? */
747 if (!NT_SUCCESS(Status
))
749 DPRINT("Failed: 0x%x\n", Status
);
750 SetLastNtError(Status
);
754 DPRINT("WinSta or Desktop opened!!\n");
760 Status
= STATUS_NOT_IMPLEMENTED
;
761 DPRINT1("UOI_FLAGS unimplemented!\n");
765 if (WinStaObject
!= NULL
)
767 pvData
= ((PUNICODE_STRING
)GET_DESKTOP_NAME(WinStaObject
))->Buffer
;
768 nDataSize
= ((PUNICODE_STRING
)GET_DESKTOP_NAME(WinStaObject
))->Length
+ 2;
769 Status
= STATUS_SUCCESS
;
771 else if (DesktopObject
!= NULL
)
773 pvData
= ((PUNICODE_STRING
)GET_DESKTOP_NAME(DesktopObject
))->Buffer
;
774 nDataSize
= ((PUNICODE_STRING
)GET_DESKTOP_NAME(DesktopObject
))->Length
+ 2;
775 Status
= STATUS_SUCCESS
;
778 Status
= STATUS_INVALID_PARAMETER
;
782 if (WinStaObject
!= NULL
)
784 pvData
= L
"WindowStation";
785 nDataSize
= (wcslen(pvData
) + 1) * sizeof(WCHAR
);
786 Status
= STATUS_SUCCESS
;
788 else if (DesktopObject
!= NULL
)
791 nDataSize
= (wcslen(pvData
) + 1) * sizeof(WCHAR
);
792 Status
= STATUS_SUCCESS
;
795 Status
= STATUS_INVALID_PARAMETER
;
799 Status
= STATUS_NOT_IMPLEMENTED
;
800 DPRINT1("UOI_USER_SID unimplemented!\n");
804 Status
= STATUS_INVALID_PARAMETER
;
808 /* try to copy data to caller */
809 if (Status
== STATUS_SUCCESS
)
811 DPRINT("Trying to copy data to caller (len = %d, len needed = %d)\n", nLength
, nDataSize
);
812 *nLengthNeeded
= nDataSize
;
813 if (nLength
>= nDataSize
)
814 Status
= MmCopyToCaller(pvInformation
, pvData
, nDataSize
);
816 Status
= STATUS_BUFFER_TOO_SMALL
;
819 /* release objects */
820 if (WinStaObject
!= NULL
)
821 ObDereferenceObject(WinStaObject
);
822 if (DesktopObject
!= NULL
)
823 ObDereferenceObject(DesktopObject
);
825 SetLastNtError(Status
);
826 return NT_SUCCESS(Status
);
830 * NtUserSetObjectInformation
832 * The NtUserSetObjectInformation function sets information about a
833 * window station or desktop object.
837 * Handle to the window station or desktop object for which to set
838 * object information. This value can be a handle of type HDESK or
842 * Specifies the object information to be set.
845 * Pointer to a buffer containing the object information.
848 * Specifies the size, in bytes, of the information contained in the
849 * buffer pointed to by pvInfo.
852 * If the function succeeds, the return value is nonzero. If the function
853 * fails the return value is zero.
861 NtUserSetObjectInformation(
867 /* FIXME: ZwQueryObject */
868 /* FIXME: ZwSetInformationObject */
869 SetLastNtError(STATUS_UNSUCCESSFUL
);
877 UserGetProcessWindowStation(VOID
)
882 if(PsGetCurrentProcess() != CsrProcess
)
884 return PsGetCurrentProcess()->Win32WindowStation
;
888 DPRINT1("Should use ObFindHandleForObject\n");
889 Status
= ObOpenObjectByPointer(PsGetWin32Thread()->Desktop
->WindowStation
,
893 ExWindowStationObjectType
,
896 if (! NT_SUCCESS(Status
))
898 SetLastNtError(Status
);
899 DPRINT1("Unable to open handle for CSRSSs winsta, status 0x%08x\n",
909 * NtUserGetProcessWindowStation
911 * Returns a handle to the current process window station.
914 * If the function succeeds, the return value is handle to the window
915 * station assigned to the current process. If the function fails, the
916 * return value is NULL.
923 NtUserGetProcessWindowStation(VOID
)
925 return UserGetProcessWindowStation();
928 PWINSTATION_OBJECT FASTCALL
929 IntGetWinStaObj(VOID
)
931 PWINSTATION_OBJECT WinStaObj
;
934 * just a temporary hack, this will be gone soon
937 if(PsGetWin32Thread() != NULL
&& PsGetWin32Thread()->Desktop
!= NULL
)
939 WinStaObj
= PsGetWin32Thread()->Desktop
->WindowStation
;
940 ObReferenceObjectByPointer(WinStaObj
, KernelMode
, ExWindowStationObjectType
, 0);
942 else if(PsGetCurrentProcess() != CsrProcess
)
944 NTSTATUS Status
= IntValidateWindowStationHandle(PsGetCurrentProcess()->Win32WindowStation
,
948 if(!NT_SUCCESS(Status
))
950 SetLastNtError(Status
);
963 * NtUserSetProcessWindowStation
965 * Assigns a window station to the current process.
969 * Handle to the window station.
979 NtUserSetProcessWindowStation(HWINSTA hWindowStation
)
982 PWINSTATION_OBJECT NewWinSta
;
985 DPRINT("About to set process window station with handle (0x%X)\n",
988 if(PsGetCurrentProcess() == CsrProcess
)
990 DPRINT1("CSRSS is not allowed to change it's window station!!!\n");
991 SetLastWin32Error(ERROR_ACCESS_DENIED
);
995 Status
= IntValidateWindowStationHandle(
1001 if (!NT_SUCCESS(Status
))
1003 DPRINT("Validation of window station handle (0x%X) failed\n",
1005 SetLastNtError(Status
);
1010 * FIXME - don't allow changing the window station if there are threads that are attached to desktops and own gui objects
1013 /* FIXME - dereference the old window station, etc... */
1014 hOld
= InterlockedExchangePointer(&PsGetCurrentProcess()->Win32WindowStation
, hWindowStation
);
1016 DPRINT("PsGetCurrentProcess()->Win32WindowStation 0x%X\n",
1017 PsGetCurrentProcess()->Win32WindowStation
);
1023 * NtUserLockWindowStation
1025 * Locks switching desktops. Only the logon application is allowed to call this function.
1032 NtUserLockWindowStation(HWINSTA hWindowStation
)
1034 PWINSTATION_OBJECT Object
;
1037 DPRINT("About to set process window station with handle (0x%X)\n",
1040 if(PsGetWin32Process() != LogonProcess
)
1042 DPRINT1("Unauthorized process attempted to lock the window station!\n");
1043 SetLastWin32Error(ERROR_ACCESS_DENIED
);
1047 Status
= IntValidateWindowStationHandle(
1052 if (!NT_SUCCESS(Status
))
1054 DPRINT("Validation of window station handle (0x%X) failed\n",
1056 SetLastNtError(Status
);
1060 Object
->Flags
|= WSS_LOCKED
;
1062 ObDereferenceObject(Object
);
1067 * NtUserUnlockWindowStation
1069 * Unlocks switching desktops. Only the logon application is allowed to call this function.
1076 NtUserUnlockWindowStation(HWINSTA hWindowStation
)
1078 PWINSTATION_OBJECT Object
;
1082 DPRINT("About to set process window station with handle (0x%X)\n",
1085 if(PsGetWin32Process() != LogonProcess
)
1087 DPRINT1("Unauthorized process attempted to unlock the window station!\n");
1088 SetLastWin32Error(ERROR_ACCESS_DENIED
);
1092 Status
= IntValidateWindowStationHandle(
1097 if (!NT_SUCCESS(Status
))
1099 DPRINT("Validation of window station handle (0x%X) failed\n",
1101 SetLastNtError(Status
);
1105 Ret
= (Object
->Flags
& WSS_LOCKED
) == WSS_LOCKED
;
1106 Object
->Flags
&= ~WSS_LOCKED
;
1108 ObDereferenceObject(Object
);
1113 * NtUserSetWindowStationUser
1120 NtUserSetWindowStationUser(
1131 static NTSTATUS FASTCALL
1132 BuildWindowStationNameList(
1135 PULONG pRequiredSize
)
1137 OBJECT_ATTRIBUTES ObjectAttributes
;
1139 HANDLE DirectoryHandle
;
1140 UNICODE_STRING DirectoryName
;
1141 char InitialBuffer
[256], *Buffer
;
1142 ULONG Context
, ReturnLength
, BufferSize
;
1144 POBJECT_DIRECTORY_INFORMATION DirEntry
;
1148 * Generate name of window station directory
1150 if (!IntGetFullWindowStationName(&DirectoryName
, NULL
, NULL
))
1152 return STATUS_INSUFFICIENT_RESOURCES
;
1156 * Try to open the directory.
1158 InitializeObjectAttributes(
1161 OBJ_CASE_INSENSITIVE
,
1165 Status
= ZwOpenDirectoryObject(
1170 ExFreePool(DirectoryName
.Buffer
);
1172 if (!NT_SUCCESS(Status
))
1177 /* First try to query the directory using a fixed-size buffer */
1180 Status
= ZwQueryDirectoryObject(DirectoryHandle
, InitialBuffer
, sizeof(InitialBuffer
),
1181 FALSE
, TRUE
, &Context
, &ReturnLength
);
1182 if (NT_SUCCESS(Status
))
1184 if (STATUS_NO_MORE_ENTRIES
== ZwQueryDirectoryObject(DirectoryHandle
, NULL
, 0, FALSE
,
1185 FALSE
, &Context
, NULL
))
1187 /* Our fixed-size buffer is large enough */
1188 Buffer
= InitialBuffer
;
1194 /* Need a larger buffer, check how large exactly */
1195 Status
= ZwQueryDirectoryObject(DirectoryHandle
, NULL
, 0, FALSE
, TRUE
, &Context
,
1197 if (STATUS_BUFFER_TOO_SMALL
== Status
)
1199 BufferSize
= ReturnLength
;
1200 Buffer
= ExAllocatePoolWithTag(PagedPool
, BufferSize
, TAG_WINSTA
);
1203 ObDereferenceObject(DirectoryHandle
);
1204 return STATUS_NO_MEMORY
;
1207 /* We should have a sufficiently large buffer now */
1209 Status
= ZwQueryDirectoryObject(DirectoryHandle
, Buffer
, BufferSize
,
1210 FALSE
, TRUE
, &Context
, &ReturnLength
);
1211 if (! NT_SUCCESS(Status
) ||
1212 STATUS_NO_MORE_ENTRIES
!= ZwQueryDirectoryObject(DirectoryHandle
, NULL
, 0, FALSE
,
1213 FALSE
, &Context
, NULL
))
1215 /* Something went wrong, maybe someone added a directory entry? Just give up. */
1217 ObDereferenceObject(DirectoryHandle
);
1218 return NT_SUCCESS(Status
) ? STATUS_INTERNAL_ERROR
: Status
;
1223 ZwClose(DirectoryHandle
);
1226 * Count the required size of buffer.
1228 ReturnLength
= sizeof(DWORD
);
1230 for (DirEntry
= (POBJECT_DIRECTORY_INFORMATION
) Buffer
; 0 != DirEntry
->ObjectName
.Length
;
1233 ReturnLength
+= DirEntry
->ObjectName
.Length
+ sizeof(WCHAR
);
1236 DPRINT("Required size: %d Entry count: %d\n", ReturnLength
, EntryCount
);
1237 if (NULL
!= pRequiredSize
)
1239 Status
= MmCopyToCaller(pRequiredSize
, &ReturnLength
, sizeof(ULONG
));
1240 if (! NT_SUCCESS(Status
))
1242 if (Buffer
!= InitialBuffer
)
1246 return STATUS_BUFFER_TOO_SMALL
;
1251 * Check if the supplied buffer is large enough.
1253 if (dwSize
< ReturnLength
)
1255 if (Buffer
!= InitialBuffer
)
1259 return STATUS_BUFFER_TOO_SMALL
;
1263 * Generate the resulting buffer contents.
1265 Status
= MmCopyToCaller(lpBuffer
, &EntryCount
, sizeof(DWORD
));
1266 if (! NT_SUCCESS(Status
))
1268 if (Buffer
!= InitialBuffer
)
1274 lpBuffer
= (PVOID
) ((PCHAR
) lpBuffer
+ sizeof(DWORD
));
1277 for (DirEntry
= (POBJECT_DIRECTORY_INFORMATION
) Buffer
; 0 != DirEntry
->ObjectName
.Length
;
1280 Status
= MmCopyToCaller(lpBuffer
, DirEntry
->ObjectName
.Buffer
, DirEntry
->ObjectName
.Length
);
1281 if (! NT_SUCCESS(Status
))
1283 if (Buffer
!= InitialBuffer
)
1289 lpBuffer
= (PVOID
) ((PCHAR
) lpBuffer
+ DirEntry
->ObjectName
.Length
);
1290 Status
= MmCopyToCaller(lpBuffer
, &NullWchar
, sizeof(WCHAR
));
1291 if (! NT_SUCCESS(Status
))
1293 if (Buffer
!= InitialBuffer
)
1299 lpBuffer
= (PVOID
) ((PCHAR
) lpBuffer
+ sizeof(WCHAR
));
1305 if (NULL
!= Buffer
&& Buffer
!= InitialBuffer
)
1310 return STATUS_SUCCESS
;
1313 static NTSTATUS FASTCALL
1314 BuildDesktopNameList(
1315 HWINSTA hWindowStation
,
1318 PULONG pRequiredSize
)
1321 PWINSTATION_OBJECT WindowStation
;
1323 PLIST_ENTRY DesktopEntry
;
1324 PDESKTOP_OBJECT DesktopObject
;
1329 Status
= IntValidateWindowStationHandle(hWindowStation
,
1333 if (! NT_SUCCESS(Status
))
1338 KeAcquireSpinLock(&WindowStation
->Lock
, &OldLevel
);
1341 * Count the required size of buffer.
1343 ReturnLength
= sizeof(DWORD
);
1345 for (DesktopEntry
= WindowStation
->DesktopListHead
.Flink
;
1346 DesktopEntry
!= &WindowStation
->DesktopListHead
;
1347 DesktopEntry
= DesktopEntry
->Flink
)
1349 DesktopObject
= CONTAINING_RECORD(DesktopEntry
, DESKTOP_OBJECT
, ListEntry
);
1350 ReturnLength
+= ((PUNICODE_STRING
)GET_DESKTOP_NAME(DesktopObject
))->Length
+ sizeof(WCHAR
);
1353 DPRINT("Required size: %d Entry count: %d\n", ReturnLength
, EntryCount
);
1354 if (NULL
!= pRequiredSize
)
1356 Status
= MmCopyToCaller(pRequiredSize
, &ReturnLength
, sizeof(ULONG
));
1357 if (! NT_SUCCESS(Status
))
1359 KeReleaseSpinLock(&WindowStation
->Lock
, OldLevel
);
1360 ObDereferenceObject(WindowStation
);
1361 return STATUS_BUFFER_TOO_SMALL
;
1366 * Check if the supplied buffer is large enough.
1368 if (dwSize
< ReturnLength
)
1370 KeReleaseSpinLock(&WindowStation
->Lock
, OldLevel
);
1371 ObDereferenceObject(WindowStation
);
1372 return STATUS_BUFFER_TOO_SMALL
;
1376 * Generate the resulting buffer contents.
1378 Status
= MmCopyToCaller(lpBuffer
, &EntryCount
, sizeof(DWORD
));
1379 if (! NT_SUCCESS(Status
))
1381 KeReleaseSpinLock(&WindowStation
->Lock
, OldLevel
);
1382 ObDereferenceObject(WindowStation
);
1385 lpBuffer
= (PVOID
) ((PCHAR
) lpBuffer
+ sizeof(DWORD
));
1388 for (DesktopEntry
= WindowStation
->DesktopListHead
.Flink
;
1389 DesktopEntry
!= &WindowStation
->DesktopListHead
;
1390 DesktopEntry
= DesktopEntry
->Flink
)
1392 DesktopObject
= CONTAINING_RECORD(DesktopEntry
, DESKTOP_OBJECT
, ListEntry
);
1393 Status
= MmCopyToCaller(lpBuffer
, ((PUNICODE_STRING
)GET_DESKTOP_NAME(DesktopObject
))->Buffer
, ((PUNICODE_STRING
)GET_DESKTOP_NAME(DesktopObject
))->Length
);
1394 if (! NT_SUCCESS(Status
))
1396 KeReleaseSpinLock(&WindowStation
->Lock
, OldLevel
);
1397 ObDereferenceObject(WindowStation
);
1400 lpBuffer
= (PVOID
) ((PCHAR
) lpBuffer
+ ((PUNICODE_STRING
)GET_DESKTOP_NAME(DesktopObject
))->Length
);
1401 Status
= MmCopyToCaller(lpBuffer
, &NullWchar
, sizeof(WCHAR
));
1402 if (! NT_SUCCESS(Status
))
1404 KeReleaseSpinLock(&WindowStation
->Lock
, OldLevel
);
1405 ObDereferenceObject(WindowStation
);
1408 lpBuffer
= (PVOID
) ((PCHAR
) lpBuffer
+ sizeof(WCHAR
));
1414 KeReleaseSpinLock(&WindowStation
->Lock
, OldLevel
);
1415 ObDereferenceObject(WindowStation
);
1417 return STATUS_SUCCESS
;
1421 * NtUserBuildNameList
1423 * Function used for enumeration of desktops or window stations.
1427 * For enumeration of window stations this parameter must be set to
1428 * zero. Otherwise it's handle for window station.
1431 * Size of buffer passed by caller.
1434 * Buffer passed by caller. If the function succedes, the buffer is
1435 * filled with window station/desktop count (in first DWORD) and
1436 * NULL-terminated window station/desktop names.
1439 * If the function suceedes, this is the number of bytes copied.
1440 * Otherwise it's size of buffer needed for function to succeed.
1447 NtUserBuildNameList(
1448 HWINSTA hWindowStation
,
1451 PULONG pRequiredSize
)
1453 /* The WindowStation name list and desktop name list are build in completely
1454 different ways. Call the appropriate function */
1455 return NULL
== hWindowStation
? BuildWindowStationNameList(dwSize
, lpBuffer
, pRequiredSize
) :
1456 BuildDesktopNameList(hWindowStation
, dwSize
, lpBuffer
, pRequiredSize
);