executable_windows.go 641 B

1234567891011121314151617181920212223242526272829303132
  1. // Copyright 2016 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package os
  5. import (
  6. "internal/syscall/windows"
  7. "syscall"
  8. )
  9. func getModuleFileName(handle syscall.Handle) (string, error) {
  10. n := uint32(1024)
  11. var buf []uint16
  12. for {
  13. buf = make([]uint16, n)
  14. r, err := windows.GetModuleFileName(handle, &buf[0], n)
  15. if err != nil {
  16. return "", err
  17. }
  18. if r < n {
  19. break
  20. }
  21. // r == n means n not big enough
  22. n += 1024
  23. }
  24. return syscall.UTF16ToString(buf), nil
  25. }
  26. func executable() (string, error) {
  27. return getModuleFileName(0)
  28. }