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

Code before:
                      0,
                      nullptr,
                      0);
}

// Routine Description:
// - For internal use. This function will send the appropriate control code verb and buffers to the driver and return a result.
// - Usage of the optional buffers depends on which verb is sent and is specific to the particular driver and its protocol.
// Arguments:
// - dwIoControlCode - The action code to send to the driver
// - pInBuffer - An optional buffer to send as input with the verb. Usage depends on the control code.
// - cbInBufferSize - The length in bytes of the optional input buffer.
// - pOutBuffer - An optional buffer to send as output with the verb. Usage depends on the control code.
// - cbOutBufferSize - The length in bytes of the optional output buffer.
// Return Value:
// - HRESULT S_OK or suitable error.
[[nodiscard]] HRESULT ConDrvDeviceComm::_CallIoctl(_In_ DWORD dwIoControlCode,
                                                   _In_reads_bytes_opt_(cbInBufferSize) PVOID pInBuffer,
                                                   _In_ DWORD cbInBufferSize,
                                                   _Out_writes_bytes_opt_(cbOutBufferSize) PVOID pOutBuffer,
                                                   _In_ DWORD cbOutBufferSize) const
{
    // See: https://msdn.microsoft.com/en-us/library/windows/desktop/aa363216(v=vs.85).aspx
    // Written is unused but cannot be nullptr because we aren't using overlapped.
    DWORD cbWritten = 0;
    RETURN_IF_WIN32_BOOL_FALSE(DeviceIoControl(_Server.get(),
                                               dwIoControlCode,
                                               pInBuffer,
                                               cbInBufferSize,
                                               pOutBuffer,
                                               cbOutBufferSize,
                                               &cbWritten,
                                               nullptr));

    return S_OK;
}

// Routine Description:
// - Implements IDeviceComm handle exchange for ConDrv.
// - "Translates" a pointer to an object into a handle value
//   that the driver can use to identify objects in a console
//   session.
// - The opposite of GetHandle
[[nodiscard]] ULONG_PTR ConDrvDeviceComm::PutHandle(const void* handle)
{
    // ConDrv will pass back whatever large integer we send it, as an opaque data blob
    // We'll use that to smuggle the actual pointer value to the handle.
    return reinterpret_cast<ULONG_PTR>(handle);
}

// Routine Description:

Review comment (bug):
This one in particular is redundant with the log calls in all the other existing places. It's IMO not very interesting to see a message stating that `DeviceIoControl` failed, if the caller then logs the returned HRESULT again. The caller signature gives much more information IMO (source and type of the call).

Code after:
                      0,
                      nullptr,
                      0);
}

// Routine Description:
// - For internal use. This function will send the appropriate control code verb and buffers to the driver and return a result.
// - Usage of the optional buffers depends on which verb is sent and is specific to the particular driver and its protocol.
// Arguments:
// - dwIoControlCode - The action code to send to the driver
// - pInBuffer - An optional buffer to send as input with the verb. Usage depends on the control code.
// - cbInBufferSize - The length in bytes of the optional input buffer.
// - pOutBuffer - An optional buffer to send as output with the verb. Usage depends on the control code.
// - cbOutBufferSize - The length in bytes of the optional output buffer.
// Return Value:
// - HRESULT S_OK or suitable error.
[[nodiscard]] HRESULT ConDrvDeviceComm::_CallIoctl(_In_ DWORD dwIoControlCode,
                                                   _In_reads_bytes_opt_(cbInBufferSize) PVOID pInBuffer,
                                                   _In_ DWORD cbInBufferSize,
                                                   _Out_writes_bytes_opt_(cbOutBufferSize) PVOID pOutBuffer,
                                                   _In_ DWORD cbOutBufferSize) const
{
    // See: https://msdn.microsoft.com/en-us/library/windows/desktop/aa363216(v=vs.85).aspx
    // Written is unused but cannot be nullptr because we aren't using overlapped.
    DWORD cbWritten = 0;
    RETURN_IF_WIN32_BOOL_FALSE_EXPECTED(DeviceIoControl(
        _Server.get(),
        dwIoControlCode,
        pInBuffer,
        cbInBufferSize,
        pOutBuffer,
        cbOutBufferSize,
        &cbWritten,
        nullptr));

    return S_OK;
}

// Routine Description:
// - Implements IDeviceComm handle exchange for ConDrv.
// - "Translates" a pointer to an object into a handle value
//   that the driver can use to identify objects in a console
//   session.
// - The opposite of GetHandle
[[nodiscard]] ULONG_PTR ConDrvDeviceComm::PutHandle(const void* handle)
{
    // ConDrv will pass back whatever large integer we send it, as an opaque data blob
    // We'll use that to smuggle the actual pointer value to the handle.
    return reinterpret_cast<ULONG_PTR>(handle);
}