Home > Backend Development > Golang > How to hide the dos window when golang calls cmd command

How to hide the dos window when golang calls cmd command

王林
Release: 2019-12-24 16:14:08
Original
4367 people have browsed it

How to hide the dos window when golang calls cmd command

When calling the cmd command through go's standard library exec, a black window will pop up. To solve this problem, you can use the win32 API WinExec under Windows.

This problem mainly occurs when a program with UI or no console calls cmd.

Add parameters when compiling go:

go build  -ldflags="-H windowsgui"
Copy after login
package main
import (
	"errors"
	"
)
import (	
    "github.com/CodyGuo/win"
)
var (
	winExecError = map[uint32]string{		
	0:  "The system is out of memory or resources.",		
	2:  "The .exe file is invalid.",		
	3:  "The specified file was not found.",		
	11: "The specified path was not found.",
	}
)
func main() {
	err := execRun("cmd /c start http://www.baidu.com")	
	if err != nil {
		log.Fatal(err)
	}
}
func execRun(cmd string) error {
	lpCmdLine := win.StringToBytePtr(cmd)	
//http://baike.baidu.com/link?url=51sQomXsIt6OlYEAV74YZ0JkHDd2GbmzXcKj_4H1R4ILXvQNf3MXIscKnAkSR93e7Fyns4iTmSatDycEb
HrXzq
	ret := win.WinExec(lpCmdLine, win.SW_HIDE)	
	if ret <= 31 {		
	    return errors.New(winExecError[ret])
	}	return nil
}
Copy after login

Recommended related articles and tutorials: golang tutorial

The above is the detailed content of How to hide the dos window when golang calls cmd command. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template