Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wait for console applications only #2921

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 30 additions & 1 deletion lib/core.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -689,8 +689,14 @@ function shim($path, $global, $name, $arg) {

if ($path -match '\.(exe|com)$') {
# for programs with no awareness of any shell
if (is_gui_application($path)) {
$shim_suffix = '-gui'
} else {
$shim_suffix = ''
}
warn_on_overwrite "$shim.shim" $path
Copy-Item (get_shim_path) "$shim.exe" -Force
# @TODO check if -gui version exists
Copy-Item (get_shim_path) "$shim$shim_suffix.exe" -Force
Write-Output "path = `"$resolved_path`"" | Out-UTF8File "$shim.shim"
if ($arg) {
Write-Output "args = $arg" | Out-UTF8File "$shim.shim" -Append
Expand Down Expand Up @@ -1122,6 +1128,29 @@ function get_magic_bytes_pretty($file, $glue = ' ') {
return (get_magic_bytes $file | ForEach-Object { $_.ToString('x2') }) -join $glue
}

# check if application has GUI subsystem
function is_gui_application($path) {
# Let's extract application subsystem. For more information see:
# https://docs.microsoft.com/en-us/windows/desktop/Debug/pe-format
$file_stream = New-Object System.IO.FileStream($path, 'Open', 'Read')
$binary_reader = New-Object System.IO.BinaryReader($file_stream)
$dos_header = $binary_reader.ReadBytes(0x40);
if ([BitConverter]::ToInt16($dos_header, 0) -eq 0x5a4d) { # Signature
$pe_offset = [BitConverter]::ToInt32($dos_header, 0x3c);
$_ = $file_stream.Seek($pe_offset, 'Begin');
$bytes = $binary_reader.ReadBytes(4 + 0x14 + 0x46); # Signature, File Header and part of Optional Header
if ([BitConverter]::ToInt32($bytes, 0) -eq 0x4550) { # Signature
if ([BitConverter]::ToInt32($bytes, 4 + 0x10) -ge 0x46) { # SizeOfOptionalHeader
$pe_format = [BitConverter]::ToInt16($bytes, 4 + 0x14); # Magic
if ($pe_format -eq 0x10b -or $pe_format -eq 0x20b) { # PE32 or PE32+
return [BitConverter]::ToInt16($bytes, 4 + 0x14 + 0x44) -eq 2; # Subsystem
}
}
}
}
return $false;
}

function Out-UTF8File {
param(
[Parameter(Mandatory = $True, Position = 0)]
Expand Down
1 change: 1 addition & 0 deletions supporting/shimexe/bin/checksum.sha256
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
fc1fe8866917e947de944874e0ecdfcbe09e22b43f4c866bd20e733a4ee48e62 *shim-gui.exe
9726c3a429009a5b22bd92cb8ab96724c670e164e7240e83f27b7c8b7bd1ca39 *shim.exe
1 change: 1 addition & 0 deletions supporting/shimexe/bin/checksum.sha512
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
228d62ab3f6ca8ed510b06ed89b1594678480fa569b0ffdf30a6fca780253daa6ee8138d408452106cee420fdbd8f9633a3e16dd4a19370225d1d9739685dbd0 *shim-gui.exe
18a737674afde4d5e7e1647d8d1e98471bb260513c57739651f92fdf1647d76c92f0cd0a9bb458daf4eae4bdab9d31404162acf6d74a041e6415752b75d722e0 *shim.exe
Binary file added supporting/shimexe/bin/shim-gui.exe
Binary file not shown.
5 changes: 4 additions & 1 deletion supporting/shimexe/build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,12 @@ if (!$Fast) {
}

$output = "$PSScriptRoot\bin"
Write-Output 'Compiling shim.cs ...'
Write-Output 'Compiling shim.cs to shim.exe ...'
& "$PSScriptRoot\packages\Microsoft.Net.Compilers.Toolset\tasks\net472\csc.exe" -deterministic -platform:anycpu -nologo -optimize -target:exe -out:"$output\shim.exe" shim.cs

Write-Output 'Compiling shim.cs to shim-gui.exe ...'
& "$PSScriptRoot\packages\Microsoft.Net.Compilers.Toolset\tasks\net472\csc.exe" -deterministic -platform:anycpu -nologo -optimize -target:winexe -out:"$output\shim-gui.exe" shim.cs

Write-Output 'Computing checksums ...'
Remove-Item "$PSScriptRoot\bin\checksum.sha256" -ErrorAction Ignore
Remove-Item "$PSScriptRoot\bin\checksum.sha512" -ErrorAction Ignore
Expand Down