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 along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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
= ExAllocatePoolWithTag(PagedPool
, sizeof(CLIPBOARDSYSTEM
), TAG_WINSTA
);
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 UNICODE_STRING WindowStationName
;
399 UNICODE_STRING FullWindowStationName
;
400 PWINSTATION_OBJECT WindowStationObject
;
401 HWINSTA WindowStation
;
402 OBJECT_ATTRIBUTES ObjectAttributes
;
406 * Generate full window station name
408 Status
= ProbeAndCaptureUnicodeString(&WindowStationName
,
410 lpszWindowStationName
);
411 if (!NT_SUCCESS(Status
))
413 DPRINT1("Failed to capture window station name (status 0x%08x)\n",
415 SetLastNtError(Status
);
418 if (!IntGetFullWindowStationName(&FullWindowStationName
,
422 ReleaseCapturedUnicodeString(&WindowStationName
, UserMode
);
423 SetLastNtError(STATUS_INSUFFICIENT_RESOURCES
);
428 * Try to open already existing window station
431 DPRINT("Trying to open window station (%wZ)\n", &FullWindowStationName
);
433 /* Initialize ObjectAttributes for the window station object */
434 InitializeObjectAttributes(
436 &FullWindowStationName
,
441 Status
= ObOpenObjectByName(
443 ExWindowStationObjectType
,
448 (PVOID
*)&WindowStation
);
450 if (NT_SUCCESS(Status
))
452 DPRINT("Successfully opened window station (%wZ)\n",
453 FullWindowStationName
);
454 ExFreePool(FullWindowStationName
.Buffer
);
455 ReleaseCapturedUnicodeString(&WindowStationName
, UserMode
);
456 return (HWINSTA
)WindowStation
;
460 * No existing window station found, try to create new one
463 DPRINT("Creating window station (%wZ)\n", &FullWindowStationName
);
465 Status
= ObCreateObject(
467 ExWindowStationObjectType
,
471 sizeof(WINSTATION_OBJECT
),
474 (PVOID
*)&WindowStationObject
);
476 if (!NT_SUCCESS(Status
))
478 DPRINT1("Failed creating window station (%wZ)\n", &FullWindowStationName
);
479 ExFreePool(FullWindowStationName
.Buffer
);
480 ReleaseCapturedUnicodeString(&WindowStationName
, UserMode
);
481 SetLastNtError(STATUS_INSUFFICIENT_RESOURCES
);
485 /* Zero out the buffer */
486 RtlZeroMemory(WindowStationObject
, sizeof(WINSTATION_OBJECT
));
488 KeInitializeSpinLock(&WindowStationObject
->Lock
);
490 InitializeListHead(&WindowStationObject
->DesktopListHead
);
492 WindowStationObject
->AtomTable
= NULL
;
493 Status
= RtlCreateAtomTable(37, &WindowStationObject
->AtomTable
);
494 WindowStationObject
->SystemMenuTemplate
= (HANDLE
)0;
496 WindowStationObject
->Name
= WindowStationName
;
498 Status
= ObInsertObject(
499 (PVOID
)WindowStationObject
,
501 STANDARD_RIGHTS_REQUIRED
,
504 (PVOID
*)&WindowStation
);
506 if (!NT_SUCCESS(Status
))
508 DPRINT1("Failed creating window station (%wZ)\n", &FullWindowStationName
);
509 ExFreePool(FullWindowStationName
.Buffer
);
510 ExFreePool(WindowStationName
.Buffer
);
511 SetLastNtError(STATUS_INSUFFICIENT_RESOURCES
);
512 ObDereferenceObject(WindowStationObject
);
517 * Initialize the new window station object
520 WindowStationObject
->ScreenSaverRunning
= FALSE
;
522 WindowStationObject
->FlatMenu
= FALSE
;
524 if (!IntSetupClipboard(WindowStationObject
))
526 DPRINT1("WindowStation: Error Setting up the clipboard!!!\n");
529 if (InputWindowStation
== NULL
)
531 InputWindowStation
= WindowStationObject
;
536 DPRINT("Window station successfully created (%wZ)\n", &FullWindowStationName
);
537 ExFreePool(FullWindowStationName
.Buffer
);
538 return WindowStation
;
542 * NtUserOpenWindowStation
544 * Opens an existing window station.
547 * lpszWindowStationName
548 * Name of the existing window station.
551 * Requested type of access.
554 * If the function succeeds, the return value is the handle to the
555 * specified window station. If the function fails, the return value
559 * The returned handle can be closed with NtUserCloseWindowStation.
566 NtUserOpenWindowStation(
567 PUNICODE_STRING lpszWindowStationName
,
568 ACCESS_MASK dwDesiredAccess
)
570 UNICODE_STRING WindowStationName
;
571 HWINSTA WindowStation
;
572 OBJECT_ATTRIBUTES ObjectAttributes
;
576 * Generate full window station name
579 if (!IntGetFullWindowStationName(&WindowStationName
, lpszWindowStationName
,
582 SetLastNtError(STATUS_INSUFFICIENT_RESOURCES
);
586 DPRINT("Trying to open window station (%wZ)\n", &WindowStationName
);
588 /* Initialize ObjectAttributes for the window station object */
589 InitializeObjectAttributes(
592 OBJ_CASE_INSENSITIVE
,
596 Status
= ObOpenObjectByName(
598 ExWindowStationObjectType
,
603 (PVOID
*)&WindowStation
);
605 if (!NT_SUCCESS(Status
))
607 SetLastNtError(Status
);
608 ExFreePool(WindowStationName
.Buffer
);
612 DPRINT("Successfully opened window station (%wZ)\n", &WindowStationName
);
613 ExFreePool(WindowStationName
.Buffer
);
615 return WindowStation
;
619 * NtUserCloseWindowStation
621 * Closes a window station handle.
625 * Handle to the window station.
631 * The window station handle can be created with NtUserCreateWindowStation
632 * or NtUserOpenWindowStation. Attemps to close a handle to the window
633 * station assigned to the calling process will fail.
641 NtUserCloseWindowStation(
644 PWINSTATION_OBJECT Object
;
647 DPRINT("About to close window station handle (0x%X)\n", hWinSta
);
649 if (hWinSta
== UserGetProcessWindowStation())
654 Status
= IntValidateWindowStationHandle(
660 if (!NT_SUCCESS(Status
))
662 DPRINT("Validation of window station handle (0x%X) failed\n", hWinSta
);
666 ObDereferenceObject(Object
);
668 DPRINT("Closing window station handle (0x%X)\n", hWinSta
);
670 Status
= ZwClose(hWinSta
);
671 if (!NT_SUCCESS(Status
))
673 SetLastNtError(Status
);
681 * NtUserGetObjectInformation
683 * The NtUserGetObjectInformation function retrieves information about a
684 * window station or desktop object.
688 * Handle to the window station or desktop object for which to
689 * return information. This can be a handle of type HDESK or HWINSTA
690 * (for example, a handle returned by NtUserCreateWindowStation,
691 * NtUserOpenWindowStation, NtUserCreateDesktop, or NtUserOpenDesktop).
694 * Specifies the object information to be retrieved.
697 * Pointer to a buffer to receive the object information.
700 * Specifies the size, in bytes, of the buffer pointed to by the
704 * Pointer to a variable receiving the number of bytes required to
705 * store the requested information. If this variable's value is
706 * greater than the value of the nLength parameter when the function
707 * returns, the function returns FALSE, and none of the information
708 * is copied to the pvInfo buffer. If the value of the variable pointed
709 * to by lpnLengthNeeded is less than or equal to the value of nLength,
710 * the entire information block is copied.
713 * If the function succeeds, the return value is nonzero. If the function
714 * fails, the return value is zero.
721 NtUserGetObjectInformation(
726 PDWORD nLengthNeeded
)
728 PWINSTATION_OBJECT WinStaObject
= NULL
;
729 PDESKTOP DesktopObject
= NULL
;
734 /* try windowstation */
735 DPRINT("Trying to open window station 0x%x\n", hObject
);
736 Status
= IntValidateWindowStationHandle(
738 UserMode
,/*ExGetPreviousMode(),*/
739 GENERIC_READ
, /* FIXME: is this ok? */
743 if (!NT_SUCCESS(Status
) && Status
!= STATUS_OBJECT_TYPE_MISMATCH
)
745 DPRINT("Failed: 0x%x\n", Status
);
746 SetLastNtError(Status
);
750 if (Status
== STATUS_OBJECT_TYPE_MISMATCH
)
753 DPRINT("Trying to open desktop 0x%x\n", hObject
);
754 Status
= IntValidateDesktopHandle(
756 UserMode
,/*ExGetPreviousMode(),*/
757 GENERIC_READ
, /* FIXME: is this ok? */
759 if (!NT_SUCCESS(Status
))
761 DPRINT("Failed: 0x%x\n", Status
);
762 SetLastNtError(Status
);
766 DPRINT("WinSta or Desktop opened!!\n");
772 Status
= STATUS_NOT_IMPLEMENTED
;
773 DPRINT1("UOI_FLAGS unimplemented!\n");
777 if (WinStaObject
!= NULL
)
779 pvData
= ((PUNICODE_STRING
)GET_DESKTOP_NAME(WinStaObject
))->Buffer
;
780 nDataSize
= ((PUNICODE_STRING
)GET_DESKTOP_NAME(WinStaObject
))->Length
+ 2;
781 Status
= STATUS_SUCCESS
;
783 else if (DesktopObject
!= NULL
)
785 pvData
= ((PUNICODE_STRING
)GET_DESKTOP_NAME(DesktopObject
))->Buffer
;
786 nDataSize
= ((PUNICODE_STRING
)GET_DESKTOP_NAME(DesktopObject
))->Length
+ 2;
787 Status
= STATUS_SUCCESS
;
790 Status
= STATUS_INVALID_PARAMETER
;
794 if (WinStaObject
!= NULL
)
796 pvData
= L
"WindowStation";
797 nDataSize
= (wcslen(pvData
) + 1) * sizeof(WCHAR
);
798 Status
= STATUS_SUCCESS
;
800 else if (DesktopObject
!= NULL
)
803 nDataSize
= (wcslen(pvData
) + 1) * sizeof(WCHAR
);
804 Status
= STATUS_SUCCESS
;
807 Status
= STATUS_INVALID_PARAMETER
;
811 Status
= STATUS_NOT_IMPLEMENTED
;
812 DPRINT1("UOI_USER_SID unimplemented!\n");
816 Status
= STATUS_INVALID_PARAMETER
;
820 /* try to copy data to caller */
821 if (Status
== STATUS_SUCCESS
)
823 DPRINT("Trying to copy data to caller (len = %d, len needed = %d)\n", nLength
, nDataSize
);
824 *nLengthNeeded
= nDataSize
;
825 if (nLength
>= nDataSize
)
826 Status
= MmCopyToCaller(pvInformation
, pvData
, nDataSize
);
828 Status
= STATUS_BUFFER_TOO_SMALL
;
831 /* release objects */
832 if (WinStaObject
!= NULL
)
833 ObDereferenceObject(WinStaObject
);
834 if (DesktopObject
!= NULL
)
835 ObDereferenceObject(DesktopObject
);
837 SetLastNtError(Status
);
838 return NT_SUCCESS(Status
);
842 * NtUserSetObjectInformation
844 * The NtUserSetObjectInformation function sets information about a
845 * window station or desktop object.
849 * Handle to the window station or desktop object for which to set
850 * object information. This value can be a handle of type HDESK or
854 * Specifies the object information to be set.
857 * Pointer to a buffer containing the object information.
860 * Specifies the size, in bytes, of the information contained in the
861 * buffer pointed to by pvInfo.
864 * If the function succeeds, the return value is nonzero. If the function
865 * fails the return value is zero.
873 NtUserSetObjectInformation(
879 /* FIXME: ZwQueryObject */
880 /* FIXME: ZwSetInformationObject */
881 SetLastNtError(STATUS_UNSUCCESSFUL
);
889 UserGetProcessWindowStation(VOID
)
895 if(PsGetCurrentProcess() != CsrProcess
)
897 return PsGetCurrentProcess()->Win32WindowStation
;
901 DPRINT1("Should use ObFindHandleForObject\n");
902 pti
= PsGetCurrentThreadWin32Thread();
903 Status
= ObOpenObjectByPointer(pti
->rpdesk
->rpwinstaParent
,
907 ExWindowStationObjectType
,
910 if (! NT_SUCCESS(Status
))
912 SetLastNtError(Status
);
913 DPRINT1("Unable to open handle for CSRSSs winsta, status 0x%08x\n",
923 * NtUserGetProcessWindowStation
925 * Returns a handle to the current process window station.
928 * If the function succeeds, the return value is handle to the window
929 * station assigned to the current process. If the function fails, the
930 * return value is NULL.
937 NtUserGetProcessWindowStation(VOID
)
939 return UserGetProcessWindowStation();
942 PWINSTATION_OBJECT FASTCALL
943 IntGetWinStaObj(VOID
)
945 PWINSTATION_OBJECT WinStaObj
;
946 PTHREADINFO Win32Thread
;
947 PEPROCESS CurrentProcess
;
950 * just a temporary hack, this will be gone soon
953 Win32Thread
= PsGetCurrentThreadWin32Thread();
954 if(Win32Thread
!= NULL
&& Win32Thread
->rpdesk
!= NULL
)
956 WinStaObj
= Win32Thread
->rpdesk
->rpwinstaParent
;
957 ObReferenceObjectByPointer(WinStaObj
, KernelMode
, ExWindowStationObjectType
, 0);
959 else if((CurrentProcess
= PsGetCurrentProcess()) != CsrProcess
)
961 NTSTATUS Status
= IntValidateWindowStationHandle(CurrentProcess
->Win32WindowStation
,
965 if(!NT_SUCCESS(Status
))
967 SetLastNtError(Status
);
980 * NtUserSetProcessWindowStation
982 * Assigns a window station to the current process.
986 * Handle to the window station.
996 NtUserSetProcessWindowStation(HWINSTA hWindowStation
)
998 PWINSTATION_OBJECT NewWinSta
;
1001 DPRINT("About to set process window station with handle (0x%X)\n",
1004 if(PsGetCurrentProcess() == CsrProcess
)
1006 DPRINT1("CSRSS is not allowed to change it's window station!!!\n");
1007 SetLastWin32Error(ERROR_ACCESS_DENIED
);
1011 Status
= IntValidateWindowStationHandle(
1017 if (!NT_SUCCESS(Status
))
1019 DPRINT("Validation of window station handle (0x%X) failed\n",
1021 SetLastNtError(Status
);
1026 * FIXME - don't allow changing the window station if there are threads that are attached to desktops and own gui objects
1029 /* FIXME - dereference the old window station, etc... */
1030 InterlockedExchangePointer(&PsGetCurrentProcess()->Win32WindowStation
, hWindowStation
);
1032 DPRINT("PsGetCurrentProcess()->Win32WindowStation 0x%X\n",
1033 PsGetCurrentProcess()->Win32WindowStation
);
1039 * NtUserLockWindowStation
1041 * Locks switching desktops. Only the logon application is allowed to call this function.
1048 NtUserLockWindowStation(HWINSTA hWindowStation
)
1050 PWINSTATION_OBJECT Object
;
1053 DPRINT("About to set process window station with handle (0x%X)\n",
1056 if(PsGetCurrentProcessWin32Process() != LogonProcess
)
1058 DPRINT1("Unauthorized process attempted to lock the window station!\n");
1059 SetLastWin32Error(ERROR_ACCESS_DENIED
);
1063 Status
= IntValidateWindowStationHandle(
1068 if (!NT_SUCCESS(Status
))
1070 DPRINT("Validation of window station handle (0x%X) failed\n",
1072 SetLastNtError(Status
);
1076 Object
->Flags
|= WSS_LOCKED
;
1078 ObDereferenceObject(Object
);
1083 * NtUserUnlockWindowStation
1085 * Unlocks switching desktops. Only the logon application is allowed to call this function.
1092 NtUserUnlockWindowStation(HWINSTA hWindowStation
)
1094 PWINSTATION_OBJECT Object
;
1098 DPRINT("About to set process window station with handle (0x%X)\n",
1101 if(PsGetCurrentProcessWin32Process() != LogonProcess
)
1103 DPRINT1("Unauthorized process attempted to unlock the window station!\n");
1104 SetLastWin32Error(ERROR_ACCESS_DENIED
);
1108 Status
= IntValidateWindowStationHandle(
1113 if (!NT_SUCCESS(Status
))
1115 DPRINT("Validation of window station handle (0x%X) failed\n",
1117 SetLastNtError(Status
);
1121 Ret
= (Object
->Flags
& WSS_LOCKED
) == WSS_LOCKED
;
1122 Object
->Flags
&= ~WSS_LOCKED
;
1124 ObDereferenceObject(Object
);
1129 * NtUserSetWindowStationUser
1136 NtUserSetWindowStationUser(
1147 static NTSTATUS FASTCALL
1148 BuildWindowStationNameList(
1151 PULONG pRequiredSize
)
1153 OBJECT_ATTRIBUTES ObjectAttributes
;
1155 HANDLE DirectoryHandle
;
1156 UNICODE_STRING DirectoryName
;
1157 char InitialBuffer
[256], *Buffer
;
1158 ULONG Context
, ReturnLength
, BufferSize
;
1160 POBJECT_DIRECTORY_INFORMATION DirEntry
;
1164 * Generate name of window station directory
1166 if (!IntGetFullWindowStationName(&DirectoryName
, NULL
, NULL
))
1168 return STATUS_INSUFFICIENT_RESOURCES
;
1172 * Try to open the directory.
1174 InitializeObjectAttributes(
1177 OBJ_CASE_INSENSITIVE
,
1181 Status
= ZwOpenDirectoryObject(
1186 ExFreePool(DirectoryName
.Buffer
);
1188 if (!NT_SUCCESS(Status
))
1193 /* First try to query the directory using a fixed-size buffer */
1196 Status
= ZwQueryDirectoryObject(DirectoryHandle
, InitialBuffer
, sizeof(InitialBuffer
),
1197 FALSE
, TRUE
, &Context
, &ReturnLength
);
1198 if (NT_SUCCESS(Status
))
1200 if (STATUS_NO_MORE_ENTRIES
== ZwQueryDirectoryObject(DirectoryHandle
, NULL
, 0, FALSE
,
1201 FALSE
, &Context
, NULL
))
1203 /* Our fixed-size buffer is large enough */
1204 Buffer
= InitialBuffer
;
1210 /* Need a larger buffer, check how large exactly */
1211 Status
= ZwQueryDirectoryObject(DirectoryHandle
, NULL
, 0, FALSE
, TRUE
, &Context
,
1213 if (STATUS_BUFFER_TOO_SMALL
== Status
)
1215 BufferSize
= ReturnLength
;
1216 Buffer
= ExAllocatePoolWithTag(PagedPool
, BufferSize
, TAG_WINSTA
);
1219 ObDereferenceObject(DirectoryHandle
);
1220 return STATUS_NO_MEMORY
;
1223 /* We should have a sufficiently large buffer now */
1225 Status
= ZwQueryDirectoryObject(DirectoryHandle
, Buffer
, BufferSize
,
1226 FALSE
, TRUE
, &Context
, &ReturnLength
);
1227 if (! NT_SUCCESS(Status
) ||
1228 STATUS_NO_MORE_ENTRIES
!= ZwQueryDirectoryObject(DirectoryHandle
, NULL
, 0, FALSE
,
1229 FALSE
, &Context
, NULL
))
1231 /* Something went wrong, maybe someone added a directory entry? Just give up. */
1232 ExFreePoolWithTag(Buffer
, TAG_WINSTA
);
1233 ObDereferenceObject(DirectoryHandle
);
1234 return NT_SUCCESS(Status
) ? STATUS_INTERNAL_ERROR
: Status
;
1239 ZwClose(DirectoryHandle
);
1242 * Count the required size of buffer.
1244 ReturnLength
= sizeof(DWORD
);
1246 for (DirEntry
= (POBJECT_DIRECTORY_INFORMATION
) Buffer
; 0 != DirEntry
->Name
.Length
;
1249 ReturnLength
+= DirEntry
->Name
.Length
+ sizeof(WCHAR
);
1252 DPRINT("Required size: %d Entry count: %d\n", ReturnLength
, EntryCount
);
1253 if (NULL
!= pRequiredSize
)
1255 Status
= MmCopyToCaller(pRequiredSize
, &ReturnLength
, sizeof(ULONG
));
1256 if (! NT_SUCCESS(Status
))
1258 if (Buffer
!= InitialBuffer
)
1260 ExFreePoolWithTag(Buffer
, TAG_WINSTA
);
1262 return STATUS_BUFFER_TOO_SMALL
;
1267 * Check if the supplied buffer is large enough.
1269 if (dwSize
< ReturnLength
)
1271 if (Buffer
!= InitialBuffer
)
1273 ExFreePoolWithTag(Buffer
, TAG_WINSTA
);
1275 return STATUS_BUFFER_TOO_SMALL
;
1279 * Generate the resulting buffer contents.
1281 Status
= MmCopyToCaller(lpBuffer
, &EntryCount
, sizeof(DWORD
));
1282 if (! NT_SUCCESS(Status
))
1284 if (Buffer
!= InitialBuffer
)
1290 lpBuffer
= (PVOID
) ((PCHAR
) lpBuffer
+ sizeof(DWORD
));
1293 for (DirEntry
= (POBJECT_DIRECTORY_INFORMATION
) Buffer
; 0 != DirEntry
->Name
.Length
;
1296 Status
= MmCopyToCaller(lpBuffer
, DirEntry
->Name
.Buffer
, DirEntry
->Name
.Length
);
1297 if (! NT_SUCCESS(Status
))
1299 if (Buffer
!= InitialBuffer
)
1305 lpBuffer
= (PVOID
) ((PCHAR
) lpBuffer
+ DirEntry
->Name
.Length
);
1306 Status
= MmCopyToCaller(lpBuffer
, &NullWchar
, sizeof(WCHAR
));
1307 if (! NT_SUCCESS(Status
))
1309 if (Buffer
!= InitialBuffer
)
1315 lpBuffer
= (PVOID
) ((PCHAR
) lpBuffer
+ sizeof(WCHAR
));
1321 if (NULL
!= Buffer
&& Buffer
!= InitialBuffer
)
1326 return STATUS_SUCCESS
;
1329 static NTSTATUS FASTCALL
1330 BuildDesktopNameList(
1331 HWINSTA hWindowStation
,
1334 PULONG pRequiredSize
)
1337 PWINSTATION_OBJECT WindowStation
;
1339 PLIST_ENTRY DesktopEntry
;
1340 PDESKTOP DesktopObject
;
1345 Status
= IntValidateWindowStationHandle(hWindowStation
,
1349 if (! NT_SUCCESS(Status
))
1354 KeAcquireSpinLock(&WindowStation
->Lock
, &OldLevel
);
1357 * Count the required size of buffer.
1359 ReturnLength
= sizeof(DWORD
);
1361 for (DesktopEntry
= WindowStation
->DesktopListHead
.Flink
;
1362 DesktopEntry
!= &WindowStation
->DesktopListHead
;
1363 DesktopEntry
= DesktopEntry
->Flink
)
1365 DesktopObject
= CONTAINING_RECORD(DesktopEntry
, DESKTOP
, ListEntry
);
1366 ReturnLength
+= ((PUNICODE_STRING
)GET_DESKTOP_NAME(DesktopObject
))->Length
+ sizeof(WCHAR
);
1369 DPRINT("Required size: %d Entry count: %d\n", ReturnLength
, EntryCount
);
1370 if (NULL
!= pRequiredSize
)
1372 Status
= MmCopyToCaller(pRequiredSize
, &ReturnLength
, sizeof(ULONG
));
1373 if (! NT_SUCCESS(Status
))
1375 KeReleaseSpinLock(&WindowStation
->Lock
, OldLevel
);
1376 ObDereferenceObject(WindowStation
);
1377 return STATUS_BUFFER_TOO_SMALL
;
1382 * Check if the supplied buffer is large enough.
1384 if (dwSize
< ReturnLength
)
1386 KeReleaseSpinLock(&WindowStation
->Lock
, OldLevel
);
1387 ObDereferenceObject(WindowStation
);
1388 return STATUS_BUFFER_TOO_SMALL
;
1392 * Generate the resulting buffer contents.
1394 Status
= MmCopyToCaller(lpBuffer
, &EntryCount
, sizeof(DWORD
));
1395 if (! NT_SUCCESS(Status
))
1397 KeReleaseSpinLock(&WindowStation
->Lock
, OldLevel
);
1398 ObDereferenceObject(WindowStation
);
1401 lpBuffer
= (PVOID
) ((PCHAR
) lpBuffer
+ sizeof(DWORD
));
1404 for (DesktopEntry
= WindowStation
->DesktopListHead
.Flink
;
1405 DesktopEntry
!= &WindowStation
->DesktopListHead
;
1406 DesktopEntry
= DesktopEntry
->Flink
)
1408 DesktopObject
= CONTAINING_RECORD(DesktopEntry
, DESKTOP
, ListEntry
);
1409 Status
= MmCopyToCaller(lpBuffer
, ((PUNICODE_STRING
)GET_DESKTOP_NAME(DesktopObject
))->Buffer
, ((PUNICODE_STRING
)GET_DESKTOP_NAME(DesktopObject
))->Length
);
1410 if (! NT_SUCCESS(Status
))
1412 KeReleaseSpinLock(&WindowStation
->Lock
, OldLevel
);
1413 ObDereferenceObject(WindowStation
);
1416 lpBuffer
= (PVOID
) ((PCHAR
) lpBuffer
+ ((PUNICODE_STRING
)GET_DESKTOP_NAME(DesktopObject
))->Length
);
1417 Status
= MmCopyToCaller(lpBuffer
, &NullWchar
, sizeof(WCHAR
));
1418 if (! NT_SUCCESS(Status
))
1420 KeReleaseSpinLock(&WindowStation
->Lock
, OldLevel
);
1421 ObDereferenceObject(WindowStation
);
1424 lpBuffer
= (PVOID
) ((PCHAR
) lpBuffer
+ sizeof(WCHAR
));
1430 KeReleaseSpinLock(&WindowStation
->Lock
, OldLevel
);
1431 ObDereferenceObject(WindowStation
);
1433 return STATUS_SUCCESS
;
1437 * NtUserBuildNameList
1439 * Function used for enumeration of desktops or window stations.
1443 * For enumeration of window stations this parameter must be set to
1444 * zero. Otherwise it's handle for window station.
1447 * Size of buffer passed by caller.
1450 * Buffer passed by caller. If the function succedes, the buffer is
1451 * filled with window station/desktop count (in first DWORD) and
1452 * NULL-terminated window station/desktop names.
1455 * If the function suceedes, this is the number of bytes copied.
1456 * Otherwise it's size of buffer needed for function to succeed.
1463 NtUserBuildNameList(
1464 HWINSTA hWindowStation
,
1467 PULONG pRequiredSize
)
1469 /* The WindowStation name list and desktop name list are build in completely
1470 different ways. Call the appropriate function */
1471 return NULL
== hWindowStation
? BuildWindowStationNameList(dwSize
, lpBuffer
, pRequiredSize
) :
1472 BuildDesktopNameList(hWindowStation
, dwSize
, lpBuffer
, pRequiredSize
);