Home >Backend Development >Golang >How to Prevent Command Line Windows from Appearing When Using `exec.Command` in Go?

How to Prevent Command Line Windows from Appearing When Using `exec.Command` in Go?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2024-11-28 02:22:15375browse

How to Prevent Command Line Windows from Appearing When Using `exec.Command` in Go?

Preventing Command Line Window from Appearing When Using Exec in Golang

In Go, using exec.Command to spawn new processes can sometimes cause a visible command line window to appear. This issue persists even when leveraging syscall.SysProcAttr.HideWindow to suppress the window.

Solution:

There exists an alternative approach to execute commands without generating a visible window. This solution involves using the cmd.exe utility to execute the desired command.

Code Sample:

import "syscall"

// Use cmd.exe to execute commands without spawning a visible window
func main() {
    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()
}

Source:

The original solution can be found here: https://www.reddit.com/r/golang/comments/2c1g3x/build_golang_app_reverse_shell_to_run_in_windows/

The above is the detailed content of How to Prevent Command Line Windows from Appearing When Using `exec.Command` in Go?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn