================
@@ -0,0 +1,135 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "lldb/Host/windows/PseudoConsole.h"
+
+#include <mutex>
+
+#include "lldb/Host/windows/windows.h"
+
+#include "llvm/Support/Errc.h"
+#include "llvm/Support/Errno.h"
+
+using namespace lldb_private;
+
+typedef HRESULT(WINAPI *CreatePseudoConsole_t)(COORD size, HANDLE hInput,
+                                               HANDLE hOutput, DWORD dwFlags,
+                                               HPCON *phPC);
+
+typedef HRESULT(WINAPI *ResizePseudoConsole_t)(HPCON hPC, COORD size);
+
+typedef VOID(WINAPI *ClosePseudoConsole_t)(HPCON hPC);
+
+class ConPTY {
+public:
+  static bool Initialize() {
+    std::lock_guard<std::mutex> guard(m_initialized_mutex);
+
+    if (!m_initialized) {
+      m_initialized = true;
+
+      HMODULE hMod = LoadLibraryW(L"kernel32.dll");
+      if (!hMod) {
+        return false;
+      }
+
+      pCreate =
+          (CreatePseudoConsole_t)GetProcAddress(hMod, "CreatePseudoConsole");
+      pClose = (ClosePseudoConsole_t)GetProcAddress(hMod, 
"ClosePseudoConsole");
----------------
compnerd wrote:

I'm half tempted to say that we should do this in a more abstract manner as we 
seem to want to maintain backwards compatibility.

```c++
struct Kernel32 {
  Kernel32() {
    hModule_ = LoadLibraryW(L"kernel32.dll");
    if (hModule_ == nullptr)
      LOG(...);

    CreatePsueodConsole_ = GetProcAddress(hModule_, "CreatePseudoConsole");
    ...
  }

  HRESULT CreatePsueodConsole(...) {
    return CreatePsueodConsole_(...);
  }

  HRESULT ClosePsuedoConsole(...) {
    return ClosePseudoConsole_(...);
  }

private;
  HMODULE hModule_;
  CreatePsuedoConsole_t CreatePsuedoConsole_;
  ClosePseudoConsole_t ClosePseudoConsole_;
};
static Kernel32 kernel32;
```

The C++ standard ensures that kernel32 is initialised safely on first use and 
we can reference it as `kernel32.CreatePsueodConsole(...)`.

This way we can scope all the modules we need, lazily initialize them and rely 
on the compiler to do the right thing. It also makes it easier to find all the 
dynamically accessed functions.

https://github.com/llvm/llvm-project/pull/168729
_______________________________________________
lldb-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits

Reply via email to