From 0d106c8f9998b9fdf98a699a2246322174c09ac1 Mon Sep 17 00:00:00 2001 From: Dustin Howett Date: Thu, 14 Nov 2019 12:24:18 -0800 Subject: [PATCH] Force the use of the static pseudoconsole functions in TConn Fixes #3553. --- .../TerminalConnection/ConptyConnection.cpp | 4 +-- .../TerminalConnection/ConptyConnection.h | 5 ++-- src/inc/conpty-static.h | 26 +++++++++++++++++ src/winconpty/dll/winconpty.def | 6 ++-- src/winconpty/winconpty.cpp | 29 ++++++++++--------- src/winconpty/winconpty.h | 12 ++++---- 6 files changed, 55 insertions(+), 27 deletions(-) create mode 100644 src/inc/conpty-static.h diff --git a/src/cascadia/TerminalConnection/ConptyConnection.cpp b/src/cascadia/TerminalConnection/ConptyConnection.cpp index 119fd0e2943..b1ffd4d7b81 100644 --- a/src/cascadia/TerminalConnection/ConptyConnection.cpp +++ b/src/cascadia/TerminalConnection/ConptyConnection.cpp @@ -32,7 +32,7 @@ namespace winrt::Microsoft::Terminal::TerminalConnection::implementation RETURN_IF_WIN32_BOOL_FALSE(CreatePipe(&inPipePseudoConsoleSide, &inPipeOurSide, NULL, 0)); RETURN_IF_WIN32_BOOL_FALSE(CreatePipe(&outPipeOurSide, &outPipePseudoConsoleSide, NULL, 0)); - RETURN_IF_FAILED(CreatePseudoConsole(size, inPipePseudoConsoleSide.get(), outPipePseudoConsoleSide.get(), dwFlags, phPC)); + RETURN_IF_FAILED(ConptyCreatePseudoConsole(size, inPipePseudoConsoleSide.get(), outPipePseudoConsoleSide.get(), dwFlags, phPC)); *phInput = inPipeOurSide.release(); *phOutput = outPipeOurSide.release(); return S_OK; @@ -250,7 +250,7 @@ namespace winrt::Microsoft::Terminal::TerminalConnection::implementation } else if (!_closing.load()) { - THROW_IF_FAILED(ResizePseudoConsole(_hPC.get(), { Utils::ClampToShortMax(columns, 1), Utils::ClampToShortMax(rows, 1) })); + THROW_IF_FAILED(ConptyResizePseudoConsole(_hPC.get(), { Utils::ClampToShortMax(columns, 1), Utils::ClampToShortMax(rows, 1) })); } } diff --git a/src/cascadia/TerminalConnection/ConptyConnection.h b/src/cascadia/TerminalConnection/ConptyConnection.h index 71f48921ec7..7a0d5592758 100644 --- a/src/cascadia/TerminalConnection/ConptyConnection.h +++ b/src/cascadia/TerminalConnection/ConptyConnection.h @@ -4,11 +4,12 @@ #pragma once #include "ConptyConnection.g.h" +#include namespace wil { // These belong in WIL upstream, so when we reingest the change that has them we'll get rid of ours. - using unique_pseudoconsole_handle = wil::unique_any; + using unique_static_pseudoconsole_handle = wil::unique_any; } namespace winrt::Microsoft::Terminal::TerminalConnection::implementation @@ -51,7 +52,7 @@ namespace winrt::Microsoft::Terminal::TerminalConnection::implementation wil::unique_hfile _outPipe; // The pipe for reading output from wil::unique_handle _hOutputThread; wil::unique_process_information _piClient; - wil::unique_pseudoconsole_handle _hPC; + wil::unique_static_pseudoconsole_handle _hPC; wil::unique_threadpool_wait _clientExitWait; DWORD _OutputThread(); diff --git a/src/inc/conpty-static.h b/src/inc/conpty-static.h new file mode 100644 index 00000000000..7f85b22eddf --- /dev/null +++ b/src/inc/conpty-static.h @@ -0,0 +1,26 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT license. + +// This header prototypes the Pseudoconsole symbols from conpty.lib with their original names. +// This is required because we cannot import __imp_CreatePseudoConsole from a static library +// as it doesn't produce an import lib. +// We can't use an /ALTERNATENAME trick because it seems that that name is only resolved when the +// linker cannot otherwise find the symbol. + +#pragma once + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +HRESULT WINAPI ConptyCreatePseudoConsole(COORD size, HANDLE hInput, HANDLE hOutput, DWORD dwFlags, HPCON* phPC); + +HRESULT WINAPI ConptyResizePseudoConsole(HPCON hPC, COORD size); + +VOID WINAPI ConptyClosePseudoConsole(HPCON hPC); + +#ifdef __cplusplus +} +#endif diff --git a/src/winconpty/dll/winconpty.def b/src/winconpty/dll/winconpty.def index 51ccf32791b..1ea7e916ae7 100644 --- a/src/winconpty/dll/winconpty.def +++ b/src/winconpty/dll/winconpty.def @@ -1,4 +1,4 @@ EXPORTS - CreatePseudoConsole - ResizePseudoConsole - ClosePseudoConsole + CreatePseudoConsole = ConptyCreatePseudoConsole + ResizePseudoConsole = ConptyResizePseudoConsole + ClosePseudoConsole = ConptyClosePseudoConsole diff --git a/src/winconpty/winconpty.cpp b/src/winconpty/winconpty.cpp index 14a1730da92..2e51933726b 100644 --- a/src/winconpty/winconpty.cpp +++ b/src/winconpty/winconpty.cpp @@ -308,21 +308,22 @@ VOID _ClosePseudoConsole(_In_ PseudoConsole* pPty) // reply to this message, the conpty will not process any input until it // does. Most *nix terminals and the Windows Console (after Windows 10 // Anniversary Update) will be able to handle such a message. -HRESULT WINAPI CreatePseudoConsole(_In_ COORD size, - _In_ HANDLE hInput, - _In_ HANDLE hOutput, - _In_ DWORD dwFlags, - _Out_ HPCON* phPC) + +extern "C" HRESULT WINAPI ConptyCreatePseudoConsole(_In_ COORD size, + _In_ HANDLE hInput, + _In_ HANDLE hOutput, + _In_ DWORD dwFlags, + _Out_ HPCON* phPC) { - return CreatePseudoConsoleAsUser(INVALID_HANDLE_VALUE, size, hInput, hOutput, dwFlags, phPC); + return ConptyCreatePseudoConsoleAsUser(INVALID_HANDLE_VALUE, size, hInput, hOutput, dwFlags, phPC); } -HRESULT CreatePseudoConsoleAsUser(_In_ HANDLE hToken, - _In_ COORD size, - _In_ HANDLE hInput, - _In_ HANDLE hOutput, - _In_ DWORD dwFlags, - _Out_ HPCON* phPC) +extern "C" HRESULT ConptyCreatePseudoConsoleAsUser(_In_ HANDLE hToken, + _In_ COORD size, + _In_ HANDLE hInput, + _In_ HANDLE hOutput, + _In_ DWORD dwFlags, + _Out_ HPCON* phPC) { if (phPC == NULL) { @@ -355,7 +356,7 @@ HRESULT CreatePseudoConsoleAsUser(_In_ HANDLE hToken, // Function Description: // Resizes the given conpty to the specified size, in characters. -HRESULT WINAPI ResizePseudoConsole(_In_ HPCON hPC, _In_ COORD size) +extern "C" HRESULT WINAPI ConptyResizePseudoConsole(_In_ HPCON hPC, _In_ COORD size) { PseudoConsole* const pPty = (PseudoConsole*)hPC; HRESULT hr = pPty == NULL ? E_INVALIDARG : S_OK; @@ -372,7 +373,7 @@ HRESULT WINAPI ResizePseudoConsole(_In_ HPCON hPC, _In_ COORD size) // console window they were running in was closed. // This can fail if the conhost hosting the pseudoconsole failed to be // terminated, or if the pseudoconsole was already terminated. -VOID WINAPI ClosePseudoConsole(_In_ HPCON hPC) +extern "C" VOID WINAPI ConptyClosePseudoConsole(_In_ HPCON hPC) { PseudoConsole* const pPty = (PseudoConsole*)hPC; if (pPty != NULL) diff --git a/src/winconpty/winconpty.h b/src/winconpty/winconpty.h index 2cfea5375fd..829befc5473 100644 --- a/src/winconpty/winconpty.h +++ b/src/winconpty/winconpty.h @@ -31,12 +31,12 @@ HRESULT _ResizePseudoConsole(_In_ const PseudoConsole* const pPty, _In_ const CO void _ClosePseudoConsoleMembers(_In_ PseudoConsole* pPty); VOID _ClosePseudoConsole(_In_ PseudoConsole* pPty); -HRESULT CreatePseudoConsoleAsUser(_In_ HANDLE hToken, - _In_ COORD size, - _In_ HANDLE hInput, - _In_ HANDLE hOutput, - _In_ DWORD dwFlags, - _Out_ HPCON* phPC); +HRESULT ConptyCreatePseudoConsoleAsUser(_In_ HANDLE hToken, + _In_ COORD size, + _In_ HANDLE hInput, + _In_ HANDLE hOutput, + _In_ DWORD dwFlags, + _Out_ HPCON* phPC); #ifdef __cplusplus }