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
)
298 UNICODE_STRING DriverName
= RTL_CONSTANT_STRING(L
"DISPLAY");
299 if (! IntCreatePrimarySurface())
303 ScreenDeviceContext
= IntGdiCreateDC(&DriverName
, NULL
, NULL
, NULL
, FALSE
);
304 if (NULL
== ScreenDeviceContext
)
306 IntDestroyPrimarySurface();
309 IntGdiSetDCOwnerEx(ScreenDeviceContext
, GDI_OBJ_HMGR_PUBLIC
, FALSE
);
311 /* Setup the cursor */
312 co_IntLoadDefaultCursors();
314 hSystemBM
= NtGdiCreateCompatibleDC(ScreenDeviceContext
);
316 NtGdiSelectFont( hSystemBM
, NtGdiGetStockObject(SYSTEM_FONT
));
317 IntGdiSetDCOwnerEx( hSystemBM
, GDI_OBJ_HMGR_PUBLIC
, FALSE
);
319 // FIXME! Move these to a update routine.
320 gpsi
->Planes
= NtGdiGetDeviceCaps(ScreenDeviceContext
, PLANES
);
321 gpsi
->BitsPixel
= NtGdiGetDeviceCaps(ScreenDeviceContext
, BITSPIXEL
);
322 gpsi
->BitCount
= gpsi
->Planes
* gpsi
->BitsPixel
;
323 gpsi
->dmLogPixels
= NtGdiGetDeviceCaps(ScreenDeviceContext
, LOGPIXELSY
);
324 // Font is realized and this dc was previously set to internal DC_ATTR.
325 gpsi
->cxSysFontChar
= IntGetCharDimensions(hSystemBM
, &tmw
, (DWORD
*)&gpsi
->cySysFontChar
);
326 gpsi
->tmSysFont
= tmw
;
332 IntEndDesktopGraphics(VOID
)
334 if (NULL
!= ScreenDeviceContext
)
335 { // No need to allocate a new dcattr.
336 DC_SetOwnership(ScreenDeviceContext
, PsGetCurrentProcess());
337 NtGdiDeleteObjectApp(ScreenDeviceContext
);
338 ScreenDeviceContext
= NULL
;
340 IntHideDesktop(IntGetActiveDesktop());
341 IntDestroyPrimarySurface();
347 return ScreenDeviceContext
;
350 /* PUBLIC FUNCTIONS ***********************************************************/
353 * NtUserCreateWindowStation
355 * Creates a new window station.
358 * lpszWindowStationName
359 * Pointer to a null-terminated string specifying the name of the
360 * window station to be created. Window station names are
361 * case-insensitive and cannot contain backslash characters (\).
362 * Only members of the Administrators group are allowed to specify a
366 * Requested type of access
369 * Security descriptor
371 * Unknown3, Unknown4, Unknown5
375 * If the function succeeds, the return value is a handle to the newly
376 * created window station. If the specified window station already
377 * exists, the function succeeds and returns a handle to the existing
378 * window station. If the function fails, the return value is NULL.
381 * Correct the prototype to match the Windows one (with 7 parameters
389 NtUserCreateWindowStation(
390 PUNICODE_STRING lpszWindowStationName
,
391 ACCESS_MASK dwDesiredAccess
,
392 LPSECURITY_ATTRIBUTES lpSecurity
,
398 PSYSTEM_CURSORINFO CurInfo
;
399 UNICODE_STRING WindowStationName
;
400 UNICODE_STRING FullWindowStationName
;
401 PWINSTATION_OBJECT WindowStationObject
;
402 HWINSTA WindowStation
;
403 OBJECT_ATTRIBUTES ObjectAttributes
;
407 * Generate full window station name
409 Status
= ProbeAndCaptureUnicodeString(&WindowStationName
,
411 lpszWindowStationName
);
412 if (!NT_SUCCESS(Status
))
414 DPRINT1("Failed to capture window station name (status 0x%08x)\n",
416 SetLastNtError(Status
);
419 if (!IntGetFullWindowStationName(&FullWindowStationName
,
423 ReleaseCapturedUnicodeString(&WindowStationName
, UserMode
);
424 SetLastNtError(STATUS_INSUFFICIENT_RESOURCES
);
429 * Try to open already existing window station
432 DPRINT("Trying to open window station (%wZ)\n", &FullWindowStationName
);
434 /* Initialize ObjectAttributes for the window station object */
435 InitializeObjectAttributes(
437 &FullWindowStationName
,
442 Status
= ObOpenObjectByName(
444 ExWindowStationObjectType
,
449 (PVOID
*)&WindowStation
);
451 if (NT_SUCCESS(Status
))
453 DPRINT("Successfully opened window station (%wZ)\n",
454 FullWindowStationName
);
455 ExFreePool(FullWindowStationName
.Buffer
);
456 ReleaseCapturedUnicodeString(&WindowStationName
, UserMode
);
457 return (HWINSTA
)WindowStation
;
461 * No existing window station found, try to create new one
464 DPRINT("Creating window station (%wZ)\n", &FullWindowStationName
);
466 Status
= ObCreateObject(
468 ExWindowStationObjectType
,
472 sizeof(WINSTATION_OBJECT
),
475 (PVOID
*)&WindowStationObject
);
477 if (!NT_SUCCESS(Status
))
479 DPRINT1("Failed creating window station (%wZ)\n", &FullWindowStationName
);
480 ExFreePool(FullWindowStationName
.Buffer
);
481 ReleaseCapturedUnicodeString(&WindowStationName
, UserMode
);
482 SetLastNtError(STATUS_INSUFFICIENT_RESOURCES
);
486 /* Zero out the buffer */
487 RtlZeroMemory(WindowStationObject
, sizeof(WINSTATION_OBJECT
));
489 KeInitializeSpinLock(&WindowStationObject
->Lock
);
491 InitializeListHead(&WindowStationObject
->DesktopListHead
);
493 WindowStationObject
->AtomTable
= NULL
;
494 Status
= RtlCreateAtomTable(37, &WindowStationObject
->AtomTable
);
495 WindowStationObject
->SystemMenuTemplate
= (HANDLE
)0;
497 WindowStationObject
->Name
= WindowStationName
;
499 Status
= ObInsertObject(
500 (PVOID
)WindowStationObject
,
502 STANDARD_RIGHTS_REQUIRED
,
505 (PVOID
*)&WindowStation
);
507 if (!NT_SUCCESS(Status
))
509 DPRINT1("Failed creating window station (%wZ)\n", &FullWindowStationName
);
510 ExFreePool(FullWindowStationName
.Buffer
);
511 ExFreePool(WindowStationName
.Buffer
);
512 SetLastNtError(STATUS_INSUFFICIENT_RESOURCES
);
513 ObDereferenceObject(WindowStationObject
);
518 * Initialize the new window station object
521 WindowStationObject
->ScreenSaverRunning
= FALSE
;
523 WindowStationObject
->FlatMenu
= FALSE
;
525 if(!(CurInfo
= ExAllocatePool(PagedPool
, sizeof(SYSTEM_CURSORINFO
))))
527 ExFreePool(FullWindowStationName
.Buffer
);
528 /* FIXME - Delete window station object */
529 ObDereferenceObject(WindowStationObject
);
530 SetLastNtError(STATUS_INSUFFICIENT_RESOURCES
);
534 CurInfo
->Enabled
= FALSE
;
535 CurInfo
->ButtonsDown
= 0;
536 CurInfo
->CursorClipInfo
.IsClipped
= FALSE
;
537 CurInfo
->LastBtnDown
= 0;
538 CurInfo
->CurrentCursorObject
= NULL
;
539 CurInfo
->ShowingCursor
= 0;
541 /* FIXME: Obtain the following information from the registry */
543 // CurInfo->WheelScroLines = 3;
544 // CurInfo->WheelScroChars = 3;
545 CurInfo
->SwapButtons
= FALSE
;
546 CurInfo
->DblClickSpeed
= 500;
547 CurInfo
->DblClickWidth
= 4;
548 CurInfo
->DblClickHeight
= 4;
550 // CurInfo->MouseSpeed = 10;
551 // CurInfo->CursorAccelerationInfo.FirstThreshold = 6;
552 // CurInfo->CursorAccelerationInfo.SecondThreshold = 10;
553 // CurInfo->CursorAccelerationInfo.Acceleration = 1;
555 // CurInfo->MouseHoverTime = 80;
556 // CurInfo->MouseHoverWidth = 4;
557 // CurInfo->MouseHoverHeight = 4;
559 // WindowStationObject->ScreenSaverActive = FALSE;
560 // WindowStationObject->ScreenSaverTimeOut = 10;
561 WindowStationObject
->SystemCursor
= CurInfo
;
563 /* END FIXME loading from register */
565 if (!IntSetupClipboard(WindowStationObject
))
567 DPRINT1("WindowStation: Error Setting up the clipboard!!!\n");
570 if (!IntSetupCurIconHandles(WindowStationObject
))
572 DPRINT1("Setting up the Cursor/Icon Handle table failed!\n");
573 /* FIXME: Complain more loudly? */
574 ExFreePool(FullWindowStationName
.Buffer
);
577 DPRINT("Window station successfully created (%wZ)\n", &FullWindowStationName
);
578 ExFreePool(FullWindowStationName
.Buffer
);
579 return WindowStation
;
583 * NtUserOpenWindowStation
585 * Opens an existing window station.
588 * lpszWindowStationName
589 * Name of the existing window station.
592 * Requested type of access.
595 * If the function succeeds, the return value is the handle to the
596 * specified window station. If the function fails, the return value
600 * The returned handle can be closed with NtUserCloseWindowStation.
607 NtUserOpenWindowStation(
608 PUNICODE_STRING lpszWindowStationName
,
609 ACCESS_MASK dwDesiredAccess
)
611 UNICODE_STRING WindowStationName
;
612 HWINSTA WindowStation
;
613 OBJECT_ATTRIBUTES ObjectAttributes
;
617 * Generate full window station name
620 if (!IntGetFullWindowStationName(&WindowStationName
, lpszWindowStationName
,
623 SetLastNtError(STATUS_INSUFFICIENT_RESOURCES
);
627 DPRINT("Trying to open window station (%wZ)\n", &WindowStationName
);
629 /* Initialize ObjectAttributes for the window station object */
630 InitializeObjectAttributes(
633 OBJ_CASE_INSENSITIVE
,
637 Status
= ObOpenObjectByName(
639 ExWindowStationObjectType
,
644 (PVOID
*)&WindowStation
);
646 if (!NT_SUCCESS(Status
))
648 SetLastNtError(Status
);
649 ExFreePool(WindowStationName
.Buffer
);
653 DPRINT("Successfully opened window station (%wZ)\n", &WindowStationName
);
654 ExFreePool(WindowStationName
.Buffer
);
656 return WindowStation
;
660 * NtUserCloseWindowStation
662 * Closes a window station handle.
666 * Handle to the window station.
672 * The window station handle can be created with NtUserCreateWindowStation
673 * or NtUserOpenWindowStation. Attemps to close a handle to the window
674 * station assigned to the calling process will fail.
682 NtUserCloseWindowStation(
685 PWINSTATION_OBJECT Object
;
688 DPRINT("About to close window station handle (0x%X)\n", hWinSta
);
690 if (hWinSta
== UserGetProcessWindowStation())
695 Status
= IntValidateWindowStationHandle(
701 if (!NT_SUCCESS(Status
))
703 DPRINT("Validation of window station handle (0x%X) failed\n", hWinSta
);
708 /* FIXME - free the cursor information when actually deleting the object!! */
709 ASSERT(Object
->SystemCursor
);
710 ExFreePool(Object
->SystemCursor
);
713 ObDereferenceObject(Object
);
715 DPRINT("Closing window station handle (0x%X)\n", hWinSta
);
717 Status
= ZwClose(hWinSta
);
718 if (!NT_SUCCESS(Status
))
720 SetLastNtError(Status
);
728 * NtUserGetObjectInformation
730 * The NtUserGetObjectInformation function retrieves information about a
731 * window station or desktop object.
735 * Handle to the window station or desktop object for which to
736 * return information. This can be a handle of type HDESK or HWINSTA
737 * (for example, a handle returned by NtUserCreateWindowStation,
738 * NtUserOpenWindowStation, NtUserCreateDesktop, or NtUserOpenDesktop).
741 * Specifies the object information to be retrieved.
744 * Pointer to a buffer to receive the object information.
747 * Specifies the size, in bytes, of the buffer pointed to by the
751 * Pointer to a variable receiving the number of bytes required to
752 * store the requested information. If this variable's value is
753 * greater than the value of the nLength parameter when the function
754 * returns, the function returns FALSE, and none of the information
755 * is copied to the pvInfo buffer. If the value of the variable pointed
756 * to by lpnLengthNeeded is less than or equal to the value of nLength,
757 * the entire information block is copied.
760 * If the function succeeds, the return value is nonzero. If the function
761 * fails, the return value is zero.
768 NtUserGetObjectInformation(
773 PDWORD nLengthNeeded
)
775 PWINSTATION_OBJECT WinStaObject
= NULL
;
776 PDESKTOP DesktopObject
= NULL
;
781 /* try windowstation */
782 DPRINT("Trying to open window station 0x%x\n", hObject
);
783 Status
= IntValidateWindowStationHandle(
785 UserMode
,/*ExGetPreviousMode(),*/
786 GENERIC_READ
, /* FIXME: is this ok? */
790 if (!NT_SUCCESS(Status
) && Status
!= STATUS_OBJECT_TYPE_MISMATCH
)
792 DPRINT("Failed: 0x%x\n", Status
);
793 SetLastNtError(Status
);
797 if (Status
== STATUS_OBJECT_TYPE_MISMATCH
)
800 DPRINT("Trying to open desktop 0x%x\n", hObject
);
801 Status
= IntValidateDesktopHandle(
803 UserMode
,/*ExGetPreviousMode(),*/
804 GENERIC_READ
, /* FIXME: is this ok? */
806 if (!NT_SUCCESS(Status
))
808 DPRINT("Failed: 0x%x\n", Status
);
809 SetLastNtError(Status
);
813 DPRINT("WinSta or Desktop opened!!\n");
819 Status
= STATUS_NOT_IMPLEMENTED
;
820 DPRINT1("UOI_FLAGS unimplemented!\n");
824 if (WinStaObject
!= NULL
)
826 pvData
= ((PUNICODE_STRING
)GET_DESKTOP_NAME(WinStaObject
))->Buffer
;
827 nDataSize
= ((PUNICODE_STRING
)GET_DESKTOP_NAME(WinStaObject
))->Length
+ 2;
828 Status
= STATUS_SUCCESS
;
830 else if (DesktopObject
!= NULL
)
832 pvData
= ((PUNICODE_STRING
)GET_DESKTOP_NAME(DesktopObject
))->Buffer
;
833 nDataSize
= ((PUNICODE_STRING
)GET_DESKTOP_NAME(DesktopObject
))->Length
+ 2;
834 Status
= STATUS_SUCCESS
;
837 Status
= STATUS_INVALID_PARAMETER
;
841 if (WinStaObject
!= NULL
)
843 pvData
= L
"WindowStation";
844 nDataSize
= (wcslen(pvData
) + 1) * sizeof(WCHAR
);
845 Status
= STATUS_SUCCESS
;
847 else if (DesktopObject
!= NULL
)
850 nDataSize
= (wcslen(pvData
) + 1) * sizeof(WCHAR
);
851 Status
= STATUS_SUCCESS
;
854 Status
= STATUS_INVALID_PARAMETER
;
858 Status
= STATUS_NOT_IMPLEMENTED
;
859 DPRINT1("UOI_USER_SID unimplemented!\n");
863 Status
= STATUS_INVALID_PARAMETER
;
867 /* try to copy data to caller */
868 if (Status
== STATUS_SUCCESS
)
870 DPRINT("Trying to copy data to caller (len = %d, len needed = %d)\n", nLength
, nDataSize
);
871 *nLengthNeeded
= nDataSize
;
872 if (nLength
>= nDataSize
)
873 Status
= MmCopyToCaller(pvInformation
, pvData
, nDataSize
);
875 Status
= STATUS_BUFFER_TOO_SMALL
;
878 /* release objects */
879 if (WinStaObject
!= NULL
)
880 ObDereferenceObject(WinStaObject
);
881 if (DesktopObject
!= NULL
)
882 ObDereferenceObject(DesktopObject
);
884 SetLastNtError(Status
);
885 return NT_SUCCESS(Status
);
889 * NtUserSetObjectInformation
891 * The NtUserSetObjectInformation function sets information about a
892 * window station or desktop object.
896 * Handle to the window station or desktop object for which to set
897 * object information. This value can be a handle of type HDESK or
901 * Specifies the object information to be set.
904 * Pointer to a buffer containing the object information.
907 * Specifies the size, in bytes, of the information contained in the
908 * buffer pointed to by pvInfo.
911 * If the function succeeds, the return value is nonzero. If the function
912 * fails the return value is zero.
920 NtUserSetObjectInformation(
926 /* FIXME: ZwQueryObject */
927 /* FIXME: ZwSetInformationObject */
928 SetLastNtError(STATUS_UNSUCCESSFUL
);
936 UserGetProcessWindowStation(VOID
)
942 if(PsGetCurrentProcess() != CsrProcess
)
944 return PsGetCurrentProcess()->Win32WindowStation
;
948 DPRINT1("Should use ObFindHandleForObject\n");
949 pti
= PsGetCurrentThreadWin32Thread();
950 Status
= ObOpenObjectByPointer(pti
->Desktop
->WindowStation
,
954 ExWindowStationObjectType
,
957 if (! NT_SUCCESS(Status
))
959 SetLastNtError(Status
);
960 DPRINT1("Unable to open handle for CSRSSs winsta, status 0x%08x\n",
970 * NtUserGetProcessWindowStation
972 * Returns a handle to the current process window station.
975 * If the function succeeds, the return value is handle to the window
976 * station assigned to the current process. If the function fails, the
977 * return value is NULL.
984 NtUserGetProcessWindowStation(VOID
)
986 return UserGetProcessWindowStation();
989 PWINSTATION_OBJECT FASTCALL
990 IntGetWinStaObj(VOID
)
992 PWINSTATION_OBJECT WinStaObj
;
993 PTHREADINFO Win32Thread
;
994 PEPROCESS CurrentProcess
;
997 * just a temporary hack, this will be gone soon
1000 Win32Thread
= PsGetCurrentThreadWin32Thread();
1001 if(Win32Thread
!= NULL
&& Win32Thread
->Desktop
!= NULL
)
1003 WinStaObj
= Win32Thread
->Desktop
->WindowStation
;
1004 ObReferenceObjectByPointer(WinStaObj
, KernelMode
, ExWindowStationObjectType
, 0);
1006 else if((CurrentProcess
= PsGetCurrentProcess()) != CsrProcess
)
1008 NTSTATUS Status
= IntValidateWindowStationHandle(CurrentProcess
->Win32WindowStation
,
1012 if(!NT_SUCCESS(Status
))
1014 SetLastNtError(Status
);
1027 * NtUserSetProcessWindowStation
1029 * Assigns a window station to the current process.
1033 * Handle to the window station.
1043 NtUserSetProcessWindowStation(HWINSTA hWindowStation
)
1046 PWINSTATION_OBJECT NewWinSta
;
1049 DPRINT("About to set process window station with handle (0x%X)\n",
1052 if(PsGetCurrentProcess() == CsrProcess
)
1054 DPRINT1("CSRSS is not allowed to change it's window station!!!\n");
1055 SetLastWin32Error(ERROR_ACCESS_DENIED
);
1059 Status
= IntValidateWindowStationHandle(
1065 if (!NT_SUCCESS(Status
))
1067 DPRINT("Validation of window station handle (0x%X) failed\n",
1069 SetLastNtError(Status
);
1074 * FIXME - don't allow changing the window station if there are threads that are attached to desktops and own gui objects
1077 /* FIXME - dereference the old window station, etc... */
1078 hOld
= InterlockedExchangePointer(&PsGetCurrentProcess()->Win32WindowStation
, hWindowStation
);
1080 DPRINT("PsGetCurrentProcess()->Win32WindowStation 0x%X\n",
1081 PsGetCurrentProcess()->Win32WindowStation
);
1087 * NtUserLockWindowStation
1089 * Locks switching desktops. Only the logon application is allowed to call this function.
1096 NtUserLockWindowStation(HWINSTA hWindowStation
)
1098 PWINSTATION_OBJECT Object
;
1101 DPRINT("About to set process window station with handle (0x%X)\n",
1104 if(PsGetCurrentProcessWin32Process() != LogonProcess
)
1106 DPRINT1("Unauthorized process attempted to lock the window station!\n");
1107 SetLastWin32Error(ERROR_ACCESS_DENIED
);
1111 Status
= IntValidateWindowStationHandle(
1116 if (!NT_SUCCESS(Status
))
1118 DPRINT("Validation of window station handle (0x%X) failed\n",
1120 SetLastNtError(Status
);
1124 Object
->Flags
|= WSS_LOCKED
;
1126 ObDereferenceObject(Object
);
1131 * NtUserUnlockWindowStation
1133 * Unlocks switching desktops. Only the logon application is allowed to call this function.
1140 NtUserUnlockWindowStation(HWINSTA hWindowStation
)
1142 PWINSTATION_OBJECT Object
;
1146 DPRINT("About to set process window station with handle (0x%X)\n",
1149 if(PsGetCurrentProcessWin32Process() != LogonProcess
)
1151 DPRINT1("Unauthorized process attempted to unlock the window station!\n");
1152 SetLastWin32Error(ERROR_ACCESS_DENIED
);
1156 Status
= IntValidateWindowStationHandle(
1161 if (!NT_SUCCESS(Status
))
1163 DPRINT("Validation of window station handle (0x%X) failed\n",
1165 SetLastNtError(Status
);
1169 Ret
= (Object
->Flags
& WSS_LOCKED
) == WSS_LOCKED
;
1170 Object
->Flags
&= ~WSS_LOCKED
;
1172 ObDereferenceObject(Object
);
1177 * NtUserSetWindowStationUser
1184 NtUserSetWindowStationUser(
1195 static NTSTATUS FASTCALL
1196 BuildWindowStationNameList(
1199 PULONG pRequiredSize
)
1201 OBJECT_ATTRIBUTES ObjectAttributes
;
1203 HANDLE DirectoryHandle
;
1204 UNICODE_STRING DirectoryName
;
1205 char InitialBuffer
[256], *Buffer
;
1206 ULONG Context
, ReturnLength
, BufferSize
;
1208 POBJECT_DIRECTORY_INFORMATION DirEntry
;
1212 * Generate name of window station directory
1214 if (!IntGetFullWindowStationName(&DirectoryName
, NULL
, NULL
))
1216 return STATUS_INSUFFICIENT_RESOURCES
;
1220 * Try to open the directory.
1222 InitializeObjectAttributes(
1225 OBJ_CASE_INSENSITIVE
,
1229 Status
= ZwOpenDirectoryObject(
1234 ExFreePool(DirectoryName
.Buffer
);
1236 if (!NT_SUCCESS(Status
))
1241 /* First try to query the directory using a fixed-size buffer */
1244 Status
= ZwQueryDirectoryObject(DirectoryHandle
, InitialBuffer
, sizeof(InitialBuffer
),
1245 FALSE
, TRUE
, &Context
, &ReturnLength
);
1246 if (NT_SUCCESS(Status
))
1248 if (STATUS_NO_MORE_ENTRIES
== ZwQueryDirectoryObject(DirectoryHandle
, NULL
, 0, FALSE
,
1249 FALSE
, &Context
, NULL
))
1251 /* Our fixed-size buffer is large enough */
1252 Buffer
= InitialBuffer
;
1258 /* Need a larger buffer, check how large exactly */
1259 Status
= ZwQueryDirectoryObject(DirectoryHandle
, NULL
, 0, FALSE
, TRUE
, &Context
,
1261 if (STATUS_BUFFER_TOO_SMALL
== Status
)
1263 BufferSize
= ReturnLength
;
1264 Buffer
= ExAllocatePoolWithTag(PagedPool
, BufferSize
, TAG_WINSTA
);
1267 ObDereferenceObject(DirectoryHandle
);
1268 return STATUS_NO_MEMORY
;
1271 /* We should have a sufficiently large buffer now */
1273 Status
= ZwQueryDirectoryObject(DirectoryHandle
, Buffer
, BufferSize
,
1274 FALSE
, TRUE
, &Context
, &ReturnLength
);
1275 if (! NT_SUCCESS(Status
) ||
1276 STATUS_NO_MORE_ENTRIES
!= ZwQueryDirectoryObject(DirectoryHandle
, NULL
, 0, FALSE
,
1277 FALSE
, &Context
, NULL
))
1279 /* Something went wrong, maybe someone added a directory entry? Just give up. */
1280 ExFreePoolWithTag(Buffer
, TAG_WINSTA
);
1281 ObDereferenceObject(DirectoryHandle
);
1282 return NT_SUCCESS(Status
) ? STATUS_INTERNAL_ERROR
: Status
;
1287 ZwClose(DirectoryHandle
);
1290 * Count the required size of buffer.
1292 ReturnLength
= sizeof(DWORD
);
1294 for (DirEntry
= (POBJECT_DIRECTORY_INFORMATION
) Buffer
; 0 != DirEntry
->Name
.Length
;
1297 ReturnLength
+= DirEntry
->Name
.Length
+ sizeof(WCHAR
);
1300 DPRINT("Required size: %d Entry count: %d\n", ReturnLength
, EntryCount
);
1301 if (NULL
!= pRequiredSize
)
1303 Status
= MmCopyToCaller(pRequiredSize
, &ReturnLength
, sizeof(ULONG
));
1304 if (! NT_SUCCESS(Status
))
1306 if (Buffer
!= InitialBuffer
)
1308 ExFreePoolWithTag(Buffer
, TAG_WINSTA
);
1310 return STATUS_BUFFER_TOO_SMALL
;
1315 * Check if the supplied buffer is large enough.
1317 if (dwSize
< ReturnLength
)
1319 if (Buffer
!= InitialBuffer
)
1321 ExFreePoolWithTag(Buffer
, TAG_WINSTA
);
1323 return STATUS_BUFFER_TOO_SMALL
;
1327 * Generate the resulting buffer contents.
1329 Status
= MmCopyToCaller(lpBuffer
, &EntryCount
, sizeof(DWORD
));
1330 if (! NT_SUCCESS(Status
))
1332 if (Buffer
!= InitialBuffer
)
1338 lpBuffer
= (PVOID
) ((PCHAR
) lpBuffer
+ sizeof(DWORD
));
1341 for (DirEntry
= (POBJECT_DIRECTORY_INFORMATION
) Buffer
; 0 != DirEntry
->Name
.Length
;
1344 Status
= MmCopyToCaller(lpBuffer
, DirEntry
->Name
.Buffer
, DirEntry
->Name
.Length
);
1345 if (! NT_SUCCESS(Status
))
1347 if (Buffer
!= InitialBuffer
)
1353 lpBuffer
= (PVOID
) ((PCHAR
) lpBuffer
+ DirEntry
->Name
.Length
);
1354 Status
= MmCopyToCaller(lpBuffer
, &NullWchar
, sizeof(WCHAR
));
1355 if (! NT_SUCCESS(Status
))
1357 if (Buffer
!= InitialBuffer
)
1363 lpBuffer
= (PVOID
) ((PCHAR
) lpBuffer
+ sizeof(WCHAR
));
1369 if (NULL
!= Buffer
&& Buffer
!= InitialBuffer
)
1374 return STATUS_SUCCESS
;
1377 static NTSTATUS FASTCALL
1378 BuildDesktopNameList(
1379 HWINSTA hWindowStation
,
1382 PULONG pRequiredSize
)
1385 PWINSTATION_OBJECT WindowStation
;
1387 PLIST_ENTRY DesktopEntry
;
1388 PDESKTOP DesktopObject
;
1393 Status
= IntValidateWindowStationHandle(hWindowStation
,
1397 if (! NT_SUCCESS(Status
))
1402 KeAcquireSpinLock(&WindowStation
->Lock
, &OldLevel
);
1405 * Count the required size of buffer.
1407 ReturnLength
= sizeof(DWORD
);
1409 for (DesktopEntry
= WindowStation
->DesktopListHead
.Flink
;
1410 DesktopEntry
!= &WindowStation
->DesktopListHead
;
1411 DesktopEntry
= DesktopEntry
->Flink
)
1413 DesktopObject
= CONTAINING_RECORD(DesktopEntry
, DESKTOP
, ListEntry
);
1414 ReturnLength
+= ((PUNICODE_STRING
)GET_DESKTOP_NAME(DesktopObject
))->Length
+ sizeof(WCHAR
);
1417 DPRINT("Required size: %d Entry count: %d\n", ReturnLength
, EntryCount
);
1418 if (NULL
!= pRequiredSize
)
1420 Status
= MmCopyToCaller(pRequiredSize
, &ReturnLength
, sizeof(ULONG
));
1421 if (! NT_SUCCESS(Status
))
1423 KeReleaseSpinLock(&WindowStation
->Lock
, OldLevel
);
1424 ObDereferenceObject(WindowStation
);
1425 return STATUS_BUFFER_TOO_SMALL
;
1430 * Check if the supplied buffer is large enough.
1432 if (dwSize
< ReturnLength
)
1434 KeReleaseSpinLock(&WindowStation
->Lock
, OldLevel
);
1435 ObDereferenceObject(WindowStation
);
1436 return STATUS_BUFFER_TOO_SMALL
;
1440 * Generate the resulting buffer contents.
1442 Status
= MmCopyToCaller(lpBuffer
, &EntryCount
, sizeof(DWORD
));
1443 if (! NT_SUCCESS(Status
))
1445 KeReleaseSpinLock(&WindowStation
->Lock
, OldLevel
);
1446 ObDereferenceObject(WindowStation
);
1449 lpBuffer
= (PVOID
) ((PCHAR
) lpBuffer
+ sizeof(DWORD
));
1452 for (DesktopEntry
= WindowStation
->DesktopListHead
.Flink
;
1453 DesktopEntry
!= &WindowStation
->DesktopListHead
;
1454 DesktopEntry
= DesktopEntry
->Flink
)
1456 DesktopObject
= CONTAINING_RECORD(DesktopEntry
, DESKTOP
, ListEntry
);
1457 Status
= MmCopyToCaller(lpBuffer
, ((PUNICODE_STRING
)GET_DESKTOP_NAME(DesktopObject
))->Buffer
, ((PUNICODE_STRING
)GET_DESKTOP_NAME(DesktopObject
))->Length
);
1458 if (! NT_SUCCESS(Status
))
1460 KeReleaseSpinLock(&WindowStation
->Lock
, OldLevel
);
1461 ObDereferenceObject(WindowStation
);
1464 lpBuffer
= (PVOID
) ((PCHAR
) lpBuffer
+ ((PUNICODE_STRING
)GET_DESKTOP_NAME(DesktopObject
))->Length
);
1465 Status
= MmCopyToCaller(lpBuffer
, &NullWchar
, sizeof(WCHAR
));
1466 if (! NT_SUCCESS(Status
))
1468 KeReleaseSpinLock(&WindowStation
->Lock
, OldLevel
);
1469 ObDereferenceObject(WindowStation
);
1472 lpBuffer
= (PVOID
) ((PCHAR
) lpBuffer
+ sizeof(WCHAR
));
1478 KeReleaseSpinLock(&WindowStation
->Lock
, OldLevel
);
1479 ObDereferenceObject(WindowStation
);
1481 return STATUS_SUCCESS
;
1485 * NtUserBuildNameList
1487 * Function used for enumeration of desktops or window stations.
1491 * For enumeration of window stations this parameter must be set to
1492 * zero. Otherwise it's handle for window station.
1495 * Size of buffer passed by caller.
1498 * Buffer passed by caller. If the function succedes, the buffer is
1499 * filled with window station/desktop count (in first DWORD) and
1500 * NULL-terminated window station/desktop names.
1503 * If the function suceedes, this is the number of bytes copied.
1504 * Otherwise it's size of buffer needed for function to succeed.
1511 NtUserBuildNameList(
1512 HWINSTA hWindowStation
,
1515 PULONG pRequiredSize
)
1517 /* The WindowStation name list and desktop name list are build in completely
1518 different ways. Call the appropriate function */
1519 return NULL
== hWindowStation
? BuildWindowStationNameList(dwSize
, lpBuffer
, pRequiredSize
) :
1520 BuildDesktopNameList(hWindowStation
, dwSize
, lpBuffer
, pRequiredSize
);