2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS kernel
5 * FILE: subsystems/win32/win32k/ntuser/desktop.c
6 * PROGRAMER: Casper S. Hornstrup (chorns@users.sourceforge.net)
8 * 06-06-2001 CSH Created
11 /* INCLUDES ******************************************************************/
24 IN OUT PDESKTOP Desktop
27 /* GLOBALS *******************************************************************/
29 /* Currently active desktop */
30 PDESKTOP InputDesktop
= NULL
;
31 HDESK InputDesktopHandle
= NULL
;
32 HDC ScreenDeviceContext
= NULL
;
33 BOOL g_PaintDesktopVersion
= FALSE
;
35 GENERIC_MAPPING IntDesktopMapping
=
37 STANDARD_RIGHTS_READ
| DESKTOP_ENUMERATE
|
39 STANDARD_RIGHTS_WRITE
| DESKTOP_CREATEMENU
|
40 DESKTOP_CREATEWINDOW
|
42 DESKTOP_JOURNALPLAYBACK
|
43 DESKTOP_JOURNALRECORD
|
45 STANDARD_RIGHTS_EXECUTE
| DESKTOP_SWITCHDESKTOP
,
46 STANDARD_RIGHTS_REQUIRED
| DESKTOP_CREATEMENU
|
47 DESKTOP_CREATEWINDOW
|
50 DESKTOP_JOURNALPLAYBACK
|
51 DESKTOP_JOURNALRECORD
|
53 DESKTOP_SWITCHDESKTOP
|
57 /* OBJECT CALLBACKS **********************************************************/
61 IntDesktopObjectParse(IN PVOID ParseObject
,
63 IN OUT PACCESS_STATE AccessState
,
64 IN KPROCESSOR_MODE AccessMode
,
66 IN OUT PUNICODE_STRING CompleteName
,
67 IN OUT PUNICODE_STRING RemainingName
,
68 IN OUT PVOID Context OPTIONAL
,
69 IN PSECURITY_QUALITY_OF_SERVICE SecurityQos OPTIONAL
,
74 OBJECT_ATTRIBUTES ObjectAttributes
;
75 PLIST_ENTRY NextEntry
, ListHead
;
76 PWINSTATION_OBJECT WinStaObject
= (PWINSTATION_OBJECT
)ParseObject
;
77 PUNICODE_STRING DesktopName
;
79 /* Set the list pointers and loop the window station */
80 ListHead
= &WinStaObject
->DesktopListHead
;
81 NextEntry
= ListHead
->Flink
;
82 while (NextEntry
!= ListHead
)
84 /* Get the current desktop */
85 Desktop
= CONTAINING_RECORD(NextEntry
, DESKTOP
, ListEntry
);
88 DesktopName
= GET_DESKTOP_NAME(Desktop
);
91 /* Compare the name */
92 if (RtlEqualUnicodeString(RemainingName
,
94 (Attributes
& OBJ_CASE_INSENSITIVE
)))
96 /* We found a match. Did this come from a create? */
99 /* Unless OPEN_IF was given, fail with an error */
100 if (!(Attributes
& OBJ_OPENIF
))
103 return STATUS_OBJECT_NAME_COLLISION
;
107 /* Otherwise, return with a warning only */
108 Status
= STATUS_OBJECT_NAME_EXISTS
;
113 /* This was a real open, so this is OK */
114 Status
= STATUS_SUCCESS
;
117 /* Reference the desktop and return it */
118 ObReferenceObject(Desktop
);
124 /* Go to the next desktop */
125 NextEntry
= NextEntry
->Flink
;
128 /* If we got here but this isn't a create, then fail */
129 if (!Context
) return STATUS_OBJECT_NAME_NOT_FOUND
;
131 /* Create the desktop object */
132 InitializeObjectAttributes(&ObjectAttributes
, RemainingName
, 0, NULL
, NULL
);
133 Status
= ObCreateObject(KernelMode
,
142 if (!NT_SUCCESS(Status
)) return Status
;
144 /* Initialize shell hook window list and set the parent */
145 RtlZeroMemory(Desktop
, sizeof(DESKTOP
));
146 InitializeListHead(&Desktop
->ShellHookWindows
);
147 Desktop
->rpwinstaParent
= (PWINSTATION_OBJECT
)ParseObject
;
149 /* Put the desktop on the window station's list of associated desktops */
150 InsertTailList(&Desktop
->rpwinstaParent
->DesktopListHead
,
151 &Desktop
->ListEntry
);
153 /* Set the desktop object and return success */
155 return STATUS_SUCCESS
;
159 IntDesktopObjectDelete(PWIN32_DELETEMETHOD_PARAMETERS Parameters
)
161 PDESKTOP Desktop
= (PDESKTOP
)Parameters
->Object
;
163 DPRINT("Deleting desktop (0x%X)\n", Desktop
);
165 /* Remove the desktop from the window station's list of associcated desktops */
166 RemoveEntryList(&Desktop
->ListEntry
);
168 IntFreeDesktopHeap(Desktop
);
171 /* PRIVATE FUNCTIONS **********************************************************/
176 InitDesktopImpl(VOID
)
178 /* Set Desktop Object Attributes */
179 ExDesktopObjectType
->TypeInfo
.DefaultNonPagedPoolCharge
= sizeof(DESKTOP
);
180 ExDesktopObjectType
->TypeInfo
.GenericMapping
= IntDesktopMapping
;
181 return STATUS_SUCCESS
;
186 CleanupDesktopImpl(VOID
)
188 return STATUS_SUCCESS
;
191 static int GetSystemVersionString(LPWSTR buffer
)
193 RTL_OSVERSIONINFOEXW versionInfo
;
196 versionInfo
.dwOSVersionInfoSize
= sizeof(RTL_OSVERSIONINFOEXW
);
198 if (!NT_SUCCESS(RtlGetVersion((PRTL_OSVERSIONINFOW
)&versionInfo
)))
201 if (versionInfo
.dwMajorVersion
<= 4)
202 len
= swprintf(buffer
,
203 L
"ReactOS Version %d.%d %s Build %d",
204 versionInfo
.dwMajorVersion
, versionInfo
.dwMinorVersion
,
205 versionInfo
.szCSDVersion
, versionInfo
.dwBuildNumber
&0xFFFF);
207 len
= swprintf(buffer
,
208 L
"ReactOS %s (Build %d)",
209 versionInfo
.szCSDVersion
, versionInfo
.dwBuildNumber
&0xFFFF);
216 IntParseDesktopPath(PEPROCESS Process
,
217 PUNICODE_STRING DesktopPath
,
221 OBJECT_ATTRIBUTES ObjectAttributes
;
222 UNICODE_STRING WinSta
, Desktop
, FullName
;
223 BOOL DesktopPresent
= FALSE
;
224 BOOL WinStaPresent
= FALSE
;
236 RtlInitUnicodeString(&WinSta
, NULL
);
237 RtlInitUnicodeString(&Desktop
, NULL
);
239 if(DesktopPath
!= NULL
&& DesktopPath
->Buffer
!= NULL
&& DesktopPath
->Length
> sizeof(WCHAR
))
241 PWCHAR c
= DesktopPath
->Buffer
;
243 USHORT l
= DesktopPath
->Length
;
246 * Parse the desktop path string which can be in the form "WinSta\Desktop"
247 * or just "Desktop". In latter case WinSta0 will be used.
254 wl
= (ULONG_PTR
)c
- (ULONG_PTR
)DesktopPath
->Buffer
;
264 WinSta
.MaximumLength
= wl
+ sizeof(WCHAR
);
265 WinSta
.Buffer
= DesktopPath
->Buffer
;
267 WinStaPresent
= TRUE
;
271 Desktop
.Length
= DesktopPath
->Length
- wl
;
274 Desktop
.Length
-= sizeof(WCHAR
);
276 if(Desktop
.Length
> 0)
278 Desktop
.MaximumLength
= Desktop
.Length
+ sizeof(WCHAR
);
279 Desktop
.Buffer
= ((wl
> 0) ? c
: DesktopPath
->Buffer
);
280 DesktopPresent
= TRUE
;
287 /* search the process handle table for (inherited) window station
288 handles, use a more appropriate one than WinSta0 if possible. */
289 if (!ObFindHandleForObject(Process
,
291 ExWindowStationObjectType
,
296 /* we had no luck searching for opened handles, use WinSta0 now */
297 RtlInitUnicodeString(&WinSta
, L
"WinSta0");
301 if(!DesktopPresent
&& hDesktop
!= NULL
)
304 /* search the process handle table for (inherited) desktop
305 handles, use a more appropriate one than Default if possible. */
306 if (!ObFindHandleForObject(Process
,
313 /* we had no luck searching for opened handles, use Desktop now */
314 RtlInitUnicodeString(&Desktop
, L
"Default");
320 if(!IntGetFullWindowStationName(&FullName
, &WinSta
, NULL
))
322 return STATUS_INSUFFICIENT_RESOURCES
;
325 /* open the window station */
326 InitializeObjectAttributes(&ObjectAttributes
,
328 OBJ_CASE_INSENSITIVE
,
332 Status
= ObOpenObjectByName(&ObjectAttributes
,
333 ExWindowStationObjectType
,
340 ExFreePoolWithTag(FullName
.Buffer
, TAG_STRING
);
342 if(!NT_SUCCESS(Status
))
344 SetLastNtError(Status
);
345 DPRINT("Failed to reference window station %wZ PID: %d!\n", &WinSta
, PsGetCurrentProcessId());
350 if(hDesktop
!= NULL
&& *hDesktop
== NULL
)
352 if(!IntGetFullWindowStationName(&FullName
, &WinSta
, &Desktop
))
356 return STATUS_INSUFFICIENT_RESOURCES
;
359 /* open the desktop object */
360 InitializeObjectAttributes(&ObjectAttributes
,
362 OBJ_CASE_INSENSITIVE
,
366 Status
= ObOpenObjectByName(&ObjectAttributes
,
374 ExFreePoolWithTag(FullName
.Buffer
, TAG_STRING
);
376 if(!NT_SUCCESS(Status
))
381 SetLastNtError(Status
);
382 DPRINT("Failed to reference desktop %wZ PID: %d!\n", &Desktop
, PsGetCurrentProcessId());
387 return STATUS_SUCCESS
;
391 * IntValidateDesktopHandle
393 * Validates the desktop handle.
396 * If the function succeeds, the handle remains referenced. If the
397 * fucntion fails, last error is set.
401 IntValidateDesktopHandle(
403 KPROCESSOR_MODE AccessMode
,
404 ACCESS_MASK DesiredAccess
,
409 Status
= ObReferenceObjectByHandle(
417 if (!NT_SUCCESS(Status
))
418 SetLastNtError(Status
);
424 IntGetActiveDesktop(VOID
)
430 * returns or creates a handle to the desktop object
433 IntGetDesktopObjectHandle(PDESKTOP DesktopObject
)
438 ASSERT(DesktopObject
);
440 if (!ObFindHandleForObject(PsGetCurrentProcess(),
446 Status
= ObOpenObjectByPointer(DesktopObject
,
453 if(!NT_SUCCESS(Status
))
455 /* unable to create a handle */
456 DPRINT1("Unable to create a desktop handle\n");
462 DPRINT1("Got handle: %lx\n", Ret
);
468 PUSER_MESSAGE_QUEUE FASTCALL
469 IntGetFocusMessageQueue(VOID
)
471 PDESKTOP pdo
= IntGetActiveDesktop();
474 DPRINT("No active desktop\n");
477 return (PUSER_MESSAGE_QUEUE
)pdo
->ActiveMessageQueue
;
481 IntSetFocusMessageQueue(PUSER_MESSAGE_QUEUE NewQueue
)
483 PUSER_MESSAGE_QUEUE Old
;
484 PDESKTOP pdo
= IntGetActiveDesktop();
487 DPRINT("No active desktop\n");
492 if(NewQueue
->Desktop
!= NULL
)
494 DPRINT("Message Queue already attached to another desktop!\n");
497 IntReferenceMessageQueue(NewQueue
);
498 (void)InterlockedExchangePointer((PVOID
*)&NewQueue
->Desktop
, pdo
);
500 Old
= (PUSER_MESSAGE_QUEUE
)InterlockedExchangePointer((PVOID
*)&pdo
->ActiveMessageQueue
, NewQueue
);
503 (void)InterlockedExchangePointer((PVOID
*)&Old
->Desktop
, 0);
504 IntDereferenceMessageQueue(Old
);
508 HWND FASTCALL
IntGetDesktopWindow(VOID
)
510 PDESKTOP pdo
= IntGetActiveDesktop();
513 DPRINT("No active desktop\n");
516 return pdo
->DesktopWindow
;
519 PWND FASTCALL
UserGetDesktopWindow(VOID
)
521 PDESKTOP pdo
= IntGetActiveDesktop();
525 DPRINT("No active desktop\n");
529 return UserGetWindowObject(pdo
->DesktopWindow
);
532 HWND FASTCALL
IntGetMessageWindow(VOID
)
534 PDESKTOP pdo
= IntGetActiveDesktop();
538 DPRINT("No active desktop\n");
541 return pdo
->spwndMessage
->head
.h
;
544 HWND FASTCALL
IntGetCurrentThreadDesktopWindow(VOID
)
546 PTHREADINFO pti
= PsGetCurrentThreadWin32Thread();
547 PDESKTOP pdo
= pti
->rpdesk
;
550 DPRINT1("Thread doesn't have a desktop\n");
553 return pdo
->DesktopWindow
;
556 BOOL FASTCALL
IntDesktopUpdatePerUserSettings(BOOL bEnable
)
560 RTL_QUERY_REGISTRY_TABLE QueryTable
[2];
563 RtlZeroMemory(QueryTable
, sizeof(QueryTable
));
565 QueryTable
[0].Flags
= RTL_QUERY_REGISTRY_DIRECT
;
566 QueryTable
[0].Name
= L
"PaintDesktopVersion";
567 QueryTable
[0].EntryContext
= &g_PaintDesktopVersion
;
569 /* Query the "PaintDesktopVersion" flag in the "Control Panel\Desktop" key */
570 Status
= RtlQueryRegistryValues(RTL_REGISTRY_USER
,
571 L
"Control Panel\\Desktop",
572 QueryTable
, NULL
, NULL
);
573 if (!NT_SUCCESS(Status
))
575 DPRINT("RtlQueryRegistryValues failed for PaintDesktopVersion (%x)\n",
577 g_PaintDesktopVersion
= FALSE
;
581 DPRINT("PaintDesktopVersion = %d\n", g_PaintDesktopVersion
);
587 g_PaintDesktopVersion
= FALSE
;
592 /* PUBLIC FUNCTIONS ***********************************************************/
595 UserGetDesktopDC(ULONG DcType
, BOOL EmptyDC
, BOOL ValidatehWnd
)
597 PWND DesktopObject
= 0;
600 if (DcType
== DC_TYPE_DIRECT
)
602 DesktopObject
= UserGetDesktopWindow();
603 DesktopHDC
= (HDC
)UserGetWindowDC(DesktopObject
);
608 hDev
= (HDEV
)pPrimarySurface
;
609 DesktopHDC
= IntGdiCreateDisplayDC(hDev
, DcType
, EmptyDC
);
621 Window
= UserGetDesktopWindow();
622 hRgn
= IntSysCreateRectRgnIndirect(&Window
->rcWindow
);
624 IntInvalidateWindows( Window
,
631 REGION_FreeRgnByHandle(hRgn
);
636 co_IntShowDesktop(PDESKTOP Desktop
, ULONG Width
, ULONG Height
)
638 CSR_API_MESSAGE Request
;
640 Request
.Type
= MAKE_CSR_API(SHOW_DESKTOP
, CSR_GUI
);
641 Request
.Data
.ShowDesktopRequest
.DesktopWindow
= Desktop
->DesktopWindow
;
642 Request
.Data
.ShowDesktopRequest
.Width
= Width
;
643 Request
.Data
.ShowDesktopRequest
.Height
= Height
;
645 return co_CsrNotify(&Request
);
649 IntHideDesktop(PDESKTOP Desktop
)
652 CSRSS_API_REQUEST Request
;
653 CSRSS_API_REPLY Reply
;
655 Request
.Type
= CSRSS_HIDE_DESKTOP
;
656 Request
.Data
.HideDesktopRequest
.DesktopWindow
= Desktop
->DesktopWindow
;
658 return NotifyCsrss(&Request
, &Reply
);
663 DesktopWnd
= IntGetWindowObject(Desktop
->DesktopWindow
);
666 return ERROR_INVALID_WINDOW_HANDLE
;
668 DesktopWnd
->style
&= ~WS_VISIBLE
;
670 return STATUS_SUCCESS
;
679 UserBuildShellHookHwndList(PDESKTOP Desktop
)
682 PSHELL_HOOK_WINDOW Current
;
685 /* fixme: if we save nb elements in desktop, we dont have to loop to find nb entries */
686 LIST_FOR_EACH(Current
, &Desktop
->ShellHookWindows
, SHELL_HOOK_WINDOW
, ListEntry
)
689 if (!entries
) return NULL
;
691 list
= ExAllocatePoolWithTag(PagedPool
, sizeof(HWND
) * (entries
+ 1), USERTAG_WINDOWLIST
); /* alloc one extra for nullterm */
696 LIST_FOR_EACH(Current
, &Desktop
->ShellHookWindows
, SHELL_HOOK_WINDOW
, ListEntry
)
697 *cursor
++ = Current
->hWnd
;
699 *cursor
= NULL
; /* nullterm list */
706 * Send the Message to the windows registered for ShellHook
707 * notifications. The lParam contents depend on the Message. See
708 * MSDN for more details (RegisterShellHookWindow)
710 VOID
co_IntShellHookNotify(WPARAM Message
, LPARAM lParam
)
712 PDESKTOP Desktop
= IntGetActiveDesktop();
715 if (!gpsi
->uiShellMsg
)
718 /* Too bad, this doesn't work.*/
721 RtlInitUnicodeString(&Str
, L
"SHELLHOOK");
722 gpsi
->uiShellMsg
= UserRegisterWindowMessage(&Str
);
725 gpsi
->uiShellMsg
= IntAddAtom(L
"SHELLHOOK");
727 DPRINT("MsgType = %x\n", gpsi
->uiShellMsg
);
728 if (!gpsi
->uiShellMsg
)
729 DPRINT1("LastError: %x\n", EngGetLastError());
734 DPRINT("IntShellHookNotify: No desktop!\n");
738 HwndList
= UserBuildShellHookHwndList(Desktop
);
741 HWND
* cursor
= HwndList
;
743 for (; *cursor
; cursor
++)
745 DPRINT("Sending notify\n");
746 co_IntPostOrSendMessage(*cursor
,
752 ExFreePool(HwndList
);
758 * Add the window to the ShellHookWindows list. The windows
759 * on that list get notifications that are important to shell
762 * TODO: Validate the window? I'm not sure if sending these messages to
763 * an unsuspecting application that is not your own is a nice thing to do.
765 BOOL
IntRegisterShellHookWindow(HWND hWnd
)
767 PTHREADINFO pti
= PsGetCurrentThreadWin32Thread();
768 PDESKTOP Desktop
= pti
->rpdesk
;
769 PSHELL_HOOK_WINDOW Entry
;
771 DPRINT("IntRegisterShellHookWindow\n");
773 /* First deregister the window, so we can be sure it's never twice in the
776 IntDeRegisterShellHookWindow(hWnd
);
778 Entry
= ExAllocatePoolWithTag(PagedPool
,
779 sizeof(SHELL_HOOK_WINDOW
),
787 InsertTailList(&Desktop
->ShellHookWindows
, &Entry
->ListEntry
);
793 * Remove the window from the ShellHookWindows list. The windows
794 * on that list get notifications that are important to shell
797 BOOL
IntDeRegisterShellHookWindow(HWND hWnd
)
799 PTHREADINFO pti
= PsGetCurrentThreadWin32Thread();
800 PDESKTOP Desktop
= pti
->rpdesk
;
801 PSHELL_HOOK_WINDOW Current
;
803 LIST_FOR_EACH(Current
, &Desktop
->ShellHookWindows
, SHELL_HOOK_WINDOW
, ListEntry
)
805 if (Current
->hWnd
== hWnd
)
807 RemoveEntryList(&Current
->ListEntry
);
817 IntFreeDesktopHeap(IN OUT PDESKTOP Desktop
)
819 if (Desktop
->hsectionDesktop
!= NULL
)
821 ObDereferenceObject(Desktop
->hsectionDesktop
);
822 Desktop
->hsectionDesktop
= NULL
;
825 /* SYSCALLS *******************************************************************/
828 * NtUserCreateDesktop
830 * Creates a new desktop.
837 * Name of the device.
846 * Requested type of access.
850 * If the function succeeds, the return value is a handle to the newly
851 * created desktop. If the specified desktop already exists, the function
852 * succeeds and returns a handle to the existing desktop. When you are
853 * finished using the handle, call the CloseDesktop function to close it.
854 * If the function fails, the return value is NULL.
862 POBJECT_ATTRIBUTES poa
,
863 PUNICODE_STRING lpszDesktopDevice
,
866 ACCESS_MASK dwDesiredAccess
)
868 OBJECT_ATTRIBUTES ObjectAttributes
;
869 PTHREADINFO W32Thread
;
870 PWINSTATION_OBJECT WinStaObject
;
871 PDESKTOP DesktopObject
;
872 UNICODE_STRING DesktopName
;
873 NTSTATUS Status
= STATUS_SUCCESS
;
875 CSR_API_MESSAGE Request
;
876 PVOID DesktopHeapSystemBase
= NULL
;
877 SIZE_T DesktopInfoSize
;
878 UNICODE_STRING SafeDesktopName
;
880 ULONG_PTR HeapSize
= 4 * 1024 * 1024; /* FIXME */
881 HWINSTA hWindowStation
= NULL
;
882 PUNICODE_STRING lpszDesktopName
= NULL
;
883 UNICODE_STRING ClassName
, MenuName
;
884 LARGE_STRING WindowName
;
885 BOOL NoHooks
= FALSE
;
889 PTHREADINFO ptiCurrent
;
890 DECLARE_RETURN(HDESK
);
892 DPRINT("Enter NtUserCreateDesktop: %wZ\n", lpszDesktopName
);
893 UserEnterExclusive();
895 ptiCurrent
= PsGetCurrentThreadWin32Thread();
898 /* Turn off hooks when calling any CreateWindowEx from inside win32k. */
899 NoHooks
= (ptiCurrent
->TIF_flags
& TIF_DISABLEHOOKS
);
900 ptiCurrent
->TIF_flags
|= TIF_DISABLEHOOKS
;
906 sizeof(OBJECT_ATTRIBUTES
),
909 hWindowStation
= poa
->RootDirectory
;
910 lpszDesktopName
= poa
->ObjectName
;
912 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
914 Status
=_SEH2_GetExceptionCode();
918 if (! NT_SUCCESS(Status
))
920 DPRINT1("Failed reading Object Attributes from user space.\n");
921 SetLastNtError(Status
);
925 Status
= IntValidateWindowStationHandle(
928 0, /* FIXME - WINSTA_CREATEDESKTOP */
931 if (! NT_SUCCESS(Status
))
933 DPRINT1("Failed validation of window station handle (0x%X), cannot create desktop %wZ\n",
934 hWindowStation
, lpszDesktopName
);
935 SetLastNtError(Status
);
938 if(lpszDesktopName
!= NULL
)
940 Status
= IntSafeCopyUnicodeString(&SafeDesktopName
, lpszDesktopName
);
941 if(!NT_SUCCESS(Status
))
943 SetLastNtError(Status
);
949 RtlInitUnicodeString(&SafeDesktopName
, NULL
);
952 if (! IntGetFullWindowStationName(&DesktopName
, &WinStaObject
->Name
,
955 SetLastNtError(STATUS_INSUFFICIENT_RESOURCES
);
956 ObDereferenceObject(WinStaObject
);
958 ExFreePoolWithTag(SafeDesktopName
.Buffer
, TAG_STRING
);
962 ExFreePoolWithTag(SafeDesktopName
.Buffer
, TAG_STRING
);
963 ObDereferenceObject(WinStaObject
);
966 * Try to open already existing desktop
969 DPRINT("Trying to open desktop (%wZ)\n", &DesktopName
);
971 /* Initialize ObjectAttributes for the desktop object */
972 InitializeObjectAttributes(
979 Status
= ObOpenObjectByName(
985 (PVOID
)&DummyContext
,
987 if (!NT_SUCCESS(Status
)) RETURN(NULL
);
988 if (Status
== STATUS_OBJECT_NAME_EXISTS
)
990 ExFreePoolWithTag(DesktopName
.Buffer
, TAG_STRING
);
994 /* Reference the desktop */
995 Status
= ObReferenceObjectByHandle(Desktop
,
999 (PVOID
)&DesktopObject
,
1001 if (!NT_SUCCESS(Status
)) RETURN(NULL
);
1003 DesktopObject
->hsectionDesktop
= NULL
;
1004 DesktopObject
->pheapDesktop
= UserCreateHeap(&DesktopObject
->hsectionDesktop
,
1005 &DesktopHeapSystemBase
,
1007 if (DesktopObject
->pheapDesktop
== NULL
)
1009 ObDereferenceObject(DesktopObject
);
1010 DPRINT1("Failed to create desktop heap!\n");
1014 DesktopInfoSize
= FIELD_OFFSET(DESKTOPINFO
,
1015 szDesktopName
[(lpszDesktopName
->Length
/ sizeof(WCHAR
)) + 1]);
1017 DesktopObject
->pDeskInfo
= RtlAllocateHeap(DesktopObject
->pheapDesktop
,
1021 if (DesktopObject
->pDeskInfo
== NULL
)
1023 ObDereferenceObject(DesktopObject
);
1024 DPRINT1("Failed to create the DESKTOP structure!\n");
1028 RtlZeroMemory(DesktopObject
->pDeskInfo
,
1031 DesktopObject
->pDeskInfo
->pvDesktopBase
= DesktopHeapSystemBase
;
1032 DesktopObject
->pDeskInfo
->pvDesktopLimit
= (PVOID
)((ULONG_PTR
)DesktopHeapSystemBase
+ HeapSize
);
1033 RtlCopyMemory(DesktopObject
->pDeskInfo
->szDesktopName
,
1034 lpszDesktopName
->Buffer
,
1035 lpszDesktopName
->Length
);
1037 /* Initialize some local (to win32k) desktop state. */
1038 InitializeListHead(&DesktopObject
->PtiList
);
1039 DesktopObject
->ActiveMessageQueue
= NULL
;
1040 /* Setup Global Hooks. */
1041 for (i
= 0; i
< NB_HOOKS
; i
++)
1043 InitializeListHead(&DesktopObject
->pDeskInfo
->aphkStart
[i
]);
1045 ExFreePoolWithTag(DesktopName
.Buffer
, TAG_STRING
);
1047 //// why is this here?
1049 if (! NT_SUCCESS(Status
))
1051 DPRINT1("Failed to create desktop handle\n");
1052 SetLastNtError(Status
);
1059 * Create a handle for CSRSS and notify CSRSS for Creating Desktop Window.
1061 * Honestly, I believe this is a cleverly written hack that allowed ReactOS
1062 * to function at the beginning of the project by ramroding the GUI into
1063 * operation and making the desktop window work from user space.
1066 Request
.Type
= MAKE_CSR_API(CREATE_DESKTOP
, CSR_GUI
);
1067 Status
= CsrInsertObject(Desktop
,
1069 (HANDLE
*)&Request
.Data
.CreateDesktopRequest
.DesktopHandle
);
1070 if (! NT_SUCCESS(Status
))
1072 DPRINT1("Failed to create desktop handle for CSRSS\n");
1074 SetLastNtError(Status
);
1078 Status
= co_CsrNotify(&Request
);
1079 if (! NT_SUCCESS(Status
))
1081 CsrCloseHandle(Request
.Data
.CreateDesktopRequest
.DesktopHandle
);
1082 DPRINT1("Failed to notify CSRSS about new desktop\n");
1084 SetLastNtError(Status
);
1087 #if 0 // Turn on when server side proc is ready.
1089 // Create desktop window.
1091 ClassName
.Buffer
= ((PWSTR
)((ULONG_PTR
)(WORD
)(gpsi
->atomSysClass
[ICLS_DESKTOP
])));
1092 ClassName
.Length
= 0;
1093 RtlZeroMemory(&MenuName
, sizeof(MenuName
));
1094 RtlZeroMemory(&WindowName
, sizeof(WindowName
));
1096 RtlZeroMemory(&Cs
, sizeof(Cs
));
1097 Cs
.x
= UserGetSystemMetrics(SM_XVIRTUALSCREEN
);
1098 Cs
.y
= UserGetSystemMetrics(SM_YVIRTUALSCREEN
);
1099 Cs
.cx
= UserGetSystemMetrics(SM_CXVIRTUALSCREEN
);
1100 Cs
.cy
= UserGetSystemMetrics(SM_CYVIRTUALSCREEN
);
1101 Cs
.style
= WS_POPUP
|WS_CLIPCHILDREN
;
1102 Cs
.hInstance
= hModClient
; // Experimental mode... Move csr stuff to User32. hModuleWin; // Server side winproc!
1103 Cs
.lpszName
= (LPCWSTR
) &WindowName
;
1104 Cs
.lpszClass
= (LPCWSTR
) &ClassName
;
1106 pWndDesktop
= co_UserCreateWindowEx(&Cs
, &ClassName
, &WindowName
);
1109 DPRINT1("Failed to create Desktop window handle\n");
1113 DesktopObject
->pDeskInfo
->spwnd
= pWndDesktop
;
1116 W32Thread
= PsGetCurrentThreadWin32Thread();
1118 if (!W32Thread
->rpdesk
) IntSetThreadDesktop(DesktopObject
,FALSE
);
1121 Based on wine/server/window.c in get_desktop_window.
1124 ClassName
.Buffer
= ((PWSTR
)((ULONG_PTR
)(WORD
)(gpsi
->atomSysClass
[ICLS_HWNDMESSAGE
])));
1125 ClassName
.Length
= 0;
1126 RtlZeroMemory(&MenuName
, sizeof(MenuName
));
1127 RtlZeroMemory(&WindowName
, sizeof(WindowName
));
1129 RtlZeroMemory(&Cs
, sizeof(Cs
));
1130 Cs
.cx
= Cs
.cy
= 100;
1131 Cs
.style
= WS_POPUP
|WS_CLIPCHILDREN
;
1132 Cs
.hInstance
= hModClient
; // hModuleWin; // Server side winproc! Leave it to Timo to not pass on notes!
1133 Cs
.lpszName
= (LPCWSTR
) &WindowName
;
1134 Cs
.lpszClass
= (LPCWSTR
) &ClassName
;
1136 pWnd
= co_UserCreateWindowEx(&Cs
, &ClassName
, &WindowName
);
1139 DPRINT1("Failed to create Message window handle\n");
1143 DesktopObject
->spwndMessage
= pWnd
;
1147 if !(WinStaObject->Flags & WSF_NOIO) is (not set) for desktop input output mode (see wiki)
1148 Create Tooltip. Saved in DesktopObject->spwndTooltip.
1149 Tooltip dwExStyle: WS_EX_TOOLWINDOW|WS_EX_TOPMOST
1150 hWndParent are spwndMessage. Use hModuleWin for server side winproc!
1151 The rest is same as message window.
1152 http://msdn.microsoft.com/en-us/library/bb760250(VS.85).aspx
1157 if (!NoHooks
&& ptiCurrent
) ptiCurrent
->TIF_flags
&= ~TIF_DISABLEHOOKS
;
1158 DPRINT("Leave NtUserCreateDesktop, ret=%i\n",_ret_
);
1166 * Opens an existing desktop.
1170 * Name of the existing desktop.
1173 * Interaction flags.
1176 * Requested type of access.
1179 * Handle to the desktop or zero on failure.
1187 PUNICODE_STRING lpszDesktopName
,
1189 ACCESS_MASK dwDesiredAccess
)
1191 OBJECT_ATTRIBUTES ObjectAttributes
;
1193 PWINSTATION_OBJECT WinStaObject
;
1194 UNICODE_STRING DesktopName
;
1195 UNICODE_STRING SafeDesktopName
;
1199 DECLARE_RETURN(HDESK
);
1201 DPRINT("Enter NtUserOpenDesktop: %wZ\n", lpszDesktopName
);
1202 UserEnterExclusive();
1205 * Validate the window station handle and compose the fully
1206 * qualified desktop name
1209 WinSta
= UserGetProcessWindowStation();
1210 Status
= IntValidateWindowStationHandle(
1216 if (!NT_SUCCESS(Status
))
1218 DPRINT1("Failed validation of window station handle (0x%X)\n", WinSta
);
1219 SetLastNtError(Status
);
1223 if(lpszDesktopName
!= NULL
)
1225 Status
= IntSafeCopyUnicodeString(&SafeDesktopName
, lpszDesktopName
);
1226 if(!NT_SUCCESS(Status
))
1228 SetLastNtError(Status
);
1234 RtlInitUnicodeString(&SafeDesktopName
, NULL
);
1237 Result
= IntGetFullWindowStationName(&DesktopName
, &WinStaObject
->Name
,
1240 if (lpszDesktopName
)
1241 ExFreePoolWithTag(SafeDesktopName
.Buffer
, TAG_STRING
);
1242 ObDereferenceObject(WinStaObject
);
1247 SetLastNtError(STATUS_INSUFFICIENT_RESOURCES
);
1252 DPRINT("Trying to open desktop (%wZ)\n", &DesktopName
);
1254 /* Initialize ObjectAttributes for the desktop object */
1255 InitializeObjectAttributes(
1262 Status
= ObOpenObjectByName(
1264 ExDesktopObjectType
,
1271 if (!NT_SUCCESS(Status
))
1273 SetLastNtError(Status
);
1274 ExFreePool(DesktopName
.Buffer
);
1278 DPRINT("Successfully opened desktop (%wZ)\n", &DesktopName
);
1279 ExFreePool(DesktopName
.Buffer
);
1284 DPRINT("Leave NtUserOpenDesktop, ret=%i\n",_ret_
);
1290 * NtUserOpenInputDesktop
1292 * Opens the input (interactive) desktop.
1296 * Interaction flags.
1299 * Inheritance option.
1302 * Requested type of access.
1305 * Handle to the input desktop or zero on failure.
1312 NtUserOpenInputDesktop(
1315 ACCESS_MASK dwDesiredAccess
)
1320 DECLARE_RETURN(HDESK
);
1322 DPRINT("Enter NtUserOpenInputDesktop\n");
1323 UserEnterExclusive();
1325 DPRINT("About to open input desktop\n");
1327 /* Get a pointer to the desktop object */
1329 Status
= IntValidateDesktopHandle(
1335 if (!NT_SUCCESS(Status
))
1337 DPRINT("Validation of input desktop handle (0x%X) failed\n", InputDesktop
);
1341 /* Create a new handle to the object */
1343 Status
= ObOpenObjectByPointer(
1348 ExDesktopObjectType
,
1352 ObDereferenceObject(Object
);
1354 if (NT_SUCCESS(Status
))
1356 DPRINT("Successfully opened input desktop\n");
1357 RETURN((HDESK
)Desktop
);
1360 SetLastNtError(Status
);
1364 DPRINT("Leave NtUserOpenInputDesktop, ret=%i\n",_ret_
);
1370 * NtUserCloseDesktop
1372 * Closes a desktop handle.
1376 * Handle to the desktop.
1382 * The desktop handle can be created with NtUserCreateDesktop or
1383 * NtUserOpenDesktop. This function will fail if any thread in the calling
1384 * process is using the specified desktop handle or if the handle refers
1385 * to the initial desktop of the calling process.
1392 NtUserCloseDesktop(HDESK hDesktop
)
1396 DECLARE_RETURN(BOOL
);
1398 DPRINT("Enter NtUserCloseDesktop\n");
1399 UserEnterExclusive();
1401 DPRINT("About to close desktop handle (0x%X)\n", hDesktop
);
1403 Status
= IntValidateDesktopHandle(
1409 if (!NT_SUCCESS(Status
))
1411 DPRINT("Validation of desktop handle (0x%X) failed\n", hDesktop
);
1415 ObDereferenceObject(Object
);
1417 DPRINT("Closing desktop handle (0x%X)\n", hDesktop
);
1419 Status
= ZwClose(hDesktop
);
1420 if (!NT_SUCCESS(Status
))
1422 SetLastNtError(Status
);
1429 DPRINT("Leave NtUserCloseDesktop, ret=%i\n",_ret_
);
1438 * NtUserPaintDesktop
1440 * The NtUserPaintDesktop function fills the clipping region in the
1441 * specified device context with the desktop pattern or wallpaper. The
1442 * function is provided primarily for shell desktops.
1446 * Handle to the device context.
1453 NtUserPaintDesktop(HDC hDC
)
1456 HBRUSH DesktopBrush
, PreviousBrush
;
1458 BOOL doPatBlt
= TRUE
;
1464 PTHREADINFO pti
= PsGetCurrentThreadWin32Thread();
1465 PWINSTATION_OBJECT WinSta
= pti
->rpdesk
->rpwinstaParent
;
1466 DECLARE_RETURN(BOOL
);
1468 UserEnterExclusive();
1469 DPRINT("Enter NtUserPaintDesktop\n");
1471 GdiGetClipBox(hDC
, &Rect
);
1473 hWndDesktop
= IntGetDesktopWindow();
1475 WndDesktop
= UserGetWindowObject(hWndDesktop
);
1481 DesktopBrush
= (HBRUSH
)WndDesktop
->pcls
->hbrBackground
;
1485 * Paint desktop background
1488 if (WinSta
->hbmWallpaper
!= NULL
)
1492 DeskWin
= UserGetWindowObject(hWndDesktop
);
1500 sz
.cx
= DeskWin
->rcWindow
.right
- DeskWin
->rcWindow
.left
;
1501 sz
.cy
= DeskWin
->rcWindow
.bottom
- DeskWin
->rcWindow
.top
;
1503 if (WinSta
->WallpaperMode
== wmStretch
||
1504 WinSta
->WallpaperMode
== wmTile
)
1511 /* Find the upper left corner, can be negtive if the bitmap is bigger then the screen */
1512 x
= (sz
.cx
/ 2) - (WinSta
->cxWallpaper
/ 2);
1513 y
= (sz
.cy
/ 2) - (WinSta
->cyWallpaper
/ 2);
1516 hWallpaperDC
= NtGdiCreateCompatibleDC(hDC
);
1517 if(hWallpaperDC
!= NULL
)
1521 /* fill in the area that the bitmap is not going to cover */
1524 /* FIXME - clip out the bitmap
1525 can be replaced with "NtGdiPatBlt(hDC, x, y, WinSta->cxWallpaper, WinSta->cyWallpaper, PATCOPY | DSTINVERT);"
1526 once we support DSTINVERT */
1527 PreviousBrush
= NtGdiSelectBrush(hDC
, DesktopBrush
);
1528 NtGdiPatBlt(hDC
, Rect
.left
, Rect
.top
, Rect
.right
, Rect
.bottom
, PATCOPY
);
1529 NtGdiSelectBrush(hDC
, PreviousBrush
);
1532 /*Do not fill the background after it is painted no matter the size of the picture */
1535 hOldBitmap
= NtGdiSelectBitmap(hWallpaperDC
, WinSta
->hbmWallpaper
);
1537 if (WinSta
->WallpaperMode
== wmStretch
)
1539 if(Rect
.right
&& Rect
.bottom
)
1540 NtGdiStretchBlt(hDC
,
1548 WinSta
->cxWallpaper
,
1549 WinSta
->cyWallpaper
,
1554 else if (WinSta
->WallpaperMode
== wmTile
)
1556 /* paint the bitmap across the screen then down */
1557 for(y
= 0; y
< Rect
.bottom
; y
+= WinSta
->cyWallpaper
)
1559 for(x
= 0; x
< Rect
.right
; x
+= WinSta
->cxWallpaper
)
1564 WinSta
->cxWallpaper
,
1565 WinSta
->cyWallpaper
,
1580 WinSta
->cxWallpaper
,
1581 WinSta
->cyWallpaper
,
1589 NtGdiSelectBitmap(hWallpaperDC
, hOldBitmap
);
1590 NtGdiDeleteObjectApp(hWallpaperDC
);
1595 /* Back ground is set to none, clear the screen */
1598 PreviousBrush
= NtGdiSelectBrush(hDC
, DesktopBrush
);
1599 NtGdiPatBlt(hDC
, Rect
.left
, Rect
.top
, Rect
.right
, Rect
.bottom
, PATCOPY
);
1600 NtGdiSelectBrush(hDC
, PreviousBrush
);
1604 * Display system version on the desktop background
1607 if (g_PaintDesktopVersion
)
1609 static WCHAR s_wszVersion
[256] = {0};
1614 len
= wcslen(s_wszVersion
);
1618 len
= GetSystemVersionString(s_wszVersion
);
1623 if (!UserSystemParametersInfo(SPI_GETWORKAREA
, 0, &rect
, 0))
1625 rect
.right
= UserGetSystemMetrics(SM_CXSCREEN
);
1626 rect
.bottom
= UserGetSystemMetrics(SM_CYSCREEN
);
1629 color_old
= IntGdiSetTextColor(hDC
, RGB(255,255,255));
1630 align_old
= IntGdiSetTextAlign(hDC
, TA_RIGHT
);
1631 mode_old
= IntGdiSetBkMode(hDC
, TRANSPARENT
);
1633 GreExtTextOutW(hDC
, rect
.right
-16, rect
.bottom
-48, 0, NULL
, s_wszVersion
, len
, NULL
, 0);
1635 IntGdiSetBkMode(hDC
, mode_old
);
1636 IntGdiSetTextAlign(hDC
, align_old
);
1637 IntGdiSetTextColor(hDC
, color_old
);
1644 DPRINT("Leave NtUserPaintDesktop, ret=%i\n",_ret_
);
1651 * NtUserSwitchDesktop
1653 * Sets the current input (interactive) desktop.
1657 * Handle to desktop.
1667 NtUserSwitchDesktop(HDESK hDesktop
)
1669 PDESKTOP DesktopObject
;
1671 DECLARE_RETURN(BOOL
);
1673 UserEnterExclusive();
1674 DPRINT("Enter NtUserSwitchDesktop\n");
1676 DPRINT("About to switch desktop (0x%X)\n", hDesktop
);
1678 Status
= IntValidateDesktopHandle(
1684 if (!NT_SUCCESS(Status
))
1686 DPRINT("Validation of desktop handle (0x%X) failed\n", hDesktop
);
1691 * Don't allow applications switch the desktop if it's locked, unless the caller
1692 * is the logon application itself
1694 if((DesktopObject
->rpwinstaParent
->Flags
& WSS_LOCKED
) &&
1695 LogonProcess
!= NULL
&& LogonProcess
!= PsGetCurrentProcessWin32Process())
1697 ObDereferenceObject(DesktopObject
);
1698 DPRINT1("Switching desktop 0x%x denied because the work station is locked!\n", hDesktop
);
1702 if(DesktopObject
->rpwinstaParent
!= InputWindowStation
)
1704 ObDereferenceObject(DesktopObject
);
1705 DPRINT1("Switching desktop 0x%x denied because desktop doesn't belong to the interactive winsta!\n", hDesktop
);
1709 /* FIXME: Fail if the process is associated with a secured
1710 desktop such as Winlogon or Screen-Saver */
1711 /* FIXME: Connect to input device */
1713 /* Set the active desktop in the desktop's window station. */
1714 InputWindowStation
->ActiveDesktop
= DesktopObject
;
1716 /* Set the global state. */
1717 InputDesktop
= DesktopObject
;
1718 InputDesktopHandle
= hDesktop
;
1720 ObDereferenceObject(DesktopObject
);
1725 DPRINT("Leave NtUserSwitchDesktop, ret=%i\n",_ret_
);
1731 * NtUserGetThreadDesktop
1738 NtUserGetThreadDesktop(DWORD dwThreadId
, DWORD Unknown1
)
1742 PDESKTOP DesktopObject
;
1743 HDESK Ret
, hThreadDesktop
;
1744 OBJECT_HANDLE_INFORMATION HandleInformation
;
1745 DECLARE_RETURN(HDESK
);
1747 UserEnterExclusive();
1748 DPRINT("Enter NtUserGetThreadDesktop\n");
1752 EngSetLastError(ERROR_INVALID_PARAMETER
);
1756 Status
= PsLookupThreadByThreadId((HANDLE
)(DWORD_PTR
)dwThreadId
, &Thread
);
1757 if(!NT_SUCCESS(Status
))
1759 EngSetLastError(ERROR_INVALID_PARAMETER
);
1763 if(Thread
->ThreadsProcess
== PsGetCurrentProcess())
1765 /* just return the handle, we queried the desktop handle of a thread running
1766 in the same context */
1767 Ret
= ((PTHREADINFO
)Thread
->Tcb
.Win32Thread
)->hdesk
;
1768 ObDereferenceObject(Thread
);
1772 /* get the desktop handle and the desktop of the thread */
1773 if(!(hThreadDesktop
= ((PTHREADINFO
)Thread
->Tcb
.Win32Thread
)->hdesk
) ||
1774 !(DesktopObject
= ((PTHREADINFO
)Thread
->Tcb
.Win32Thread
)->rpdesk
))
1776 ObDereferenceObject(Thread
);
1777 DPRINT1("Desktop information of thread 0x%x broken!?\n", dwThreadId
);
1781 /* we could just use DesktopObject instead of looking up the handle, but latter
1782 may be a bit safer (e.g. when the desktop is being destroyed */
1783 /* switch into the context of the thread we're trying to get the desktop from,
1784 so we can use the handle */
1785 KeAttachProcess(&Thread
->ThreadsProcess
->Pcb
);
1786 Status
= ObReferenceObjectByHandle(hThreadDesktop
,
1788 ExDesktopObjectType
,
1790 (PVOID
*)&DesktopObject
,
1791 &HandleInformation
);
1794 /* the handle couldn't be found, there's nothing to get... */
1795 if(!NT_SUCCESS(Status
))
1797 ObDereferenceObject(Thread
);
1801 /* lookup our handle table if we can find a handle to the desktop object,
1802 if not, create one */
1803 Ret
= IntGetDesktopObjectHandle(DesktopObject
);
1805 /* all done, we got a valid handle to the desktop */
1806 ObDereferenceObject(DesktopObject
);
1807 ObDereferenceObject(Thread
);
1811 DPRINT("Leave NtUserGetThreadDesktop, ret=%i\n",_ret_
);
1817 IntUnmapDesktopView(IN PDESKTOP DesktopObject
)
1820 PPROCESSINFO CurrentWin32Process
;
1821 PW32HEAP_USER_MAPPING HeapMapping
, *PrevLink
;
1822 NTSTATUS Status
= STATUS_SUCCESS
;
1826 CurrentWin32Process
= PsGetCurrentProcessWin32Process();
1827 PrevLink
= &CurrentWin32Process
->HeapMappings
.Next
;
1829 /* unmap if we're the last thread using the desktop */
1830 HeapMapping
= *PrevLink
;
1831 while (HeapMapping
!= NULL
)
1833 if (HeapMapping
->KernelMapping
== (PVOID
)DesktopObject
->pheapDesktop
)
1835 if (--HeapMapping
->Count
== 0)
1837 *PrevLink
= HeapMapping
->Next
;
1839 Status
= MmUnmapViewOfSection(PsGetCurrentProcess(),
1840 HeapMapping
->UserMapping
);
1842 ObDereferenceObject(DesktopObject
);
1844 UserHeapFree(HeapMapping
);
1849 PrevLink
= &HeapMapping
->Next
;
1850 HeapMapping
= HeapMapping
->Next
;
1853 ti
= GetW32ThreadInfo();
1856 GetWin32ClientInfo()->pDeskInfo
= NULL
;
1858 GetWin32ClientInfo()->ulClientDelta
= 0;
1864 IntMapDesktopView(IN PDESKTOP DesktopObject
)
1866 PPROCESSINFO CurrentWin32Process
;
1867 PW32HEAP_USER_MAPPING HeapMapping
, *PrevLink
;
1868 PVOID UserBase
= NULL
;
1869 SIZE_T ViewSize
= 0;
1870 LARGE_INTEGER Offset
;
1873 CurrentWin32Process
= PsGetCurrentProcessWin32Process();
1874 PrevLink
= &CurrentWin32Process
->HeapMappings
.Next
;
1876 /* find out if another thread already mapped the desktop heap */
1877 HeapMapping
= *PrevLink
;
1878 while (HeapMapping
!= NULL
)
1880 if (HeapMapping
->KernelMapping
== (PVOID
)DesktopObject
->pheapDesktop
)
1882 HeapMapping
->Count
++;
1883 return STATUS_SUCCESS
;
1886 PrevLink
= &HeapMapping
->Next
;
1887 HeapMapping
= HeapMapping
->Next
;
1890 /* we're the first, map the heap */
1891 DPRINT("Noone mapped the desktop heap %p yet, so - map it!\n", DesktopObject
->pheapDesktop
);
1892 Offset
.QuadPart
= 0;
1893 Status
= MmMapViewOfSection(DesktopObject
->hsectionDesktop
,
1894 PsGetCurrentProcess(),
1902 PAGE_EXECUTE_READ
); /* would prefer PAGE_READONLY, but thanks to RTL heaps... */
1903 if (!NT_SUCCESS(Status
))
1905 DPRINT1("Failed to map desktop\n");
1909 /* add the mapping */
1910 HeapMapping
= UserHeapAlloc(sizeof(W32HEAP_USER_MAPPING
));
1911 if (HeapMapping
== NULL
)
1913 MmUnmapViewOfSection(PsGetCurrentProcess(),
1915 DPRINT1("UserHeapAlloc() failed!\n");
1916 return STATUS_NO_MEMORY
;
1919 HeapMapping
->Next
= NULL
;
1920 HeapMapping
->KernelMapping
= (PVOID
)DesktopObject
->pheapDesktop
;
1921 HeapMapping
->UserMapping
= UserBase
;
1922 HeapMapping
->Limit
= ViewSize
;
1923 HeapMapping
->Count
= 1;
1924 *PrevLink
= HeapMapping
;
1926 ObReferenceObject(DesktopObject
);
1928 return STATUS_SUCCESS
;
1932 IntSetThreadDesktop(IN PDESKTOP DesktopObject
,
1933 IN BOOL FreeOnFailure
)
1935 PDESKTOP OldDesktop
;
1936 PTHREADINFO W32Thread
;
1939 CLIENTTHREADINFO ctiSave
;
1941 DPRINT("IntSetThreadDesktop() DO=%p, FOF=%d\n", DesktopObject
, FreeOnFailure
);
1942 MapHeap
= (PsGetCurrentProcess() != PsInitialSystemProcess
);
1943 W32Thread
= PsGetCurrentThreadWin32Thread();
1945 if (W32Thread
->rpdesk
!= DesktopObject
)
1947 OldDesktop
= W32Thread
->rpdesk
;
1949 if (!IsListEmpty(&W32Thread
->WindowListHead
))
1951 DPRINT1("Attempted to change thread desktop although the thread has windows!\n");
1952 EngSetLastError(ERROR_BUSY
);
1956 W32Thread
->rpdesk
= DesktopObject
;
1958 if (MapHeap
&& DesktopObject
!= NULL
)
1960 Status
= IntMapDesktopView(DesktopObject
);
1961 if (!NT_SUCCESS(Status
))
1963 SetLastNtError(Status
);
1966 W32Thread
->pDeskInfo
= DesktopObject
->pDeskInfo
;
1969 RtlZeroMemory(&ctiSave
, sizeof(CLIENTTHREADINFO
));
1971 if (W32Thread
->pcti
&& OldDesktop
&& NtCurrentTeb())
1973 RtlCopyMemory(&ctiSave
, W32Thread
->pcti
, sizeof(CLIENTTHREADINFO
));
1974 DPRINT("Free ClientThreadInfo\n");
1975 DesktopHeapFree(OldDesktop
, W32Thread
->pcti
);
1976 W32Thread
->pcti
= NULL
;
1979 if (!W32Thread
->pcti
&& DesktopObject
&& NtCurrentTeb())
1981 DPRINT("Allocate ClientThreadInfo\n");
1982 W32Thread
->pcti
= DesktopHeapAlloc( DesktopObject
,
1983 sizeof(CLIENTTHREADINFO
));
1984 RtlCopyMemory(W32Thread
->pcti
, &ctiSave
, sizeof(CLIENTTHREADINFO
));
1989 PCLIENTINFO pci
= GetWin32ClientInfo();
1990 pci
->ulClientDelta
= DesktopHeapGetUserDelta();
1993 pci
->pDeskInfo
= (PVOID
)((ULONG_PTR
)DesktopObject
->pDeskInfo
- pci
->ulClientDelta
);
1994 if (W32Thread
->pcti
) pci
->pClientThreadInfo
= (PVOID
)((ULONG_PTR
)W32Thread
->pcti
- pci
->ulClientDelta
);
1998 if (OldDesktop
!= NULL
&&
1999 !IntCheckProcessDesktopClasses(OldDesktop
,
2002 DPRINT1("Failed to move process classes to shared heap!\n");
2004 /* failed to move desktop classes to the shared heap,
2005 unmap the view and return the error */
2006 if (MapHeap
&& DesktopObject
!= NULL
)
2007 IntUnmapDesktopView(DesktopObject
);
2012 /* Remove the thread from the old desktop's list */
2013 RemoveEntryList(&W32Thread
->PtiLink
);
2015 if (DesktopObject
!= NULL
)
2017 ObReferenceObject(DesktopObject
);
2018 /* Insert into new desktop's list */
2019 InsertTailList(&DesktopObject
->PtiList
, &W32Thread
->PtiLink
);
2022 if (OldDesktop
!= NULL
)
2026 IntUnmapDesktopView(OldDesktop
);
2029 ObDereferenceObject(OldDesktop
);
2037 * NtUserSetThreadDesktop
2044 NtUserSetThreadDesktop(HDESK hDesktop
)
2046 PDESKTOP DesktopObject
;
2048 DECLARE_RETURN(BOOL
);
2050 UserEnterExclusive();
2051 DPRINT("Enter NtUserSetThreadDesktop\n");
2053 /* Validate the new desktop. */
2054 Status
= IntValidateDesktopHandle(
2060 if (!NT_SUCCESS(Status
))
2062 DPRINT("Validation of desktop handle (0x%X) failed\n", hDesktop
);
2066 /* FIXME: Should check here to see if the thread has any windows. */
2068 if (!IntSetThreadDesktop(DesktopObject
,
2077 DPRINT("Leave NtUserSetThreadDesktop, ret=%i\n",_ret_
);