Browse Source

Revisit -no-console option for Windows

The reason for ShowWindow opose to your FreeConsole is because if you start up
cmd.exe and do syncthing.exe -no-output it actually hides the existing cmd.exe
window oppose to opening a separate window and then hiding it, which keeps the
existing console hanging on syncthing.exe running.

I tried playing around with compiling as GUI, then given the option is not present
allocating a console, and redirecting the std streams to the new console, but that
seems ugly as I'd have to make quite a few calls. But that does get of the initial
flash.
Audrius Butkevicius 11 years ago
parent
commit
80dca96ee8
3 changed files with 21 additions and 0 deletions
  1. 8 0
      cmd/syncthing/main.go
  2. 2 0
      internal/osutil/hidden_unix.go
  3. 11 0
      internal/osutil/hidden_windows.go

+ 8 - 0
cmd/syncthing/main.go

@@ -188,6 +188,7 @@ var (
 	doUpgrade         bool
 	doUpgradeCheck    bool
 	noBrowser         bool
+	noConsole         bool
 	generateDir       string
 	logFile           string
 	noRestart         = os.Getenv("STNORESTART") != ""
@@ -214,6 +215,9 @@ func main() {
 
 		logFile = filepath.Join(defConfDir, "syncthing.log")
 		flag.StringVar(&logFile, "logfile", logFile, "Log file name (blank for stdout)")
+
+		// We also add an option to hide the console window
+		flag.BoolVar(&noConsole, "no-console", false, "Hide console window")
 	}
 
 	flag.StringVar(&generateDir, "generate", "", "Generate key and config in specified dir, then exit")
@@ -232,6 +236,10 @@ func main() {
 	flag.Usage = usageFor(flag.CommandLine, usage, fmt.Sprintf(extraUsage, defConfDir))
 	flag.Parse()
 
+	if noConsole {
+		osutil.HideConsole()
+	}
+
 	if confDir == "" {
 		// Not set as default above because the string can be really long.
 		confDir = defConfDir

+ 2 - 0
internal/osutil/hidden_unix.go

@@ -24,3 +24,5 @@ func HideFile(path string) error {
 func ShowFile(path string) error {
 	return nil
 }
+
+func HideConsole() {}

+ 11 - 0
internal/osutil/hidden_windows.go

@@ -48,3 +48,14 @@ func ShowFile(path string) error {
 	attrs &^= syscall.FILE_ATTRIBUTE_HIDDEN
 	return syscall.SetFileAttributes(p, attrs)
 }
+
+func HideConsole() {
+	getConsoleWindow := syscall.NewLazyDLL("kernel32.dll").NewProc("GetConsoleWindow")
+	showWindow := syscall.NewLazyDLL("user32.dll").NewProc("ShowWindow")
+	if getConsoleWindow.Find() == nil && showWindow.Find() == nil {
+		hwnd, _, _ := getConsoleWindow.Call()
+		if hwnd != 0 {
+			showWindow.Call(hwnd, 0)
+		}
+	}
+}