hidden_windows.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. // Copyright (C) 2014 The Syncthing Authors.
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  5. // You can obtain one at http://mozilla.org/MPL/2.0/.
  6. // +build windows
  7. package osutil
  8. import "syscall"
  9. func HideFile(path string) error {
  10. p, err := syscall.UTF16PtrFromString(path)
  11. if err != nil {
  12. return err
  13. }
  14. attrs, err := syscall.GetFileAttributes(p)
  15. if err != nil {
  16. return err
  17. }
  18. attrs |= syscall.FILE_ATTRIBUTE_HIDDEN
  19. return syscall.SetFileAttributes(p, attrs)
  20. }
  21. func ShowFile(path string) error {
  22. p, err := syscall.UTF16PtrFromString(path)
  23. if err != nil {
  24. return err
  25. }
  26. attrs, err := syscall.GetFileAttributes(p)
  27. if err != nil {
  28. return err
  29. }
  30. attrs &^= syscall.FILE_ATTRIBUTE_HIDDEN
  31. return syscall.SetFileAttributes(p, attrs)
  32. }
  33. func HideConsole() {
  34. getConsoleWindow := syscall.NewLazyDLL("kernel32.dll").NewProc("GetConsoleWindow")
  35. showWindow := syscall.NewLazyDLL("user32.dll").NewProc("ShowWindow")
  36. if getConsoleWindow.Find() == nil && showWindow.Find() == nil {
  37. hwnd, _, _ := getConsoleWindow.Call()
  38. if hwnd != 0 {
  39. showWindow.Call(hwnd, 0)
  40. }
  41. }
  42. }