An example / demo project for managing a child process in the same way across Windows/Mac/Linux
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

67 lines
1.2 KiB

// +build windows
package main
import (
"os"
"os/signal"
"syscall"
"golang.org/x/sys/windows"
)
var processAttributesOSIndependent = &windows.SysProcAttr{
CreationFlags: windows.CREATE_UNICODE_ENVIRONMENT | windows.CREATE_NEW_PROCESS_GROUP,
}
func terminateProcessOSIndependent(pid int, _ os.Signal) error {
if pid == 0 || pid == -1 {
return nil
}
dll, err := windows.LoadDLL("kernel32.dll")
if err != nil {
return err
}
defer dll.Release()
f, err := dll.FindProc("AttachConsole")
if err != nil {
return err
}
r1, _, err := f.Call(uintptr(pid))
if r1 == 0 && err != syscall.ERROR_ACCESS_DENIED {
return err
}
f, err = dll.FindProc("SetConsoleCtrlHandler")
if err != nil {
return err
}
r1, _, err = f.Call(0, 1)
if r1 == 0 {
return err
}
f, err = dll.FindProc("GenerateConsoleCtrlEvent")
if err != nil {
return err
}
r1, _, err = f.Call(windows.CTRL_BREAK_EVENT, uintptr(pid))
if r1 == 0 {
return err
}
r1, _, err = f.Call(windows.CTRL_C_EVENT, uintptr(pid))
if r1 == 0 {
return err
}
return nil
}
func killProcessOSIndependent(process *os.Process) error {
return process.Kill()
}
func getSignalChannelOSIndependent() <-chan os.Signal {
sc := make(chan os.Signal, 10)
signal.Notify(sc, os.Interrupt)
return sc
}