-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
Allow disabling the console in --compile
on windows. (/SUBSYSTEM:WINDOWS
)
#13084
Comments
this is suggesting allowing a flag to set |
Yes, currently I am using Golang is like this . I love bun ~ |
--compile
on windows. (/SUBSYSTEM:WINDOWS
)
editing title so its searchable. related is #10823, which also involves editing the binary after bundling |
@716774 hi what is |
MSVC in Visual Studio
|
This is the script I wrote, you can modify it to your directory @echo off
title=go run
echo 正在编译
call bun build app.js --compile --minify --outfile app
echo.
call "C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.40.33807\bin\Hostx64\x64\editbin.exe" /SUBSYSTEM:WINDOWS app.exe
echo.
pause |
Thanks a lot @716774 the console is hidden now 🎉 |
If you need an easy way to hide the console without actually running a windows executable (like in CI) then this script should be able to do it. package main
import (
"encoding/binary"
"fmt"
"os"
)
const (
IMAGE_SUBSYSTEM_WINDOWS_GUI = 2
PE_HEADER_OFFSET_LOCATION = 0x3C
SUBSYSTEM_OFFSET = 0x5C
)
func main() {
if len(os.Args) != 2 {
fmt.Println("Usage: go run main.go <path-to-binary>")
os.Exit(1)
}
filePath := os.Args[1]
file, err := os.OpenFile(filePath, os.O_RDWR, 0644)
if err != nil {
fmt.Printf("Error opening file: %v\n", err)
os.Exit(1)
}
defer file.Close()
// Find the PE header offset
_, err = file.Seek(PE_HEADER_OFFSET_LOCATION, 0)
if err != nil {
fmt.Printf("Error seeking to PE header offset: %v\n", err)
os.Exit(1)
}
var peHeaderOffset uint32
err = binary.Read(file, binary.LittleEndian, &peHeaderOffset)
if err != nil {
fmt.Printf("Error reading PE header offset: %v\n", err)
os.Exit(1)
}
// Seek to the subsystem field in the PE header
subsystemOffset := int64(peHeaderOffset) + SUBSYSTEM_OFFSET
_, err = file.Seek(subsystemOffset, 0)
if err != nil {
fmt.Printf("Error seeking to subsystem field: %v\n", err)
os.Exit(1)
}
// Change the subsystem to GUI
err = binary.Write(file, binary.LittleEndian, uint16(IMAGE_SUBSYSTEM_WINDOWS_GUI))
if err != nil {
fmt.Printf("Error writing to subsystem field: %v\n", err)
os.Exit(1)
}
fmt.Println("Successfully modified the binary to hide the console at startup.")
} |
wait it's THAT easy??! we should port this to zig stuff for this is in |
It seems like. The output of something like import fs from 'node:fs'
const IMAGE_SUBSYSTEM_WINDOWS_GUI = 2;
const PE_HEADER_OFFSET_LOCATION = 0x3C;
const SUBSYSTEM_OFFSET = 0x5C;
function modifyBinarySubsystem(filePath: string) {
const fd = fs.openSync(filePath, 'r+');
const buffer = Buffer.alloc(4);
// Read PE header offset from 0x3C
fs.readSync(fd, buffer, 0, 4, PE_HEADER_OFFSET_LOCATION);
const peHeaderOffset = buffer.readUInt32LE(0);
// Seek to the subsystem field in the PE header
const subsystemOffset = peHeaderOffset + SUBSYSTEM_OFFSET;
const subsystemBuffer = Buffer.alloc(2);
subsystemBuffer.writeUInt16LE(IMAGE_SUBSYSTEM_WINDOWS_GUI, 0);
// Write the new subsystem value
fs.writeSync(fd, subsystemBuffer, 0, 2, subsystemOffset);
fs.closeSync(fd);
console.log("Successfully modified the binary to hide the console at startup.");
}
// Usage: Provide the path to the binary
if (process.argv.length !== 3) {
console.log('Usage: node modify-binary.js <path-to-binary>');
process.exit(1);
}
const binaryPath = process.argv[2];
modifyBinarySubsystem(binaryPath); This admittedly goes well over my head for how its working. I had some help from Claude on figuring it out. Its entirely possible that its missing some nuance. But it does seem to work. |
It works because windows reads the bytes at those two offsets and depending on the value, it opens a console or not |
OH MY GOD I love you |
What is the problem this feature would solve?
Windows GUI
What is the feature you are proposing to solve the problem?
golang >
go build -ldflags="-s -w -H windowsgui"
The terminal window will not be displayed.
What alternatives have you considered?
No response
The text was updated successfully, but these errors were encountered: