Repository: microsoft/terminal
File: src/server/ApiDispatchers.cpp (C++)

Code before:
    RETURN_HR_IF_NULL(E_HANDLE, pObjectHandle);

    SCREEN_INFORMATION* pObj;
    RETURN_IF_FAILED(pObjectHandle->GetScreenBuffer(GENERIC_WRITE, &pObj));

    CONSOLE_SCREEN_BUFFER_INFOEX ex = { 0 };
    ex.cbSize = sizeof(CONSOLE_SCREEN_BUFFER_INFOEX);
    ex.bFullscreenSupported = a->FullscreenSupported;
    const auto ColorTableSizeInBytes = RTL_NUMBER_OF_V2(ex.ColorTable) * sizeof(*ex.ColorTable);
    CopyMemory(ex.ColorTable, a->ColorTable, ColorTableSizeInBytes);
    ex.dwCursorPosition = a->CursorPosition;
    ex.dwMaximumWindowSize = a->MaximumWindowSize;
    ex.dwSize = a->Size;
    ex.srWindow = { 0 };
    ex.srWindow.Left = a->ScrollPosition.X;
    ex.srWindow.Top = a->ScrollPosition.Y;
    ex.srWindow.Right = ex.srWindow.Left + a->CurrentWindowSize.X;
    ex.srWindow.Bottom = ex.srWindow.Top + a->CurrentWindowSize.Y;
    ex.wAttributes = a->Attributes;
    ex.wPopupAttributes = a->PopupAttributes;

    TraceConsoleAPICallWithOrigin("SetConsoleScreenBufferInfoEx",
                                  TraceLoggingInt32(a->Size.X, "BufferWidthInChars"),
                                  TraceLoggingInt32(a->Size.Y, "BufferHeightInChars"),
                                  TraceLoggingInt32(a->CurrentWindowSize.X, "WindowWidthInChars"),
                                  TraceLoggingInt32(a->CurrentWindowSize.Y, "WindowHeightInChars"),
                                  TraceLoggingInt32(a->MaximumWindowSize.X, "MaxWindowWidthInChars"),
                                  TraceLoggingInt32(a->MaximumWindowSize.Y, "MaxWindowHeightInChars"));

    return m->_pApiRoutines->SetConsoleScreenBufferInfoExImpl(*pObj, ex);
}

[[nodiscard]] HRESULT ApiDispatchers::ServerSetConsoleScreenBufferSize(_Inout_ CONSOLE_API_MSG* const m,
                                                                       _Inout_ BOOL* const /*pbReplyPending*/)
{
    Telemetry::Instance().LogApiCall(Telemetry::ApiCall::SetConsoleScreenBufferSize);
    const auto a = &m->u.consoleMsgL2.SetConsoleScreenBufferSize;

    const auto pObjectHandle = m->GetObjectHandle();
    RETURN_HR_IF_NULL(E_HANDLE, pObjectHandle);

    SCREEN_INFORMATION* pObj;
    RETURN_IF_FAILED(pObjectHandle->GetScreenBuffer(GENERIC_WRITE, &pObj));

    TraceConsoleAPICallWithOrigin("SetConsoleScreenBufferSize",
                                  TraceLoggingInt32(a->Size.X, "BufferWidthInChars"),
                                  TraceLoggingInt32(a->Size.Y, "BufferHeightInChars"));

    return m->_pApiRoutines->SetConsoleScreenBufferSizeImpl(*pObj, til::wrap_coord_size(a->Size));
}


Review comment (refactor):
nit 1: These have a different name than the actual fields.

nit 2: Technically these are Int16 right? We can improve the way `COORD`s are formatted in traces if we use `TraceLoggingStruct` here. If we add a new macro like `TraceLoggingConsoleCoord` that abstracts this away, I think it might look pretty neat. Something like
```cpp
#define TraceLoggingConsoleCoord(value, name) \
    TraceLoggingStruct(2, name), \
    TraceLoggingInt16(value.X, "X"), \
    TraceLoggingInt16(value.Y, "Y")
```

Alternatively, we could also use `TraceLoggingInt16FixedArray(&a->CurrentWindowSize, "CurrentWindowSize")`.

nit 3: People depend on our trace logs and we can't change their names? 😟

Code after:
    a->CurrentWindowSize.X = ex.srWindow.Right - ex.srWindow.Left;
    a->CurrentWindowSize.Y = ex.srWindow.Bottom - ex.srWindow.Top;
    a->Attributes = ex.wAttributes;
    a->PopupAttributes = ex.wPopupAttributes;

    return S_OK;
}

[[nodiscard]] HRESULT ApiDispatchers::ServerSetConsoleScreenBufferInfo(_Inout_ CONSOLE_API_MSG* const m,
                                                                       _Inout_ BOOL* const /*pbReplyPending*/)
{
    Telemetry::Instance().LogApiCall(Telemetry::ApiCall::SetConsoleScreenBufferInfoEx);
    const auto a = &m->u.consoleMsgL2.SetConsoleScreenBufferInfo;

    const auto pObjectHandle = m->GetObjectHandle();
    RETURN_HR_IF_NULL(E_HANDLE, pObjectHandle);

    SCREEN_INFORMATION* pObj;
    RETURN_IF_FAILED(pObjectHandle->GetScreenBuffer(GENERIC_WRITE, &pObj));

    CONSOLE_SCREEN_BUFFER_INFOEX ex = { 0 };
    ex.cbSize = sizeof(CONSOLE_SCREEN_BUFFER_INFOEX);
    ex.bFullscreenSupported = a->FullscreenSupported;
    const auto ColorTableSizeInBytes = RTL_NUMBER_OF_V2(ex.ColorTable) * sizeof(*ex.ColorTable);
    CopyMemory(ex.ColorTable, a->ColorTable, ColorTableSizeInBytes);
    ex.dwCursorPosition = a->CursorPosition;
    ex.dwMaximumWindowSize = a->MaximumWindowSize;
    ex.dwSize = a->Size;
    ex.srWindow = { 0 };
    ex.srWindow.Left = a->ScrollPosition.X;
    ex.srWindow.Top = a->ScrollPosition.Y;
    ex.srWindow.Right = ex.srWindow.Left + a->CurrentWindowSize.X;
    ex.srWindow.Bottom = ex.srWindow.Top + a->CurrentWindowSize.Y;
    ex.wAttributes = a->Attributes;
    ex.wPopupAttributes = a->PopupAttributes;

    TraceConsoleAPICallWithOrigin(
        "SetConsoleScreenBufferInfoEx",
        TraceLoggingConsoleCoord(a->Size, "BufferSize"),
        TraceLoggingConsoleCoord(a->CurrentWindowSize, "WindowSize"),
        TraceLoggingConsoleCoord(a->MaximumWindowSize, "MaxWindowSize"));

    return m->_pApiRoutines->SetConsoleScreenBufferInfoExImpl(*pObj, ex);
}

[[nodiscard]] HRESULT ApiDispatchers::ServerSetConsoleScreenBufferSize(_Inout_ CONSOLE_API_MSG* const m,
                                                                       _Inout_ BOOL* const /*pbReplyPending*/)
{
    Telemetry::Instance().LogApiCall(Telemetry::ApiCall::SetConsoleScreenBufferSize);
    const auto a = &m->u.consoleMsgL2.SetConsoleScreenBufferSize;