user_windows.go 871 B

123456789101112131415161718192021222324252627
  1. // Package pq is a pure Go Postgres driver for the database/sql package.
  2. package pq
  3. import (
  4. "path/filepath"
  5. "syscall"
  6. )
  7. // Perform Windows user name lookup identically to libpq.
  8. //
  9. // The PostgreSQL code makes use of the legacy Win32 function
  10. // GetUserName, and that function has not been imported into stock Go.
  11. // GetUserNameEx is available though, the difference being that a
  12. // wider range of names are available. To get the output to be the
  13. // same as GetUserName, only the base (or last) component of the
  14. // result is returned.
  15. func userCurrent() (string, error) {
  16. pw_name := make([]uint16, 128)
  17. pwname_size := uint32(len(pw_name)) - 1
  18. err := syscall.GetUserNameEx(syscall.NameSamCompatible, &pw_name[0], &pwname_size)
  19. if err != nil {
  20. return "", ErrCouldNotDetectUsername
  21. }
  22. s := syscall.UTF16ToString(pw_name)
  23. u := filepath.Base(s)
  24. return u, nil
  25. }