[WINSRV] Implement SrvGetThreadConsoleDesktop(). CORE-13470
authorHermès Bélusca-Maïto <hermes.belusca-maito@reactos.org>
Sun, 28 Apr 2019 00:40:20 +0000 (02:40 +0200)
committerHermès Bélusca-Maïto <hermes.belusca-maito@reactos.org>
Sun, 28 Apr 2019 00:50:38 +0000 (02:50 +0200)
It retrieves the handle to the desktop assigned to the specified
thread belonging to a console application, which is going to be
where the input thread of this console is.

sdk/include/reactos/subsys/win/winmsg.h
win32ss/user/winsrv/consrv/api.h
win32ss/user/winsrv/consrv/frontendctl.c
win32ss/user/winsrv/consrv/frontends/gui/guiterm.c
win32ss/user/winsrv/consrv/frontends/tui/tuiterm.c
win32ss/user/winsrv/consrv/include/conio_winsrv.h
win32ss/user/winsrv/consrv/include/term.h
win32ss/user/winsrv/usersrv/init.c

index 6134538..e445331 100644 (file)
@@ -68,7 +68,7 @@ typedef struct _USER_LOGON
 typedef struct _USER_GET_THREAD_CONSOLE_DESKTOP
 {
     ULONG_PTR ThreadId;
-    HANDLE ConsoleDesktop;
+    HDESK ConsoleDesktop;
 } USER_GET_THREAD_CONSOLE_DESKTOP, *PUSER_GET_THREAD_CONSOLE_DESKTOP;
 
 typedef struct _USER_REGISTER_SERVICES_PROCESS
index c9e62d4..45aca66 100644 (file)
@@ -78,6 +78,14 @@ CSR_API(SrvShowConsoleCursor);
 CSR_API(SrvSetConsoleCursor);
 CSR_API(SrvConsoleMenuControl);
 CSR_API(SrvSetConsoleMenuClose);
+
+/* Used by USERSRV!SrvGetThreadConsoleDesktop() */
+NTSTATUS
+NTAPI
+GetThreadConsoleDesktop(
+    IN ULONG_PTR ThreadId,
+    OUT HDESK* ConsoleDesktop);
+
 CSR_API(SrvGetConsoleWindow);
 CSR_API(SrvSetConsoleIcon);
 CSR_API(SrvGetConsoleSelectionInfo);
index 9ea1fe9..18ef2ed 100644 (file)
@@ -275,6 +275,42 @@ CSR_API(SrvSetConsoleMenuClose)
     return (Success ? STATUS_SUCCESS : STATUS_UNSUCCESSFUL);
 }
 
