[KENREL32]: Silent few DPRINTs.
[reactos.git] / dll / win32 / kernel32 / client / console / readwrite.c
index 8e7a6c4..9db3ab5 100644 (file)
 #include <debug.h>
 
 
+/* See consrv/include/rect.h */
+#define ConioRectHeight(Rect) \
+    (((Rect)->Top) > ((Rect)->Bottom) ? 0 : ((Rect)->Bottom) - ((Rect)->Top) + 1)
+#define ConioRectWidth(Rect) \
+    (((Rect)->Left) > ((Rect)->Right) ? 0 : ((Rect)->Right) - ((Rect)->Left) + 1)
+
+
 /* PRIVATE FUNCTIONS **********************************************************/
 
 /******************
  * Read functions *
  ******************/
 
+DWORD
+WINAPI
+GetConsoleInputExeNameW(DWORD nBufferLength, LPWSTR lpBuffer);
+
 static
 BOOL
-IntReadConsole(HANDLE hConsoleInput,
-               PVOID lpBuffer,
-               DWORD nNumberOfCharsToRead,
-               LPDWORD lpNumberOfCharsRead,
-               PCONSOLE_READCONSOLE_CONTROL pInputControl,
-               BOOL bUnicode)
+IntReadConsole(IN HANDLE hConsoleInput,
+               OUT PVOID lpBuffer,
+               IN DWORD nNumberOfCharsToRead,
+               OUT LPDWORD lpNumberOfCharsRead,
+               IN PCONSOLE_READCONSOLE_CONTROL pInputControl OPTIONAL,
+               IN BOOLEAN bUnicode)
 {
+    BOOL Success;
     CONSOLE_API_MESSAGE ApiMessage;
     PCONSOLE_READCONSOLE ReadConsoleRequest = &ApiMessage.Data.ReadConsoleRequest;
-    PCSR_CAPTURE_BUFFER CaptureBuffer;
-    ULONG CharSize;
+    PCSR_CAPTURE_BUFFER CaptureBuffer = NULL;
+    ULONG CharSize, SizeBytes;
+
+    DPRINT("IntReadConsole\n");
+
+    /* Set up the data to send to the Console Server */
+    ReadConsoleRequest->ConsoleHandle = NtCurrentPeb()->ProcessParameters->ConsoleHandle;
+    ReadConsoleRequest->InputHandle   = hConsoleInput;
+    ReadConsoleRequest->Unicode       = bUnicode;
+
+    /*
+     * Retrieve the current console executable name string and length (always
+     * in UNICODE format).
+     * FIXME: Do not use GetConsoleInputExeNameW but use something else...
+     */
+    // 1- Get the exe name length in characters, including NULL character.
+    ReadConsoleRequest->ExeLength =
+        (USHORT)GetConsoleInputExeNameW(0, (PWCHAR)ReadConsoleRequest->StaticBuffer);
+    // 2- Get the exe name (GetConsoleInputExeNameW returns 1 in case of success).
+    if (GetConsoleInputExeNameW(ReadConsoleRequest->ExeLength,
+                                (PWCHAR)ReadConsoleRequest->StaticBuffer) != 1)
+    {
+        // Nothing
+        ReadConsoleRequest->ExeLength = 0;
+    }
+    else
+    {
+        // Remove the NULL character, and convert in number of bytes.
+        ReadConsoleRequest->ExeLength--;
+        ReadConsoleRequest->ExeLength *= sizeof(WCHAR);
+    }
+
+    /*** For DEBUGGING purposes ***/
+    {
+        UNICODE_STRING ExeName;
+        ExeName.Length = ExeName.MaximumLength = ReadConsoleRequest->ExeLength;
+        ExeName.Buffer = (PWCHAR)ReadConsoleRequest->StaticBuffer;
+        DPRINT1("IntReadConsole(ExeName = %wZ)\n", &ExeName);
+    }
+    /******************************/
 
     /* Determine the needed size */
-    CharSize = (bUnicode ? sizeof(WCHAR) : sizeof(CHAR));
-    ReadConsoleRequest->BufferSize = nNumberOfCharsToRead * CharSize;
+    CharSize  = (bUnicode ? sizeof(WCHAR) : sizeof(CHAR));
+    SizeBytes = nNumberOfCharsToRead * CharSize;
+
+    ReadConsoleRequest->CaptureBufferSize =
+    ReadConsoleRequest->NumBytes          = SizeBytes;
 
-    /* Allocate a Capture Buffer */
-    CaptureBuffer = CsrAllocateCaptureBuffer(1, ReadConsoleRequest->BufferSize);
-    if (CaptureBuffer == NULL)
+    /*
+     * For optimization purposes, Windows (and hence ReactOS, too, for
+     * compatibility reasons) uses a static buffer if no more than eighty
+     * bytes are read. Otherwise a new buffer is allocated.
+     * This behaviour is also expected in the server-side.
+     */
+    if (SizeBytes <= sizeof(ReadConsoleRequest->StaticBuffer))
     {
-        DPRINT1("CsrAllocateCaptureBuffer failed!\n");
-        SetLastError(ERROR_NOT_ENOUGH_MEMORY);
-        return FALSE;
+        ReadConsoleRequest->Buffer = ReadConsoleRequest->StaticBuffer;
+        // CaptureBuffer = NULL;
+    }
+    else
+    {
+        /* Allocate a Capture Buffer */
+        CaptureBuffer = CsrAllocateCaptureBuffer(1, SizeBytes);
+        if (CaptureBuffer == NULL)
+        {
+            DPRINT1("CsrAllocateCaptureBuffer failed!\n");
+            SetLastError(ERROR_NOT_ENOUGH_MEMORY);
+            return FALSE;
+        }
+
+        /* Allocate space in the Buffer */
+        CsrAllocateMessagePointer(CaptureBuffer,
+                                  SizeBytes,
+                                  (PVOID*)&ReadConsoleRequest->Buffer);
     }
 
-    /* Allocate space in the Buffer */
-    CsrAllocateMessagePointer(CaptureBuffer,
-                              ReadConsoleRequest->BufferSize,
-                              (PVOID*)&ReadConsoleRequest->Buffer);
+    ReadConsoleRequest->InitialNumBytes = 0;
+    ReadConsoleRequest->CtrlWakeupMask  = 0;
+    ReadConsoleRequest->ControlKeyState = 0;
 
-    /* Set up the data to send to the Console Server */
-    ReadConsoleRequest->InputHandle = hConsoleInput;
-    ReadConsoleRequest->Unicode = bUnicode;
-    ReadConsoleRequest->NrCharactersToRead = nNumberOfCharsToRead;
-    ReadConsoleRequest->NrCharactersRead = 0;
-    ReadConsoleRequest->CtrlWakeupMask = 0;
-    if (pInputControl && pInputControl->nLength == sizeof(CONSOLE_READCONSOLE_CONTROL))
+    /*
+     * From MSDN (ReadConsole function), the description
+     * for pInputControl says:
+     * "This parameter requires Unicode input by default.
+     * For ANSI mode, set this parameter to NULL."
+     */
+    _SEH2_TRY
+    {
+        if (bUnicode && pInputControl &&
+            pInputControl->nLength == sizeof(CONSOLE_READCONSOLE_CONTROL))
+        {
+            /* Sanity check */
+            if (pInputControl->nInitialChars <= nNumberOfCharsToRead)
+            {
+                ReadConsoleRequest->InitialNumBytes =
+                    pInputControl->nInitialChars * sizeof(WCHAR); // CharSize
+
+                if (pInputControl->nInitialChars != 0)
+                {
+                    /*
+                     * It is possible here to overwrite the static buffer, in case
+                     * the number of bytes to read was smaller than the static buffer.
+                     * In this case, this means we are continuing a pending read,
+                     * and we do not need in fact the executable name that was
+                     * stored in the static buffer because it was first grabbed when
+                     * we started the first read.
+                     */
+                    RtlCopyMemory(ReadConsoleRequest->Buffer,
+                                  lpBuffer,
+                                  ReadConsoleRequest->InitialNumBytes);
+                }
+
+                ReadConsoleRequest->CtrlWakeupMask = pInputControl->dwCtrlWakeupMask;
+            }
+            else
+            {
+                // Status = STATUS_INVALID_PARAMETER;
+            }
+        }
+        else
+        {
+            /* We are in a situation where pInputControl has no meaning */
+            pInputControl = NULL;
+        }
+    }
+    _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
+    {
+        // HACK
+        if (CaptureBuffer) CsrFreeCaptureBuffer(CaptureBuffer);
+        SetLastError(ERROR_INVALID_ACCESS);
+        _SEH2_YIELD(return FALSE);
+    }
+    _SEH2_END;
+
+    /* Check for sanity */
+/*
+    if (!NT_SUCCESS(Status) && pInputControl)
     {
-        /*
-         * From MSDN (ReadConsole function), the description
-         * for pInputControl says:
-         * "This parameter requires Unicode input by default.
-         * For ANSI mode, set this parameter to NULL."
-         */
-        ReadConsoleRequest->NrCharactersRead = pInputControl->nInitialChars;
-        RtlCopyMemory(ReadConsoleRequest->Buffer,
-                      lpBuffer,
-                      pInputControl->nInitialChars * sizeof(WCHAR));
-        ReadConsoleRequest->CtrlWakeupMask = pInputControl->dwCtrlWakeupMask;
+        // Free CaptureBuffer if needed
+        // Set last error to last status
+        // Return FALSE
     }
