[KERNEL32] Update some console API prototypes.
[reactos.git] / dll / win32 / kernel32 / client / console / console.c
index 857a30a..6547c0c 100644 (file)
@@ -3,8 +3,8 @@
  * PROJECT:         ReactOS system libraries
  * FILE:            dll/win32/kernel32/client/console/console.c
  * PURPOSE:         Win32 server console functions
- * PROGRAMMERS:     James Tabor
- *                  <jimtabor@adsl-64-217-116-74.dsl.hstntx.swbell.net>
+ * PROGRAMMERS:     James Tabor <jimtabor@adsl-64-217-116-74.dsl.hstntx.swbell.net>
+ *                  Hermes Belusca-Maito (hermes.belusca@sfr.fr)
  */
 
 /* INCLUDES *******************************************************************/
 /* GLOBALS ********************************************************************/
 
 extern RTL_CRITICAL_SECTION ConsoleLock;
-extern BOOL ConsoleInitialized;
-extern BOOL WINAPI IsDebuggerPresent(VOID);
+extern BOOLEAN ConsoleInitialized;
 
 /* Console reserved "file" names */
 static LPCWSTR BaseConFileName       = CONSOLE_FILE_NAME;
 static LPCWSTR BaseConInputFileName  = CONSOLE_INPUT_FILE_NAME;
 static LPCWSTR BaseConOutputFileName = CONSOLE_OUTPUT_FILE_NAME;
 
-PHANDLER_ROUTINE InitialHandler[1];
-PHANDLER_ROUTINE* CtrlHandlers;
-ULONG NrCtrlHandlers;
-ULONG NrAllocatedHandlers;
-BOOL LastCloseNotify = FALSE;
+/* Console Control handling */
+static PHANDLER_ROUTINE InitialHandler[1];
+static PHANDLER_ROUTINE* CtrlHandlers;
+static ULONG NrCtrlHandlers;
+static ULONG NrAllocatedHandlers;
+static BOOLEAN LastCloseNotify = FALSE;
+
+extern BOOL WINAPI IsDebuggerPresent(VOID);
 
+/* Console Input facilities */
 HANDLE InputWaitHandle = INVALID_HANDLE_VALUE;
 
-#define INPUTEXENAME_BUFLEN 256
-static WCHAR InputExeName[INPUTEXENAME_BUFLEN];
+#define EXENAME_LENGTH 255 + 1
+static RTL_CRITICAL_SECTION ExeNameLock;
+static BOOLEAN ExeNameInitialized;
+static WCHAR ExeNameBuffer[EXENAME_LENGTH]; // NULL-terminated
+static USHORT ExeNameLength;    // Count in number of characters without NULL
+static WCHAR StartDirBuffer[MAX_PATH + 1];  // NULL-terminated
+static USHORT StartDirLength;   // Count in number of characters without NULL
 
 
 /* Default Console Control Handler ********************************************/
 
-BOOL
+static BOOL
 WINAPI
 DefaultConsoleCtrlHandler(DWORD Event)
 {
@@ -193,8 +201,7 @@ ConsoleControlDispatcher(IN LPVOID lpThreadParameter)
 }
 
 VOID
-WINAPI
-InitConsoleCtrlHandling(VOID)
+InitializeCtrlHandling(VOID)
 {
     /* Initialize Console Ctrl Handler */
     NrAllocatedHandlers = NrCtrlHandlers = 1;
@@ -203,6 +210,135 @@ InitConsoleCtrlHandling(VOID)
 }
 
 
+/* Input EXE Name Support *****************************************************/
+
+VOID
+InitExeName(VOID)
+{
+    NTSTATUS Status;
+    PPEB Peb = NtCurrentPeb();
+    PCURDIR CurrentDirectory = &Peb->ProcessParameters->CurrentDirectory;
+    PLDR_DATA_TABLE_ENTRY ImageEntry;
+
+    if (ExeNameInitialized) return;
+
+    /* Initialize the EXE name lock */
+    Status = RtlInitializeCriticalSection(&ExeNameLock);
+    if (!NT_SUCCESS(Status)) return;
+    ExeNameInitialized = TRUE;
+
+    ImageEntry = CONTAINING_RECORD(Peb->Ldr->InLoadOrderModuleList.Flink,
+                                   LDR_DATA_TABLE_ENTRY,
+                                   InLoadOrderLinks);
+
+    /* Retrieve the EXE name, NULL-terminate it... */
+    ExeNameLength = min(sizeof(ExeNameBuffer)/sizeof(ExeNameBuffer[0]),
+                        ImageEntry->BaseDllName.Length / sizeof(WCHAR));
+    RtlCopyMemory(ExeNameBuffer,
+                  ImageEntry->BaseDllName.Buffer,
+                  ImageEntry->BaseDllName.Length);
+    ExeNameBuffer[ExeNameLength] = UNICODE_NULL;
+
+    /* ... and retrieve the current directory path and NULL-terminate it. */
+    StartDirLength = min(sizeof(StartDirBuffer)/sizeof(StartDirBuffer[0]),
+                         CurrentDirectory->DosPath.Length / sizeof(WCHAR));
+    RtlCopyMemory(StartDirBuffer,
+                  CurrentDirectory->DosPath.Buffer,
+                  CurrentDirectory->DosPath.Length);
+    StartDirBuffer[StartDirLength] = UNICODE_NULL;
+}
+
+/*
+ * NOTE:
+ * The "LPDWORD Length" parameters point on input to the maximum size of
+ * the buffers that can hold data (if != 0), and on output they hold the
+ * real size of the data. If "Length" are == 0 on input, then on output
+ * they receive the full size of the data.
+ * The "LPWSTR* String" parameters have a double meaning:
+ * - when "CaptureStrings" is TRUE, data is copied to the buffers pointed
+ *   by the pointers (*String).
+ * - when "CaptureStrings" is FALSE, "*String" are set to the addresses of
+ *   the source data.
+ */
+VOID
+SetUpAppName(IN BOOLEAN CaptureStrings,
+             IN OUT LPDWORD CurDirLength,
+             IN OUT LPWSTR* CurDir,
+             IN OUT LPDWORD AppNameLength,
+             IN OUT LPWSTR* AppName)
+{
+    DWORD Length;
+
+    /* Retrieve the needed buffer size */
+    Length = (StartDirLength + 1) * sizeof(WCHAR);
+    if (*CurDirLength > 0) Length = min(Length, *CurDirLength);
+    *CurDirLength = Length;
+
+    /* Capture the data if needed, or, return a pointer to it */
+    if (CaptureStrings)
+    {
+        /*
+         * Length is always >= sizeof(WCHAR). Copy everything but the
+         * possible trailing NULL character, and then NULL-terminate.
+         */
+        Length -= sizeof(WCHAR);
+        RtlCopyMemory(*CurDir, StartDirBuffer, Length);
+        (*CurDir)[Length / sizeof(WCHAR)] = UNICODE_NULL;
+    }
+    else
+    {
+        *CurDir = StartDirBuffer;
+    }
+
+    /* Retrieve the needed buffer size */
+    Length = (ExeNameLength + 1) * sizeof(WCHAR);
+    if (*AppNameLength > 0) Length = min(Length, *AppNameLength);
+    *AppNameLength = Length;
+
+    /* Capture the data if needed, or, return a pointer to it */
+    if (CaptureStrings)
+    {
+        /*
+         * Length is always >= sizeof(WCHAR). Copy everything but the
+         * possible trailing NULL character, and then NULL-terminate.
+         */
+        Length -= sizeof(WCHAR);
+        RtlCopyMemory(*AppName, ExeNameBuffer, Length);
+        (*AppName)[Length / sizeof(WCHAR)] = UNICODE_NULL;
+    }
+    else
+    {
+        *AppName = ExeNameBuffer;
+    }
+}
+
+USHORT
+GetCurrentExeName(OUT PWCHAR ExeName,
+                  IN USHORT BufferSize)
+{
+    USHORT ExeLength;
+
+    if (ExeNameInitialized)
+    {
+        RtlEnterCriticalSection(&ExeNameLock);
+
+        if (BufferSize > ExeNameLength * sizeof(WCHAR))
+            BufferSize = ExeNameLength * sizeof(WCHAR);
+
+        RtlCopyMemory(ExeName, ExeNameBuffer, BufferSize);
+
+        RtlLeaveCriticalSection(&ExeNameLock);
+        ExeLength = BufferSize;
+    }
+    else
+    {
+        *ExeName  = UNICODE_NULL;
+        ExeLength = 0;
+    }
+
+    return ExeLength;
+}
+
 /* FUNCTIONS ******************************************************************/
 
 LPCWSTR
