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
);
295 co_IntInitializeDesktopGraphics(VOID
)
297 UNICODE_STRING DriverName
= RTL_CONSTANT_STRING(L
"DISPLAY");
298 if (! IntCreatePrimarySurface())
302 ScreenDeviceContext
= IntGdiCreateDC(&DriverName
, NULL
, NULL
, NULL
, FALSE
);
303 if (NULL
== ScreenDeviceContext
)
305 IntDestroyPrimarySurface();
308 IntGdiSetDCOwnerEx(ScreenDeviceContext
, GDI_OBJ_HMGR_PUBLIC
, FALSE
);
310 /* Setup the cursor */
311 co_IntLoadDefaultCursors();
313 hSystemBM
= NtGdiCreateCompatibleDC(ScreenDeviceContext
);
315 NtGdiSelectFont( hSystemBM
, NtGdiGetStockObject(SYSTEM_FONT
));
316 IntGdiSetDCOwnerEx( hSystemBM
, GDI_OBJ_HMGR_PUBLIC
, FALSE
);
322 IntEndDesktopGraphics(VOID
)
324 if (NULL
!= ScreenDeviceContext
)
325 { // No need to allocate a new dcattr.
326 DC_SetOwnership(ScreenDeviceContext
, PsGetCurrentProcess());
327 NtGdiDeleteObjectApp(ScreenDeviceContext
);
328 ScreenDeviceContext
= NULL
;
330 IntHideDesktop(IntGetActiveDesktop());
331 IntDestroyPrimarySurface();
337 return ScreenDeviceContext
;
340 /* PUBLIC FUNCTIONS ***********************************************************/
343 * NtUserCreateWindowStation
345 * Creates a new window station.
348 * lpszWindowStationName
349 * Pointer to a null-terminated string specifying the name of the
350 * window station to be created. Window station names are
351 * case-insensitive and cannot contain backslash characters (\).
352 * Only members of the Administrators group are allowed to specify a
356 * Requested type of access
359 * Security descriptor
361 * Unknown3, Unknown4, Unknown5
365 * If the function succeeds, the return value is a handle to the newly
366 * created window station. If the specified window station already
367 * exists, the function succeeds and returns a handle to the existing
368 * window station. If the function fails, the return value is NULL.
371 * Correct the prototype to match the Windows one (with 7 parameters
379 NtUserCreateWindowStation(
380 PUNICODE_STRING lpszWindowStationName
,
381 ACCESS_MASK dwDesiredAccess
,
382 LPSECURITY_ATTRIBUTES lpSecurity
,
388 PSYSTEM_CURSORINFO CurInfo
;
389 UNICODE_STRING WindowStationName
;
390 UNICODE_STRING FullWindowStationName
;
391 PWINSTATION_OBJECT WindowStationObject
;
392 HWINSTA WindowStation
;
393 OBJECT_ATTRIBUTES ObjectAttributes
;
397 * Generate full window station name
399 Status
= ProbeAndCaptureUnicodeString(&WindowStationName
,
401 lpszWindowStationName
);
402 if (!NT_SUCCESS(Status
))
404 DPRINT1("Failed to capture window station name (status 0x%08x)\n",
406 SetLastNtError(Status
);
409 if (!IntGetFullWindowStationName(&FullWindowStationName
,
413 ReleaseCapturedUnicodeString(&WindowStationName
, UserMode
);
414 SetLastNtError(STATUS_INSUFFICIENT_RESOURCES
);
419 * Try to open already existing window station
422 DPRINT("Trying to open window station (%wZ)\n", &FullWindowStationName
);
424 /* Initialize ObjectAttributes for the window station object */
425 InitializeObjectAttributes(
427 &FullWindowStationName
,
432 Status
= ObOpenObjectByName(
434 ExWindowStationObjectType
,
439 (PVOID
*)&WindowStation
);
441 if (NT_SUCCESS(Status
))
443 DPRINT("Successfully opened window station (%wZ)\n",
444 FullWindowStationName
);
445 ExFreePool(FullWindowStationName
.Buffer
);
446 ReleaseCapturedUnicodeString(&WindowStationName
, UserMode
);
447 return (HWINSTA
)WindowStation
;
451 * No existing window station found, try to create new one
454 DPRINT("Creating window station (%wZ)\n", &FullWindowStationName
);
456 Status
= ObCreateObject(
458 ExWindowStationObjectType
,
462 sizeof(WINSTATION_OBJECT
),
465 (PVOID
*)&WindowStationObject
);
467 if (!NT_SUCCESS(Status
))
469 DPRINT1("Failed creating window station (%wZ)\n", &FullWindowStationName
);
470 ExFreePool(FullWindowStationName
.Buffer
);
471 ReleaseCapturedUnicodeString(&WindowStationName
, UserMode
);
472 SetLastNtError(STATUS_INSUFFICIENT_RESOURCES
);
476 KeInitializeSpinLock(&WindowStationObject
->Lock
);
478 InitializeListHead(&WindowStationObject
->DesktopListHead
);
480 WindowStationObject
->AtomTable
= NULL
;
481 Status
= RtlCreateAtomTable(37, &WindowStationObject
->AtomTable
);
482 WindowStationObject
->SystemMenuTemplate
= (HANDLE
)0;
484 WindowStationObject
->Name
= WindowStationName
;
486 Status
= ObInsertObject(
487 (PVOID
)WindowStationObject
,
489 STANDARD_RIGHTS_REQUIRED
,
492 (PVOID
*)&WindowStation
);
494 if (!NT_SUCCESS(Status
))
496 DPRINT1("Failed creating window station (%wZ)\n", &FullWindowStationName
);
497 ExFreePool(FullWindowStationName
.Buffer
);
498 ExFreePool(WindowStationName
.Buffer
);
499 SetLastNtError(STATUS_INSUFFICIENT_RESOURCES
);
500 ObDereferenceObject(WindowStationObject
);
505 * Initialize the new window station object
508 WindowStationObject
->ScreenSaverRunning
= FALSE
;
510 WindowStationObject
->FlatMenu
= FALSE
;
512 if(!(CurInfo
= ExAllocatePool(PagedPool
, sizeof(SYSTEM_CURSORINFO
))))
514 ExFreePool(FullWindowStationName
.Buffer
);
515 /* FIXME - Delete window station object */
516 ObDereferenceObject(WindowStationObject
);
517 SetLastNtError(STATUS_INSUFFICIENT_RESOURCES
);
521 CurInfo
->Enabled
= FALSE
;
522 CurInfo
->ButtonsDown
= 0;
523 CurInfo
->CursorClipInfo
.IsClipped
= FALSE
;
524 CurInfo
->LastBtnDown
= 0;
525 CurInfo
->CurrentCursorObject
= NULL
;
526 CurInfo
->ShowingCursor
= 0;
528 /* FIXME: Obtain the following information from the registry */
530 CurInfo
->WheelScroLines
= 3;
531 CurInfo
->WheelScroChars
= 3;
532 CurInfo
->SwapButtons
= FALSE
;
533 CurInfo
->DblClickSpeed
= 500;
534 CurInfo
->DblClickWidth
= 4;
535 CurInfo
->DblClickHeight
= 4;
537 CurInfo
->MouseSpeed
= 10;
538 CurInfo
->CursorAccelerationInfo
.FirstThreshold
= 6;
539 CurInfo
->CursorAccelerationInfo
.SecondThreshold
= 10;
540 CurInfo
->CursorAccelerationInfo
.Acceleration
= 1;
542 CurInfo
->MouseHoverTime
= 80;
543 CurInfo
->MouseHoverWidth
= 4;
544 CurInfo
->MouseHoverHeight
= 4;
546 WindowStationObject
->ScreenSaverActive
= FALSE
;
547 WindowStationObject
->ScreenSaverTimeOut
= 10;
548 WindowStationObject
->SystemCursor
= CurInfo
;
550 /* END FIXME loading from register */
552 if (!IntSetupClipboard(WindowStationObject
))
554 DPRINT1("WindowStation: Error Setting up the clipboard!!!\n");
557 if (!IntSetupCurIconHandles(WindowStationObject
))
559 DPRINT1("Setting up the Cursor/Icon Handle table failed!\n");
560 /* FIXME: Complain more loudly? */
561 ExFreePool(FullWindowStationName
.Buffer
);
564 DPRINT("Window station successfully created (%wZ)\n", &FullWindowStationName
);
565 ExFreePool(FullWindowStationName
.Buffer
);
566 return WindowStation
;
570 * NtUserOpenWindowStation
572 * Opens an existing window station.
575 * lpszWindowStationName
576 * Name of the existing window station.
579 * Requested type of access.
582 * If the function succeeds, the return value is the handle to the
583 * specified window station. If the function fails, the return value
587 * The returned handle can be closed with NtUserCloseWindowStation.
594 NtUserOpenWindowStation(
595 PUNICODE_STRING lpszWindowStationName
,
596 ACCESS_MASK dwDesiredAccess
)
598 UNICODE_STRING WindowStationName
;
599 HWINSTA WindowStation
;
600 OBJECT_ATTRIBUTES ObjectAttributes
;
604 * Generate full window station name
607 if (!IntGetFullWindowStationName(&WindowStationName
, lpszWindowStationName
,
610 SetLastNtError(STATUS_INSUFFICIENT_RESOURCES
);
614 DPRINT("Trying to open window station (%wZ)\n", &WindowStationName
);
616 /* Initialize ObjectAttributes for the window station object */
617 InitializeObjectAttributes(
624 Status
= ObOpenObjectByName(
626 ExWindowStationObjectType
,
631 (PVOID
*)&WindowStation
);
633 if (!NT_SUCCESS(Status
))
635 SetLastNtError(Status
);
636 ExFreePool(WindowStationName
.Buffer
);
640 DPRINT("Successfully opened window station (%wZ)\n", &WindowStationName
);
641 ExFreePool(WindowStationName
.Buffer
);
643 return WindowStation
;
647 * NtUserCloseWindowStation
649 * Closes a window station handle.
653 * Handle to the window station.
659 * The window station handle can be created with NtUserCreateWindowStation
660 * or NtUserOpenWindowStation. Attemps to close a handle to the window
661 * station assigned to the calling process will fail.
669 NtUserCloseWindowStation(
672 PWINSTATION_OBJECT Object
;
675 DPRINT("About to close window station handle (0x%X)\n", hWinSta
);
677 Status
= IntValidateWindowStationHandle(
683 if (!NT_SUCCESS(Status
))
685 DPRINT("Validation of window station handle (0x%X) failed\n", hWinSta
);
690 /* FIXME - free the cursor information when actually deleting the object!! */
691 ASSERT(Object
->SystemCursor
);
692 ExFreePool(Object
->SystemCursor
);
695 ObDereferenceObject(Object
);
697 DPRINT("Closing window station handle (0x%X)\n", hWinSta
);
699 Status
= ZwClose(hWinSta
);
700 if (!NT_SUCCESS(Status
))
702 SetLastNtError(Status
);
710 * NtUserGetObjectInformation
712 * The NtUserGetObjectInformation function retrieves information about a
713 * window station or desktop object.
717 * Handle to the window station or desktop object for which to
718 * return information. This can be a handle of type HDESK or HWINSTA
719 * (for example, a handle returned by NtUserCreateWindowStation,
720 * NtUserOpenWindowStation, NtUserCreateDesktop, or NtUserOpenDesktop).
723 * Specifies the object information to be retrieved.
726 * Pointer to a buffer to receive the object information.
729 * Specifies the size, in bytes, of the buffer pointed to by the
733 * Pointer to a variable receiving the number of bytes required to
734 * store the requested information. If this variable's value is
735 * greater than the value of the nLength parameter when the function
736 * returns, the function returns FALSE, and none of the information
737 * is copied to the pvInfo buffer. If the value of the variable pointed
738 * to by lpnLengthNeeded is less than or equal to the value of nLength,
739 * the entire information block is copied.
742 * If the function succeeds, the return value is nonzero. If the function
743 * fails, the return value is zero.
750 NtUserGetObjectInformation(
755 PDWORD nLengthNeeded
)
757 PWINSTATION_OBJECT WinStaObject
= NULL
;
758 PDESKTOP DesktopObject
= NULL
;
763 /* try windowstation */
764 DPRINT("Trying to open window station 0x%x\n", hObject
);
765 Status
= IntValidateWindowStationHandle(
767 UserMode
,/*ExGetPreviousMode(),*/
768 GENERIC_READ
, /* FIXME: is this ok? */
772 if (!NT_SUCCESS(Status
) && Status
!= STATUS_OBJECT_TYPE_MISMATCH
)
774 DPRINT("Failed: 0x%x\n", Status
);
775 SetLastNtError(Status
);
779 if (Status
== STATUS_OBJECT_TYPE_MISMATCH
)
782 DPRINT("Trying to open desktop 0x%x\n", hObject
);
783 Status
= IntValidateDesktopHandle(
785 UserMode
,/*ExGetPreviousMode(),*/
786 GENERIC_READ
, /* FIXME: is this ok? */
788 if (!NT_SUCCESS(Status
))
790 DPRINT("Failed: 0x%x\n", Status
);
791 SetLastNtError(Status
);
795 DPRINT("WinSta or Desktop opened!!\n");
801 Status
= STATUS_NOT_IMPLEMENTED
;
802 DPRINT1("UOI_FLAGS unimplemented!\n");
806 if (WinStaObject
!= NULL
)
808 pvData
= ((PUNICODE_STRING
)GET_DESKTOP_NAME(WinStaObject
))->Buffer
;
809 nDataSize
= ((PUNICODE_STRING
)GET_DESKTOP_NAME(WinStaObject
))->Length
+ 2;
810 Status
= STATUS_SUCCESS
;
812 else if (DesktopObject
!= NULL
)
814 pvData
= ((PUNICODE_STRING
)GET_DESKTOP_NAME(DesktopObject
))->Buffer
;
815 nDataSize
= ((PUNICODE_STRING
)GET_DESKTOP_NAME(DesktopObject
))->Length
+ 2;
816 Status
= STATUS_SUCCESS
;
819 Status
= STATUS_INVALID_PARAMETER
;
823 if (WinStaObject
!= NULL
)
825 pvData
= L
"WindowStation";
826 nDataSize
= (wcslen(pvData
) + 1) * sizeof(WCHAR
);
827 Status
= STATUS_SUCCESS
;
829 else if (DesktopObject
!= NULL
)
832 nDataSize
= (wcslen(pvData
) + 1) * sizeof(WCHAR
);
833 Status
= STATUS_SUCCESS
;
836 Status
= STATUS_INVALID_PARAMETER
;
840 Status
= STATUS_NOT_IMPLEMENTED
;
841 DPRINT1("UOI_USER_SID unimplemented!\n");
845 Status
= STATUS_INVALID_PARAMETER
;
849 /* try to copy data to caller */
850 if (Status
== STATUS_SUCCESS
)
852 DPRINT("Trying to copy data to caller (len = %d, len needed = %d)\n", nLength
, nDataSize
);
853 *nLengthNeeded
= nDataSize
;
854 if (nLength
>= nDataSize
)
855 Status
= MmCopyToCaller(pvInformation
, pvData
, nDataSize
);
857 Status
= STATUS_BUFFER_TOO_SMALL
;
860 /* release objects */
861 if (WinStaObject
!= NULL
)
862 ObDereferenceObject(WinStaObject
);
863 if (DesktopObject
!= NULL
)
864 ObDereferenceObject(DesktopObject
);
866 SetLastNtError(Status
);
867 return NT_SUCCESS(Status
);
871 * NtUserSetObjectInformation
873 * The NtUserSetObjectInformation function sets information about a
874 * window station or desktop object.
878 * Handle to the window station or desktop object for which to set
879 * object information. This value can be a handle of type HDESK or
883 * Specifies the object information to be set.
886 * Pointer to a buffer containing the object information.
889 * Specifies the size, in bytes, of the information contained in the
890 * buffer pointed to by pvInfo.
893 * If the function succeeds, the return value is nonzero. If the function
894 * fails the return value is zero.
902 NtUserSetObjectInformation(
908 /* FIXME: ZwQueryObject */
909 /* FIXME: ZwSetInformationObject */
910 SetLastNtError(STATUS_UNSUCCESSFUL
);
918 UserGetProcessWindowStation(VOID
)
924 if(PsGetCurrentProcess() != CsrProcess
)
926 return PsGetCurrentProcess()->Win32WindowStation
;
930 DPRINT1("Should use ObFindHandleForObject\n");
931 pti
= PsGetCurrentThreadWin32Thread();
932 Status
= ObOpenObjectByPointer(pti
->Desktop
->WindowStation
,
936 ExWindowStationObjectType
,
939 if (! NT_SUCCESS(Status
))
941 SetLastNtError(Status
);
942 DPRINT1("Unable to open handle for CSRSSs winsta, status 0x%08x\n",
952 * NtUserGetProcessWindowStation
954 * Returns a handle to the current process window station.
957 * If the function succeeds, the return value is handle to the window
958 * station assigned to the current process. If the function fails, the
959 * return value is NULL.
966 NtUserGetProcessWindowStation(VOID
)
968 return UserGetProcessWindowStation();
971 PWINSTATION_OBJECT FASTCALL
972 IntGetWinStaObj(VOID
)
974 PWINSTATION_OBJECT WinStaObj
;
975 PTHREADINFO Win32Thread
;
976 PEPROCESS CurrentProcess
;
979 * just a temporary hack, this will be gone soon
982 Win32Thread
= PsGetCurrentThreadWin32Thread();
983 if(Win32Thread
!= NULL
&& Win32Thread
->Desktop
!= NULL
)
985 WinStaObj
= Win32Thread
->Desktop
->WindowStation
;
986 ObReferenceObjectByPointer(WinStaObj
, KernelMode
, ExWindowStationObjectType
, 0);
988 else if((CurrentProcess
= PsGetCurrentProcess()) != CsrProcess
)
990 NTSTATUS Status
= IntValidateWindowStationHandle(CurrentProcess
->Win32WindowStation
,
994 if(!NT_SUCCESS(Status
))
996 SetLastNtError(Status
);
1009 * NtUserSetProcessWindowStation
1011 * Assigns a window station to the current process.
1015 * Handle to the window station.
1025 NtUserSetProcessWindowStation(HWINSTA hWindowStation
)
1028 PWINSTATION_OBJECT NewWinSta
;
1031 DPRINT("About to set process window station with handle (0x%X)\n",
1034 if(PsGetCurrentProcess() == CsrProcess
)
1036 DPRINT1("CSRSS is not allowed to change it's window station!!!\n");
1037 SetLastWin32Error(ERROR_ACCESS_DENIED
);
1041 Status
= IntValidateWindowStationHandle(
1047 if (!NT_SUCCESS(Status
))
1049 DPRINT("Validation of window station handle (0x%X) failed\n",
1051 SetLastNtError(Status
);
1056 * FIXME - don't allow changing the window station if there are threads that are attached to desktops and own gui objects
1059 /* FIXME - dereference the old window station, etc... */
1060 hOld
= InterlockedExchangePointer(&PsGetCurrentProcess()->Win32WindowStation
, hWindowStation
);
1062 DPRINT("PsGetCurrentProcess()->Win32WindowStation 0x%X\n",
1063 PsGetCurrentProcess()->Win32WindowStation
);
1069 * NtUserLockWindowStation
1071 * Locks switching desktops. Only the logon application is allowed to call this function.
1078 NtUserLockWindowStation(HWINSTA hWindowStation
)
1080 PWINSTATION_OBJECT Object
;
1083 DPRINT("About to set process window station with handle (0x%X)\n",
1086 if(PsGetCurrentProcessWin32Process() != LogonProcess
)
1088 DPRINT1("Unauthorized process attempted to lock the window station!\n");
1089 SetLastWin32Error(ERROR_ACCESS_DENIED
);
1093 Status
= IntValidateWindowStationHandle(
1098 if (!NT_SUCCESS(Status
))
1100 DPRINT("Validation of window station handle (0x%X) failed\n",
1102 SetLastNtError(Status
);
1106 Object
->Flags
|= WSS_LOCKED
;
1108 ObDereferenceObject(Object
);
1113 * NtUserUnlockWindowStation
1115 * Unlocks switching desktops. Only the logon application is allowed to call this function.
1122 NtUserUnlockWindowStation(HWINSTA hWindowStation
)
1124 PWINSTATION_OBJECT Object
;
1128 DPRINT("About to set process window station with handle (0x%X)\n",
1131 if(PsGetCurrentProcessWin32Process() != LogonProcess
)
1133 DPRINT1("Unauthorized process attempted to unlock the window station!\n");
1134 SetLastWin32Error(ERROR_ACCESS_DENIED
);
1138 Status
= IntValidateWindowStationHandle(
1143 if (!NT_SUCCESS(Status
))
1145 DPRINT("Validation of window station handle (0x%X) failed\n",
1147 SetLastNtError(Status
);
1151 Ret
= (Object
->Flags
& WSS_LOCKED
) == WSS_LOCKED
;
1152 Object
->Flags
&= ~WSS_LOCKED
;
1154 ObDereferenceObject(Object
);
1159 * NtUserSetWindowStationUser
1166 NtUserSetWindowStationUser(
1177 static NTSTATUS FASTCALL
1178 BuildWindowStationNameList(
1181 PULONG pRequiredSize
)
1183 OBJECT_ATTRIBUTES ObjectAttributes
;
1185 HANDLE DirectoryHandle
;
1186 UNICODE_STRING DirectoryName
;
1187 char InitialBuffer
[256], *Buffer
;
1188 ULONG Context
, ReturnLength
, BufferSize
;
1190 POBJECT_DIRECTORY_INFORMATION DirEntry
;
1194 * Generate name of window station directory
1196 if (!IntGetFullWindowStationName(&DirectoryName
, NULL
, NULL
))
1198 return STATUS_INSUFFICIENT_RESOURCES
;
1202 * Try to open the directory.
1204 InitializeObjectAttributes(
1207 OBJ_CASE_INSENSITIVE
,
1211 Status
= ZwOpenDirectoryObject(
1216 ExFreePool(DirectoryName
.Buffer
);
1218 if (!NT_SUCCESS(Status
))
1223 /* First try to query the directory using a fixed-size buffer */
1226 Status
= ZwQueryDirectoryObject(DirectoryHandle
, InitialBuffer
, sizeof(InitialBuffer
),
1227 FALSE
, TRUE
, &Context
, &ReturnLength
);
1228 if (NT_SUCCESS(Status
))
1230 if (STATUS_NO_MORE_ENTRIES
== ZwQueryDirectoryObject(DirectoryHandle
, NULL
, 0, FALSE
,
1231 FALSE
, &Context
, NULL
))
1233 /* Our fixed-size buffer is large enough */
1234 Buffer
= InitialBuffer
;
1240 /* Need a larger buffer, check how large exactly */
1241 Status
= ZwQueryDirectoryObject(DirectoryHandle
, NULL
, 0, FALSE
, TRUE
, &Context
,
1243 if (STATUS_BUFFER_TOO_SMALL
== Status
)
1245 BufferSize
= ReturnLength
;
1246 Buffer
= ExAllocatePoolWithTag(PagedPool
, BufferSize
, TAG_WINSTA
);
1249 ObDereferenceObject(DirectoryHandle
);
1250 return STATUS_NO_MEMORY
;
1253 /* We should have a sufficiently large buffer now */
1255 Status
= ZwQueryDirectoryObject(DirectoryHandle
, Buffer
, BufferSize
,
1256 FALSE
, TRUE
, &Context
, &ReturnLength
);
1257 if (! NT_SUCCESS(Status
) ||
1258 STATUS_NO_MORE_ENTRIES
!= ZwQueryDirectoryObject(DirectoryHandle
, NULL
, 0, FALSE
,
1259 FALSE
, &Context
, NULL
))
1261 /* Something went wrong, maybe someone added a directory entry? Just give up. */
1263 ObDereferenceObject(DirectoryHandle
);
1264 return NT_SUCCESS(Status
) ? STATUS_INTERNAL_ERROR
: Status
;
1269 ZwClose(DirectoryHandle
);
1272 * Count the required size of buffer.
1274 ReturnLength
= sizeof(DWORD
);
1276 for (DirEntry
= (POBJECT_DIRECTORY_INFORMATION
) Buffer
; 0 != DirEntry
->Name
.Length
;
1279 ReturnLength
+= DirEntry
->Name
.Length
+ sizeof(WCHAR
);
1282 DPRINT("Required size: %d Entry count: %d\n", ReturnLength
, EntryCount
);
1283 if (NULL
!= pRequiredSize
)
1285 Status
= MmCopyToCaller(pRequiredSize
, &ReturnLength
, sizeof(ULONG
));
1286 if (! NT_SUCCESS(Status
))
1288 if (Buffer
!= InitialBuffer
)
1292 return STATUS_BUFFER_TOO_SMALL
;
1297 * Check if the supplied buffer is large enough.
1299 if (dwSize
< ReturnLength
)
1301 if (Buffer
!= InitialBuffer
)
1305 return STATUS_BUFFER_TOO_SMALL
;
1309 * Generate the resulting buffer contents.
1311 Status
= MmCopyToCaller(lpBuffer
, &EntryCount
, sizeof(DWORD
));
1312 if (! NT_SUCCESS(Status
))
1314 if (Buffer
!= InitialBuffer
)
1320 lpBuffer
= (PVOID
) ((PCHAR
) lpBuffer
+ sizeof(DWORD
));
1323 for (DirEntry
= (POBJECT_DIRECTORY_INFORMATION
) Buffer
; 0 != DirEntry
->Name
.Length
;
1326 Status
= MmCopyToCaller(lpBuffer
, DirEntry
->Name
.Buffer
, DirEntry
->Name
.Length
);
1327 if (! NT_SUCCESS(Status
))
1329 if (Buffer
!= InitialBuffer
)
1335 lpBuffer
= (PVOID
) ((PCHAR
) lpBuffer
+ DirEntry
->Name
.Length
);
1336 Status
= MmCopyToCaller(lpBuffer
, &NullWchar
, sizeof(WCHAR
));
1337 if (! NT_SUCCESS(Status
))
1339 if (Buffer
!= InitialBuffer
)
1345 lpBuffer
= (PVOID
) ((PCHAR
) lpBuffer
+ sizeof(WCHAR
));
1351 if (NULL
!= Buffer
&& Buffer
!= InitialBuffer
)
1356 return STATUS_SUCCESS
;
1359 static NTSTATUS FASTCALL
1360 BuildDesktopNameList(
1361 HWINSTA hWindowStation
,
1364 PULONG pRequiredSize
)
1367 PWINSTATION_OBJECT WindowStation
;
1369 PLIST_ENTRY DesktopEntry
;
1370 PDESKTOP DesktopObject
;
1375 Status
= IntValidateWindowStationHandle(hWindowStation
,
1379 if (! NT_SUCCESS(Status
))
1384 KeAcquireSpinLock(&WindowStation
->Lock
, &OldLevel
);
1387 * Count the required size of buffer.
1389 ReturnLength
= sizeof(DWORD
);
1391 for (DesktopEntry
= WindowStation
->DesktopListHead
.Flink
;
1392 DesktopEntry
!= &WindowStation
->DesktopListHead
;
1393 DesktopEntry
= DesktopEntry
->Flink
)
1395 DesktopObject
= CONTAINING_RECORD(DesktopEntry
, DESKTOP
, ListEntry
);
1396 ReturnLength
+= ((PUNICODE_STRING
)GET_DESKTOP_NAME(DesktopObject
))->Length
+ sizeof(WCHAR
);
1399 DPRINT("Required size: %d Entry count: %d\n", ReturnLength
, EntryCount
);
1400 if (NULL
!= pRequiredSize
)
1402 Status
= MmCopyToCaller(pRequiredSize
, &ReturnLength
, sizeof(ULONG
));
1403 if (! NT_SUCCESS(Status
))
1405 KeReleaseSpinLock(&WindowStation
->Lock
, OldLevel
);
1406 ObDereferenceObject(WindowStation
);
1407 return STATUS_BUFFER_TOO_SMALL
;
1412 * Check if the supplied buffer is large enough.
1414 if (dwSize
< ReturnLength
)
1416 KeReleaseSpinLock(&WindowStation
->Lock
, OldLevel
);
1417 ObDereferenceObject(WindowStation
);
1418 return STATUS_BUFFER_TOO_SMALL
;
1422 * Generate the resulting buffer contents.
1424 Status
= MmCopyToCaller(lpBuffer
, &EntryCount
, sizeof(DWORD
));
1425 if (! NT_SUCCESS(Status
))
1427 KeReleaseSpinLock(&WindowStation
->Lock
, OldLevel
);
1428 ObDereferenceObject(WindowStation
);
1431 lpBuffer
= (PVOID
) ((PCHAR
) lpBuffer
+ sizeof(DWORD
));
1434 for (DesktopEntry
= WindowStation
->DesktopListHead
.Flink
;
1435 DesktopEntry
!= &WindowStation
->DesktopListHead
;
1436 DesktopEntry
= DesktopEntry
->Flink
)
1438 DesktopObject
= CONTAINING_RECORD(DesktopEntry
, DESKTOP
, ListEntry
);
1439 Status
= MmCopyToCaller(lpBuffer
, ((PUNICODE_STRING
)GET_DESKTOP_NAME(DesktopObject
))->Buffer
, ((PUNICODE_STRING
)GET_DESKTOP_NAME(DesktopObject
))->Length
);
1440 if (! NT_SUCCESS(Status
))
1442 KeReleaseSpinLock(&WindowStation
->Lock
, OldLevel
);
1443 ObDereferenceObject(WindowStation
);
1446 lpBuffer
= (PVOID
) ((PCHAR
) lpBuffer
+ ((PUNICODE_STRING
)GET_DESKTOP_NAME(DesktopObject
))->Length
);
1447 Status
= MmCopyToCaller(lpBuffer
, &NullWchar
, sizeof(WCHAR
));
1448 if (! NT_SUCCESS(Status
))
1450 KeReleaseSpinLock(&WindowStation
->Lock
, OldLevel
);
1451 ObDereferenceObject(WindowStation
);
1454 lpBuffer
= (PVOID
) ((PCHAR
) lpBuffer
+ sizeof(WCHAR
));
1460 KeReleaseSpinLock(&WindowStation
->Lock
, OldLevel
);
1461 ObDereferenceObject(WindowStation
);
1463 return STATUS_SUCCESS
;
1467 * NtUserBuildNameList
1469 * Function used for enumeration of desktops or window stations.
1473 * For enumeration of window stations this parameter must be set to
1474 * zero. Otherwise it's handle for window station.
1477 * Size of buffer passed by caller.
1480 * Buffer passed by caller. If the function succedes, the buffer is
1481 * filled with window station/desktop count (in first DWORD) and
1482 * NULL-terminated window station/desktop names.
1485 * If the function suceedes, this is the number of bytes copied.
1486 * Otherwise it's size of buffer needed for function to succeed.
1493 NtUserBuildNameList(
1494 HWINSTA hWindowStation
,
1497 PULONG pRequiredSize
)
1499 /* The WindowStation name list and desktop name list are build in completely
1500 different ways. Call the appropriate function */
1501 return NULL
== hWindowStation
? BuildWindowStationNameList(dwSize
, lpBuffer
, pRequiredSize
) :
1502 BuildDesktopNameList(hWindowStation
, dwSize
, lpBuffer
, pRequiredSize
);