Skip to content

Commit

Permalink
fix(shim): Allow GUI applications to attach to the shell's console wh…
Browse files Browse the repository at this point in the history
…en launched using the GUI shim (#5721)
  • Loading branch information
spider2048 committed Mar 20, 2024
1 parent 3186fef commit 90766f9
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -44,6 +44,7 @@
- **scoop-checkup:** Don't throw 7zip error when external 7zip is used ([#5703](https://github.com/ScoopInstaller/Scoop/issues/5703))
- **config:** Warn users about misconfigured GitHub token ([#5777](https://github.com/ScoopInstaller/Scoop/issues/5777))
- **update/uninstall:** Remove items from PATH correctly ([#5833](https://github.com/ScoopInstaller/Scoop/issues/5833))
- **shim:** Allow GUI applications to attach to the shell's console when launched using the GUI shim ([#5721](https://github.com/ScoopInstaller/Scoop/issues/5721))

### Performance Improvements

Expand Down
2 changes: 1 addition & 1 deletion supporting/shimexe/bin/checksum.sha256
@@ -1 +1 @@
9726c3a429009a5b22bd92cb8ab96724c670e164e7240e83f27b7c8b7bd1ca39 *shim.exe
1e4a9e4b305ef78b375159b25e2f8a90243584efd56ed6994072a8dfc2147ade *shim.exe
2 changes: 1 addition & 1 deletion supporting/shimexe/bin/checksum.sha512
@@ -1 +1 @@
18a737674afde4d5e7e1647d8d1e98471bb260513c57739651f92fdf1647d76c92f0cd0a9bb458daf4eae4bdab9d31404162acf6d74a041e6415752b75d722e0 *shim.exe
f580833905e45b02a433c03647fb0caa3692336c8bde9621c286684629c78d1c56a292bea2887ea0ae941b8beec50322f31975c23ab5644c2b85f3c2108f383d *shim.exe
Binary file modified supporting/shimexe/bin/shim.exe
Binary file not shown.
13 changes: 12 additions & 1 deletion supporting/shimexe/shim.cs
Expand Up @@ -21,6 +21,12 @@ class Program {
out PROCESS_INFORMATION lpProcessInformation);
const int ERROR_ELEVATION_REQUIRED = 740;

[DllImport("kernel32.dll", SetLastError = true)]
private static extern bool AttachConsole(int dwProcessId);

[DllImport("kernel32.dll", SetLastError = true)]
private static extern IntPtr GetConsoleWindow();

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
struct STARTUPINFO {
public Int32 cb;
Expand Down Expand Up @@ -89,7 +95,12 @@ internal struct PROCESS_INFORMATION {
cmd_args += pass_args;
}
if(!string.IsNullOrEmpty(cmd_args)) cmd_args = " " + cmd_args;
var cmd = "\"" + path + "\"" + cmd_args;
var cmd = path + cmd_args;

// Fix when GUI applications want to write to a console
if (GetConsoleWindow() == IntPtr.Zero) {
AttachConsole(-1);
}

if(!CreateProcess(null, cmd, IntPtr.Zero, IntPtr.Zero,
bInheritHandles: true,
Expand Down

0 comments on commit 90766f9

Please sign in to comment.