在Golang中使用Exec时如何隐藏命令提示符窗口? [英] How to hide command prompt window when using Exec in Golang?

查看:648
本文介绍了在Golang中使用Exec时如何隐藏命令提示符窗口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有以下代码,使用 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

推荐答案

T这里是一个更好的解决方案,它可以运行 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()

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

这篇关于在Golang中使用Exec时如何隐藏命令提示符窗口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