Repository: microsoft/terminal (language: cpp)

Issue:
[1.18, 1.19, 1.20] `wt $COMMAND` is **very broken**
### Windows Terminal version

Stable 1.19

### Windows build number

-

### Other Software

_No response_

### Steps to reproduce

Win+R

`wt powershell`

4/5 times it shows up and says "powershell encountered an internal error".
1/5 times Terminal doesn't even start.

### Expected Behavior

_No response_

### Actual Behavior

-


Discussion / hints:

Fix patch:
diff --git a/src/cascadia/WindowsTerminal/WindowEmperor.cpp b/src/cascadia/WindowsTerminal/WindowEmperor.cpp
index e81ccf049b8..3ff3f06f546 100644
--- a/src/cascadia/WindowsTerminal/WindowEmperor.cpp
+++ b/src/cascadia/WindowsTerminal/WindowEmperor.cpp
@@ -74,7 +74,23 @@ void WindowEmperor::HandleCommandlineArgs(int nCmdShow)
         }
     }
 
-    const Remoting::CommandlineArgs eventArgs{ args, cwd, gsl::narrow_cast<uint32_t>(nCmdShow), GetEnvironmentStringsW() };
+    // GetEnvironmentStringsW() returns a double-null terminated string.
+    // The hstring(wchar_t*) constructor however only works for regular null-terminated strings.
+    // Due to that we need to manually search for the terminator.
+    winrt::hstring env;
+    {
+        const wil::unique_environstrings_ptr strings{ GetEnvironmentStringsW() };
+        const auto beg = strings.get();
+        auto end = beg;
+
+        for (; *end; end += wcsnlen(end, SIZE_T_MAX) + 1)
+        {
+        }
+
+        env = winrt::hstring{ beg, gsl::narrow<uint32_t>(end - beg) };
+    }
+
+    const Remoting::CommandlineArgs eventArgs{ args, cwd, gsl::narrow_cast<uint32_t>(nCmdShow), std::move(env) };
     const auto isolatedMode{ _app.Logic().IsolatedMode() };
     const auto result = _manager.ProposeCommandline(eventArgs, isolatedMode);
     int exitCode = 0;


Test patch: