donglaoping9702 2017-02-28 05:02
浏览 428
已采纳

在Golang中使用Exec时如何隐藏命令提示符窗口?

say i have the following code, using syscall to hide command line window

process := exec.Command(name, args...)
process.SysProcAttr = &syscall.SysProcAttr{HideWindow: true}
err := process.Start()
if err != nil {
    log.Print(err)
}

but when i compiled it and tried to run it in Windows, command line window showed up again

what can i do to prevent command line window from appearing?

PS i already know how to compile golang source into a Windows GUI executable using go build -ldflags -H=windowsgui, but doing so only ensures the program itself doesn't bring up a command line window, Exec will show those windows anyway

  • 写回答

2条回答 默认 最新

  • duanmie9682 2018-01-21 10:44
    关注

    There is a better solution, which can run exec.Command() without spawn a visible window, ( ͡° ͜ʖ ͡°).

    Here is my code:

    Firstly import "syscall"

    cmd_path := "C:\\Windows\\system32\\cmd.exe"
    cmd_instance := exec.Command(cmd_path, "/c", "notepad")
    cmd_instance.SysProcAttr = &syscall.SysProcAttr{HideWindow: true}
    cmd_output, err := cmd_instance.Output()
    

    Origin: https://www.reddit.com/r/golang/comments/2c1g3x/build_golang_app_reverse_shell_to_run_in_windows/

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?