- Run without console by default - Log to file once console hidden - Recover panics in background goroutines - Restart agent after unexpected errors - Use shared instance launch helper
38 lines
720 B
Go
38 lines
720 B
Go
//go:build windows
|
|
|
|
package instance
|
|
|
|
import (
|
|
"os"
|
|
"syscall"
|
|
)
|
|
|
|
var (
|
|
kernel32 = syscall.NewLazyDLL("kernel32.dll")
|
|
procFreeConsole = kernel32.NewProc("FreeConsole")
|
|
user32 = syscall.NewLazyDLL("user32.dll")
|
|
procShowWindow = user32.NewProc("ShowWindow")
|
|
)
|
|
|
|
const swHide = 0
|
|
|
|
// HideConsole hides any console window and detaches from it.
|
|
func HideConsole() {
|
|
hwnd, _, _ := kernel32.NewProc("GetConsoleWindow").Call()
|
|
if hwnd != 0 {
|
|
_, _, _ = procShowWindow.Call(hwnd, swHide)
|
|
}
|
|
_, _, _ = procFreeConsole.Call()
|
|
redirectStdioToNul()
|
|
}
|
|
|
|
func redirectStdioToNul() {
|
|
nul, err := os.OpenFile("NUL", os.O_RDWR, 0)
|
|
if err != nil {
|
|
return
|
|
}
|
|
os.Stdin = nul
|
|
os.Stdout = nul
|
|
os.Stderr = nul
|
|
}
|