问题描述
说我有以下代码,使用 syscall
隐藏命令行窗口
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)
}
但是当我编译它并尝试在Windows中运行时,命令行窗口再次出现
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我已经知道如何使用 go go -ldflags -H = windowsgui
,但是这样做只能确保程序本身不会弹出命令行窗口, Exec
仍然会显示那些窗口
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
推荐答案
是一个更好的解决方案,它可以运行 exec.Command()
而不会生成可见窗口((°͜ʖ°)。
There is a better solution, which can run exec.Command()
without spawn a visible window, ( ͡° ͜ʖ ͡°).
这是我的代码:
首先 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()
来源:
这篇关于在Golang中使用Exec时如何隐藏命令提示符窗口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!