+*/
 
     /* Call the server */
     CsrClientCallServer((PCSR_API_MESSAGE)&ApiMessage,
@@ -84,63 +198,78 @@ IntReadConsole(HANDLE hConsoleInput,
                         sizeof(*ReadConsoleRequest));
 
     /* Check for success */
-    if (NT_SUCCESS(ApiMessage.Status))
+    Success = NT_SUCCESS(ApiMessage.Status);
+
+    /* Retrieve the results */
+    if (Success)
     {
-        RtlCopyMemory(lpBuffer,
-                      ReadConsoleRequest->Buffer,
-                      ReadConsoleRequest->NrCharactersRead * CharSize);
+        _SEH2_TRY
+        {
+            *lpNumberOfCharsRead = ReadConsoleRequest->NumBytes / CharSize;
 
-        if (lpNumberOfCharsRead != NULL)
-            *lpNumberOfCharsRead = ReadConsoleRequest->NrCharactersRead;
+            if (bUnicode && pInputControl)
+                pInputControl->dwControlKeyState = ReadConsoleRequest->ControlKeyState;
 
-        if (pInputControl && pInputControl->nLength == sizeof(CONSOLE_READCONSOLE_CONTROL))
-            pInputControl->dwControlKeyState = ReadConsoleRequest->ControlKeyState;
+            RtlCopyMemory(lpBuffer,
+                          ReadConsoleRequest->Buffer,
+                          ReadConsoleRequest->NumBytes);
+        }
+        _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
+        {
+            SetLastError(ERROR_INVALID_ACCESS);
+            Success = FALSE;
+        }
+        _SEH2_END;
     }
     else
     {
-        DPRINT1("CSR returned error in ReadConsole\n");
-
-        if (lpNumberOfCharsRead != NULL)
-            *lpNumberOfCharsRead = 0;
-
-        /* Error out */
         BaseSetLastNTError(ApiMessage.Status);
     }
 
-    CsrFreeCaptureBuffer(CaptureBuffer);
+    /* Release the capture buffer if needed */
+    if (CaptureBuffer) CsrFreeCaptureBuffer(CaptureBuffer);
+
+    if (Success)
+    {
+        /* Yield execution to another thread if Ctrl-C or Ctrl-Break happened */
+        if (ApiMessage.Status == STATUS_ALERTED /* || ApiMessage.Status == STATUS_CANCELLED */)
+        {
+            NtYieldExecution();
+            SetLastError(ERROR_OPERATION_ABORTED); // STATUS_CANCELLED
+        }
+    }
 
-    /* Return TRUE or FALSE */
-    // return TRUE;
-    return (ReadConsoleRequest->NrCharactersRead > 0);
-    // return NT_SUCCESS(ApiMessage.Status);
+    /* Return success status */
+    return Success;
 }
 
 
 static
 BOOL
-IntGetConsoleInput(HANDLE hConsoleInput,
-                   PINPUT_RECORD lpBuffer,
-                   DWORD nLength,
-                   LPDWORD lpNumberOfEventsRead,
-                   WORD wFlags,
-                   BOOLEAN bUnicode)
+IntGetConsoleInput(IN HANDLE hConsoleInput,
+                   OUT PINPUT_RECORD lpBuffer,
+                   IN DWORD nLength,
+                   OUT LPDWORD lpNumberOfEventsRead,
+                   IN WORD wFlags,
+                   IN BOOLEAN bUnicode)
 {
+    BOOL Success;
     CONSOLE_API_MESSAGE ApiMessage;
     PCONSOLE_GETINPUT GetInputRequest = &ApiMessage.Data.GetInputRequest;
     PCSR_CAPTURE_BUFFER CaptureBuffer = NULL;
 
-    if (lpBuffer == NULL)
-    {
-        SetLastError(ERROR_INVALID_ACCESS);
-        return FALSE;
-    }
-
     if (!IsConsoleHandle(hConsoleInput))
     {
-        SetLastError(ERROR_INVALID_HANDLE);
-
-        if (lpNumberOfEventsRead != NULL)
+        _SEH2_TRY
+        {
             *lpNumberOfEventsRead = 0;
+            SetLastError(ERROR_INVALID_HANDLE);
+        }
+        _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
+        {
+            SetLastError(ERROR_INVALID_ACCESS);
+        }
+        _SEH2_END;
 
         return FALSE;
     }
@@ -191,80 +320,116 @@ IntGetConsoleInput(HANDLE hConsoleInput,
                         sizeof(*GetInputRequest));
 
     /* Check for success */
-    if (NT_SUCCESS(ApiMessage.Status))
+    Success = NT_SUCCESS(ApiMessage.Status);
+
+    /* Retrieve the results */
+    _SEH2_TRY
     {
-        /* Return the number of events read */
         DPRINT("Events read: %lx\n", GetInputRequest->NumRecords);
+        *lpNumberOfEventsRead = GetInputRequest->NumRecords;
 
-        if (lpNumberOfEventsRead != NULL)
-            *lpNumberOfEventsRead = GetInputRequest->NumRecords;
-
-        /* Copy into the buffer */
-        RtlCopyMemory(lpBuffer,
-                      GetInputRequest->RecordBufPtr,
-                      GetInputRequest->NumRecords * sizeof(INPUT_RECORD));
+        if (Success)
+        {
+            RtlCopyMemory(lpBuffer,
+                          GetInputRequest->RecordBufPtr,
+                          GetInputRequest->NumRecords * sizeof(INPUT_RECORD));
+        }
+        else
+        {
+            BaseSetLastNTError(ApiMessage.Status);
+        }
     }
-    else
+    _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
     {
-        if (lpNumberOfEventsRead != NULL)
-            *lpNumberOfEventsRead = 0;
-
-        /* Error out */
-        BaseSetLastNTError(ApiMessage.Status);
+        SetLastError(ERROR_INVALID_ACCESS);
+        Success = FALSE;
     }
+    _SEH2_END;
 
     /* Release the capture buffer if needed */
     if (CaptureBuffer) CsrFreeCaptureBuffer(CaptureBuffer);
 
-    /* Return TRUE or FALSE */
-    return NT_SUCCESS(ApiMessage.Status);
+    /* Return success status */
+    return Success;
 }
 
 
 static
 BOOL
