github.com/suiyunonghen/DxCommonLib@v0.5.3/system/redirect_windows.go (about)

     1  // +build windows
     2  
     3  package system
     4  
     5  import (
     6  	"os"
     7  	"syscall"
     8  )
     9  
    10  var(
    11  	kernerl32 = syscall.MustLoadDLL("kernel32.dll")
    12  	setHandleProc = kernerl32.MustFindProc("SetStdHandle")
    13  )
    14  
    15  func setStdHandle(stdHandle int32, handle syscall.Handle) error {
    16  	r0, _, e1 := syscall.Syscall(setHandleProc.Addr(), 2, uintptr(stdHandle), uintptr(handle), 0)
    17  	if r0 == 0 {
    18  		if e1 != 0 {
    19  			return error(e1)
    20  		}
    21  		return syscall.EINVAL
    22  	}
    23  	return nil
    24  }
    25  
    26  
    27  func redirectStdErr(f *os.File)error  {
    28  	err := setStdHandle(syscall.STD_ERROR_HANDLE,syscall.Handle(f.Fd()))
    29  	if err != nil{
    30  		return err
    31  	}
    32  	os.Stderr = f
    33  	return nil
    34  }