@@ -256,6 +392,7 @@ IntCheckForConsoleFileName(IN LPCWSTR pszName,
  */
 HMENU
 WINAPI
+DECLSPEC_HOTPATCH
 ConsoleMenuControl(HANDLE hConsoleOutput,
                    DWORD dwCmdIdLow,
                    DWORD dwCmdIdHigh)
@@ -283,6 +420,7 @@ ConsoleMenuControl(HANDLE hConsoleOutput,
  */
 HANDLE
 WINAPI
+DECLSPEC_HOTPATCH
 DuplicateConsoleHandle(HANDLE hConsole,
                        DWORD dwDesiredAccess,
                        BOOL bInheritHandle,
@@ -415,33 +553,85 @@ GetConsoleDisplayMode(LPDWORD lpModeFlags)
 
 
 /*
- * @unimplemented (Undocumented)
+ * @implemented (Undocumented)
+ * @note See http://cboard.cprogramming.com/windows-programming/102187-console-font-size.html
  */
 DWORD
 WINAPI
-GetConsoleFontInfo(HANDLE hConsoleOutput,
-                   BOOL bMaximumWindow,
-                   DWORD nFontCount,
-                   PCONSOLE_FONT_INFO lpConsoleFontInfo)
+GetConsoleFontInfo(IN HANDLE hConsoleOutput,
+                   IN BOOL bMaximumWindow,
+                   IN DWORD nFontCount,
+                   OUT PCONSOLE_FONT_INFO lpConsoleFontInfo)
 {
-    DPRINT1("GetConsoleFontInfo(0x%p, %d, %lu, 0x%p) UNIMPLEMENTED!\n", hConsoleOutput, bMaximumWindow, nFontCount, lpConsoleFontInfo);
-    SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
-    return 0;
+    CONSOLE_API_MESSAGE ApiMessage;
+    PCONSOLE_GETFONTINFO GetFontInfoRequest = &ApiMessage.Data.GetFontInfoRequest;
+    PCSR_CAPTURE_BUFFER CaptureBuffer;
+
+    GetFontInfoRequest->ConsoleHandle = NtCurrentPeb()->ProcessParameters->ConsoleHandle;
+    GetFontInfoRequest->OutputHandle  = hConsoleOutput;
+    GetFontInfoRequest->MaximumWindow = bMaximumWindow;
+    GetFontInfoRequest->NumFonts      = nFontCount;
+
+    CaptureBuffer = CsrAllocateCaptureBuffer(1, nFontCount * sizeof(CONSOLE_FONT_INFO));
+    if (CaptureBuffer == NULL)
+    {
+        DPRINT1("CsrAllocateCaptureBuffer failed!\n");
+        SetLastError(ERROR_NOT_ENOUGH_MEMORY);
+        return 0;
+    }
+
+    CsrAllocateMessagePointer(CaptureBuffer,
+                              nFontCount * sizeof(CONSOLE_FONT_INFO),
+                              (PVOID*)&GetFontInfoRequest->FontInfo);
+
+    CsrClientCallServer((PCSR_API_MESSAGE)&ApiMessage,
+                        CaptureBuffer,
+                        CSR_CREATE_API_NUMBER(CONSRV_SERVERDLL_INDEX, ConsolepGetFontInfo),
+                        sizeof(*GetFontInfoRequest));
+    if (!NT_SUCCESS(ApiMessage.Status))
+    {
+        BaseSetLastNTError(ApiMessage.Status);
+    }
+    else
+    {
+        RtlCopyMemory(lpConsoleFontInfo,
+                      GetFontInfoRequest->FontInfo,
+                      GetFontInfoRequest->NumFonts * sizeof(CONSOLE_FONT_INFO));
+    }
+
+    CsrFreeCaptureBuffer(CaptureBuffer);
+    return GetFontInfoRequest->NumFonts;
 }
 
 
 /*
- * @unimplemented
+ * @implemented
  */
 COORD
 WINAPI
-GetConsoleFontSize(HANDLE hConsoleOutput,
-                   DWORD nFont)
+DECLSPEC_HOTPATCH
+GetConsoleFontSize(IN HANDLE hConsoleOutput,
+                   IN DWORD nFont)
 {
+    CONSOLE_API_MESSAGE ApiMessage;
+    PCONSOLE_GETFONTSIZE GetFontSizeRequest = &ApiMessage.Data.GetFontSizeRequest;
     COORD Empty = {0, 0};
-    DPRINT1("GetConsoleFontSize(0x%p, 0x%x) UNIMPLEMENTED!\n", hConsoleOutput, nFont);
-    SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
-    return Empty;
+
+    GetFontSizeRequest->ConsoleHandle = NtCurrentPeb()->ProcessParameters->ConsoleHandle;
+    GetFontSizeRequest->OutputHandle  = hConsoleOutput;
+    GetFontSizeRequest->FontIndex     = nFont;
+
+    CsrClientCallServer((PCSR_API_MESSAGE)&ApiMessage,
+                        NULL,
+                        CSR_CREATE_API_NUMBER(CONSRV_SERVERDLL_INDEX, ConsolepGetFontSize),
+                        sizeof(*GetFontSizeRequest));
+    if (!NT_SUCCESS(ApiMessage.Status))
+    {
+        BaseSetLastNTError(ApiMessage.Status);
+        return Empty;
+    }
+
+    return GetFontSizeRequest->FontSize;
 }
 
 
@@ -497,30 +687,63 @@ GetConsoleInputWaitHandle(VOID)
 
 
 /*
- * @unimplemented
+ * @implemented
  */
 BOOL
 WINAPI
-GetCurrentConsoleFont(HANDLE hConsoleOutput,
-                      BOOL bMaximumWindow,
-                      PCONSOLE_FONT_INFO lpConsoleCurrentFont)
+GetCurrentConsoleFont(IN HANDLE hConsoleOutput,
+                      IN BOOL bMaximumWindow,
+                      OUT PCONSOLE_FONT_INFO lpConsoleCurrentFont)
 {
-    DPRINT1("GetCurrentConsoleFont(0x%p, 0x%x, 0x%p) UNIMPLEMENTED!\n", hConsoleOutput, bMaximumWindow, lpConsoleCurrentFont);
-    SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
-    return 0;
+    CONSOLE_API_MESSAGE ApiMessage;
+    PCONSOLE_GETCURRENTFONT GetCurrentFontRequest = &ApiMessage.Data.GetCurrentFontRequest;
+
+    GetCurrentFontRequest->ConsoleHandle = NtCurrentPeb()->ProcessParameters->ConsoleHandle;
+    GetCurrentFontRequest->OutputHandle  = hConsoleOutput;
+    GetCurrentFontRequest->MaximumWindow = bMaximumWindow;
+
+    CsrClientCallServer((PCSR_API_MESSAGE)&ApiMessage,
+                        NULL,
+                        CSR_CREATE_API_NUMBER(CONSRV_SERVERDLL_INDEX, ConsolepGetCurrentFont),
+                        sizeof(*GetCurrentFontRequest));
+    if (!NT_SUCCESS(ApiMessage.Status))
+    {
+        BaseSetLastNTError(ApiMessage.Status);
+        return FALSE;
+    }
+
+    lpConsoleCurrentFont->dwFontSize = GetCurrentFontRequest->FontSize;
+    lpConsoleCurrentFont->nFont      = GetCurrentFontRequest->FontIndex;
+
+    return TRUE;
 }
 
 
 /*
- * @unimplemented (Undocumented)
+ * @implemented (Undocumented)
+ * @note See http://cboard.cprogramming.com/windows-programming/102187-console-font-size.html
  */
-ULONG
+DWORD
 WINAPI
+DECLSPEC_HOTPATCH
 GetNumberOfConsoleFonts(VOID)
 {
-    DPRINT1("GetNumberOfConsoleFonts() UNIMPLEMENTED!\n");
-    SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
-    return 1;
+    CONSOLE_API_MESSAGE ApiMessage;
+    PCONSOLE_GETNUMFONTS GetNumFontsRequest = &ApiMessage.Data.GetNumFontsRequest;
+
+    GetNumFontsRequest->ConsoleHandle = NtCurrentPeb()->ProcessParameters->ConsoleHandle;
+
+    CsrClientCallServer((PCSR_API_MESSAGE)&ApiMessage,
+                        NULL,
+                        CSR_CREATE_API_NUMBER(CONSRV_SERVERDLL_INDEX, ConsolepGetNumberOfFonts),
+                        sizeof(*GetNumFontsRequest));
+    if (!NT_SUCCESS(ApiMessage.Status))
+    {
+        BaseSetLastNTError(ApiMessage.Status);
+        return 0;
+    }
+
+    return GetNumFontsRequest->NumFonts;
 }
 
 
@@ -561,7 +784,7 @@ InvalidateConsoleDIBits(IN HANDLE hConsoleOutput,
 
 
 /*
- * @unimplemented (Undocumented)
+ * @implemented (Undocumented)
  */
 HANDLE
 WINAPI
@@ -621,6 +844,7 @@ OpenConsoleW(LPCWSTR wsName,
  */
 BOOL
 WINAPI
+DECLSPEC_HOTPATCH
 SetConsoleCursor(HANDLE  hConsoleOutput,
                  HCURSOR hCursor)
 {
@@ -682,16 +906,33 @@ SetConsoleDisplayMode(HANDLE hConsoleOutput,
 
 
 /*
- * @unimplemented (Undocumented)
+ * @implemented (Undocumented)
+ * @note See http://cboard.cprogramming.com/windows-programming/102187-console-font-size.html
  */
 BOOL
 WINAPI
-SetConsoleFont(HANDLE hConsoleOutput,
-               DWORD nFont)
+DECLSPEC_HOTPATCH
+SetConsoleFont(IN HANDLE hConsoleOutput,
+               IN DWORD nFont)
 {
-    DPRINT1("SetConsoleFont(0x%p, %lu) UNIMPLEMENTED!\n", hConsoleOutput, nFont);
-    SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
-    return FALSE;
+    CONSOLE_API_MESSAGE ApiMessage;
+    PCONSOLE_SETFONT SetFontRequest = &ApiMessage.Data.SetFontRequest;
+
+    SetFontRequest->ConsoleHandle = NtCurrentPeb()->ProcessParameters->ConsoleHandle;
+    SetFontRequest->OutputHandle  = hConsoleOutput;
+    SetFontRequest->FontIndex     = nFont;
+
+    CsrClientCallServer((PCSR_API_MESSAGE)&ApiMessage,
+                        NULL,
+                        CSR_CREATE_API_NUMBER(CONSRV_SERVERDLL_INDEX, ConsolepSetFont),
+                        sizeof(*SetFontRequest));
+    if (!NT_SUCCESS(ApiMessage.Status))
+    {
+        BaseSetLastNTError(ApiMessage.Status);
+        return FALSE;
+    }
+
+    return TRUE;
 }
 
 
@@ -733,6 +974,7 @@ SetConsoleHardwareState(HANDLE hConsoleOutput,
  */
 BOOL
 WINAPI
+DECLSPEC_HOTPATCH
 SetConsoleKeyShortcuts(DWORD Unknown0,
                        DWORD Unknown1,
                        DWORD Unknown2,
@@ -765,6 +1007,7 @@ SetConsoleMaximumWindowSize(HANDLE hConsoleOutput,
  */
 BOOL
 WINAPI
+DECLSPEC_HOTPATCH
 SetConsoleMenuClose(BOOL bEnable)
 {
     CONSOLE_API_MESSAGE ApiMessage;
@@ -794,6 +1037,7 @@ SetConsoleMenuClose(BOOL bEnable)
  */
 BOOL
 WINAPI
+DECLSPEC_HOTPATCH
 SetConsolePalette(HANDLE hConsoleOutput,
                   HPALETTE hPalette,
                   UINT dwUsage)
@@ -825,6 +1069,7 @@ SetConsolePalette(HANDLE hConsoleOutput,
  */
 INT
 WINAPI
+DECLSPEC_HOTPATCH
 ShowConsoleCursor(HANDLE hConsoleOutput,
                   BOOL bShow)
 {
@@ -861,6 +1106,7 @@ ShowConsoleCursor(HANDLE hConsoleOutput,
  */
 BOOL
 WINAPI
+DECLSPEC_HOTPATCH
 VerifyConsoleIoHandle(HANDLE hIoHandle)
 {
     CONSOLE_API_MESSAGE ApiMessage;
@@ -892,6 +1138,7 @@ VerifyConsoleIoHandle(HANDLE hIoHandle)
  */
 BOOL
 WINAPI
+DECLSPEC_HOTPATCH
 CloseConsoleHandle(HANDLE hHandle)
 {
     CONSOLE_API_MESSAGE ApiMessage;
@@ -919,6 +1166,7 @@ CloseConsoleHandle(HANDLE hHandle)
  */
 HANDLE
 WINAPI
+DECLSPEC_HOTPATCH
 GetStdHandle(DWORD nStdHandle)
 /*
  * FUNCTION: Get a handle for the standard input, standard output
@@ -961,6 +1209,7 @@ GetStdHandle(DWORD nStdHandle)
  */
 BOOL
 WINAPI
+DECLSPEC_HOTPATCH
 SetStdHandle(DWORD  nStdHandle,
              HANDLE hHandle)
 /*
@@ -999,111 +1248,249 @@ SetStdHandle(DWORD  nStdHandle,
 }
 
 
-/*--------------------------------------------------------------
- *    AllocConsole
- *
+/*
  * @implemented
  */
-BOOL
-WINAPI
-AllocConsole(VOID)
-{
+static BOOL
+IntAllocConsole(LPWSTR Title,
+                DWORD TitleLength,
+                LPWSTR Desktop,
+                DWORD DesktopLength,
+                LPWSTR CurDir,
+                DWORD CurDirLength,
+                LPWSTR AppName,
+                DWORD AppNameLength,
+                LPTHREAD_START_ROUTINE CtrlRoutine,
+                LPTHREAD_START_ROUTINE PropRoutine,
+                PCONSOLE_START_INFO ConsoleStartInfo)
+{
+    BOOL Success = TRUE;
     NTSTATUS Status;
-    PRTL_USER_PROCESS_PARAMETERS Parameters = NtCurrentPeb()->ProcessParameters;
+
     CONSOLE_API_MESSAGE ApiMessage;
     PCONSOLE_ALLOCCONSOLE AllocConsoleRequest = &ApiMessage.Data.AllocConsoleRequest;
     PCSR_CAPTURE_BUFFER CaptureBuffer;
 
-    if (Parameters->ConsoleHandle)
-    {
-        DPRINT1("AllocConsole: Allocating a console to a process already having one\n");
-        SetLastError(ERROR_ACCESS_DENIED);
-        return FALSE;
-    }
+    AllocConsoleRequest->CtrlRoutine = CtrlRoutine;
+    AllocConsoleRequest->PropRoutine = PropRoutine;
 
-    CaptureBuffer = CsrAllocateCaptureBuffer(1, sizeof(CONSOLE_START_INFO));
+    CaptureBuffer = CsrAllocateCaptureBuffer(5, TitleLength   +
+                                                DesktopLength +
+                                                CurDirLength  +
+                                                AppNameLength +
+                                                sizeof(CONSOLE_START_INFO));
     if (CaptureBuffer == NULL)
     {
-        DPRINT1("CsrAllocateCaptureBuffer failed!\n");
         SetLastError(ERROR_NOT_ENOUGH_MEMORY);
-        return FALSE;
+        Success = FALSE;
+        goto Quit;
     }
 
-    CsrAllocateMessagePointer(CaptureBuffer,
-                              sizeof(CONSOLE_START_INFO),
-                              (PVOID*)&AllocConsoleRequest->ConsoleStartInfo);
+    CsrCaptureMessageBuffer(CaptureBuffer,
+                            ConsoleStartInfo,
+                            sizeof(CONSOLE_START_INFO),
+                            (PVOID*)&AllocConsoleRequest->ConsoleStartInfo);
 
-    InitConsoleInfo(AllocConsoleRequest->ConsoleStartInfo,
-                    &Parameters->ImagePathName);
+    AllocConsoleRequest->TitleLength = TitleLength;
+    CsrCaptureMessageBuffer(CaptureBuffer,
+                            Title,
+                            TitleLength,
+                            (PVOID*)&AllocConsoleRequest->ConsoleTitle);
 
-    AllocConsoleRequest->ConsoleHandle  = NULL;
-    AllocConsoleRequest->CtrlDispatcher = ConsoleControlDispatcher;
-    AllocConsoleRequest->PropDispatcher = PropDialogHandler;
+    AllocConsoleRequest->DesktopLength = DesktopLength;
+    CsrCaptureMessageBuffer(CaptureBuffer,
+                            Desktop,
+                            DesktopLength,
+                            (PVOID*)&AllocConsoleRequest->Desktop);
 
-    Status = CsrClientCallServer((PCSR_API_MESSAGE)&ApiMessage,
-                                 CaptureBuffer,
-                                 CSR_CREATE_API_NUMBER(CONSRV_SERVERDLL_INDEX, ConsolepAlloc),
-                                 sizeof(CONSOLE_ALLOCCONSOLE));
+    AllocConsoleRequest->CurDirLength = CurDirLength;
+    CsrCaptureMessageBuffer(CaptureBuffer,
+                            CurDir,
+                            CurDirLength,
+                            (PVOID*)&AllocConsoleRequest->CurDir);
 
-    CsrFreeCaptureBuffer(CaptureBuffer);
+    AllocConsoleRequest->AppNameLength = AppNameLength;
+    CsrCaptureMessageBuffer(CaptureBuffer,
+                            AppName,
+                            AppNameLength,
+                            (PVOID*)&AllocConsoleRequest->AppName);
 
+    CsrClientCallServer((PCSR_API_MESSAGE)&ApiMessage,
+                        CaptureBuffer,
+                        CSR_CREATE_API_NUMBER(CONSRV_SERVERDLL_INDEX, ConsolepAlloc),
+                        sizeof(*AllocConsoleRequest));
+    if (!NT_SUCCESS(ApiMessage.Status))
+    {
+        BaseSetLastNTError(ApiMessage.Status);
+        Success = FALSE;
+        goto Quit;
+    }
+
+    // Is AllocConsoleRequest->ConsoleStartInfo->InitEvents aligned on handle boundary ????
+    Status = NtWaitForMultipleObjects(MAX_INIT_EVENTS,
+                                      AllocConsoleRequest->ConsoleStartInfo->InitEvents,
+                                      WaitAny, FALSE, NULL);
     if (!NT_SUCCESS(Status))
     {
         BaseSetLastNTError(Status);
-        return FALSE;
+        Success = FALSE;
+        goto Quit;
     }
 
-    Parameters->ConsoleHandle = AllocConsoleRequest->ConsoleHandle;
-    SetStdHandle(STD_INPUT_HANDLE , AllocConsoleRequest->InputHandle );
-    SetStdHandle(STD_OUTPUT_HANDLE, AllocConsoleRequest->OutputHandle);
-    SetStdHandle(STD_ERROR_HANDLE , AllocConsoleRequest->ErrorHandle );
+    NtClose(AllocConsoleRequest->ConsoleStartInfo->InitEvents[INIT_SUCCESS]);
+    NtClose(AllocConsoleRequest->ConsoleStartInfo->InitEvents[INIT_FAILURE]);
+    if (Status != INIT_SUCCESS)
+    {
+        NtCurrentPeb()->ProcessParameters->ConsoleHandle = NULL;
+        Success = FALSE;
+    }
+    else
+    {
+        RtlCopyMemory(ConsoleStartInfo,
+                      AllocConsoleRequest->ConsoleStartInfo,
+                      sizeof(CONSOLE_START_INFO));
+        Success = TRUE;
+    }
 
-    /* Initialize Console Ctrl Handler */
-    InitConsoleCtrlHandling();
+Quit:
+    if (CaptureBuffer) CsrFreeCaptureBuffer(CaptureBuffer);
+    return Success;
+}
 
-    InputWaitHandle = AllocConsoleRequest->InputWaitHandle;
+BOOL
+WINAPI
+DECLSPEC_HOTPATCH
+AllocConsole(VOID)
+{
+    BOOL Success;
+    CONSOLE_START_INFO ConsoleStartInfo;
 
-    return TRUE;
+    PWCHAR ConsoleTitle;
+    PWCHAR Desktop;
+    PWCHAR AppName;
+    PWCHAR CurDir;
+
+    ULONG TitleLength   = (MAX_PATH + 1) * sizeof(WCHAR);
+    ULONG DesktopLength = (MAX_PATH + 1) * sizeof(WCHAR);
+    ULONG AppNameLength = 128 * sizeof(WCHAR);
+    ULONG CurDirLength  = (MAX_PATH + 1) * sizeof(WCHAR);
+
+    LCID lcid;
+
+    RtlEnterCriticalSection(&ConsoleLock);
+
+    if (NtCurrentPeb()->ProcessParameters->ConsoleHandle)
+    {
+        DPRINT1("AllocConsole: Allocating a console to a process already having one\n");
+        SetLastError(ERROR_ACCESS_DENIED);
+        Success = FALSE;
+        goto Quit;
+    }
+
+    /* Set up the console properties */
+    SetUpConsoleInfo(FALSE,
+                     &TitleLength,
+                     &ConsoleTitle,
+                     &DesktopLength,
+                     &Desktop,
+                     &ConsoleStartInfo);
+    DPRINT("ConsoleTitle = '%S' - Desktop = '%S'\n",
+           ConsoleTitle, Desktop);
+
+    /* Initialize the Input EXE name */
+    InitExeName();
+    SetUpAppName(FALSE,
+                 &CurDirLength,
+                 &CurDir,
+                 &AppNameLength,
+                 &AppName);
+    DPRINT("CurDir = '%S' - AppName = '%S'\n",
+           CurDir, AppName);
+
+    Success = IntAllocConsole(ConsoleTitle,
+                              TitleLength,
+                              Desktop,
+                              DesktopLength,
+                              CurDir,
+                              CurDirLength,
+                              AppName,
+                              AppNameLength,
+                              ConsoleControlDispatcher,
+                              PropDialogHandler,
+                              &ConsoleStartInfo);
+    if (Success)
+    {
+        /* Set up the handles */
+        SetUpHandles(&ConsoleStartInfo);
+        InputWaitHandle = ConsoleStartInfo.InputWaitHandle;
+
+        /* Initialize Console Ctrl Handling */
+        InitializeCtrlHandling();
+
+        /* Sets the current console locale for this thread */
+        SetTEBLangID(lcid);
+    }
+
+Quit:
+    RtlLeaveCriticalSection(&ConsoleLock);
+    return Success;
 }
 
 
-/*--------------------------------------------------------------
- *    FreeConsole
- *
+/*
  * @implemented
  */
 BOOL
 WINAPI
+DECLSPEC_HOTPATCH
 FreeConsole(VOID)
 {
-    // AG: I'm not sure if this is correct (what happens to std handles?)
-    // but I just tried to reverse what AllocConsole() does...
-
-    NTSTATUS Status;
+    BOOL Success = TRUE;
     CONSOLE_API_MESSAGE ApiMessage;
+    PCONSOLE_FREECONSOLE FreeConsoleRequest = &ApiMessage.Data.FreeConsoleRequest;
+    HANDLE ConsoleHandle = NtCurrentPeb()->ProcessParameters->ConsoleHandle;
 
-    Status = CsrClientCallServer((PCSR_API_MESSAGE)&ApiMessage,
-                                 NULL,
-                                 CSR_CREATE_API_NUMBER(CONSRV_SERVERDLL_INDEX, ConsolepFree),
-                                 sizeof(CONSOLE_FREECONSOLE));
-    if (!NT_SUCCESS(Status))
+    RtlEnterCriticalSection(&ConsoleLock);
+
+    /* We must have a non-trivial handle to close */
+    if (ConsoleHandle == NULL) // IsConsoleHandle(ConsoleHandle)
     {
-        BaseSetLastNTError(Status);
-        return FALSE;
+        SetLastError(ERROR_INVALID_PARAMETER);
+        Success = FALSE;
+        goto Quit;
+    }
+
+    /* Set up the data to send to the Console Server */
+    FreeConsoleRequest->ConsoleHandle = ConsoleHandle;
+
+    /* Call the server */
+    CsrClientCallServer((PCSR_API_MESSAGE)&ApiMessage,
+                        NULL,
+                        CSR_CREATE_API_NUMBER(CONSRV_SERVERDLL_INDEX, ConsolepFree),
+                        sizeof(*FreeConsoleRequest));
+
+    /* Check for success */
+    if (!NT_SUCCESS(ApiMessage.Status))
+    {
+        BaseSetLastNTError(ApiMessage.Status);
+        Success = FALSE;
+        goto Quit;
     }
 
+    /* Reset the console handle */
     NtCurrentPeb()->ProcessParameters->ConsoleHandle = NULL;
 
+    /* Close the associated input handle */
     CloseHandle(InputWaitHandle);
     InputWaitHandle = INVALID_HANDLE_VALUE;
 
-    return TRUE;
+Quit:
+    RtlLeaveCriticalSection(&ConsoleLock);
+    return Success;
 }
 
 
-/*--------------------------------------------------------------
- *    GetConsoleScreenBufferInfo
- *
+/*
  * @implemented
  */
 BOOL
@@ -1146,13 +1533,12 @@ GetConsoleScreenBufferInfo(HANDLE hConsoleOutput,
 }
 
 
-/*--------------------------------------------------------------
- *    SetConsoleCursorPosition
- *
+/*
  * @implemented
  */
 BOOL
 WINAPI
+DECLSPEC_HOTPATCH
 SetConsoleCursorPosition(HANDLE hConsoleOutput,
                          COORD dwCursorPosition)
 {
@@ -1177,9 +1563,7 @@ SetConsoleCursorPosition(HANDLE hConsoleOutput,
 }
 
 
-/*--------------------------------------------------------------
- *     GetConsoleMode
- *
+/*
  * @implemented
  */
 BOOL
@@ -1215,13 +1599,12 @@ GetConsoleMode(HANDLE hConsoleHandle,
 }
 
 
-/*--------------------------------------------------------------
- *     SetConsoleMode
- *
+/*
  * @implemented
  */
 BOOL
 WINAPI
+DECLSPEC_HOTPATCH
 SetConsoleMode(HANDLE hConsoleHandle,
                DWORD dwMode)
 {
@@ -1246,9 +1629,7 @@ SetConsoleMode(HANDLE hConsoleHandle,
 }
 
 
-/*--------------------------------------------------------------
- *     GetNumberOfConsoleInputEvents
- *
+/*
  * @implemented
  */
 BOOL
@@ -1285,13 +1666,12 @@ GetNumberOfConsoleInputEvents(HANDLE hConsoleInput,
 }
 
 
-/*--------------------------------------------------------------
- *     GetLargestConsoleWindowSize
- *
+/*
  * @implemented
  */
 COORD
 WINAPI
+DECLSPEC_HOTPATCH
 GetLargestConsoleWindowSize(HANDLE hConsoleOutput)
 {
     CONSOLE_API_MESSAGE ApiMessage;
@@ -1311,14 +1691,12 @@ GetLargestConsoleWindowSize(HANDLE hConsoleOutput)
         BaseSetLastNTError(ApiMessage.Status);
     }
 
-    DPRINT1("GetLargestConsoleWindowSize, X = %d, Y = %d\n", GetLargestWindowSizeRequest->Size.X, GetLargestWindowSizeRequest->Size.Y);
+    DPRINT("GetLargestConsoleWindowSize, X = %d, Y = %d\n", GetLargestWindowSizeRequest->Size.X, GetLargestWindowSizeRequest->Size.Y);
     return GetLargestWindowSizeRequest->Size;
 }
 
 
-/*--------------------------------------------------------------
- *    GetConsoleCursorInfo
- *
+/*
  * @implemented
  */
 BOOL
@@ -1358,9 +1736,7 @@ GetConsoleCursorInfo(HANDLE hConsoleOutput,
 }
 
 
-/*--------------------------------------------------------------
- *     SetConsoleCursorInfo
- *
+/*
  * @implemented
  */
 BOOL
@@ -1389,28 +1765,39 @@ SetConsoleCursorInfo(HANDLE hConsoleOutput,
 }
 
 
-/*--------------------------------------------------------------
- *     GetNumberOfConsoleMouseButtons
- *
- * @unimplemented
+/*
+ * @implemented
  */
 BOOL
 WINAPI
 GetNumberOfConsoleMouseButtons(LPDWORD lpNumberOfMouseButtons)
 {
-    DPRINT1("GetNumberOfConsoleMouseButtons(0x%p) UNIMPLEMENTED!\n", lpNumberOfMouseButtons);
-    SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
-    return FALSE;
+    CONSOLE_API_MESSAGE ApiMessage;
+    PCONSOLE_GETMOUSEINFO GetMouseInfoRequest = &ApiMessage.Data.GetMouseInfoRequest;
+
+    GetMouseInfoRequest->ConsoleHandle = NtCurrentPeb()->ProcessParameters->ConsoleHandle;
+
+    CsrClientCallServer((PCSR_API_MESSAGE)&ApiMessage,
+                        NULL,
+                        CSR_CREATE_API_NUMBER(CONSRV_SERVERDLL_INDEX, ConsolepGetMouseInfo),
+                        sizeof(*GetMouseInfoRequest));
+    if (!NT_SUCCESS(ApiMessage.Status))
+    {
+        BaseSetLastNTError(ApiMessage.Status);
+        return FALSE;
+    }
+
+    *lpNumberOfMouseButtons = GetMouseInfoRequest->NumButtons;
+    return TRUE;
 }
 
 
-/*--------------------------------------------------------------
- *     SetConsoleActiveScreenBuffer
- *
+/*
  * @implemented
  */
 BOOL
 WINAPI
+DECLSPEC_HOTPATCH
 SetConsoleActiveScreenBuffer(HANDLE hConsoleOutput)
 {
     CONSOLE_API_MESSAGE ApiMessage;
@@ -1433,13 +1820,12 @@ SetConsoleActiveScreenBuffer(HANDLE hConsoleOutput)
 }
 
 
-/*--------------------------------------------------------------
- *     FlushConsoleInputBuffer
- *
+/*
  * @implemented
  */
 BOOL
 WINAPI
+DECLSPEC_HOTPATCH
 FlushConsoleInputBuffer(HANDLE hConsoleInput)
 {
     CONSOLE_API_MESSAGE ApiMessage;
@@ -1462,13 +1848,12 @@ FlushConsoleInputBuffer(HANDLE hConsoleInput)
 }
 
 
-/*--------------------------------------------------------------
- *     SetConsoleScreenBufferSize
- *
+/*
  * @implemented
  */
 BOOL
 WINAPI
+DECLSPEC_HOTPATCH
 SetConsoleScreenBufferSize(HANDLE hConsoleOutput,
                            COORD dwSize)
 {
@@ -1496,18 +1881,17 @@ SetConsoleScreenBufferSize(HANDLE hConsoleOutput,
 static
 BOOL
 IntScrollConsoleScreenBuffer(HANDLE hConsoleOutput,
-                             const SMALL_RECT *lpScrollRectangle,
-                             const SMALL_RECT *lpClipRectangle,
+                             CONST SMALL_RECT* lpScrollRectangle,
+                             CONST SMALL_RECT* lpClipRectangle,
                              COORD dwDestinationOrigin,
-                             const CHAR_INFO *lpFill,
+                             CONST CHAR_INFO* lpFill,
                              BOOL bUnicode)
 {
-    NTSTATUS Status;
     CONSOLE_API_MESSAGE ApiMessage;
     PCONSOLE_SCROLLSCREENBUFFER ScrollScreenBufferRequest = &ApiMessage.Data.ScrollScreenBufferRequest;
 
-    ScrollScreenBufferRequest->OutputHandle = hConsoleOutput;
-    ScrollScreenBufferRequest->Unicode = bUnicode;
+    ScrollScreenBufferRequest->ConsoleHandle = NtCurrentPeb()->ProcessParameters->ConsoleHandle;
+    ScrollScreenBufferRequest->OutputHandle  = hConsoleOutput;
     ScrollScreenBufferRequest->ScrollRectangle = *lpScrollRectangle;
 
     if (lpClipRectangle != NULL)
@@ -1521,16 +1905,16 @@ IntScrollConsoleScreenBuffer(HANDLE hConsoleOutput,
     }
 
     ScrollScreenBufferRequest->DestinationOrigin = dwDestinationOrigin;
-    ScrollScreenBufferRequest->Fill = *lpFill;
-
-    Status = CsrClientCallServer((PCSR_API_MESSAGE)&ApiMessage,
-                                 NULL,
-                                 CSR_CREATE_API_NUMBER(CONSRV_SERVERDLL_INDEX, ConsolepScrollScreenBuffer),
-                                 sizeof(CONSOLE_SCROLLSCREENBUFFER));
+    ScrollScreenBufferRequest->Fill    = *lpFill;
+    ScrollScreenBufferRequest->Unicode = bUnicode;
 
-    if (!NT_SUCCESS(Status))
+    CsrClientCallServer((PCSR_API_MESSAGE)&ApiMessage,
+                        NULL,
+                        CSR_CREATE_API_NUMBER(CONSRV_SERVERDLL_INDEX, ConsolepScrollScreenBuffer),
+                        sizeof(*ScrollScreenBufferRequest));
+    if (!NT_SUCCESS(ApiMessage.Status))
     {
-        BaseSetLastNTError(Status);
+        BaseSetLastNTError(ApiMessage.Status);
         return FALSE;
     }
 
@@ -1538,35 +1922,33 @@ IntScrollConsoleScreenBuffer(HANDLE hConsoleOutput,
 }
 
 
-/*--------------------------------------------------------------
- *    ScrollConsoleScreenBufferA
- *
+/*
  * @implemented
  */
 BOOL
 WINAPI
+DECLSPEC_HOTPATCH
 ScrollConsoleScreenBufferA(HANDLE hConsoleOutput,
-                           CONST SMALL_RECT *lpScrollRectangle,
-                           CONST SMALL_RECT *lpClipRectangle,
+                           CONST SMALL_RECTlpScrollRectangle,
+                           CONST SMALL_RECTlpClipRectangle,
                            COORD dwDestinationOrigin,
-                           CONST CHAR_INFO *lpFill)
+                           CONST CHAR_INFOlpFill)
 {
     return IntScrollConsoleScreenBuffer(hConsoleOutput,
-                                        (PSMALL_RECT)lpScrollRectangle,
-                                        (PSMALL_RECT)lpClipRectangle,
+                                        lpScrollRectangle,
+                                        lpClipRectangle,
                                         dwDestinationOrigin,
-                                        (PCHAR_INFO)lpFill,
+                                        lpFill,
                                         FALSE);
 }
 
 
-/*--------------------------------------------------------------
- *     ScrollConsoleScreenBufferW
- *
+/*
  * @implemented
  */
 BOOL
 WINAPI
+DECLSPEC_HOTPATCH
 ScrollConsoleScreenBufferW(HANDLE hConsoleOutput,
                            CONST SMALL_RECT *lpScrollRectangle,
                            CONST SMALL_RECT *lpClipRectangle,
@@ -1582,9 +1964,7 @@ ScrollConsoleScreenBufferW(HANDLE hConsoleOutput,
 }
 
 
-/*--------------------------------------------------------------
- *     SetConsoleWindowInfo
- *
+/*
  * @implemented
  */
 BOOL
@@ -1621,13 +2001,12 @@ SetConsoleWindowInfo(HANDLE hConsoleOutput,
 }
 
 
-/*--------------------------------------------------------------
- *      SetConsoleTextAttribute
- *
+/*
  * @implemented
  */
 BOOL
 WINAPI
+DECLSPEC_HOTPATCH
 SetConsoleTextAttribute(HANDLE hConsoleOutput,
                         WORD wAttributes)
 {
@@ -1728,33 +2107,30 @@ RemoveConsoleCtrlHandler(PHANDLER_ROUTINE HandlerRoutine)
  */
 BOOL
 WINAPI
+DECLSPEC_HOTPATCH
 SetConsoleCtrlHandler(PHANDLER_ROUTINE HandlerRoutine,
                       BOOL Add)
 {
     BOOL Ret;
 
-    RtlEnterCriticalSection(&BaseDllDirectoryLock);
+    RtlEnterCriticalSection(&ConsoleLock);
+
     if (Add)
-    {
         Ret = AddConsoleCtrlHandler(HandlerRoutine);
-    }
     else
-    {
         Ret = RemoveConsoleCtrlHandler(HandlerRoutine);
-    }
 
-    RtlLeaveCriticalSection(&BaseDllDirectoryLock);
-    return(Ret);
+    RtlLeaveCriticalSection(&ConsoleLock);
+    return Ret;
 }
 
 
-/*--------------------------------------------------------------
- *     GenerateConsoleCtrlEvent
- *
+/*
  * @implemented
  */
 BOOL
 WINAPI
+DECLSPEC_HOTPATCH
 GenerateConsoleCtrlEvent(DWORD dwCtrlEvent,
                          DWORD dwProcessGroupId)
 {
@@ -1825,12 +2201,12 @@ IntGetConsoleTitle(LPVOID lpConsoleTitle, DWORD dwNumChars, BOOLEAN bUnicode)
 
     if (dwNumChars > 0)
     {
-        memcpy(lpConsoleTitle, TitleRequest->Title, TitleRequest->Length);
+        RtlCopyMemory(lpConsoleTitle, TitleRequest->Title, TitleRequest->Length);
 
         if (bUnicode)
-            ((LPWSTR)lpConsoleTitle)[dwNumChars] = L'\0';
+            ((LPWSTR)lpConsoleTitle)[dwNumChars] = UNICODE_NULL;
         else
-            ((LPSTR)lpConsoleTitle)[dwNumChars] = '\0';
+            ((LPSTR)lpConsoleTitle)[dwNumChars] = ANSI_NULL;
     }
 
     CsrFreeCaptureBuffer(CaptureBuffer);
@@ -1839,13 +2215,12 @@ IntGetConsoleTitle(LPVOID lpConsoleTitle, DWORD dwNumChars, BOOLEAN bUnicode)
 }
 
 
-/*--------------------------------------------------------------
- *    GetConsoleTitleW
- *
+/*
  * @implemented
  */
 DWORD
 WINAPI
+DECLSPEC_HOTPATCH
 GetConsoleTitleW(LPWSTR lpConsoleTitle,
                  DWORD nSize)
 {
@@ -1853,13 +2228,12 @@ GetConsoleTitleW(LPWSTR lpConsoleTitle,
 }
 
 
-/*--------------------------------------------------------------
- *     GetConsoleTitleA
- *
+/*
  * @implemented
  */
 DWORD
 WINAPI
+DECLSPEC_HOTPATCH
 GetConsoleTitleA(LPSTR lpConsoleTitle,
                  DWORD nSize)
 {
@@ -1868,14 +2242,16 @@ GetConsoleTitleA(LPSTR lpConsoleTitle,
 
 
 static BOOL
-IntSetConsoleTitle(CONST VOID *lpConsoleTitle, DWORD dwNumChars, BOOLEAN bUnicode)
+IntSetConsoleTitle(CONST VOID *lpConsoleTitle, BOOLEAN bUnicode)
 {
     CONSOLE_API_MESSAGE ApiMessage;
     PCONSOLE_GETSETCONSOLETITLE TitleRequest = &ApiMessage.Data.TitleRequest;
     PCSR_CAPTURE_BUFFER CaptureBuffer;
 
+    ULONG NumChars = (ULONG)(lpConsoleTitle ? (bUnicode ? wcslen(lpConsoleTitle) : strlen(lpConsoleTitle)) : 0);
+
     TitleRequest->ConsoleHandle = NtCurrentPeb()->ProcessParameters->ConsoleHandle;
-    TitleRequest->Length        = dwNumChars * (bUnicode ? sizeof(WCHAR) : sizeof(CHAR));
+    TitleRequest->Length        = NumChars * (bUnicode ? sizeof(WCHAR) : sizeof(CHAR));
     TitleRequest->Unicode       = bUnicode;
 
     CaptureBuffer = CsrAllocateCaptureBuffer(1, TitleRequest->Length);
@@ -1907,35 +2283,31 @@ IntSetConsoleTitle(CONST VOID *lpConsoleTitle, DWORD dwNumChars, BOOLEAN bUnicod
     return TRUE;
 }
 
-/*--------------------------------------------------------------
- *    SetConsoleTitleW
- *
+/*
  * @implemented
  */
 BOOL
 WINAPI
+DECLSPEC_HOTPATCH
 SetConsoleTitleW(LPCWSTR lpConsoleTitle)
 {
-    return IntSetConsoleTitle(lpConsoleTitle, wcslen(lpConsoleTitle), TRUE);
+    return IntSetConsoleTitle(lpConsoleTitle, TRUE);
 }
 
 
-/*--------------------------------------------------------------
- *    SetConsoleTitleA
- *
+/*
  * @implemented
  */
 BOOL
 WINAPI
+DECLSPEC_HOTPATCH
 SetConsoleTitleA(LPCSTR lpConsoleTitle)
 {
-    return IntSetConsoleTitle(lpConsoleTitle, strlen(lpConsoleTitle), FALSE);
+    return IntSetConsoleTitle(lpConsoleTitle, FALSE);
 }
 
 
-/*--------------------------------------------------------------
- *    CreateConsoleScreenBuffer
- *
+/*
  * @implemented
  */
 HANDLE
@@ -2012,13 +2384,12 @@ CreateConsoleScreenBuffer(DWORD dwDesiredAccess,
 }
 
 
-/*--------------------------------------------------------------
- *    GetConsoleCP
- *
+/*
  * @implemented
  */
 UINT
 WINAPI
+DECLSPEC_HOTPATCH
 GetConsoleCP(VOID)
 {
     CONSOLE_API_MESSAGE ApiMessage;
@@ -2042,13 +2413,12 @@ GetConsoleCP(VOID)
 }
 
 
-/*--------------------------------------------------------------
- *    SetConsoleCP
- *
+/*
  * @implemented
  */
 BOOL
 WINAPI
+DECLSPEC_HOTPATCH
 SetConsoleCP(UINT wCodePageID)
 {
     CONSOLE_API_MESSAGE ApiMessage;
@@ -2074,13 +2444,12 @@ SetConsoleCP(UINT wCodePageID)
 }
 
 
-/*--------------------------------------------------------------
- *    GetConsoleOutputCP
- *
+/*
  * @implemented
  */
 UINT
 WINAPI
+DECLSPEC_HOTPATCH
 GetConsoleOutputCP(VOID)
 {
     CONSOLE_API_MESSAGE ApiMessage;
@@ -2104,13 +2473,12 @@ GetConsoleOutputCP(VOID)
 }
 
 
-/*--------------------------------------------------------------
- *    SetConsoleOutputCP
- *
+/*
  * @implemented
  */
 BOOL
 WINAPI
+DECLSPEC_HOTPATCH
 SetConsoleOutputCP(UINT wCodePageID)
 {
     CONSOLE_API_MESSAGE ApiMessage;
@@ -2136,9 +2504,7 @@ SetConsoleOutputCP(UINT wCodePageID)
 }
 
 
-/*--------------------------------------------------------------
- *     GetConsoleProcessList
- *
+/*
  * @implemented
  */
 DWORD
@@ -2185,7 +2551,7 @@ GetConsoleProcessList(LPDWORD lpdwProcessList,
         nProcesses = GetProcessListRequest->ProcessCount;
         if (dwProcessCount >= nProcesses)
         {
-            memcpy(lpdwProcessList, GetProcessListRequest->ProcessIdsList, nProcesses * sizeof(DWORD));
+            RtlCopyMemory(lpdwProcessList, GetProcessListRequest->ProcessIdsList, nProcesses * sizeof(DWORD));
         }
     }
 
@@ -2194,9 +2560,7 @@ GetConsoleProcessList(LPDWORD lpdwProcessList,
 }
 
 
-/*--------------------------------------------------------------
- *     GetConsoleSelectionInfo
- *
+/*
  * @implemented
  */
 BOOL
@@ -2230,64 +2594,139 @@ GetConsoleSelectionInfo(PCONSOLE_SELECTION_INFO lpConsoleSelectionInfo)
 }
 
 
-/*--------------------------------------------------------------
- *     AttachConsole
- *
+/*
  * @implemented
- *
  * @note Strongly inspired by AllocConsole.
  */
-BOOL
-WINAPI
-AttachConsole(DWORD dwProcessId)
+static BOOL
+IntAttachConsole(DWORD ProcessId,
+                 LPTHREAD_START_ROUTINE CtrlRoutine,
+                 LPTHREAD_START_ROUTINE PropRoutine,
+                 PCONSOLE_START_INFO ConsoleStartInfo)
 {
+    BOOL Success = TRUE;
     NTSTATUS Status;
-    PRTL_USER_PROCESS_PARAMETERS Parameters = NtCurrentPeb()->ProcessParameters;
+
     CONSOLE_API_MESSAGE ApiMessage;
     PCONSOLE_ATTACHCONSOLE AttachConsoleRequest = &ApiMessage.Data.AttachConsoleRequest;
+    PCSR_CAPTURE_BUFFER CaptureBuffer;
 
-    if (Parameters->ConsoleHandle)
+    AttachConsoleRequest->ProcessId   = ProcessId;
+    AttachConsoleRequest->CtrlRoutine = CtrlRoutine;
+    AttachConsoleRequest->PropRoutine = PropRoutine;
+
+    CaptureBuffer = CsrAllocateCaptureBuffer(1, sizeof(CONSOLE_START_INFO));
+    if (CaptureBuffer == NULL)
     {
-        DPRINT1("AttachConsole: Attaching a console to a process already having one\n");
-        SetLastError(ERROR_ACCESS_DENIED);
-        return FALSE;
+        SetLastError(ERROR_NOT_ENOUGH_MEMORY);
+        Success = FALSE;
+        goto Quit;
     }
 
-    AttachConsoleRequest->ProcessId = dwProcessId;
-    AttachConsoleRequest->CtrlDispatcher = ConsoleControlDispatcher;
-    AttachConsoleRequest->PropDispatcher = PropDialogHandler;
+    CsrCaptureMessageBuffer(CaptureBuffer,
+                            ConsoleStartInfo,
+                            sizeof(CONSOLE_START_INFO),
+                            (PVOID*)&AttachConsoleRequest->ConsoleStartInfo);
 
-    Status = CsrClientCallServer((PCSR_API_MESSAGE)&ApiMessage,
-                                 NULL,
-                                 CSR_CREATE_API_NUMBER(CONSRV_SERVERDLL_INDEX, ConsolepAttach),
-                                 sizeof(CONSOLE_ATTACHCONSOLE));
+    CsrClientCallServer((PCSR_API_MESSAGE)&ApiMessage,
+                        CaptureBuffer,
+                        CSR_CREATE_API_NUMBER(CONSRV_SERVERDLL_INDEX, ConsolepAttach),
+                        sizeof(*AttachConsoleRequest));
+    if (!NT_SUCCESS(ApiMessage.Status))
+    {
+        BaseSetLastNTError(ApiMessage.Status);
+        Success = FALSE;
+        goto Quit;
+    }
+
+    // Is AttachConsoleRequest->ConsoleStartInfo->InitEvents aligned on handle boundary ????
+    Status = NtWaitForMultipleObjects(MAX_INIT_EVENTS,
+                                      AttachConsoleRequest->ConsoleStartInfo->InitEvents,
+                                      WaitAny, FALSE, NULL);
     if (!NT_SUCCESS(Status))
     {
         BaseSetLastNTError(Status);
-        return FALSE;
+        Success = FALSE;
+        goto Quit;
     }
 
-    Parameters->ConsoleHandle = AttachConsoleRequest->ConsoleHandle;
-    SetStdHandle(STD_INPUT_HANDLE , AttachConsoleRequest->InputHandle );
-    SetStdHandle(STD_OUTPUT_HANDLE, AttachConsoleRequest->OutputHandle);
-    SetStdHandle(STD_ERROR_HANDLE , AttachConsoleRequest->ErrorHandle );
+    NtClose(AttachConsoleRequest->ConsoleStartInfo->InitEvents[INIT_SUCCESS]);
+    NtClose(AttachConsoleRequest->ConsoleStartInfo->InitEvents[INIT_FAILURE]);
+    if (Status != INIT_SUCCESS)
+    {
+        NtCurrentPeb()->ProcessParameters->ConsoleHandle = NULL;
+        Success = FALSE;
+    }
+    else
+    {
+        RtlCopyMemory(ConsoleStartInfo,
+                      AttachConsoleRequest->ConsoleStartInfo,
+                      sizeof(CONSOLE_START_INFO));
+        Success = TRUE;
+    }
 
-    /* Initialize Console Ctrl Handler */
-    InitConsoleCtrlHandling();
+Quit:
+    if (CaptureBuffer) CsrFreeCaptureBuffer(CaptureBuffer);
+    return Success;
+}
 
-    InputWaitHandle = AttachConsoleRequest->InputWaitHandle;
+BOOL
+WINAPI
+AttachConsole(DWORD dwProcessId)
+{
+    BOOL Success;
+    CONSOLE_START_INFO ConsoleStartInfo;
 
-    return TRUE;
+    DWORD dummy;
+    LCID lcid;
+
+    RtlEnterCriticalSection(&ConsoleLock);
+
+    if (NtCurrentPeb()->ProcessParameters->ConsoleHandle)
+    {
+        DPRINT1("AttachConsole: Attaching a console to a process already having one\n");
+        SetLastError(ERROR_ACCESS_DENIED);
+        Success = FALSE;
+        goto Quit;
+    }
+
+    /* Set up the console properties */
+    SetUpConsoleInfo(FALSE,
+                     &dummy,
+                     NULL,
+                     &dummy,
+                     NULL,
+                     &ConsoleStartInfo);
+
+    Success = IntAttachConsole(dwProcessId,
+                               ConsoleControlDispatcher,
+                               PropDialogHandler,
+                               &ConsoleStartInfo);
+    if (Success)
+    {
+        /* Set up the handles */
+        SetUpHandles(&ConsoleStartInfo);
+        InputWaitHandle = ConsoleStartInfo.InputWaitHandle;
+
+        /* Initialize Console Ctrl Handling */
+        InitializeCtrlHandling();
+
+        /* Sets the current console locale for this thread */
+        SetTEBLangID(lcid);
+    }
+
+Quit:
+    RtlLeaveCriticalSection(&ConsoleLock);
+    return Success;
 }
 
 
-/*--------------------------------------------------------------
- *     GetConsoleWindow
- *
+/*
  * @implemented
  */
 HWND
 WINAPI
+DECLSPEC_HOTPATCH
 GetConsoleWindow(VOID)
 {
     CONSOLE_API_MESSAGE ApiMessage;
@@ -2309,13 +2748,12 @@ GetConsoleWindow(VOID)
 }
 
 
-/*--------------------------------------------------------------
- *     SetConsoleIcon
- *
+/*
  * @implemented
  */
 BOOL
 WINAPI
+DECLSPEC_HOTPATCH
 SetConsoleIcon(HICON hIcon)
 {
     CONSOLE_API_MESSAGE ApiMessage;
@@ -2341,35 +2779,36 @@ SetConsoleIcon(HICON hIcon)
 /******************************************************************************
  * \name SetConsoleInputExeNameW
  * \brief Sets the console input file name from a unicode string.
- * \param lpInputExeName Pointer to a unicode string with the name.
- * \return TRUE if successful, FALSE if unsuccsedful.
- * \remarks If lpInputExeName is 0 or the string length is 0 or greater than 255,
+ * \param lpExeName Pointer to a unicode string with the name.
+ * \return TRUE if successful, FALSE if unsuccessful.
+ * \remarks If lpExeName is 0 or the string length is 0 or greater than 255,
  *          the function fails and sets last error to ERROR_INVALID_PARAMETER.
  */
 BOOL
 WINAPI
-SetConsoleInputExeNameW(LPCWSTR lpInputExeName)
+DECLSPEC_HOTPATCH
+SetConsoleInputExeNameW(IN LPCWSTR lpExeName)
 {
-    int lenName;
+    DWORD ExeLength;
 
-    if ( !lpInputExeName                            ||
-        (lenName = lstrlenW(lpInputExeName)) == 0   ||
-         lenName > INPUTEXENAME_BUFLEN - 1 )
+    ExeLength = lstrlenW(lpExeName);
+    if ((ExeLength == 0) || (ExeLength >= EXENAME_LENGTH))
     {
         /* Fail if string is empty or too long */
         SetLastError(ERROR_INVALID_PARAMETER);
         return FALSE;
     }
 
-    RtlEnterCriticalSection(&ConsoleLock);
+    RtlEnterCriticalSection(&ExeNameLock);
     _SEH2_TRY
     {
-        RtlCopyMemory(InputExeName, lpInputExeName, lenName * sizeof(WCHAR));
-        InputExeName[lenName] = L'\0';
+        /* Set the input EXE name, not NULL terminated */
+        RtlCopyMemory(ExeNameBuffer, lpExeName, ExeLength * sizeof(WCHAR));
+        ExeNameLength = (USHORT)ExeLength;
     }
     _SEH2_FINALLY
     {
-        RtlLeaveCriticalSection(&ConsoleLock);
+        RtlLeaveCriticalSection(&ExeNameLock);
     }
     _SEH2_END;
 
@@ -2380,42 +2819,73 @@ SetConsoleInputExeNameW(LPCWSTR lpInputExeName)
 /******************************************************************************
  * \name SetConsoleInputExeNameA
  * \brief Sets the console input file name from an ansi string.
- * \param lpInputExeName Pointer to an ansi string with the name.
- * \return TRUE if successful, FALSE if unsuccsedful.
- * \remarks If lpInputExeName is 0 or the string length is 0 or greater than 255,
+ * \param lpExeName Pointer to an ansi string with the name.
+ * \return TRUE if successful, FALSE if unsuccessful.
+ * \remarks If lpExeName is 0 or the string length is 0 or greater than 255,
  *          the function fails and sets last error to ERROR_INVALID_PARAMETER.
  */
 BOOL
 WINAPI
-SetConsoleInputExeNameA(LPCSTR lpInputExeName)
+DECLSPEC_HOTPATCH
+SetConsoleInputExeNameA(IN LPCSTR lpExeName)
 {
-    WCHAR Buffer[INPUTEXENAME_BUFLEN];
-    ANSI_STRING InputExeNameA;
-    UNICODE_STRING InputExeNameU;
     NTSTATUS Status;
+#ifdef USE_TEB_STATIC_USTR
+    PUNICODE_STRING ExeNameU;
+#else
+    UNICODE_STRING ExeNameU;
+#endif
+    ANSI_STRING ExeNameA;
+#ifndef USE_TEB_STATIC_USTR
+    WCHAR Buffer[EXENAME_LENGTH];
+#endif
+
+#ifdef USE_TEB_STATIC_USTR
+    /*
+     * Use the TEB static UNICODE string for storage. It is already
+     * initialized at process creation time by the Memory Manager.
+     */
+    ExeNameU = &NtCurrentTeb()->StaticUnicodeString;
+#endif
 
-    RtlInitAnsiString(&InputExeNameA, lpInputExeName);
+    /* Initialize string for conversion */
+    RtlInitAnsiString(&ExeNameA, lpExeName);
 
-    if ( InputExeNameA.Length == 0 || 
-         InputExeNameA.Length > INPUTEXENAME_BUFLEN - 1 )
+#if 1
+    if ((ExeNameA.Length == 0) || (ExeNameA.Length >= EXENAME_LENGTH))
     {
         /* Fail if string is empty or too long */
         SetLastError(ERROR_INVALID_PARAMETER);
         return FALSE;
     }
-
-    InputExeNameU.Buffer = Buffer;
-    InputExeNameU.MaximumLength = sizeof(Buffer);
-    InputExeNameU.Length = 0;
-
-    Status = RtlAnsiStringToUnicodeString(&InputExeNameU, &InputExeNameA, FALSE);
+#endif
+#ifndef USE_TEB_STATIC_USTR
+    ExeNameU.Length = 0;
+    ExeNameU.MaximumLength = (USHORT)sizeof(Buffer);
+    ExeNameU.Buffer = Buffer;
+#endif
+
+#ifdef USE_TEB_STATIC_USTR
+    Status = RtlAnsiStringToUnicodeString(ExeNameU, &ExeNameA, FALSE);
+#else
+    Status = RtlAnsiStringToUnicodeString(&ExeNameU, &ExeNameA, FALSE);
+#endif
     if (!NT_SUCCESS(Status))
     {
-        BaseSetLastNTError(Status);
+        /* Fail if string is empty or too long */
+        if (Status == STATUS_BUFFER_OVERFLOW)
+            SetLastError(ERROR_FILENAME_EXCED_RANGE);
+        else
+            SetLastError(ERROR_INVALID_PARAMETER);
+
         return FALSE;
     }
 
-    return SetConsoleInputExeNameW(InputExeNameU.Buffer);
+#ifdef USE_TEB_STATIC_USTR
+    return SetConsoleInputExeNameW(ExeNameU->Buffer);
+#else
+    return SetConsoleInputExeNameW(ExeNameU.Buffer);
+#endif
 }
 
 
@@ -2432,37 +2902,31 @@ SetConsoleInputExeNameA(LPCSTR lpInputExeName)
  */
 DWORD
 WINAPI
-GetConsoleInputExeNameW(DWORD nBufferLength, LPWSTR lpBuffer)
+DECLSPEC_HOTPATCH
+GetConsoleInputExeNameW(IN DWORD nBufferLength,
+                        OUT LPWSTR lpExeName)
 {
-    ULONG lenName = lstrlenW(InputExeName);
-
-    if (nBufferLength == 0)
-    {
-        /* Buffer size is requested, return it */
-        return lenName + 1;
-    }
-
-    if (lenName + 1 > nBufferLength)
+    if (nBufferLength <= ExeNameLength)
     {
-        /* Buffer is not large enough! */
+        /* Buffer is not large enough! Return the correct size. */
         SetLastError(ERROR_BUFFER_OVERFLOW);
-        return 2;
+        return ExeNameLength + 1;
     }
 
-    RtlEnterCriticalSection(&ConsoleLock);
+    RtlEnterCriticalSection(&ExeNameLock);
     _SEH2_TRY
     {
-        RtlCopyMemory(lpBuffer, InputExeName, lenName * sizeof(WCHAR));
-        lpBuffer[lenName] = '\0';
+        /* Copy the input EXE name and NULL-terminate it */
+        RtlCopyMemory(lpExeName, ExeNameBuffer, ExeNameLength * sizeof(WCHAR));
+        lpExeName[ExeNameLength] = UNICODE_NULL;
     }
     _SEH2_FINALLY
     {
-        RtlLeaveCriticalSection(&ConsoleLock);
+        RtlLeaveCriticalSection(&ExeNameLock);
     }
     _SEH2_END;
 
-    /* Success, return 1 */
-    return 1;
+    return TRUE;
 }
 
 
@@ -2477,33 +2941,41 @@ GetConsoleInputExeNameW(DWORD nBufferLength, LPWSTR lpBuffer)
  */
 DWORD
 WINAPI
-GetConsoleInputExeNameA(DWORD nBufferLength, LPSTR lpBuffer)
+DECLSPEC_HOTPATCH
+GetConsoleInputExeNameA(IN DWORD nBufferLength,
+                        OUT LPSTR lpExeName)
 {
-    WCHAR Buffer[INPUTEXENAME_BUFLEN];
-    DWORD Ret;
+    NTSTATUS Status;
+    DWORD ExeLength;
     UNICODE_STRING BufferU;
     ANSI_STRING BufferA;
+    WCHAR Buffer[EXENAME_LENGTH];
+
+    /* Get the UNICODE name */
+    ExeLength = GetConsoleInputExeNameW(EXENAME_LENGTH, Buffer);
 
-    /* Get the unicode name */
-    Ret = GetConsoleInputExeNameW(sizeof(Buffer) / sizeof(Buffer[0]), Buffer);
+    if ((ExeLength == 0) || (ExeLength >= EXENAME_LENGTH))
+        return ExeLength;
 
-    /* Initialize strings for conversion */
+    /* Initialize the strings for conversion */
     RtlInitUnicodeString(&BufferU, Buffer);
     BufferA.Length = 0;
     BufferA.MaximumLength = (USHORT)nBufferLength;
-    BufferA.Buffer = lpBuffer;
+    BufferA.Buffer = lpExeName;
 
-    /* Convert unicode name to ansi, copying as much chars as fit */
-    RtlUnicodeStringToAnsiString(&BufferA, &BufferU, FALSE);
-
-    /* Error handling */
-    if (nBufferLength <= BufferU.Length / sizeof(WCHAR))
+    /* Convert UNICODE name to ANSI, copying as much chars as it can fit */
+    Status = RtlUnicodeStringToAnsiString(&BufferA, &BufferU, FALSE);
+    if (!NT_SUCCESS(Status))
     {
-        SetLastError(ERROR_BUFFER_OVERFLOW);
-        return 2;
+        if (Status == STATUS_BUFFER_OVERFLOW)
+        {
+            SetLastError(ERROR_BUFFER_OVERFLOW);
+            return ExeLength + 1;
+        }
+        SetLastError(ERROR_INVALID_PARAMETER);
     }
 
-    return Ret;
+    return ExeLength;
 }
 
 BOOL
@@ -2516,6 +2988,7 @@ GetConsoleCharType(HANDLE hConsole, COORD Coord, PDWORD Type)
 
 BOOL
 WINAPI
+DECLSPEC_HOTPATCH
 GetConsoleCursorMode(HANDLE hConsole, PBOOL pUnknown1, PBOOL pUnknown2)
 {
     STUB;
@@ -2524,7 +2997,8 @@ GetConsoleCursorMode(HANDLE hConsole, PBOOL pUnknown1, PBOOL pUnknown2)
 
 BOOL
 WINAPI
-GetConsoleNlsMode(HANDLE hConsole, LPDWORD lpMode)
+DECLSPEC_HOTPATCH
+SetConsoleCursorMode(HANDLE hConsole, BOOL Unknown1, BOOL Unknown2)
 {
     STUB;
     return FALSE;
@@ -2532,7 +3006,8 @@ GetConsoleNlsMode(HANDLE hConsole, LPDWORD lpMode)
 
 BOOL
 WINAPI
-SetConsoleCursorMode(HANDLE hConsole, BOOL Unknown1, BOOL Unknown2)
+DECLSPEC_HOTPATCH
+GetConsoleNlsMode(HANDLE hConsole, LPDWORD lpMode)
 {
     STUB;
     return FALSE;
@@ -2540,7 +3015,8 @@ SetConsoleCursorMode(HANDLE hConsole, BOOL Unknown1, BOOL Unknown2)
 
 BOOL
 WINAPI
-SetConsoleLocalEUDC(DWORD Unknown1, DWORD Unknown2, DWORD Unknown3, DWORD Unknown4)
+DECLSPEC_HOTPATCH
+SetConsoleNlsMode(HANDLE hConsole, DWORD dwMode)
 {
     STUB;
     return FALSE;
@@ -2548,7 +3024,8 @@ SetConsoleLocalEUDC(DWORD Unknown1, DWORD Unknown2, DWORD Unknown3, DWORD Unknow
 
 BOOL
 WINAPI
-SetConsoleNlsMode(HANDLE hConsole, DWORD dwMode)
+DECLSPEC_HOTPATCH
+SetConsoleLocalEUDC(DWORD Unknown1, DWORD Unknown2, DWORD Unknown3, DWORD Unknown4)
 {
     STUB;
     return FALSE;
@@ -2556,6 +3033,7 @@ SetConsoleNlsMode(HANDLE hConsole, DWORD dwMode)
 
 BOOL
 WINAPI
+DECLSPEC_HOTPATCH
 RegisterConsoleIME(HWND hWnd, LPDWORD ThreadId)
 {
     STUB;
@@ -2564,6 +3042,7 @@ RegisterConsoleIME(HWND hWnd, LPDWORD ThreadId)
 
 BOOL
 WINAPI
+DECLSPEC_HOTPATCH
 RegisterConsoleOS2(BOOL bUnknown)
 {
     STUB;
@@ -2572,6 +3051,7 @@ RegisterConsoleOS2(BOOL bUnknown)
 
 BOOL
 WINAPI
+DECLSPEC_HOTPATCH
 SetConsoleOS2OemFormat(BOOL bUnknown)
 {
     STUB;
@@ -2580,6 +3060,7 @@ SetConsoleOS2OemFormat(BOOL bUnknown)
 
 BOOL
 WINAPI
+DECLSPEC_HOTPATCH
 UnregisterConsoleIME(VOID)
 {
     STUB;
@@ -2587,22 +3068,70 @@ UnregisterConsoleIME(VOID)
 }
 
 
+static
+BOOL
+IntGetConsoleKeyboardLayoutName(OUT PVOID pszLayoutName,
+                                IN BOOL bAnsi)
+{
+    CONSOLE_API_MESSAGE ApiMessage;
+    PCONSOLE_GETKBDLAYOUTNAME GetKbdLayoutNameRequest = &ApiMessage.Data.GetKbdLayoutNameRequest;
+
+    /* Set up the data to send to the Console Server */
+    GetKbdLayoutNameRequest->ConsoleHandle = NtCurrentPeb()->ProcessParameters->ConsoleHandle;
+    GetKbdLayoutNameRequest->Ansi          = bAnsi;
+
+    /* Call the server */
+    CsrClientCallServer((PCSR_API_MESSAGE)&ApiMessage,
+                        NULL,
+                        CSR_CREATE_API_NUMBER(CONSRV_SERVERDLL_INDEX, ConsolepGetKeyboardLayoutName),
+                        sizeof(*GetKbdLayoutNameRequest));
+
+    /* Check for success */
+    if (!NT_SUCCESS(ApiMessage.Status))
+    {
+        BaseSetLastNTError(ApiMessage.Status);
+        return FALSE;
+    }
+
+    /* Retrieve the results */
+    _SEH2_TRY
+    {
+        /* Copy only KL_NAMELENGTH == 9 characters, ANSI or UNICODE */
+        if (bAnsi)
+            strncpy(pszLayoutName, (PCHAR)GetKbdLayoutNameRequest->LayoutBuffer, KL_NAMELENGTH);
+        else
+            wcsncpy(pszLayoutName, (PWCHAR)GetKbdLayoutNameRequest->LayoutBuffer, KL_NAMELENGTH);
+    }
+    _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
+    {
+        SetLastError(ERROR_INVALID_ACCESS);
+        _SEH2_YIELD(return FALSE);
+    }
+    _SEH2_END;
+
+    return TRUE;
+}
+
 /*
- * @unimplemented
+ * @implemented (undocumented)
  */
-BOOL WINAPI GetConsoleKeyboardLayoutNameA(LPSTR name)
+BOOL
+WINAPI
+DECLSPEC_HOTPATCH
+GetConsoleKeyboardLayoutNameA(OUT LPSTR pszLayoutName)
 {
-    STUB;
-    return 0;
+    return IntGetConsoleKeyboardLayoutName(pszLayoutName, TRUE);
 }
 
 /*
- * @unimplemented
+ * @implemented (undocumented)
  */
-BOOL WINAPI GetConsoleKeyboardLayoutNameW(LPWSTR name)
+BOOL
+WINAPI
+DECLSPEC_HOTPATCH
+GetConsoleKeyboardLayoutNameW(OUT LPWSTR pszLayoutName)
 {
-    STUB;
-    return 0;
+    return IntGetConsoleKeyboardLayoutName(pszLayoutName, FALSE);
 }
 
 /*