-IntReadConsoleOutput(HANDLE hConsoleOutput,
-                     PCHAR_INFO lpBuffer,
-                     COORD dwBufferSize,
-                     COORD dwBufferCoord,
-                     PSMALL_RECT lpReadRegion,
-                     BOOL bUnicode)
+IntReadConsoleOutput(IN HANDLE hConsoleOutput,
+                     OUT PCHAR_INFO lpBuffer,
+                     IN COORD dwBufferSize,
+                     IN COORD dwBufferCoord,
+                     IN OUT PSMALL_RECT lpReadRegion,
+                     IN BOOLEAN bUnicode)
 {
+    BOOL Success;
     CONSOLE_API_MESSAGE ApiMessage;
     PCONSOLE_READOUTPUT ReadOutputRequest = &ApiMessage.Data.ReadOutputRequest;
-    PCSR_CAPTURE_BUFFER CaptureBuffer;
-    DWORD Size, SizeX, SizeY;
+    PCSR_CAPTURE_BUFFER CaptureBuffer = NULL;
+
+    SHORT SizeX, SizeY;
+    ULONG NumCells;
+
+    /* Set up the data to send to the Console Server */
+    ReadOutputRequest->ConsoleHandle = NtCurrentPeb()->ProcessParameters->ConsoleHandle;
+    ReadOutputRequest->OutputHandle  = hConsoleOutput;
+    ReadOutputRequest->Unicode       = bUnicode;
+
+    /* Update lpReadRegion */
+    _SEH2_TRY
+    {
+        SizeX = min(dwBufferSize.X - dwBufferCoord.X, ConioRectWidth(lpReadRegion));
+        SizeY = min(dwBufferSize.Y - dwBufferCoord.Y, ConioRectHeight(lpReadRegion));
+        if (SizeX <= 0 || SizeY <= 0)
+        {
+            SetLastError(ERROR_INVALID_PARAMETER);
+            _SEH2_YIELD(return FALSE);
+        }
+        lpReadRegion->Right  = lpReadRegion->Left + SizeX - 1;
+        lpReadRegion->Bottom = lpReadRegion->Top  + SizeY - 1;
 
-    if (lpBuffer == NULL)
+        ReadOutputRequest->ReadRegion = *lpReadRegion;
+    }
+    _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
     {
         SetLastError(ERROR_INVALID_ACCESS);
-        return FALSE;
+        _SEH2_YIELD(return FALSE);
     }
+    _SEH2_END;
 
-    Size = dwBufferSize.X * dwBufferSize.Y * sizeof(CHAR_INFO);
-
-    DPRINT("IntReadConsoleOutput: %lx %p\n", Size, lpReadRegion);
+    NumCells = SizeX * SizeY;
+    DPRINT("IntReadConsoleOutput: (%d x %d)\n", SizeX, SizeY);
 
-    /* Allocate a Capture Buffer */
-    CaptureBuffer = CsrAllocateCaptureBuffer(1, Size);
-    if (CaptureBuffer == NULL)
+    /*
+     * For optimization purposes, Windows (and hence ReactOS, too, for
+     * compatibility reasons) uses a static buffer if no more than one
+     * cell is read. Otherwise a new buffer is allocated.
+     * This behaviour is also expected in the server-side.
+     */
+    if (NumCells <= 1)
     {
-        DPRINT1("CsrAllocateCaptureBuffer failed with size 0x%x!\n", Size);
-        SetLastError(ERROR_NOT_ENOUGH_MEMORY);
-        return FALSE;
+        ReadOutputRequest->CharInfo = &ReadOutputRequest->StaticBuffer;
+        // CaptureBuffer = NULL;
     }
+    else
+    {
+        ULONG Size = NumCells * sizeof(CHAR_INFO);
 
-    /* Allocate space in the Buffer */
-    CsrAllocateMessagePointer(CaptureBuffer,
-                              Size,
-                              (PVOID*)&ReadOutputRequest->CharInfo);
+        /* Allocate a Capture Buffer */
+        CaptureBuffer = CsrAllocateCaptureBuffer(1, Size);
+        if (CaptureBuffer == NULL)
+        {
+            DPRINT1("CsrAllocateCaptureBuffer failed with size %ld!\n", Size);
+            SetLastError(ERROR_NOT_ENOUGH_MEMORY);
+            return FALSE;
+        }
 
-    /* Set up the data to send to the Console Server */
-    ReadOutputRequest->OutputHandle = hConsoleOutput;
-    ReadOutputRequest->Unicode = bUnicode;
-    ReadOutputRequest->BufferSize = dwBufferSize;
-    ReadOutputRequest->BufferCoord = dwBufferCoord;
-    ReadOutputRequest->ReadRegion = *lpReadRegion;
+        /* Allocate space in the Buffer */
+        CsrAllocateMessagePointer(CaptureBuffer,
+                                  Size,
+                                  (PVOID*)&ReadOutputRequest->CharInfo);
+    }
 
     /* Call the server */
     CsrClientCallServer((PCSR_API_MESSAGE)&ApiMessage,
@@ -273,52 +438,84 @@ IntReadConsoleOutput(HANDLE hConsoleOutput,
                         sizeof(*ReadOutputRequest));
 
     /* Check for success */
-    if (NT_SUCCESS(ApiMessage.Status))
+    Success = NT_SUCCESS(ApiMessage.Status);
+
+    /* Retrieve the results */
+    _SEH2_TRY
     {
-        /* Copy into the buffer */
-        DPRINT("Copying to buffer\n");
-        SizeX = ReadOutputRequest->ReadRegion.Right -
-                ReadOutputRequest->ReadRegion.Left + 1;
-        SizeY = ReadOutputRequest->ReadRegion.Bottom -
-                ReadOutputRequest->ReadRegion.Top + 1;
-        RtlCopyMemory(lpBuffer,
-                      ReadOutputRequest->CharInfo,
-                      sizeof(CHAR_INFO) * SizeX * SizeY);
+        *lpReadRegion = ReadOutputRequest->ReadRegion;
+
+        if (Success)
+        {
+#if 0
+            SHORT x, X;
+#endif
+            SHORT y, Y;
+
+            /* Copy into the buffer */
+
+            SizeX = ReadOutputRequest->ReadRegion.Right -
+                    ReadOutputRequest->ReadRegion.Left + 1;
+
+            for (y = 0, Y = ReadOutputRequest->ReadRegion.Top; Y <= ReadOutputRequest->ReadRegion.Bottom; ++y, ++Y)
+            {
+                RtlCopyMemory(lpBuffer + (y + dwBufferCoord.Y) * dwBufferSize.X + dwBufferCoord.X,
+                              ReadOutputRequest->CharInfo + y * SizeX,
+                              SizeX * sizeof(CHAR_INFO));
+#if 0
+                for (x = 0, X = ReadOutputRequest->ReadRegion.Left; X <= ReadOutputRequest->ReadRegion.Right; ++x, ++X)
+                {
+                    *(lpBuffer + (y + dwBufferCoord.Y) * dwBufferSize.X + (x + dwBufferCoord.X)) =
+                    *(ReadOutputRequest->CharInfo + y * SizeX + x);
+                }
+#endif
+            }
+        }
+        else
+        {
+            BaseSetLastNTError(ApiMessage.Status);
+        }
     }
-    else
+    _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
     {
-        /* Error out */
-        BaseSetLastNTError(ApiMessage.Status);
+        SetLastError(ERROR_INVALID_ACCESS);
+        Success = FALSE;
     }
+    _SEH2_END;
 
-    /* Return the read region */
-    DPRINT("read region: %p\n", ReadOutputRequest->ReadRegion);
-    *lpReadRegion = ReadOutputRequest->ReadRegion;
-
-    /* Release the capture buffer */
-    CsrFreeCaptureBuffer(CaptureBuffer);
+    /* Release the capture buffer if needed */
+    if (CaptureBuffer) CsrFreeCaptureBuffer(CaptureBuffer);
 
-    /* Return TRUE or FALSE */
-    return NT_SUCCESS(ApiMessage.Status);
+    /* Return success status */
+    return Success;
 }
 
 
 static
 BOOL
-IntReadConsoleOutputCode(HANDLE hConsoleOutput,
-                         CODE_TYPE CodeType,
-                         PVOID pCode,
-                         DWORD nLength,
-                         COORD dwReadCoord,
-                         LPDWORD lpNumberOfCodesRead)
+IntReadConsoleOutputCode(IN HANDLE hConsoleOutput,
+                         IN CODE_TYPE CodeType,
+                         OUT PVOID pCode,
+                         IN DWORD nLength,
+                         IN COORD dwReadCoord,
+                         OUT LPDWORD lpNumberOfCodesRead)
 {
+    BOOL Success;
     CONSOLE_API_MESSAGE ApiMessage;
     PCONSOLE_READOUTPUTCODE ReadOutputCodeRequest = &ApiMessage.Data.ReadOutputCodeRequest;
     PCSR_CAPTURE_BUFFER CaptureBuffer = NULL;
-    ULONG SizeBytes, CodeSize;
+    ULONG CodeSize, SizeBytes;
 
     DPRINT("IntReadConsoleOutputCode\n");
 
+    if ( (CodeType != CODE_ASCII    ) &&
+         (CodeType != CODE_UNICODE  ) &&
+         (CodeType != CODE_ATTRIBUTE) )
+    {
+        SetLastError(ERROR_INVALID_PARAMETER);
+        return FALSE;
+    }
+
     /* Set up the data to send to the Console Server */
     ReadOutputCodeRequest->ConsoleHandle = NtCurrentPeb()->ProcessParameters->ConsoleHandle;
     ReadOutputCodeRequest->OutputHandle  = hConsoleOutput;
@@ -330,20 +527,16 @@ IntReadConsoleOutputCode(HANDLE hConsoleOutput,
     switch (CodeType)
     {
         case CODE_ASCII:
-            CodeSize = sizeof(CHAR);
+            CodeSize = RTL_FIELD_SIZE(CODE_ELEMENT, AsciiChar);
             break;
 
         case CODE_UNICODE:
-            CodeSize = sizeof(WCHAR);
+            CodeSize = RTL_FIELD_SIZE(CODE_ELEMENT, UnicodeChar);
             break;
 
         case CODE_ATTRIBUTE:
-            CodeSize = sizeof(WORD);
+            CodeSize = RTL_FIELD_SIZE(CODE_ELEMENT, Attribute);
             break;
-
-        default:
-            SetLastError(ERROR_INVALID_PARAMETER);
-            return FALSE;
     }
     SizeBytes = nLength * CodeSize;
 
@@ -355,7 +548,7 @@ IntReadConsoleOutputCode(HANDLE hConsoleOutput,
      */
     if (SizeBytes <= sizeof(ReadOutputCodeRequest->CodeStaticBuffer))
     {
-        ReadOutputCodeRequest->pCode.pCode = ReadOutputCodeRequest->CodeStaticBuffer;
+        ReadOutputCodeRequest->pCode = ReadOutputCodeRequest->CodeStaticBuffer;
         // CaptureBuffer = NULL;
     }
     else
@@ -372,7 +565,7 @@ IntReadConsoleOutputCode(HANDLE hConsoleOutput,
         /* Allocate space in the Buffer */
         CsrAllocateMessagePointer(CaptureBuffer,
                                   SizeBytes,
-                                  (PVOID*)&ReadOutputCodeRequest->pCode.pCode);
+                                  (PVOID*)&ReadOutputCodeRequest->pCode);
     }
 
     /* Call the server */
@@ -382,30 +575,36 @@ IntReadConsoleOutputCode(HANDLE hConsoleOutput,
                         sizeof(*ReadOutputCodeRequest));
 
     /* Check for success */
-    if (NT_SUCCESS(ApiMessage.Status))
+    Success = NT_SUCCESS(ApiMessage.Status);
+
+    /* Retrieve the results */
+    _SEH2_TRY
     {
-        DWORD NumCodes = ReadOutputCodeRequest->NumCodes;
-        RtlCopyMemory(pCode,
-                      ReadOutputCodeRequest->pCode.pCode,
-                      NumCodes * CodeSize);
+        *lpNumberOfCodesRead = ReadOutputCodeRequest->NumCodes;
 
-        if (lpNumberOfCodesRead != NULL)
-            *lpNumberOfCodesRead = NumCodes;
+        if (Success)
+        {
+            RtlCopyMemory(pCode,
+                          ReadOutputCodeRequest->pCode,
+                          ReadOutputCodeRequest->NumCodes * CodeSize);
+        }
+        else
+        {
+            BaseSetLastNTError(ApiMessage.Status);
+        }
     }
-    else
+    _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
     {
-        if (lpNumberOfCodesRead != NULL)
-            *lpNumberOfCodesRead = 0;
-
-        /* Error out */
-        BaseSetLastNTError(ApiMessage.Status);
+        SetLastError(ERROR_INVALID_ACCESS);
+        Success = FALSE;
     }
+    _SEH2_END;
 
     /* Release the capture buffer if needed */
     if (CaptureBuffer) CsrFreeCaptureBuffer(CaptureBuffer);
 
-    /* Return TRUE or FALSE */
-    return NT_SUCCESS(ApiMessage.Status);
+    /* Return success status */
+    return Success;
 }
 
 
@@ -415,42 +614,79 @@ IntReadConsoleOutputCode(HANDLE hConsoleOutput,
 
 static
 BOOL
-IntWriteConsole(HANDLE hConsoleOutput,
-                PVOID lpBuffer,
-                DWORD nNumberOfCharsToWrite,
-                LPDWORD lpNumberOfCharsWritten,
+IntWriteConsole(IN HANDLE hConsoleOutput,
+                IN PVOID lpBuffer,
+                IN DWORD nNumberOfCharsToWrite,
+                OUT LPDWORD lpNumberOfCharsWritten,
                 LPVOID lpReserved,
-                BOOL bUnicode)
+                IN BOOLEAN bUnicode)
 {
-    BOOL bRet = TRUE;
+    BOOL Success;
     CONSOLE_API_MESSAGE ApiMessage;
     PCONSOLE_WRITECONSOLE WriteConsoleRequest = &ApiMessage.Data.WriteConsoleRequest;
-    PCSR_CAPTURE_BUFFER CaptureBuffer;
-    ULONG CharSize;
+    PCSR_CAPTURE_BUFFER CaptureBuffer = NULL;
+    ULONG CharSize, SizeBytes;
+
+    DPRINT("IntWriteConsole\n");
+
+    /* Set up the data to send to the Console Server */
+    WriteConsoleRequest->ConsoleHandle = NtCurrentPeb()->ProcessParameters->ConsoleHandle;
+    WriteConsoleRequest->OutputHandle  = hConsoleOutput;
+    WriteConsoleRequest->Unicode       = bUnicode;
+
+    /* Those members are unused by the client, on Windows */
+    WriteConsoleRequest->Reserved1 = 0;
+    // WriteConsoleRequest->Reserved2 = {0};
 
     /* Determine the needed size */
-    CharSize = (bUnicode ? sizeof(WCHAR) : sizeof(CHAR));
-    WriteConsoleRequest->BufferSize = nNumberOfCharsToWrite * CharSize;
+    CharSize  = (bUnicode ? sizeof(WCHAR) : sizeof(CHAR));
+    SizeBytes = nNumberOfCharsToWrite * CharSize;
+
+    WriteConsoleRequest->NumBytes = SizeBytes;
 
-    /* Allocate a Capture Buffer */
-    CaptureBuffer = CsrAllocateCaptureBuffer(1, WriteConsoleRequest->BufferSize);
-    if (CaptureBuffer == NULL)
+    /*
+     * For optimization purposes, Windows (and hence ReactOS, too, for
+     * compatibility reasons) uses a static buffer if no more than eighty
+     * bytes are written. Otherwise a new buffer is allocated.
+     * This behaviour is also expected in the server-side.
+     */
+    if (SizeBytes <= sizeof(WriteConsoleRequest->StaticBuffer))
     {
-        DPRINT1("CsrAllocateCaptureBuffer failed!\n");
-        SetLastError(ERROR_NOT_ENOUGH_MEMORY);
-        return FALSE;
-    }
+        WriteConsoleRequest->Buffer = WriteConsoleRequest->StaticBuffer;
+        // CaptureBuffer = NULL;
+        WriteConsoleRequest->UsingStaticBuffer = TRUE;
 
-    /* Capture the buffer to write */
-    CsrCaptureMessageBuffer(CaptureBuffer,
-                            (PVOID)lpBuffer,
-                            WriteConsoleRequest->BufferSize,
-                            (PVOID*)&WriteConsoleRequest->Buffer);
+        _SEH2_TRY
+        {
+            RtlCopyMemory(WriteConsoleRequest->Buffer,
+                          lpBuffer,
+                          SizeBytes);
+        }
+        _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
+        {
+            SetLastError(ERROR_INVALID_ACCESS);
+            _SEH2_YIELD(return FALSE);
+        }
+        _SEH2_END;
+    }
+    else
+    {
+        /* Allocate a Capture Buffer */
+        CaptureBuffer = CsrAllocateCaptureBuffer(1, SizeBytes);
+        if (CaptureBuffer == NULL)
+        {
+            DPRINT1("CsrAllocateCaptureBuffer failed!\n");
+            SetLastError(ERROR_NOT_ENOUGH_MEMORY);
+            return FALSE;
+        }
 
-    /* Start writing */
-    WriteConsoleRequest->NrCharactersToWrite = nNumberOfCharsToWrite;
-    WriteConsoleRequest->OutputHandle = hConsoleOutput;
-    WriteConsoleRequest->Unicode = bUnicode;
+        /* Capture the buffer to write */
+        CsrCaptureMessageBuffer(CaptureBuffer,
+                                (PVOID)lpBuffer,
+                                SizeBytes,
+                                (PVOID*)&WriteConsoleRequest->Buffer);
+        WriteConsoleRequest->UsingStaticBuffer = FALSE;
+    }
 
     /* Call the server */
     CsrClientCallServer((PCSR_API_MESSAGE)&ApiMessage,
@@ -459,48 +695,49 @@ IntWriteConsole(HANDLE hConsoleOutput,
                         sizeof(*WriteConsoleRequest));
 
     /* Check for success */
-    if (NT_SUCCESS(ApiMessage.Status))
-    {
-        if (lpNumberOfCharsWritten != NULL)
-            *lpNumberOfCharsWritten = WriteConsoleRequest->NrCharactersWritten;
+    Success = NT_SUCCESS(ApiMessage.Status);
+
+    /* Release the capture buffer if needed */
+    if (CaptureBuffer) CsrFreeCaptureBuffer(CaptureBuffer);
 
-        bRet = TRUE;
+    /* Retrieve the results */
+    if (Success)
+    {
+        _SEH2_TRY
+        {
+            *lpNumberOfCharsWritten = WriteConsoleRequest->NumBytes / CharSize;
+        }
+        _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
+        {
+            SetLastError(ERROR_INVALID_ACCESS);
+            Success = FALSE;
+        }
+        _SEH2_END;
     }
     else
     {
-        if (lpNumberOfCharsWritten != NULL)
-            *lpNumberOfCharsWritten = 0;
-
-        /* Error out */
         BaseSetLastNTError(ApiMessage.Status);
-        bRet = FALSE;
     }
 
-    CsrFreeCaptureBuffer(CaptureBuffer);
-
-    return bRet;
+    /* Return success status */
+    return Success;
 }
 
 
 static
 BOOL
-IntWriteConsoleInput(HANDLE hConsoleInput,
-                     PINPUT_RECORD lpBuffer,
-                     DWORD nLength,
-                     LPDWORD lpNumberOfEventsWritten,
-                     BOOLEAN bUnicode,
-                     BOOLEAN bAppendToEnd)
+IntWriteConsoleInput(IN HANDLE hConsoleInput,
+                     IN PINPUT_RECORD lpBuffer,
+                     IN DWORD nLength,
+                     OUT LPDWORD lpNumberOfEventsWritten,
+                     IN BOOLEAN bUnicode,
+                     IN BOOLEAN bAppendToEnd)
 {
+    BOOL Success;
     CONSOLE_API_MESSAGE ApiMessage;
     PCONSOLE_WRITEINPUT WriteInputRequest = &ApiMessage.Data.WriteInputRequest;
     PCSR_CAPTURE_BUFFER CaptureBuffer = NULL;
 
-    if (lpBuffer == NULL)
-    {
-        SetLastError(ERROR_INVALID_ACCESS);
-        return FALSE;
-    }
-
     DPRINT("IntWriteConsoleInput: %lx %p\n", nLength, lpNumberOfEventsWritten);
 
     /* Set up the data to send to the Console Server */
@@ -521,9 +758,18 @@ IntWriteConsoleInput(HANDLE hConsoleInput,
         WriteInputRequest->RecordBufPtr = WriteInputRequest->RecordStaticBuffer;
         // CaptureBuffer = NULL;
 
-        RtlCopyMemory(WriteInputRequest->RecordBufPtr,
-                      lpBuffer,
-                      nLength * sizeof(INPUT_RECORD));
+        _SEH2_TRY
+        {
+            RtlCopyMemory(WriteInputRequest->RecordBufPtr,
+                          lpBuffer,
+                          nLength * sizeof(INPUT_RECORD));
+        }
+        _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
+        {
+            SetLastError(ERROR_INVALID_ACCESS);
+            _SEH2_YIELD(return FALSE);
+        }
+        _SEH2_END;
     }
     else
     {
@@ -551,84 +797,143 @@ IntWriteConsoleInput(HANDLE hConsoleInput,
                         CSR_CREATE_API_NUMBER(CONSRV_SERVERDLL_INDEX, ConsolepWriteConsoleInput),
                         sizeof(*WriteInputRequest));
 
+    /* Check for success */
+    Success = NT_SUCCESS(ApiMessage.Status);
+
     /* Release the capture buffer if needed */
     if (CaptureBuffer) CsrFreeCaptureBuffer(CaptureBuffer);
 
-    /* Check for success */
-    if (NT_SUCCESS(ApiMessage.Status))
+    /* Retrieve the results */
+    _SEH2_TRY
     {
-        /* Return the number of events written */
         DPRINT("Events written: %lx\n", WriteInputRequest->NumRecords);
+        *lpNumberOfEventsWritten = WriteInputRequest->NumRecords;
 
-        if (lpNumberOfEventsWritten != NULL)
-            *lpNumberOfEventsWritten = WriteInputRequest->NumRecords;
+        if (!Success)
+            BaseSetLastNTError(ApiMessage.Status);
     }
-    else
+    _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
     {
-        if (lpNumberOfEventsWritten != NULL)
-            *lpNumberOfEventsWritten = 0;
-
-        /* Error out */
-        BaseSetLastNTError(ApiMessage.Status);
+        SetLastError(ERROR_INVALID_ACCESS);
+        Success = FALSE;
     }
+    _SEH2_END;
 
-    /* Return TRUE or FALSE */
-    return NT_SUCCESS(ApiMessage.Status);
+    /* Return success status */
+    return Success;
 }
 
 
 static
 BOOL
-IntWriteConsoleOutput(HANDLE hConsoleOutput,
-                      CONST CHAR_INFO *lpBuffer,
-                      COORD dwBufferSize,
-                      COORD dwBufferCoord,
-                      PSMALL_RECT lpWriteRegion,
-                      BOOL bUnicode)
+IntWriteConsoleOutput(IN HANDLE hConsoleOutput,
+                      IN CONST CHAR_INFO *lpBuffer,
+                      IN COORD dwBufferSize,
+                      IN COORD dwBufferCoord,
+                      IN OUT PSMALL_RECT lpWriteRegion,
+                      IN BOOLEAN bUnicode)
 {
+    BOOL Success;
     CONSOLE_API_MESSAGE ApiMessage;
     PCONSOLE_WRITEOUTPUT WriteOutputRequest = &ApiMessage.Data.WriteOutputRequest;
-    PCSR_CAPTURE_BUFFER CaptureBuffer;
-    ULONG Size;
+    PCSR_CAPTURE_BUFFER CaptureBuffer = NULL;
 
-    if ((lpBuffer == NULL) || (lpWriteRegion == NULL))
+    SHORT SizeX, SizeY;
+    ULONG NumCells;
+
+    /* Set up the data to send to the Console Server */
+    WriteOutputRequest->ConsoleHandle = NtCurrentPeb()->ProcessParameters->ConsoleHandle;
+    WriteOutputRequest->OutputHandle  = hConsoleOutput;
+    WriteOutputRequest->Unicode       = bUnicode;
+
+    /* Update lpWriteRegion */
+    _SEH2_TRY
+    {
+        SizeX = min(dwBufferSize.X - dwBufferCoord.X, ConioRectWidth(lpWriteRegion));
+        SizeY = min(dwBufferSize.Y - dwBufferCoord.Y, ConioRectHeight(lpWriteRegion));
+        if (SizeX <= 0 || SizeY <= 0)
+        {
+            SetLastError(ERROR_INVALID_PARAMETER);
+            _SEH2_YIELD(return FALSE);
+        }
+        lpWriteRegion->Right  = lpWriteRegion->Left + SizeX - 1;
+        lpWriteRegion->Bottom = lpWriteRegion->Top  + SizeY - 1;
+
+        WriteOutputRequest->WriteRegion = *lpWriteRegion;
+    }
+    _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
     {
         SetLastError(ERROR_INVALID_ACCESS);
-        return FALSE;
+        _SEH2_YIELD(return FALSE);
     }
+    _SEH2_END;
+
+    NumCells = SizeX * SizeY;
+    DPRINT("IntWriteConsoleOutput: (%d x %d)\n", SizeX, SizeY);
+
     /*
-    if (lpWriteRegion == NULL)
+     * For optimization purposes, Windows (and hence ReactOS, too, for
+     * compatibility reasons) uses a static buffer if no more than one
+     * cell is written. Otherwise a new buffer is allocated.
+     * This behaviour is also expected in the server-side.
+     */
+    if (NumCells <= 1)
     {
-        SetLastError(ERROR_INVALID_PARAMETER);
-        return FALSE;
+        WriteOutputRequest->CharInfo = &WriteOutputRequest->StaticBuffer;
+        // CaptureBuffer = NULL;
     }
-    */
+    else
+    {
+        ULONG Size = NumCells * sizeof(CHAR_INFO);
 
-    Size = dwBufferSize.Y * dwBufferSize.X * sizeof(CHAR_INFO);
+        /* Allocate a Capture Buffer */
+        CaptureBuffer = CsrAllocateCaptureBuffer(1, Size);
+        if (CaptureBuffer == NULL)
+        {
+            DPRINT1("CsrAllocateCaptureBuffer failed with size %ld!\n", Size);
+            SetLastError(ERROR_NOT_ENOUGH_MEMORY);
+            return FALSE;
+        }
 
-    DPRINT("IntWriteConsoleOutput: %lx %p\n", Size, lpWriteRegion);
+        /* Allocate space in the Buffer */
+        CsrAllocateMessagePointer(CaptureBuffer,
+                                  Size,
+                                  (PVOID*)&WriteOutputRequest->CharInfo);
+    }
 
-    /* Allocate a Capture Buffer */
-    CaptureBuffer = CsrAllocateCaptureBuffer(1, Size);
-    if (CaptureBuffer == NULL)
+    /* Capture the user buffer contents */
+    _SEH2_TRY
     {
-        DPRINT1("CsrAllocateCaptureBuffer failed!\n");
-        SetLastError(ERROR_NOT_ENOUGH_MEMORY);
-        return FALSE;
-    }
+#if 0
+        SHORT x, X;
+#endif
+        SHORT y, Y;
+
+        /* Copy into the buffer */
 
-    /* Capture the user buffer */
-    CsrCaptureMessageBuffer(CaptureBuffer,
-                            (PVOID)lpBuffer,
-                            Size,
-                            (PVOID*)&WriteOutputRequest->CharInfo);
+        SizeX = WriteOutputRequest->WriteRegion.Right -
+                WriteOutputRequest->WriteRegion.Left + 1;
 
-    /* Set up the data to send to the Console Server */
-    WriteOutputRequest->OutputHandle = hConsoleOutput;
-    WriteOutputRequest->Unicode = bUnicode;
-    WriteOutputRequest->BufferSize = dwBufferSize;
-    WriteOutputRequest->BufferCoord = dwBufferCoord;
-    WriteOutputRequest->WriteRegion = *lpWriteRegion;
+        for (y = 0, Y = WriteOutputRequest->WriteRegion.Top; Y <= WriteOutputRequest->WriteRegion.Bottom; ++y, ++Y)
+        {
+            RtlCopyMemory(WriteOutputRequest->CharInfo + y * SizeX,
+                          lpBuffer + (y + dwBufferCoord.Y) * dwBufferSize.X + dwBufferCoord.X,
+                          SizeX * sizeof(CHAR_INFO));
+#if 0
+            for (x = 0, X = WriteOutputRequest->WriteRegion.Left; X <= WriteOutputRequest->WriteRegion.Right; ++x, ++X)
+            {
+                *(WriteOutputRequest->CharInfo + y * SizeX + x) =
+                *(lpBuffer + (y + dwBufferCoord.Y) * dwBufferSize.X + (x + dwBufferCoord.X));
+            }
+#endif
+        }
+    }
+    _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
+    {
+        SetLastError(ERROR_INVALID_ACCESS);
+        _SEH2_YIELD(return FALSE);
+    }
+    _SEH2_END;
 
     /* Call the server */
     CsrClientCallServer((PCSR_API_MESSAGE)&ApiMessage,
@@ -637,41 +942,51 @@ IntWriteConsoleOutput(HANDLE hConsoleOutput,
                         sizeof(*WriteOutputRequest));
 
     /* Check for success */
-    if (!NT_SUCCESS(ApiMessage.Status))
-    {
-        /* Error out */
-        BaseSetLastNTError(ApiMessage.Status);
-    }
+    Success = NT_SUCCESS(ApiMessage.Status);
 
-    /* Return the read region */
-    DPRINT("read region: %p\n", WriteOutputRequest->WriteRegion);
-    *lpWriteRegion = WriteOutputRequest->WriteRegion;
+    /* Release the capture buffer if needed */
+    if (CaptureBuffer) CsrFreeCaptureBuffer(CaptureBuffer);
+
+    /* Retrieve the results */
+    _SEH2_TRY
+    {
+        *lpWriteRegion = WriteOutputRequest->WriteRegion;
 
-    /* Release the capture buffer */
-    CsrFreeCaptureBuffer(CaptureBuffer);
+        if (!Success)
+            BaseSetLastNTError(ApiMessage.Status);
+    }
+    _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
+    {
+        SetLastError(ERROR_INVALID_ACCESS);
+        Success = FALSE;
+    }
+    _SEH2_END;
 
-    /* Return TRUE or FALSE */
-    return NT_SUCCESS(ApiMessage.Status);
+    /* Return success status */
+    return Success;
 }
 
 
 static
 BOOL
-IntWriteConsoleOutputCode(HANDLE hConsoleOutput,
-                          CODE_TYPE CodeType,
-                          CONST VOID *pCode,
-                          DWORD nLength,
-                          COORD dwWriteCoord,
-                          LPDWORD lpNumberOfCodesWritten)
+IntWriteConsoleOutputCode(IN HANDLE hConsoleOutput,
+                          IN CODE_TYPE CodeType,
+                          IN CONST VOID *pCode,
+                          IN DWORD nLength,
+                          IN COORD dwWriteCoord,
+                          OUT LPDWORD lpNumberOfCodesWritten)
 {
+    BOOL Success;
     CONSOLE_API_MESSAGE ApiMessage;
     PCONSOLE_WRITEOUTPUTCODE WriteOutputCodeRequest = &ApiMessage.Data.WriteOutputCodeRequest;
     PCSR_CAPTURE_BUFFER CaptureBuffer = NULL;
-    ULONG SizeBytes, CodeSize;
+    ULONG CodeSize, SizeBytes;
 
-    if (pCode == NULL)
+    if ( (CodeType != CODE_ASCII    ) &&
+         (CodeType != CODE_UNICODE  ) &&
+         (CodeType != CODE_ATTRIBUTE) )
     {
-        SetLastError(ERROR_INVALID_ACCESS);
+        SetLastError(ERROR_INVALID_PARAMETER);
         return FALSE;
     }
 
@@ -688,20 +1003,16 @@ IntWriteConsoleOutputCode(HANDLE hConsoleOutput,
     switch (CodeType)
     {
         case CODE_ASCII:
-            CodeSize = sizeof(CHAR);
+            CodeSize = RTL_FIELD_SIZE(CODE_ELEMENT, AsciiChar);
             break;
 
         case CODE_UNICODE:
-            CodeSize = sizeof(WCHAR);
+            CodeSize = RTL_FIELD_SIZE(CODE_ELEMENT, UnicodeChar);
             break;
 
         case CODE_ATTRIBUTE:
-            CodeSize = sizeof(WORD);
+            CodeSize = RTL_FIELD_SIZE(CODE_ELEMENT, Attribute);
             break;
-
-        default:
-            SetLastError(ERROR_INVALID_PARAMETER);
-            return FALSE;
     }
     SizeBytes = nLength * CodeSize;
 
@@ -713,12 +1024,21 @@ IntWriteConsoleOutputCode(HANDLE hConsoleOutput,
      */
     if (SizeBytes <= sizeof(WriteOutputCodeRequest->CodeStaticBuffer))
     {
-        WriteOutputCodeRequest->pCode.pCode = WriteOutputCodeRequest->CodeStaticBuffer;
+        WriteOutputCodeRequest->pCode = WriteOutputCodeRequest->CodeStaticBuffer;
         // CaptureBuffer = NULL;
 
-        RtlCopyMemory(WriteOutputCodeRequest->pCode.pCode,
-                      pCode,
-                      SizeBytes);
+        _SEH2_TRY
+        {
+            RtlCopyMemory(WriteOutputCodeRequest->pCode,
+                          pCode,
+                          SizeBytes);
+        }
+        _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
+        {
+            SetLastError(ERROR_INVALID_ACCESS);
+            _SEH2_YIELD(return FALSE);
+        }
+        _SEH2_END;
     }
     else
     {
@@ -735,7 +1055,7 @@ IntWriteConsoleOutputCode(HANDLE hConsoleOutput,
         CsrCaptureMessageBuffer(CaptureBuffer,
                                 (PVOID)pCode,
                                 SizeBytes,
-                                (PVOID*)&WriteOutputCodeRequest->pCode.pCode);
+                                (PVOID*)&WriteOutputCodeRequest->pCode);
     }
 
     /* Call the server */
@@ -744,66 +1064,62 @@ IntWriteConsoleOutputCode(HANDLE hConsoleOutput,
                         CSR_CREATE_API_NUMBER(CONSRV_SERVERDLL_INDEX, ConsolepWriteConsoleOutputString),
                         sizeof(*WriteOutputCodeRequest));
 
+    /* Check for success */
+    Success = NT_SUCCESS(ApiMessage.Status);
+
     /* Release the capture buffer if needed */
     if (CaptureBuffer) CsrFreeCaptureBuffer(CaptureBuffer);
 
-    /* Check for success */
-    if (NT_SUCCESS(ApiMessage.Status))
+    /* Retrieve the results */
+    _SEH2_TRY
     {
-        if (lpNumberOfCodesWritten != NULL)
-            *lpNumberOfCodesWritten = WriteOutputCodeRequest->NumCodes;
+        *lpNumberOfCodesWritten = WriteOutputCodeRequest->NumCodes;
+
+        if (!Success)
+            BaseSetLastNTError(ApiMessage.Status);
     }
-    else
+    _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
     {
-        if (lpNumberOfCodesWritten != NULL)
-            *lpNumberOfCodesWritten = 0;
-
-        /* Error out */
-        BaseSetLastNTError(ApiMessage.Status);
+        SetLastError(ERROR_INVALID_ACCESS);
+        Success = FALSE;
     }
+    _SEH2_END;
 
-    /* Return TRUE or FALSE */
-    return NT_SUCCESS(ApiMessage.Status);
+    /* Return success status */
+    return Success;
 }
 
 
 static
 BOOL
-IntFillConsoleOutputCode(HANDLE hConsoleOutput,
-                         CODE_TYPE CodeType,
-                         PVOID pCode,
-                         DWORD nLength,
-                         COORD dwWriteCoord,
-                         LPDWORD lpNumberOfCodesWritten)
+IntFillConsoleOutputCode(IN HANDLE hConsoleOutput,
+                         IN CODE_TYPE CodeType,
+                         IN CODE_ELEMENT Code,
+                         IN DWORD nLength,
+                         IN COORD dwWriteCoord,
+                         OUT LPDWORD lpNumberOfCodesWritten)
 {
+    BOOL Success;
     CONSOLE_API_MESSAGE ApiMessage;
     PCONSOLE_FILLOUTPUTCODE FillOutputRequest = &ApiMessage.Data.FillOutputRequest;
 
+    DPRINT("IntFillConsoleOutputCode\n");
+
+    if ( (CodeType != CODE_ASCII    ) &&
+         (CodeType != CODE_UNICODE  ) &&
+         (CodeType != CODE_ATTRIBUTE) )
+    {
+        SetLastError(ERROR_INVALID_PARAMETER);
+        return FALSE;
+    }
+
     /* Set up the data to send to the Console Server */
     FillOutputRequest->ConsoleHandle = NtCurrentPeb()->ProcessParameters->ConsoleHandle;
     FillOutputRequest->OutputHandle  = hConsoleOutput;
     FillOutputRequest->WriteCoord    = dwWriteCoord;
-    FillOutputRequest->NumCodes      = nLength;
-
     FillOutputRequest->CodeType = CodeType;
-    switch (CodeType)
-    {
-        case CODE_ASCII:
-            FillOutputRequest->Code.AsciiChar = *(PCHAR)pCode;
-            break;
-
-        case CODE_UNICODE:
-            FillOutputRequest->Code.UnicodeChar = *(PWCHAR)pCode;
-            break;
-
-        case CODE_ATTRIBUTE:
-            FillOutputRequest->Code.Attribute = *(PWORD)pCode;
-            break;
-
-        default:
-            SetLastError(ERROR_INVALID_PARAMETER);
-            return FALSE;
-    }
+    FillOutputRequest->Code     = Code;
+    FillOutputRequest->NumCodes = nLength;
 
     /* Call the server */
     CsrClientCallServer((PCSR_API_MESSAGE)&ApiMessage,
@@ -812,21 +1128,25 @@ IntFillConsoleOutputCode(HANDLE hConsoleOutput,
                         sizeof(*FillOutputRequest));
 
     /* Check for success */
-    if (NT_SUCCESS(ApiMessage.Status))
+    Success = NT_SUCCESS(ApiMessage.Status);
+
+    /* Retrieve the results */
+    _SEH2_TRY
     {
-        if (lpNumberOfCodesWritten != NULL)
-            *lpNumberOfCodesWritten = FillOutputRequest->NumCodes;
+        *lpNumberOfCodesWritten = FillOutputRequest->NumCodes;
+
+        if (!Success)
+            BaseSetLastNTError(ApiMessage.Status);
     }
-    else
+    _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
     {
-        if (lpNumberOfCodesWritten != NULL)
-            *lpNumberOfCodesWritten = 0;
-
-        BaseSetLastNTError(ApiMessage.Status);
+        SetLastError(ERROR_INVALID_ACCESS);
+        Success = FALSE;
     }
+    _SEH2_END;
 
-    /* Return TRUE or FALSE */
-    return NT_SUCCESS(ApiMessage.Status);
+    /* Return success status */
+    return Success;
 }
 
 
@@ -843,11 +1163,11 @@ IntFillConsoleOutputCode(HANDLE hConsoleOutput,
  */
 BOOL
 WINAPI
-ReadConsoleW(HANDLE hConsoleInput,
-             LPVOID lpBuffer,
-             DWORD nNumberOfCharsToRead,
-             LPDWORD lpNumberOfCharsRead,
-             PCONSOLE_READCONSOLE_CONTROL pInputControl)
+ReadConsoleW(IN HANDLE hConsoleInput,
+             OUT LPVOID lpBuffer,
+             IN DWORD nNumberOfCharsToRead,
+             OUT LPDWORD lpNumberOfCharsRead,
+             IN PCONSOLE_READCONSOLE_CONTROL pInputControl OPTIONAL)
 {
     return IntReadConsole(hConsoleInput,
                           lpBuffer,
@@ -865,11 +1185,11 @@ ReadConsoleW(HANDLE hConsoleInput,
  */
 BOOL
 WINAPI
-ReadConsoleA(HANDLE hConsoleInput,
-             LPVOID lpBuffer,
-             DWORD nNumberOfCharsToRead,
-             LPDWORD lpNumberOfCharsRead,
-             PCONSOLE_READCONSOLE_CONTROL pInputControl)
+ReadConsoleA(IN HANDLE hConsoleInput,
+             OUT LPVOID lpBuffer,
+             IN DWORD nNumberOfCharsToRead,
+             OUT LPDWORD lpNumberOfCharsRead,
+             IN PCONSOLE_READCONSOLE_CONTROL pInputControl OPTIONAL)
 {
     return IntReadConsole(hConsoleInput,
                           lpBuffer,
@@ -887,10 +1207,10 @@ ReadConsoleA(HANDLE hConsoleInput,
  */
 BOOL
 WINAPI
-PeekConsoleInputW(HANDLE hConsoleInput,
-                  PINPUT_RECORD lpBuffer,
-                  DWORD nLength,
-                  LPDWORD lpNumberOfEventsRead)
+PeekConsoleInputW(IN HANDLE hConsoleInput,
+                  OUT PINPUT_RECORD lpBuffer,
+                  IN DWORD nLength,
+                  OUT LPDWORD lpNumberOfEventsRead)
 {
     return IntGetConsoleInput(hConsoleInput,
                               lpBuffer,
@@ -908,10 +1228,10 @@ PeekConsoleInputW(HANDLE hConsoleInput,
  */
 BOOL
 WINAPI
-PeekConsoleInputA(HANDLE hConsoleInput,
-                  PINPUT_RECORD lpBuffer,
-                  DWORD nLength,
-                  LPDWORD lpNumberOfEventsRead)
+PeekConsoleInputA(IN HANDLE hConsoleInput,
+                  OUT PINPUT_RECORD lpBuffer,
+                  IN DWORD nLength,
+                  OUT LPDWORD lpNumberOfEventsRead)
 {
     return IntGetConsoleInput(hConsoleInput,
                               lpBuffer,
@@ -929,10 +1249,10 @@ PeekConsoleInputA(HANDLE hConsoleInput,
  */
 BOOL
 WINAPI
-ReadConsoleInputW(HANDLE hConsoleInput,
-                  PINPUT_RECORD lpBuffer,
-                  DWORD nLength,
-                  LPDWORD lpNumberOfEventsRead)
+ReadConsoleInputW(IN HANDLE hConsoleInput,
+                  OUT PINPUT_RECORD lpBuffer,
+                  IN DWORD nLength,
+                  OUT LPDWORD lpNumberOfEventsRead)
 {
     return IntGetConsoleInput(hConsoleInput,
                               lpBuffer,
@@ -950,10 +1270,10 @@ ReadConsoleInputW(HANDLE hConsoleInput,
  */
 BOOL
 WINAPI
-ReadConsoleInputA(HANDLE hConsoleInput,
-                  PINPUT_RECORD lpBuffer,
-                  DWORD nLength,
-                  LPDWORD lpNumberOfEventsRead)
+ReadConsoleInputA(IN HANDLE hConsoleInput,
+                  OUT PINPUT_RECORD lpBuffer,
+                  IN DWORD nLength,
+                  OUT LPDWORD lpNumberOfEventsRead)
 {
     return IntGetConsoleInput(hConsoleInput,
                               lpBuffer,
@@ -971,11 +1291,11 @@ ReadConsoleInputA(HANDLE hConsoleInput,
  */
 BOOL
 WINAPI
-ReadConsoleInputExW(HANDLE hConsoleInput,
-                    PINPUT_RECORD lpBuffer,
-                    DWORD nLength,
-                    LPDWORD lpNumberOfEventsRead,
-                    WORD wFlags)
+ReadConsoleInputExW(IN HANDLE hConsoleInput,
+                    OUT PINPUT_RECORD lpBuffer,
+                    IN DWORD nLength,
+                    OUT LPDWORD lpNumberOfEventsRead,
+                    IN WORD wFlags)
 {
     return IntGetConsoleInput(hConsoleInput,
                               lpBuffer,
@@ -993,11 +1313,11 @@ ReadConsoleInputExW(HANDLE hConsoleInput,
  */
 BOOL
 WINAPI
-ReadConsoleInputExA(HANDLE hConsoleInput,
-                    PINPUT_RECORD lpBuffer,
-                    DWORD nLength,
-                    LPDWORD lpNumberOfEventsRead,
-                    WORD wFlags)
+ReadConsoleInputExA(IN HANDLE hConsoleInput,
+                    OUT PINPUT_RECORD lpBuffer,
+                    IN DWORD nLength,
+                    OUT LPDWORD lpNumberOfEventsRead,
+                    IN WORD wFlags)
 {
     return IntGetConsoleInput(hConsoleInput,
                               lpBuffer,
@@ -1015,11 +1335,11 @@ ReadConsoleInputExA(HANDLE hConsoleInput,
  */
 BOOL
 WINAPI
-ReadConsoleOutputW(HANDLE hConsoleOutput,
-                   PCHAR_INFO lpBuffer,
-                   COORD dwBufferSize,
-                   COORD dwBufferCoord,
-                   PSMALL_RECT lpReadRegion)
+ReadConsoleOutputW(IN HANDLE hConsoleOutput,
+                   OUT PCHAR_INFO lpBuffer,
+                   IN COORD dwBufferSize,
+                   IN COORD dwBufferCoord,
+                   IN OUT PSMALL_RECT lpReadRegion)
 {
     return IntReadConsoleOutput(hConsoleOutput,
                                 lpBuffer,
@@ -1037,11 +1357,11 @@ ReadConsoleOutputW(HANDLE hConsoleOutput,
  */
 BOOL
 WINAPI
-ReadConsoleOutputA(HANDLE hConsoleOutput,
-                   PCHAR_INFO lpBuffer,
-                   COORD dwBufferSize,
-                   COORD dwBufferCoord,
-                   PSMALL_RECT lpReadRegion)
+ReadConsoleOutputA(IN HANDLE hConsoleOutput,
+                   OUT PCHAR_INFO lpBuffer,
+                   IN COORD dwBufferSize,
+                   IN COORD dwBufferCoord,
+                   IN OUT PSMALL_RECT lpReadRegion)
 {
     return IntReadConsoleOutput(hConsoleOutput,
                                 lpBuffer,
@@ -1059,11 +1379,11 @@ ReadConsoleOutputA(HANDLE hConsoleOutput,
  */
 BOOL
 WINAPI
-ReadConsoleOutputCharacterW(HANDLE hConsoleOutput,
-                            LPWSTR lpCharacter,
-                            DWORD nLength,
-                            COORD dwReadCoord,
-                            LPDWORD lpNumberOfCharsRead)
+ReadConsoleOutputCharacterW(IN HANDLE hConsoleOutput,
+                            OUT LPWSTR lpCharacter,
+                            IN DWORD nLength,
+                            IN COORD dwReadCoord,
+                            OUT LPDWORD lpNumberOfCharsRead)
 {
     return IntReadConsoleOutputCode(hConsoleOutput,
                                     CODE_UNICODE,
@@ -1081,11 +1401,11 @@ ReadConsoleOutputCharacterW(HANDLE hConsoleOutput,
  */
 BOOL
 WINAPI
-ReadConsoleOutputCharacterA(HANDLE hConsoleOutput,
-                            LPSTR lpCharacter,
-                            DWORD nLength,
-                            COORD dwReadCoord,
-                            LPDWORD lpNumberOfCharsRead)
+ReadConsoleOutputCharacterA(IN HANDLE hConsoleOutput,
+                            OUT LPSTR lpCharacter,
+                            IN DWORD nLength,
+                            IN COORD dwReadCoord,
+                            OUT LPDWORD lpNumberOfCharsRead)
 {
     return IntReadConsoleOutputCode(hConsoleOutput,
                                     CODE_ASCII,
@@ -1103,11 +1423,11 @@ ReadConsoleOutputCharacterA(HANDLE hConsoleOutput,
  */
 BOOL
 WINAPI
-ReadConsoleOutputAttribute(HANDLE hConsoleOutput,
-                           LPWORD lpAttribute,
-                           DWORD nLength,
-                           COORD dwReadCoord,
-                           LPDWORD lpNumberOfAttrsRead)
+ReadConsoleOutputAttribute(IN HANDLE hConsoleOutput,
+                           OUT LPWORD lpAttribute,
+                           IN DWORD nLength,
+                           IN COORD dwReadCoord,
+                           OUT LPDWORD lpNumberOfAttrsRead)
 {
     return IntReadConsoleOutputCode(hConsoleOutput,
                                     CODE_ATTRIBUTE,
@@ -1129,10 +1449,10 @@ ReadConsoleOutputAttribute(HANDLE hConsoleOutput,
  */
 BOOL
 WINAPI
-WriteConsoleW(HANDLE hConsoleOutput,
-              CONST VOID *lpBuffer,
-              DWORD nNumberOfCharsToWrite,
-              LPDWORD lpNumberOfCharsWritten,
+WriteConsoleW(IN HANDLE hConsoleOutput,
+              IN CONST VOID *lpBuffer,
+              IN DWORD nNumberOfCharsToWrite,
+              OUT LPDWORD lpNumberOfCharsWritten,
               LPVOID lpReserved)
 {
     return IntWriteConsole(hConsoleOutput,
@@ -1151,10 +1471,10 @@ WriteConsoleW(HANDLE hConsoleOutput,
  */
 BOOL
 WINAPI
-WriteConsoleA(HANDLE hConsoleOutput,
-              CONST VOID *lpBuffer,
-              DWORD nNumberOfCharsToWrite,
-              LPDWORD lpNumberOfCharsWritten,
+WriteConsoleA(IN HANDLE hConsoleOutput,
+              IN CONST VOID *lpBuffer,
+              IN DWORD nNumberOfCharsToWrite,
+              OUT LPDWORD lpNumberOfCharsWritten,
               LPVOID lpReserved)
 {
     return IntWriteConsole(hConsoleOutput,
@@ -1173,10 +1493,10 @@ WriteConsoleA(HANDLE hConsoleOutput,
  */
 BOOL
 WINAPI
-WriteConsoleInputW(HANDLE hConsoleInput,
-                   CONST INPUT_RECORD *lpBuffer,
-                   DWORD nLength,
-                   LPDWORD lpNumberOfEventsWritten)
+WriteConsoleInputW(IN HANDLE hConsoleInput,
+                   IN CONST INPUT_RECORD *lpBuffer,
+                   IN DWORD nLength,
+                   OUT LPDWORD lpNumberOfEventsWritten)
 {
     return IntWriteConsoleInput(hConsoleInput,
                                 (PINPUT_RECORD)lpBuffer,
@@ -1194,10 +1514,10 @@ WriteConsoleInputW(HANDLE hConsoleInput,
  */
 BOOL
 WINAPI
-WriteConsoleInputA(HANDLE hConsoleInput,
-                   CONST INPUT_RECORD *lpBuffer,
-                   DWORD nLength,
-                   LPDWORD lpNumberOfEventsWritten)
+WriteConsoleInputA(IN HANDLE hConsoleInput,
+                   IN CONST INPUT_RECORD *lpBuffer,
+                   IN DWORD nLength,
+                   OUT LPDWORD lpNumberOfEventsWritten)
 {
     return IntWriteConsoleInput(hConsoleInput,
                                 (PINPUT_RECORD)lpBuffer,
@@ -1215,10 +1535,10 @@ WriteConsoleInputA(HANDLE hConsoleInput,
  */
 BOOL
 WINAPI
-WriteConsoleInputVDMW(HANDLE hConsoleInput,
-                      CONST INPUT_RECORD *lpBuffer,
-                      DWORD nLength,
-                      LPDWORD lpNumberOfEventsWritten)
+WriteConsoleInputVDMW(IN HANDLE hConsoleInput,
+                      IN CONST INPUT_RECORD *lpBuffer,
+                      IN DWORD nLength,
+                      OUT LPDWORD lpNumberOfEventsWritten)
 {
     return IntWriteConsoleInput(hConsoleInput,
                                 (PINPUT_RECORD)lpBuffer,
@@ -1236,10 +1556,10 @@ WriteConsoleInputVDMW(HANDLE hConsoleInput,
  */
 BOOL
 WINAPI
-WriteConsoleInputVDMA(HANDLE hConsoleInput,
-                      CONST INPUT_RECORD *lpBuffer,
-                      DWORD nLength,
-                      LPDWORD lpNumberOfEventsWritten)
+WriteConsoleInputVDMA(IN HANDLE hConsoleInput,
+                      IN CONST INPUT_RECORD *lpBuffer,
+                      IN DWORD nLength,
+                      OUT LPDWORD lpNumberOfEventsWritten)
 {
     return IntWriteConsoleInput(hConsoleInput,
                                 (PINPUT_RECORD)lpBuffer,
@@ -1257,11 +1577,11 @@ WriteConsoleInputVDMA(HANDLE hConsoleInput,
  */
 BOOL
 WINAPI
-WriteConsoleOutputW(HANDLE hConsoleOutput,
-                    CONST CHAR_INFO *lpBuffer,
-                    COORD dwBufferSize,
-                    COORD dwBufferCoord,
-                    PSMALL_RECT lpWriteRegion)
+WriteConsoleOutputW(IN HANDLE hConsoleOutput,
+                    IN CONST CHAR_INFO *lpBuffer,
+                    IN COORD dwBufferSize,
+                    IN COORD dwBufferCoord,
+                    IN OUT PSMALL_RECT lpWriteRegion)
 {
     return IntWriteConsoleOutput(hConsoleOutput,
                                  lpBuffer,
@@ -1279,11 +1599,11 @@ WriteConsoleOutputW(HANDLE hConsoleOutput,
  */
 BOOL
 WINAPI
-WriteConsoleOutputA(HANDLE hConsoleOutput,
-                    CONST CHAR_INFO *lpBuffer,
-                    COORD dwBufferSize,
-                    COORD dwBufferCoord,
-                    PSMALL_RECT lpWriteRegion)
+WriteConsoleOutputA(IN HANDLE hConsoleOutput,
+                    IN CONST CHAR_INFO *lpBuffer,
+                    IN COORD dwBufferSize,
+                    IN COORD dwBufferCoord,
+                    IN OUT PSMALL_RECT lpWriteRegion)
 {
     return IntWriteConsoleOutput(hConsoleOutput,
                                  lpBuffer,
@@ -1301,11 +1621,11 @@ WriteConsoleOutputA(HANDLE hConsoleOutput,
  */
 BOOL
 WINAPI
-WriteConsoleOutputCharacterW(HANDLE hConsoleOutput,
-                             LPCWSTR lpCharacter,
-                             DWORD nLength,
-                             COORD dwWriteCoord,
-                             LPDWORD lpNumberOfCharsWritten)
+WriteConsoleOutputCharacterW(IN HANDLE hConsoleOutput,
+                             IN LPCWSTR lpCharacter,
+                             IN DWORD nLength,
+                             IN COORD dwWriteCoord,
+                             OUT LPDWORD lpNumberOfCharsWritten)
 {
     return IntWriteConsoleOutputCode(hConsoleOutput,
                                      CODE_UNICODE,
@@ -1323,11 +1643,11 @@ WriteConsoleOutputCharacterW(HANDLE hConsoleOutput,
  */
 BOOL
 WINAPI
-WriteConsoleOutputCharacterA(HANDLE hConsoleOutput,
-                             LPCSTR lpCharacter,
-                             DWORD nLength,
-                             COORD dwWriteCoord,
-                             LPDWORD lpNumberOfCharsWritten)
+WriteConsoleOutputCharacterA(IN HANDLE hConsoleOutput,
+                             IN LPCSTR lpCharacter,
+                             IN DWORD nLength,
+                             IN COORD dwWriteCoord,
+                             OUT LPDWORD lpNumberOfCharsWritten)
 {
     return IntWriteConsoleOutputCode(hConsoleOutput,
                                      CODE_ASCII,
@@ -1345,11 +1665,11 @@ WriteConsoleOutputCharacterA(HANDLE hConsoleOutput,
  */
 BOOL
 WINAPI
-WriteConsoleOutputAttribute(HANDLE hConsoleOutput,
-                            CONST WORD *lpAttribute,
-                            DWORD nLength,
-                            COORD dwWriteCoord,
-                            LPDWORD lpNumberOfAttrsWritten)
+WriteConsoleOutputAttribute(IN HANDLE hConsoleOutput,
+                            IN CONST WORD *lpAttribute,
+                            IN DWORD nLength,
+                            IN COORD dwWriteCoord,
+                            OUT LPDWORD lpNumberOfAttrsWritten)
 {
     return IntWriteConsoleOutputCode(hConsoleOutput,
                                      CODE_ATTRIBUTE,
@@ -1367,15 +1687,17 @@ WriteConsoleOutputAttribute(HANDLE hConsoleOutput,
  */
 BOOL
 WINAPI
-FillConsoleOutputCharacterW(HANDLE hConsoleOutput,
-                            WCHAR cCharacter,
-                            DWORD nLength,
-                            COORD dwWriteCoord,
-                            LPDWORD lpNumberOfCharsWritten)
+FillConsoleOutputCharacterW(IN HANDLE hConsoleOutput,
+                            IN WCHAR cCharacter,
+                            IN DWORD nLength,
+                            IN COORD dwWriteCoord,
+                            OUT LPDWORD lpNumberOfCharsWritten)
 {
+    CODE_ELEMENT Code;
+    Code.UnicodeChar = cCharacter;
     return IntFillConsoleOutputCode(hConsoleOutput,
                                     CODE_UNICODE,
-                                    &cCharacter,
+                                    Code,
                                     nLength,
                                     dwWriteCoord,
                                     lpNumberOfCharsWritten);
@@ -1389,15 +1711,17 @@ FillConsoleOutputCharacterW(HANDLE hConsoleOutput,
  */
 BOOL
 WINAPI
-FillConsoleOutputCharacterA(HANDLE hConsoleOutput,
-                            CHAR cCharacter,
-                            DWORD nLength,
-                            COORD dwWriteCoord,
+FillConsoleOutputCharacterA(IN HANDLE hConsoleOutput,
+                            IN CHAR cCharacter,
+                            IN DWORD nLength,
+                            IN COORD dwWriteCoord,
                             LPDWORD lpNumberOfCharsWritten)
 {
+    CODE_ELEMENT Code;
+    Code.AsciiChar = cCharacter;
     return IntFillConsoleOutputCode(hConsoleOutput,
                                     CODE_ASCII,
-                                    &cCharacter,
+                                    Code,
                                     nLength,
                                     dwWriteCoord,
                                     lpNumberOfCharsWritten);
@@ -1411,15 +1735,17 @@ FillConsoleOutputCharacterA(HANDLE hConsoleOutput,
  */
 BOOL
 WINAPI
-FillConsoleOutputAttribute(HANDLE hConsoleOutput,
-                           WORD wAttribute,
-                           DWORD nLength,
-                           COORD dwWriteCoord,
-                           LPDWORD lpNumberOfAttrsWritten)
+FillConsoleOutputAttribute(IN HANDLE hConsoleOutput,
+                           IN WORD wAttribute,
+                           IN DWORD nLength,
+                           IN COORD dwWriteCoord,
+                           OUT LPDWORD lpNumberOfAttrsWritten)
 {
+    CODE_ELEMENT Code;
+    Code.Attribute = wAttribute;
     return IntFillConsoleOutputCode(hConsoleOutput,
                                     CODE_ATTRIBUTE,
-                                    &wAttribute,
+                                    Code,
                                     nLength,
                                     dwWriteCoord,
                                     lpNumberOfAttrsWritten);