Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/go/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Windows 在Golang中使用Exec时如何隐藏命令提示窗口?_Windows_Go_Command Line_System Calls - Fatal编程技术网

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

Windows 在Golang中使用Exec时如何隐藏命令提示窗口?,windows,go,command-line,system-calls,Windows,Go,Command Line,System Calls,假设我有以下代码,使用syscall隐藏命令行窗口 process:=exec.Command(名称、参数…) process.SysProcAttr=&syscall.SysProcAttr{HideWindow:true} 错误:=进程。开始() 如果出错!=零{ 日志打印(错误) } 但当我编译它并试图在Windows中运行它时,命令行窗口再次出现 如何防止出现命令行窗口 PS我已经知道如何使用go build-ldflags-H=windowsgui将golang源代码编译成一个Win

假设我有以下代码,使用
syscall
隐藏命令行窗口

process:=exec.Command(名称、参数…)
process.SysProcAttr=&syscall.SysProcAttr{HideWindow:true}
错误:=进程。开始()
如果出错!=零{
日志打印(错误)
}
但当我编译它并试图在Windows中运行它时,命令行窗口再次出现

如何防止出现命令行窗口


PS我已经知道如何使用
go build-ldflags-H=windowsgui
将golang源代码编译成一个Windows GUI可执行文件,但是这样做只会确保程序本身不会出现命令行窗口,
Exec
将显示那些窗口,如果您使用
-ldflags-H=windowsgui
进行编译,每个exec.命令将生成一个新的控制台窗口

如果构建时不使用该标志,则会得到一个控制台窗口,所有执行命令都会打印到该窗口中

因此,我当前的解决方案是在不使用标志的情况下构建,即在程序启动时有一个控制台窗口,然后在程序启动时立即用以下代码隐藏控制台窗口:

import "github.com/gonutz/w32/v2"

func hideConsole() {
    console := w32.GetConsoleWindow()
    if console == 0 {
        return // no console attached
    }
    // If this application is the process that created the console window, then
    // this program was not compiled with the -H=windowsgui flag and on start-up
    // it created a console along with the main application window. In this case
    // hide the console window.
    // See
    // http://stackoverflow.com/questions/9009333/how-to-check-if-the-program-is-run-from-a-console
    _, consoleProcID := w32.GetWindowThreadProcessId(console)
    if w32.GetCurrentProcessId() == consoleProcID {
        w32.ShowWindowAsync(console, w32.SW_HIDE)
    }
}
有关进程ID的详细信息,请参阅

发生的情况是,所有exec.命令现在都将其输出打印到隐藏的控制台窗口,而不是生成自己的输出

这里的折衷方案是,您的程序在启动控制台窗口时会闪烁一次,但在它隐藏之前只会闪烁一小会儿。

是一个更好的解决方案,它可以运行
exec.Command()
,而不会生成一个可见窗口

这是我的密码:

首先
导入“syscall”

来源:

您应该能够通过以下方式防止控制台窗口被隐藏:

cmd := exec.Command(...)
cmd.SysProcAttr = &syscall.SysProcAttr{CreationFlags: 0x08000000} // CREATE_NO_WINDOW

您能更准确地说明您试图隐藏的提示吗?您已经说明了正确的命令隐藏了Go命令行窗口和exec生成的窗口。我目前的最佳猜测是,您正在执行的命令生成了一个额外的窗口。这也是可能的,我将检查哪个命令生成了命令提示符(虽然很快就会消失)@jm33_m0我刚刚尝试过,您问题中的sysProcAttr方法,这是一个巨大的改进-windows只会短暂闪烁。你有没有找到一个完全防止闪存的解决方案?我只在win 7上看到了一个问题,而不是在win 8上,但我刚刚在windows 7机器上重新测试了这个问题,它确实按照描述的那样在那里工作-我不确定我之前尝试时为什么会出错-删除注释以避免混淆。回答向上投票,因为我现在将使用这种方法这实际上回答了这个问题。谢谢不过,对目录使用
os.Getenv(“windir”)
可能更安全。@KarelBílek是的,但我没有看到windows系统中的cmd不在该位置:)。我不知道该如何感谢您。从昨天开始,我已经尝试了至少50种方法来隐藏这个终端窗口。我对终端窗口感到失望,我开始讨厌现实生活中的黑色。再次感谢。你是一个救世主。这似乎在Windows中起作用,但在WSL ubuntu中,当我编译(使用较旧版本的golang)时,我得到了
csc/sclaunch.go:759:42:syscall类型的struct literal中的未知字段“CreationFlags”。sysprocatr
CreationFlags仅适用于Windows,而不是Linux
cmd := exec.Command(...)
cmd.SysProcAttr = &syscall.SysProcAttr{CreationFlags: 0x08000000} // CREATE_NO_WINDOW