//go:build windows package logging import ( "syscall" "git.wxccs.org/iceking2nd/winauth-go/internal/global" ) // AttachConsole attempts to attach the current process to the parent // console (if any). When a GUI subsystem binary is launched from a shell, // stdout/stderr are not visible by default; this allows --console to make // them visible. // // This is a best-effort helper: any error is logged at Debug level and // otherwise ignored. The process keeps running without a console. func AttachConsole() { const fn = "internal.logging.AttachConsole" kernel32, err := syscall.LoadLibrary("kernel32.dll") if err != nil { global.Log.WithField("func", fn).WithError(err).Debug("LoadLibrary kernel32 failed") return } defer syscall.FreeLibrary(kernel32) attach, err := syscall.GetProcAddress(kernel32, "AttachConsole") if err != nil { global.Log.WithField("func", fn).WithError(err).Debug("GetProcAddress AttachConsole failed") return } alloc, err := syscall.GetProcAddress(kernel32, "AllocConsole") if err != nil { global.Log.WithField("func", fn).WithError(err).Debug("GetProcAddress AllocConsole failed") return } const attachParentProcess = ^uintptr(0) // -1 r1, _, _ := syscall.SyscallN(attach, attachParentProcess) if r1 == 0 { // no parent console — allocate a new one r1, _, _ = syscall.SyscallN(alloc) if r1 == 0 { global.Log.WithField("func", fn).Debug("AllocConsole failed") return } } global.Log.WithField("func", fn).Debug("console attached") }