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
60 InitWindowStationImpl(VOID
)
62 OBJECT_ATTRIBUTES ObjectAttributes
;
63 HANDLE WindowStationsDirectory
;
64 UNICODE_STRING UnicodeString
;
68 * Create the '\Windows\WindowStations' directory
71 RtlInitUnicodeString(&UnicodeString
, WINSTA_ROOT_NAME
);
72 InitializeObjectAttributes(&ObjectAttributes
, &UnicodeString
,
74 Status
= ZwCreateDirectoryObject(&WindowStationsDirectory
, 0,
76 if (!NT_SUCCESS(Status
))
78 DPRINT("Could not create \\Windows\\WindowStations directory "
79 "(Status 0x%X)\n", Status
);
83 /* Set Winsta Object Attributes */
84 ExWindowStationObjectType
->TypeInfo
.DefaultNonPagedPoolCharge
= sizeof(WINSTATION_OBJECT
);
85 ExWindowStationObjectType
->TypeInfo
.GenericMapping
= IntWindowStationMapping
;
87 return STATUS_SUCCESS
;
91 CleanupWindowStationImpl(VOID
)
93 return STATUS_SUCCESS
;
97 IntSetupClipboard(PWINSTATION_OBJECT WinStaObj
)
99 WinStaObj
->Clipboard
= ExAllocatePool(PagedPool
, sizeof(CLIPBOARDSYSTEM
));
100 if (WinStaObj
->Clipboard
)
102 RtlZeroMemory(WinStaObj
->Clipboard
, sizeof(CLIPBOARDSYSTEM
));
108 /* OBJECT CALLBACKS **********************************************************/
111 IntWinStaObjectDelete(PWIN32_DELETEMETHOD_PARAMETERS Parameters
)
113 PWINSTATION_OBJECT WinSta
= (PWINSTATION_OBJECT
)Parameters
->Object
;
115 DPRINT("Deleting window station (0x%X)\n", WinSta
);
117 RtlDestroyAtomTable(WinSta
->AtomTable
);
119 RtlFreeUnicodeString(&WinSta
->Name
);
124 IntWinStaObjectParse(PWIN32_PARSEMETHOD_PARAMETERS Parameters
)
126 PUNICODE_STRING RemainingName
= Parameters
->RemainingName
;
128 /* Assume we don't find anything */
129 *Parameters
->Object
= NULL
;
131 /* Check for an empty name */
132 if (!RemainingName
->Length
)
134 /* Make sure this is a window station, can't parse a desktop now */
135 if (Parameters
->ObjectType
!= ExWindowStationObjectType
)
138 return STATUS_OBJECT_TYPE_MISMATCH
;
141 /* Reference the window station and return */
142 ObReferenceObject(Parameters
->ParseObject
);
143 *Parameters
->Object
= Parameters
->ParseObject
;
144 return STATUS_SUCCESS
;
147 /* Check for leading slash */
148 if (RemainingName
->Buffer
[0] == OBJ_NAME_PATH_SEPARATOR
)
151 RemainingName
->Buffer
++;
152 RemainingName
->Length
-= sizeof(WCHAR
);
153 RemainingName
->MaximumLength
-= sizeof(WCHAR
);
156 /* Check if there is still a slash */
157 if (wcschr(RemainingName
->Buffer
, OBJ_NAME_PATH_SEPARATOR
))
159 /* In this case, fail */
160 return STATUS_OBJECT_PATH_INVALID
;
164 * Check if we are parsing a desktop.
166 if (Parameters
->ObjectType
== ExDesktopObjectType
)
168 /* Then call the desktop parse routine */
169 return IntDesktopObjectParse(Parameters
->ParseObject
,
170 Parameters
->ObjectType
,
171 Parameters
->AccessState
,
172 Parameters
->AccessMode
,
173 Parameters
->Attributes
,
174 Parameters
->CompleteName
,
177 Parameters
->SecurityQos
,
181 /* Should hopefully never get here */
182 return STATUS_OBJECT_TYPE_MISMATCH
;
185 /* PRIVATE FUNCTIONS **********************************************************/
188 * IntGetFullWindowStationName
190 * Get a full window station object name from a name specified in
191 * NtUserCreateWindowStation, NtUserOpenWindowStation, NtUserCreateDesktop
192 * or NtUserOpenDesktop.
195 * TRUE on success, FALSE on failure.
199 IntGetFullWindowStationName(
200 OUT PUNICODE_STRING FullName
,
201 IN PUNICODE_STRING WinStaName
,
202 IN OPTIONAL PUNICODE_STRING DesktopName
)
206 FullName
->Length
= WINSTA_ROOT_NAME_LENGTH
* sizeof(WCHAR
);
207 if (WinStaName
!= NULL
)
208 FullName
->Length
+= WinStaName
->Length
+ sizeof(WCHAR
);
209 if (DesktopName
!= NULL
)
210 FullName
->Length
+= DesktopName
->Length
+ sizeof(WCHAR
);
211 FullName
->MaximumLength
= FullName
->Length
;
212 FullName
->Buffer
= ExAllocatePoolWithTag(PagedPool
, FullName
->Length
, TAG_STRING
);
213 if (FullName
->Buffer
== NULL
)
218 Buffer
= FullName
->Buffer
;
219 memcpy(Buffer
, WINSTA_ROOT_NAME
, WINSTA_ROOT_NAME_LENGTH
* sizeof(WCHAR
));
220 Buffer
+= WINSTA_ROOT_NAME_LENGTH
;
221 if (WinStaName
!= NULL
)
223 memcpy(Buffer
, L
"\\", sizeof(WCHAR
));
225 memcpy(Buffer
, WinStaName
->Buffer
, WinStaName
->Length
);
227 if (DesktopName
!= NULL
)
229 Buffer
+= WinStaName
->Length
/ sizeof(WCHAR
);
230 memcpy(Buffer
, L
"\\", sizeof(WCHAR
));
232 memcpy(Buffer
, DesktopName
->Buffer
, DesktopName
->Length
);
240 * IntValidateWindowStationHandle
242 * Validates the window station handle.
245 * If the function succeeds, the handle remains referenced. If the
246 * fucntion fails, last error is set.
250 IntValidateWindowStationHandle(
251 HWINSTA WindowStation
,
252 KPROCESSOR_MODE AccessMode
,
253 ACCESS_MASK DesiredAccess
,
254 PWINSTATION_OBJECT
*Object
)
258 if (WindowStation
== NULL
)
260 // DPRINT1("Invalid window station handle\n");
261 SetLastWin32Error(ERROR_INVALID_HANDLE
);
262 return STATUS_INVALID_HANDLE
;
265 Status
= ObReferenceObjectByHandle(
268 ExWindowStationObjectType
,
273 if (!NT_SUCCESS(Status
))
274 SetLastNtError(Status
);
280 IntGetWindowStationObject(PWINSTATION_OBJECT Object
)
284 Status
= ObReferenceObjectByPointer(
287 ExWindowStationObjectType
,
290 return NT_SUCCESS(Status
);
294 co_IntInitializeDesktopGraphics(VOID
)
296 UNICODE_STRING DriverName
;
297 if (! IntCreatePrimarySurface())
301 RtlInitUnicodeString(&DriverName
, L
"DISPLAY");
302 ScreenDeviceContext
= IntGdiCreateDC(&DriverName
, NULL
, NULL
, NULL
, FALSE
);
303 if (NULL
== ScreenDeviceContext
)
305 IntDestroyPrimarySurface();
308 DC_SetOwnership(ScreenDeviceContext
, NULL
);
310 /* Setup the cursor */
311 co_IntLoadDefaultCursors();
317 IntEndDesktopGraphics(VOID
)
319 if (NULL
!= ScreenDeviceContext
)
321 DC_SetOwnership(ScreenDeviceContext
, PsGetCurrentProcess());
322 NtGdiDeleteObjectApp(ScreenDeviceContext
);
323 ScreenDeviceContext
= NULL
;
325 IntHideDesktop(IntGetActiveDesktop());
326 IntDestroyPrimarySurface();
332 return ScreenDeviceContext
;
335 /* PUBLIC FUNCTIONS ***********************************************************/
338 * NtUserCreateWindowStation
340 * Creates a new window station.
343 * lpszWindowStationName
344 * Pointer to a null-terminated string specifying the name of the
345 * window station to be created. Window station names are
346 * case-insensitive and cannot contain backslash characters (\).
347 * Only members of the Administrators group are allowed to specify a
351 * Requested type of access
354 * Security descriptor
356 * Unknown3, Unknown4, Unknown5
360 * If the function succeeds, the return value is a handle to the newly
361 * created window station. If the specified window station already
362 * exists, the function succeeds and returns a handle to the existing
363 * window station. If the function fails, the return value is NULL.
366 * Correct the prototype to match the Windows one (with 7 parameters
374 NtUserCreateWindowStation(
375 PUNICODE_STRING lpszWindowStationName
,
376 ACCESS_MASK dwDesiredAccess
,
377 LPSECURITY_ATTRIBUTES lpSecurity
,
382 PSYSTEM_CURSORINFO CurInfo
;
383 UNICODE_STRING WindowStationName
;
384 UNICODE_STRING FullWindowStationName
;
385 PWINSTATION_OBJECT WindowStationObject
;
386 HWINSTA WindowStation
;
387 OBJECT_ATTRIBUTES ObjectAttributes
;
391 * Generate full window station name
393 Status
= ProbeAndCaptureUnicodeString(&WindowStationName
,
395 lpszWindowStationName
);
396 if (!NT_SUCCESS(Status
))
398 DPRINT1("Failed to capture window station name (status 0x%08x)\n",
400 SetLastNtError(Status
);
403 if (!IntGetFullWindowStationName(&FullWindowStationName
,
407 ReleaseCapturedUnicodeString(&WindowStationName
, UserMode
);
408 SetLastNtError(STATUS_INSUFFICIENT_RESOURCES
);
413 * Try to open already existing window station
416 DPRINT("Trying to open window station (%wZ)\n", &FullWindowStationName
);
418 /* Initialize ObjectAttributes for the window station object */
419 InitializeObjectAttributes(
421 &FullWindowStationName
,
426 Status
= ObOpenObjectByName(
428 ExWindowStationObjectType
,
433 (PVOID
*)&WindowStation
);
435 if (NT_SUCCESS(Status
))
437 DPRINT("Successfully opened window station (%wZ)\n",
438 FullWindowStationName
);
439 ExFreePool(FullWindowStationName
.Buffer
);
440 ReleaseCapturedUnicodeString(&WindowStationName
, UserMode
);
441 return (HWINSTA
)WindowStation
;
445 * No existing window station found, try to create new one
448 DPRINT("Creating window station (%wZ)\n", &FullWindowStationName
);
450 Status
= ObCreateObject(
452 ExWindowStationObjectType
,
456 sizeof(WINSTATION_OBJECT
),
459 (PVOID
*)&WindowStationObject
);
461 if (!NT_SUCCESS(Status
))
463 DPRINT1("Failed creating window station (%wZ)\n", &FullWindowStationName
);
464 ExFreePool(FullWindowStationName
.Buffer
);
465 ReleaseCapturedUnicodeString(&WindowStationName
, UserMode
);
466 SetLastNtError(STATUS_INSUFFICIENT_RESOURCES
);
470 KeInitializeSpinLock(&WindowStationObject
->Lock
);
472 InitializeListHead(&WindowStationObject
->DesktopListHead
);
474 WindowStationObject
->AtomTable
= NULL
;
475 Status
= RtlCreateAtomTable(37, &WindowStationObject
->AtomTable
);
476 WindowStationObject
->SystemMenuTemplate
= (HANDLE
)0;
478 WindowStationObject
->Name
= WindowStationName
;
480 Status
= ObInsertObject(
481 (PVOID
)WindowStationObject
,
483 STANDARD_RIGHTS_REQUIRED
,
486 (PVOID
*)&WindowStation
);
488 if (!NT_SUCCESS(Status
))
490 DPRINT1("Failed creating window station (%wZ)\n", &FullWindowStationName
);
491 ExFreePool(FullWindowStationName
.Buffer
);
492 ExFreePool(WindowStationName
.Buffer
);
493 SetLastNtError(STATUS_INSUFFICIENT_RESOURCES
);
494 ObDereferenceObject(WindowStationObject
);
499 * Initialize the new window station object
501 WindowStationObject
->ScreenSaverRunning
= FALSE
;
502 WindowStationObject
->ScreenSaverTimeOut
= 10 * 60;
503 WindowStationObject
->FlatMenu
= FALSE
;
505 if(!(CurInfo
= ExAllocatePool(PagedPool
, sizeof(SYSTEM_CURSORINFO
))))
507 ExFreePool(FullWindowStationName
.Buffer
);
508 /* FIXME - Delete window station object */
509 ObDereferenceObject(WindowStationObject
);
510 SetLastNtError(STATUS_INSUFFICIENT_RESOURCES
);
514 CurInfo
->Enabled
= FALSE
;
515 CurInfo
->ButtonsDown
= 0;
516 CurInfo
->CursorClipInfo
.IsClipped
= FALSE
;
517 CurInfo
->LastBtnDown
= 0;
518 CurInfo
->CurrentCursorObject
= NULL
;
519 CurInfo
->ShowingCursor
= 0;
521 CurInfo
->WheelScroLines
= 3;
522 CurInfo
->WheelScroChars
= 3;
524 /* FIXME: Obtain the following information from the registry */
525 CurInfo
->SwapButtons
= FALSE
;
526 CurInfo
->DblClickSpeed
= 500;
527 CurInfo
->DblClickWidth
= 4;
528 CurInfo
->DblClickHeight
= 4;
530 CurInfo
->MouseSpeed
= 10;
531 CurInfo
->CursorAccelerationInfo
.FirstThreshold
= 6;
532 CurInfo
->CursorAccelerationInfo
.SecondThreshold
= 10;
533 CurInfo
->CursorAccelerationInfo
.Acceleration
= 1;
535 CurInfo
->MouseHoverTime
= 80;
536 CurInfo
->MouseHoverWidth
= 4;
537 CurInfo
->MouseHoverHeight
= 4;
539 WindowStationObject
->SystemCursor
= CurInfo
;
541 if (!IntSetupClipboard(WindowStationObject
))
543 DPRINT1("WindowStation: Error Setting up the clipboard!!!\n");
546 if (!IntSetupCurIconHandles(WindowStationObject
))
548 DPRINT1("Setting up the Cursor/Icon Handle table failed!\n");
549 /* FIXME: Complain more loudly? */
550 ExFreePool(FullWindowStationName
.Buffer
);
553 DPRINT("Window station successfully created (%wZ)\n", FullWindowStationName
);
554 ExFreePool(FullWindowStationName
.Buffer
);
555 return WindowStation
;
559 * NtUserOpenWindowStation
561 * Opens an existing window station.
564 * lpszWindowStationName
565 * Name of the existing window station.
568 * Requested type of access.
571 * If the function succeeds, the return value is the handle to the
572 * specified window station. If the function fails, the return value
576 * The returned handle can be closed with NtUserCloseWindowStation.
583 NtUserOpenWindowStation(
584 PUNICODE_STRING lpszWindowStationName
,
585 ACCESS_MASK dwDesiredAccess
)
587 UNICODE_STRING WindowStationName
;
588 HWINSTA WindowStation
;
589 OBJECT_ATTRIBUTES ObjectAttributes
;
593 * Generate full window station name
596 if (!IntGetFullWindowStationName(&WindowStationName
, lpszWindowStationName
,
599 SetLastNtError(STATUS_INSUFFICIENT_RESOURCES
);
603 DPRINT("Trying to open window station (%wZ)\n", &WindowStationName
);
605 /* Initialize ObjectAttributes for the window station object */
606 InitializeObjectAttributes(
613 Status
= ObOpenObjectByName(
615 ExWindowStationObjectType
,
620 (PVOID
*)&WindowStation
);
622 if (!NT_SUCCESS(Status
))
624 SetLastNtError(Status
);
625 ExFreePool(WindowStationName
.Buffer
);
629 DPRINT("Successfully opened window station (%wZ)\n", &WindowStationName
);
630 ExFreePool(WindowStationName
.Buffer
);
632 return WindowStation
;
636 * NtUserCloseWindowStation
638 * Closes a window station handle.
642 * Handle to the window station.
648 * The window station handle can be created with NtUserCreateWindowStation
649 * or NtUserOpenWindowStation. Attemps to close a handle to the window
650 * station assigned to the calling process will fail.
658 NtUserCloseWindowStation(
661 PWINSTATION_OBJECT Object
;
664 DPRINT("About to close window station handle (0x%X)\n", hWinSta
);
666 Status
= IntValidateWindowStationHandle(
672 if (!NT_SUCCESS(Status
))
674 DPRINT("Validation of window station handle (0x%X) failed\n", hWinSta
);
679 /* FIXME - free the cursor information when actually deleting the object!! */
680 ASSERT(Object
->SystemCursor
);
681 ExFreePool(Object
->SystemCursor
);
684 ObDereferenceObject(Object
);
686 DPRINT("Closing window station handle (0x%X)\n", hWinSta
);
688 Status
= ZwClose(hWinSta
);
689 if (!NT_SUCCESS(Status
))
691 SetLastNtError(Status
);
699 * NtUserGetObjectInformation
701 * The NtUserGetObjectInformation function retrieves information about a
702 * window station or desktop object.
706 * Handle to the window station or desktop object for which to
707 * return information. This can be a handle of type HDESK or HWINSTA
708 * (for example, a handle returned by NtUserCreateWindowStation,
709 * NtUserOpenWindowStation, NtUserCreateDesktop, or NtUserOpenDesktop).
712 * Specifies the object information to be retrieved.
715 * Pointer to a buffer to receive the object information.
718 * Specifies the size, in bytes, of the buffer pointed to by the
722 * Pointer to a variable receiving the number of bytes required to
723 * store the requested information. If this variable's value is
724 * greater than the value of the nLength parameter when the function
725 * returns, the function returns FALSE, and none of the information
726 * is copied to the pvInfo buffer. If the value of the variable pointed
727 * to by lpnLengthNeeded is less than or equal to the value of nLength,
728 * the entire information block is copied.
731 * If the function succeeds, the return value is nonzero. If the function
732 * fails, the return value is zero.
739 NtUserGetObjectInformation(
744 PDWORD nLengthNeeded
)
746 PWINSTATION_OBJECT WinStaObject
= NULL
;
747 PDESKTOP_OBJECT DesktopObject
= NULL
;
752 /* try windowstation */
753 DPRINT("Trying to open window station 0x%x\n", hObject
);
754 Status
= IntValidateWindowStationHandle(
756 UserMode
,/*ExGetPreviousMode(),*/
757 GENERIC_READ
, /* FIXME: is this ok? */
761 if (!NT_SUCCESS(Status
) && Status
!= STATUS_OBJECT_TYPE_MISMATCH
)
763 DPRINT("Failed: 0x%x\n", Status
);
764 SetLastNtError(Status
);
768 if (Status
== STATUS_OBJECT_TYPE_MISMATCH
)
771 DPRINT("Trying to open desktop 0x%x\n", hObject
);
772 Status
= IntValidateDesktopHandle(
774 UserMode
,/*ExGetPreviousMode(),*/
775 GENERIC_READ
, /* FIXME: is this ok? */
777 if (!NT_SUCCESS(Status
))
779 DPRINT("Failed: 0x%x\n", Status
);
780 SetLastNtError(Status
);
784 DPRINT("WinSta or Desktop opened!!\n");
790 Status
= STATUS_NOT_IMPLEMENTED
;
791 DPRINT1("UOI_FLAGS unimplemented!\n");
795 if (WinStaObject
!= NULL
)
797 pvData
= ((PUNICODE_STRING
)GET_DESKTOP_NAME(WinStaObject
))->Buffer
;
798 nDataSize
= ((PUNICODE_STRING
)GET_DESKTOP_NAME(WinStaObject
))->Length
+ 2;
799 Status
= STATUS_SUCCESS
;
801 else if (DesktopObject
!= NULL
)
803 pvData
= ((PUNICODE_STRING
)GET_DESKTOP_NAME(DesktopObject
))->Buffer
;
804 nDataSize
= ((PUNICODE_STRING
)GET_DESKTOP_NAME(DesktopObject
))->Length
+ 2;
805 Status
= STATUS_SUCCESS
;
808 Status
= STATUS_INVALID_PARAMETER
;
812 if (WinStaObject
!= NULL
)
814 pvData
= L
"WindowStation";
815 nDataSize
= (wcslen(pvData
) + 1) * sizeof(WCHAR
);
816 Status
= STATUS_SUCCESS
;
818 else if (DesktopObject
!= NULL
)
821 nDataSize
= (wcslen(pvData
) + 1) * sizeof(WCHAR
);
822 Status
= STATUS_SUCCESS
;
825 Status
= STATUS_INVALID_PARAMETER
;
829 Status
= STATUS_NOT_IMPLEMENTED
;
830 DPRINT1("UOI_USER_SID unimplemented!\n");
834 Status
= STATUS_INVALID_PARAMETER
;
838 /* try to copy data to caller */
839 if (Status
== STATUS_SUCCESS
)
841 DPRINT("Trying to copy data to caller (len = %d, len needed = %d)\n", nLength
, nDataSize
);
842 *nLengthNeeded
= nDataSize
;
843 if (nLength
>= nDataSize
)
844 Status
= MmCopyToCaller(pvInformation
, pvData
, nDataSize
);
846 Status
= STATUS_BUFFER_TOO_SMALL
;
849 /* release objects */
850 if (WinStaObject
!= NULL
)
851 ObDereferenceObject(WinStaObject
);
852 if (DesktopObject
!= NULL
)
853 ObDereferenceObject(DesktopObject
);
855 SetLastNtError(Status
);
856 return NT_SUCCESS(Status
);
860 * NtUserSetObjectInformation
862 * The NtUserSetObjectInformation function sets information about a
863 * window station or desktop object.
867 * Handle to the window station or desktop object for which to set
868 * object information. This value can be a handle of type HDESK or
872 * Specifies the object information to be set.
875 * Pointer to a buffer containing the object information.
878 * Specifies the size, in bytes, of the information contained in the
879 * buffer pointed to by pvInfo.
882 * If the function succeeds, the return value is nonzero. If the function
883 * fails the return value is zero.
891 NtUserSetObjectInformation(
897 /* FIXME: ZwQueryObject */
898 /* FIXME: ZwSetInformationObject */
899 SetLastNtError(STATUS_UNSUCCESSFUL
);
907 UserGetProcessWindowStation(VOID
)
912 if(PsGetCurrentProcess() != CsrProcess
)
914 return PsGetCurrentProcess()->Win32WindowStation
;
918 DPRINT1("Should use ObFindHandleForObject\n");
919 Status
= ObOpenObjectByPointer(PsGetCurrentThreadWin32Thread()->Desktop
->WindowStation
,
923 ExWindowStationObjectType
,
926 if (! NT_SUCCESS(Status
))
928 SetLastNtError(Status
);
929 DPRINT1("Unable to open handle for CSRSSs winsta, status 0x%08x\n",
939 * NtUserGetProcessWindowStation
941 * Returns a handle to the current process window station.
944 * If the function succeeds, the return value is handle to the window
945 * station assigned to the current process. If the function fails, the
946 * return value is NULL.
953 NtUserGetProcessWindowStation(VOID
)
955 return UserGetProcessWindowStation();
958 PWINSTATION_OBJECT FASTCALL
959 IntGetWinStaObj(VOID
)
961 PWINSTATION_OBJECT WinStaObj
;
964 * just a temporary hack, this will be gone soon
967 if(PsGetCurrentThreadWin32Thread() != NULL
&& PsGetCurrentThreadWin32Thread()->Desktop
!= NULL
)
969 WinStaObj
= PsGetCurrentThreadWin32Thread()->Desktop
->WindowStation
;
970 ObReferenceObjectByPointer(WinStaObj
, KernelMode
, ExWindowStationObjectType
, 0);
972 else if(PsGetCurrentProcess() != CsrProcess
)
974 NTSTATUS Status
= IntValidateWindowStationHandle(PsGetCurrentProcess()->Win32WindowStation
,
978 if(!NT_SUCCESS(Status
))
980 SetLastNtError(Status
);
993 * NtUserSetProcessWindowStation
995 * Assigns a window station to the current process.
999 * Handle to the window station.
1009 NtUserSetProcessWindowStation(HWINSTA hWindowStation
)
1012 PWINSTATION_OBJECT NewWinSta
;
1015 DPRINT("About to set process window station with handle (0x%X)\n",
1018 if(PsGetCurrentProcess() == CsrProcess
)
1020 DPRINT1("CSRSS is not allowed to change it's window station!!!\n");
1021 SetLastWin32Error(ERROR_ACCESS_DENIED
);
1025 Status
= IntValidateWindowStationHandle(
1031 if (!NT_SUCCESS(Status
))
1033 DPRINT("Validation of window station handle (0x%X) failed\n",
1035 SetLastNtError(Status
);
1040 * FIXME - don't allow changing the window station if there are threads that are attached to desktops and own gui objects
1043 /* FIXME - dereference the old window station, etc... */
1044 hOld
= InterlockedExchangePointer(&PsGetCurrentProcess()->Win32WindowStation
, hWindowStation
);
1046 DPRINT("PsGetCurrentProcess()->Win32WindowStation 0x%X\n",
1047 PsGetCurrentProcess()->Win32WindowStation
);
1053 * NtUserLockWindowStation
1055 * Locks switching desktops. Only the logon application is allowed to call this function.
1062 NtUserLockWindowStation(HWINSTA hWindowStation
)
1064 PWINSTATION_OBJECT Object
;
1067 DPRINT("About to set process window station with handle (0x%X)\n",
1070 if(PsGetCurrentProcessWin32Process() != LogonProcess
)
1072 DPRINT1("Unauthorized process attempted to lock the window station!\n");
1073 SetLastWin32Error(ERROR_ACCESS_DENIED
);
1077 Status
= IntValidateWindowStationHandle(
1082 if (!NT_SUCCESS(Status
))
1084 DPRINT("Validation of window station handle (0x%X) failed\n",
1086 SetLastNtError(Status
);
1090 Object
->Flags
|= WSS_LOCKED
;
1092 ObDereferenceObject(Object
);
1097 * NtUserUnlockWindowStation
1099 * Unlocks switching desktops. Only the logon application is allowed to call this function.
1106 NtUserUnlockWindowStation(HWINSTA hWindowStation
)
1108 PWINSTATION_OBJECT Object
;
1112 DPRINT("About to set process window station with handle (0x%X)\n",
1115 if(PsGetCurrentProcessWin32Process() != LogonProcess
)
1117 DPRINT1("Unauthorized process attempted to unlock the window station!\n");
1118 SetLastWin32Error(ERROR_ACCESS_DENIED
);
1122 Status
= IntValidateWindowStationHandle(
1127 if (!NT_SUCCESS(Status
))
1129 DPRINT("Validation of window station handle (0x%X) failed\n",
1131 SetLastNtError(Status
);
1135 Ret
= (Object
->Flags
& WSS_LOCKED
) == WSS_LOCKED
;
1136 Object
->Flags
&= ~WSS_LOCKED
;
1138 ObDereferenceObject(Object
);
1143 * NtUserSetWindowStationUser
1150 NtUserSetWindowStationUser(
1161 static NTSTATUS FASTCALL
1162 BuildWindowStationNameList(
1165 PULONG pRequiredSize
)
1167 OBJECT_ATTRIBUTES ObjectAttributes
;
1169 HANDLE DirectoryHandle
;
1170 UNICODE_STRING DirectoryName
;
1171 char InitialBuffer
[256], *Buffer
;
1172 ULONG Context
, ReturnLength
, BufferSize
;
1174 POBJECT_DIRECTORY_INFORMATION DirEntry
;
1178 * Generate name of window station directory
1180 if (!IntGetFullWindowStationName(&DirectoryName
, NULL
, NULL
))
1182 return STATUS_INSUFFICIENT_RESOURCES
;
1186 * Try to open the directory.
1188 InitializeObjectAttributes(
1191 OBJ_CASE_INSENSITIVE
,
1195 Status
= ZwOpenDirectoryObject(
1200 ExFreePool(DirectoryName
.Buffer
);
1202 if (!NT_SUCCESS(Status
))
1207 /* First try to query the directory using a fixed-size buffer */
1210 Status
= ZwQueryDirectoryObject(DirectoryHandle
, InitialBuffer
, sizeof(InitialBuffer
),
1211 FALSE
, TRUE
, &Context
, &ReturnLength
);
1212 if (NT_SUCCESS(Status
))
1214 if (STATUS_NO_MORE_ENTRIES
== ZwQueryDirectoryObject(DirectoryHandle
, NULL
, 0, FALSE
,
1215 FALSE
, &Context
, NULL
))
1217 /* Our fixed-size buffer is large enough */
1218 Buffer
= InitialBuffer
;
1224 /* Need a larger buffer, check how large exactly */
1225 Status
= ZwQueryDirectoryObject(DirectoryHandle
, NULL
, 0, FALSE
, TRUE
, &Context
,
1227 if (STATUS_BUFFER_TOO_SMALL
== Status
)
1229 BufferSize
= ReturnLength
;
1230 Buffer
= ExAllocatePoolWithTag(PagedPool
, BufferSize
, TAG_WINSTA
);
1233 ObDereferenceObject(DirectoryHandle
);
1234 return STATUS_NO_MEMORY
;
1237 /* We should have a sufficiently large buffer now */
1239 Status
= ZwQueryDirectoryObject(DirectoryHandle
, Buffer
, BufferSize
,
1240 FALSE
, TRUE
, &Context
, &ReturnLength
);
1241 if (! NT_SUCCESS(Status
) ||
1242 STATUS_NO_MORE_ENTRIES
!= ZwQueryDirectoryObject(DirectoryHandle
, NULL
, 0, FALSE
,
1243 FALSE
, &Context
, NULL
))
1245 /* Something went wrong, maybe someone added a directory entry? Just give up. */
1247 ObDereferenceObject(DirectoryHandle
);
1248 return NT_SUCCESS(Status
) ? STATUS_INTERNAL_ERROR
: Status
;
1253 ZwClose(DirectoryHandle
);
1256 * Count the required size of buffer.
1258 ReturnLength
= sizeof(DWORD
);
1260 for (DirEntry
= (POBJECT_DIRECTORY_INFORMATION
) Buffer
; 0 != DirEntry
->Name
.Length
;
1263 ReturnLength
+= DirEntry
->Name
.Length
+ sizeof(WCHAR
);
1266 DPRINT("Required size: %d Entry count: %d\n", ReturnLength
, EntryCount
);
1267 if (NULL
!= pRequiredSize
)
1269 Status
= MmCopyToCaller(pRequiredSize
, &ReturnLength
, sizeof(ULONG
));
1270 if (! NT_SUCCESS(Status
))
1272 if (Buffer
!= InitialBuffer
)
1276 return STATUS_BUFFER_TOO_SMALL
;
1281 * Check if the supplied buffer is large enough.
1283 if (dwSize
< ReturnLength
)
1285 if (Buffer
!= InitialBuffer
)
1289 return STATUS_BUFFER_TOO_SMALL
;
1293 * Generate the resulting buffer contents.
1295 Status
= MmCopyToCaller(lpBuffer
, &EntryCount
, sizeof(DWORD
));
1296 if (! NT_SUCCESS(Status
))
1298 if (Buffer
!= InitialBuffer
)
1304 lpBuffer
= (PVOID
) ((PCHAR
) lpBuffer
+ sizeof(DWORD
));
1307 for (DirEntry
= (POBJECT_DIRECTORY_INFORMATION
) Buffer
; 0 != DirEntry
->Name
.Length
;
1310 Status
= MmCopyToCaller(lpBuffer
, DirEntry
->Name
.Buffer
, DirEntry
->Name
.Length
);
1311 if (! NT_SUCCESS(Status
))
1313 if (Buffer
!= InitialBuffer
)
1319 lpBuffer
= (PVOID
) ((PCHAR
) lpBuffer
+ DirEntry
->Name
.Length
);
1320 Status
= MmCopyToCaller(lpBuffer
, &NullWchar
, sizeof(WCHAR
));
1321 if (! NT_SUCCESS(Status
))
1323 if (Buffer
!= InitialBuffer
)
1329 lpBuffer
= (PVOID
) ((PCHAR
) lpBuffer
+ sizeof(WCHAR
));
1335 if (NULL
!= Buffer
&& Buffer
!= InitialBuffer
)
1340 return STATUS_SUCCESS
;
1343 static NTSTATUS FASTCALL
1344 BuildDesktopNameList(
1345 HWINSTA hWindowStation
,
1348 PULONG pRequiredSize
)
1351 PWINSTATION_OBJECT WindowStation
;
1353 PLIST_ENTRY DesktopEntry
;
1354 PDESKTOP_OBJECT DesktopObject
;
1359 Status
= IntValidateWindowStationHandle(hWindowStation
,
1363 if (! NT_SUCCESS(Status
))
1368 KeAcquireSpinLock(&WindowStation
->Lock
, &OldLevel
);
1371 * Count the required size of buffer.
1373 ReturnLength
= sizeof(DWORD
);
1375 for (DesktopEntry
= WindowStation
->DesktopListHead
.Flink
;
1376 DesktopEntry
!= &WindowStation
->DesktopListHead
;
1377 DesktopEntry
= DesktopEntry
->Flink
)
1379 DesktopObject
= CONTAINING_RECORD(DesktopEntry
, DESKTOP_OBJECT
, ListEntry
);
1380 ReturnLength
+= ((PUNICODE_STRING
)GET_DESKTOP_NAME(DesktopObject
))->Length
+ sizeof(WCHAR
);
1383 DPRINT("Required size: %d Entry count: %d\n", ReturnLength
, EntryCount
);
1384 if (NULL
!= pRequiredSize
)
1386 Status
= MmCopyToCaller(pRequiredSize
, &ReturnLength
, sizeof(ULONG
));
1387 if (! NT_SUCCESS(Status
))
1389 KeReleaseSpinLock(&WindowStation
->Lock
, OldLevel
);
1390 ObDereferenceObject(WindowStation
);
1391 return STATUS_BUFFER_TOO_SMALL
;
1396 * Check if the supplied buffer is large enough.
1398 if (dwSize
< ReturnLength
)
1400 KeReleaseSpinLock(&WindowStation
->Lock
, OldLevel
);
1401 ObDereferenceObject(WindowStation
);
1402 return STATUS_BUFFER_TOO_SMALL
;
1406 * Generate the resulting buffer contents.
1408 Status
= MmCopyToCaller(lpBuffer
, &EntryCount
, sizeof(DWORD
));
1409 if (! NT_SUCCESS(Status
))
1411 KeReleaseSpinLock(&WindowStation
->Lock
, OldLevel
);
1412 ObDereferenceObject(WindowStation
);
1415 lpBuffer
= (PVOID
) ((PCHAR
) lpBuffer
+ sizeof(DWORD
));
1418 for (DesktopEntry
= WindowStation
->DesktopListHead
.Flink
;
1419 DesktopEntry
!= &WindowStation
->DesktopListHead
;
1420 DesktopEntry
= DesktopEntry
->Flink
)
1422 DesktopObject
= CONTAINING_RECORD(DesktopEntry
, DESKTOP_OBJECT
, ListEntry
);
1423 Status
= MmCopyToCaller(lpBuffer
, ((PUNICODE_STRING
)GET_DESKTOP_NAME(DesktopObject
))->Buffer
, ((PUNICODE_STRING
)GET_DESKTOP_NAME(DesktopObject
))->Length
);
1424 if (! NT_SUCCESS(Status
))
1426 KeReleaseSpinLock(&WindowStation
->Lock
, OldLevel
);
1427 ObDereferenceObject(WindowStation
);
1430 lpBuffer
= (PVOID
) ((PCHAR
) lpBuffer
+ ((PUNICODE_STRING
)GET_DESKTOP_NAME(DesktopObject
))->Length
);
1431 Status
= MmCopyToCaller(lpBuffer
, &NullWchar
, sizeof(WCHAR
));
1432 if (! NT_SUCCESS(Status
))
1434 KeReleaseSpinLock(&WindowStation
->Lock
, OldLevel
);
1435 ObDereferenceObject(WindowStation
);
1438 lpBuffer
= (PVOID
) ((PCHAR
) lpBuffer
+ sizeof(WCHAR
));
1444 KeReleaseSpinLock(&WindowStation
->Lock
, OldLevel
);
1445 ObDereferenceObject(WindowStation
);
1447 return STATUS_SUCCESS
;
1451 * NtUserBuildNameList
1453 * Function used for enumeration of desktops or window stations.
1457 * For enumeration of window stations this parameter must be set to
1458 * zero. Otherwise it's handle for window station.
1461 * Size of buffer passed by caller.
1464 * Buffer passed by caller. If the function succedes, the buffer is
1465 * filled with window station/desktop count (in first DWORD) and
1466 * NULL-terminated window station/desktop names.
1469 * If the function suceedes, this is the number of bytes copied.
1470 * Otherwise it's size of buffer needed for function to succeed.
1477 NtUserBuildNameList(
1478 HWINSTA hWindowStation
,
1481 PULONG pRequiredSize
)
1483 /* The WindowStation name list and desktop name list are build in completely
1484 different ways. Call the appropriate function */
1485 return NULL
== hWindowStation
? BuildWindowStationNameList(dwSize
, lpBuffer
, pRequiredSize
) :
1486 BuildDesktopNameList(hWindowStation
, dwSize
, lpBuffer
, pRequiredSize
);