+/* Used by USERSRV!SrvGetThreadConsoleDesktop() */
+NTSTATUS
+NTAPI
+GetThreadConsoleDesktop(
+    IN ULONG_PTR ThreadId,
+    OUT HDESK* ConsoleDesktop)
+{
+    NTSTATUS Status;
+    PCSR_THREAD CsrThread;
+    PCONSRV_CONSOLE Console;
+
+    /* No console desktop handle by default */
+    *ConsoleDesktop = NULL;
+
+    /* Retrieve and lock the thread */
+    Status = CsrLockThreadByClientId(ULongToHandle(ThreadId), &CsrThread);
+    if (!NT_SUCCESS(Status))
+        return Status;
+
+    ASSERT(CsrThread->Process);
+
+    /* Retrieve the console to which the process is attached, and unlock the thread */
+    Status = ConSrvGetConsole(ConsoleGetPerProcessData(CsrThread->Process),
+                              &Console, TRUE);
+    CsrUnlockThread(CsrThread);
+
+    if (!NT_SUCCESS(Status))
+        return Status;
+
+    /* Retrieve the console desktop handle, and release the console */
+    *ConsoleDesktop = TermGetThreadConsoleDesktop(Console);
+    ConSrvReleaseConsole(Console, TRUE);
+
+    return STATUS_SUCCESS;
+}
+
 CSR_API(SrvGetConsoleWindow)
 {
     NTSTATUS Status;
index 1468363..2b2e9d4 100644 (file)
@@ -911,6 +911,13 @@ GuiChangeIcon(IN OUT PFRONTEND This,
     return TRUE;
 }
 
+static HDESK NTAPI
+GuiGetThreadConsoleDesktop(IN OUT PFRONTEND This)
+{
+    PGUI_CONSOLE_DATA GuiData = This->Context;
+    return GuiData->Desktop;
+}
+
 static HWND NTAPI
 GuiGetConsoleWindowHandle(IN OUT PFRONTEND This)
 {
@@ -1148,6 +1155,7 @@ static FRONTEND_VTBL GuiVtbl =
     GuiRefreshInternalInfo,
     GuiChangeTitle,
     GuiChangeIcon,
+    GuiGetThreadConsoleDesktop,
     GuiGetConsoleWindowHandle,
     GuiGetLargestConsoleWindowSize,
     GuiGetSelectionInfo,
index e1e96d6..e66203a 100644 (file)
@@ -831,6 +831,13 @@ TuiChangeIcon(IN OUT PFRONTEND This,
     return TRUE;
 }
 
+static HDESK NTAPI
+TuiGetThreadConsoleDesktop(IN OUT PFRONTEND This)
+{
+    // PTUI_CONSOLE_DATA TuiData = This->Context;
+    return NULL;
+}
+
 static HWND NTAPI
 TuiGetConsoleWindowHandle(IN OUT PFRONTEND This)
 {
@@ -920,6 +927,7 @@ static FRONTEND_VTBL TuiVtbl =
     TuiRefreshInternalInfo,
     TuiChangeTitle,
     TuiChangeIcon,
+    TuiGetThreadConsoleDesktop,
     TuiGetConsoleWindowHandle,
     TuiGetLargestConsoleWindowSize,
     TuiGetSelectionInfo,
index 9398065..79e0c04 100644 (file)
@@ -89,6 +89,7 @@ typedef struct _FRONTEND_VTBL
     VOID (NTAPI *ChangeTitle)(IN OUT PFRONTEND This);
     BOOL (NTAPI *ChangeIcon)(IN OUT PFRONTEND This,
                              HICON IconHandle);
+    HDESK (NTAPI *GetThreadConsoleDesktop)(IN OUT PFRONTEND This);
     HWND (NTAPI *GetConsoleWindowHandle)(IN OUT PFRONTEND This);
     VOID (NTAPI *GetLargestConsoleWindowSize)(IN OUT PFRONTEND This,
                                               PCOORD pSize);
index a672499..2766ec8 100644 (file)
@@ -47,6 +47,8 @@
     (Console)->FrontEndIFace.Vtbl->ChangeTitle(&(Console)->FrontEndIFace)
 #define TermChangeIcon(Console, IconHandle) \
     (Console)->FrontEndIFace.Vtbl->ChangeIcon(&(Console)->FrontEndIFace, (IconHandle))
+#define TermGetThreadConsoleDesktop(Console) \
+    (Console)->FrontEndIFace.Vtbl->GetThreadConsoleDesktop(&(Console)->FrontEndIFace)
 #define TermGetConsoleWindowHandle(Console) \
     (Console)->FrontEndIFace.Vtbl->GetConsoleWindowHandle(&(Console)->FrontEndIFace)
 #define TermGetSelectionInfo(Console, pSelectionInfo) \
index 2e65099..f3c5be8 100644 (file)
@@ -10,8 +10,8 @@
 /* INCLUDES *******************************************************************/
 
 #include "usersrv.h"
-
-#include "api.h"
+#include "api.h"            // USERSRV Public server APIs definitions
+#include "../consrv/api.h"  //  CONSRV Public server APIs definitions
 
 #define NDEBUG
 #include <debug.h>
@@ -138,14 +138,18 @@ CSR_API(SrvActivateDebugger)
 
 CSR_API(SrvGetThreadConsoleDesktop)
 {
+    NTSTATUS Status;
     PUSER_GET_THREAD_CONSOLE_DESKTOP GetThreadConsoleDesktopRequest = &((PUSER_API_MESSAGE)ApiMessage)->Data.GetThreadConsoleDesktopRequest;
 
-    UNIMPLEMENTED_ONCE;
-
-    /* Return nothing for the moment... */
-    GetThreadConsoleDesktopRequest->ConsoleDesktop = NULL;
+    Status = GetThreadConsoleDesktop(GetThreadConsoleDesktopRequest->ThreadId,
+                                     &GetThreadConsoleDesktopRequest->ConsoleDesktop);
+    if (!NT_SUCCESS(Status))
+    {
+        DPRINT1("GetThreadConsoleDesktop(%lu) failed with Status 0x%08x\n",
+                GetThreadConsoleDesktopRequest->ThreadId, Status);
+    }
 
-    /* Always succeeds */
+    /* Windows-compatibility: Always return success since User32 relies on this! */
     return STATUS_SUCCESS;
 }