hidden_windows.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. // Copyright (C) 2014 The Syncthing Authors.
  2. //
  3. // This program is free software: you can redistribute it and/or modify it
  4. // under the terms of the GNU General Public License as published by the Free
  5. // Software Foundation, either version 3 of the License, or (at your option)
  6. // any later version.
  7. //
  8. // This program is distributed in the hope that it will be useful, but WITHOUT
  9. // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. // more details.
  12. //
  13. // You should have received a copy of the GNU General Public License along
  14. // with this program. If not, see <http://www.gnu.org/licenses/>.
  15. // +build windows
  16. package osutil
  17. import "syscall"
  18. func HideFile(path string) error {
  19. p, err := syscall.UTF16PtrFromString(path)
  20. if err != nil {
  21. return err
  22. }
  23. attrs, err := syscall.GetFileAttributes(p)
  24. if err != nil {
  25. return err
  26. }
  27. attrs |= syscall.FILE_ATTRIBUTE_HIDDEN
  28. return syscall.SetFileAttributes(p, attrs)
  29. }
  30. func ShowFile(path string) error {
  31. p, err := syscall.UTF16PtrFromString(path)
  32. if err != nil {
  33. return err
  34. }
  35. attrs, err := syscall.GetFileAttributes(p)
  36. if err != nil {
  37. return err
  38. }
  39. attrs &^= syscall.FILE_ATTRIBUTE_HIDDEN
  40. return syscall.SetFileAttributes(p, attrs)
  41. }
  42. func HideConsole() {
  43. getConsoleWindow := syscall.NewLazyDLL("kernel32.dll").NewProc("GetConsoleWindow")
  44. showWindow := syscall.NewLazyDLL("user32.dll").NewProc("ShowWindow")
  45. if getConsoleWindow.Find() == nil && showWindow.Find() == nil {
  46. hwnd, _, _ := getConsoleWindow.Call()
  47. if hwnd != 0 {
  48. showWindow.Call(hwnd, 0)
  49. }
  50. }
  51. }