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
Release: 2024-11-28 02:22:15
Original
376 people have browsed it

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()
}
Copy after login

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template