[NTUSER] Fix a few tests related to desktops and window stations
authorGiannis Adamopoulos <gadamopoulos@reactos.org>
Wed, 2 Jan 2019 14:50:35 +0000 (16:50 +0200)
committerGiannis Adamopoulos <gadamopoulos@reactos.org>
Wed, 2 Jan 2019 15:15:40 +0000 (17:15 +0200)
- NtUserOpenInputDesktop: Don't crash if there is no input desktop yet
- NtUserOpenInputDesktop: Fail if the process doesn't belong to the interactive window station
- NtUserCreateWindowStation: Clear error on success
- DesktopWindowProc: Use UserOpenInputDesktop to get a handle to the input desktop

win32ss/user/ntuser/desktop.c
win32ss/user/ntuser/desktop.h
win32ss/user/ntuser/winsta.c

index e066198..875cc4b 100644 (file)
@@ -1471,7 +1471,7 @@ DesktopWindowProc(PWND Wnd, UINT Msg, WPARAM wParam, LPARAM lParam, LRESULT *lRe
             PWINDOWPOS pWindowPos = (PWINDOWPOS)lParam;
             if ((pWindowPos->flags & SWP_SHOWWINDOW) != 0)
             {
-                HDESK hdesk = IntGetDesktopObjectHandle(gpdeskInputDesktop);
+                HDESK hdesk = UserOpenInputDesktop(0, FALSE, DESKTOP_ALL_ACCESS);
                 IntSetThreadDesktop(hdesk, FALSE);
             }
             break;
@@ -2569,6 +2569,48 @@ NtUserOpenDesktop(
     return Desktop;
 }
 
+HDESK UserOpenInputDesktop(DWORD dwFlags,
+                           BOOL fInherit,
+                           ACCESS_MASK dwDesiredAccess)
+{
+    PTHREADINFO pti = PsGetCurrentThreadWin32Thread();
+    NTSTATUS Status;
+    ULONG HandleAttributes = 0;
+    HDESK hdesk = NULL;
+
+    if (!gpdeskInputDesktop)
+    {
+        return NULL;
+    }
+
+    if (pti->ppi->prpwinsta != InputWindowStation)
+    {
+        ERR("Tried to open input desktop from non interactive winsta!\n");
+        EngSetLastError(ERROR_INVALID_FUNCTION);
+        return NULL;
+    }
+
+    if (fInherit) HandleAttributes = OBJ_INHERIT;
+
+    /* Create a new handle to the object */
+    Status = ObOpenObjectByPointer(
+                 gpdeskInputDesktop,
+                 HandleAttributes,
+                 NULL,
+                 dwDesiredAccess,
+                 ExDesktopObjectType,
+                 UserMode,
+                 (PHANDLE)&hdesk);
+
+    if (!NT_SUCCESS(Status))
+    {
+        ERR("Failed to open input desktop object\n");
+        SetLastNtError(Status);
+    }
+
+    return hdesk;
+}
+
 /*
  * NtUserOpenInputDesktop
  *
@@ -2597,30 +2639,12 @@ NtUserOpenInputDesktop(
     BOOL fInherit,
     ACCESS_MASK dwDesiredAccess)
 {
-    NTSTATUS Status;
-    HDESK hdesk = NULL;
-    ULONG HandleAttributes = 0;
+    HDESK hdesk;
 
     UserEnterExclusive();
     TRACE("Enter NtUserOpenInputDesktop gpdeskInputDesktop 0x%p\n",gpdeskInputDesktop);
 
-    if (fInherit) HandleAttributes = OBJ_INHERIT;
-
-    /* Create a new handle to the object */
-    Status = ObOpenObjectByPointer(
-                 gpdeskInputDesktop,
-                 HandleAttributes,
-                 NULL,
-                 dwDesiredAccess,
-                 ExDesktopObjectType,
-                 UserMode,
-                 (PHANDLE)&hdesk);
-
-    if (!NT_SUCCESS(Status))
-    {
-        ERR("Failed to open input desktop object\n");
-        SetLastNtError(Status);
-    }
+    hdesk = UserOpenInputDesktop(dwFlags, fInherit, dwDesiredAccess);
 
     TRACE("NtUserOpenInputDesktop returning 0x%p\n",hdesk);
     UserLeave();
index 6f15bb1..8ec595a 100644 (file)
@@ -357,5 +357,6 @@ BOOL FASTCALL IntPaintDesktop(HDC);
 BOOL FASTCALL DesktopWindowProc(PWND, UINT, WPARAM, LPARAM, LRESULT *);
 BOOL FASTCALL UserMessageWindowProc(PWND pwnd, UINT Msg, WPARAM wParam, LPARAM lParam, LRESULT *lResult);
 VOID NTAPI DesktopThreadMain(VOID);
+HDESK UserOpenInputDesktop(DWORD dwFlags, BOOL fInherit, ACCESS_MASK dwDesiredAccess);
 
 /* EOF */
index 63325fe..4556e74 100644 (file)
@@ -521,6 +521,8 @@ IntCreateWindowStation(
           ObjectAttributes->ObjectName, WindowStation, hWinSta);
 
     *phWinSta = hWinSta;
+    EngSetLastError(ERROR_SUCCESS);
+
     return STATUS_SUCCESS;
 }