env_windows.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. // Copyright 2010 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. // Windows environment variables.
  5. package windows
  6. import (
  7. "syscall"
  8. "unsafe"
  9. )
  10. func Getenv(key string) (value string, found bool) {
  11. return syscall.Getenv(key)
  12. }
  13. func Setenv(key, value string) error {
  14. return syscall.Setenv(key, value)
  15. }
  16. func Clearenv() {
  17. syscall.Clearenv()
  18. }
  19. func Environ() []string {
  20. return syscall.Environ()
  21. }
  22. // Returns a default environment associated with the token, rather than the current
  23. // process. If inheritExisting is true, then this environment also inherits the
  24. // environment of the current process.
  25. func (token Token) Environ(inheritExisting bool) (env []string, err error) {
  26. var block *uint16
  27. err = CreateEnvironmentBlock(&block, token, inheritExisting)
  28. if err != nil {
  29. return nil, err
  30. }
  31. defer DestroyEnvironmentBlock(block)
  32. blockp := uintptr(unsafe.Pointer(block))
  33. for {
  34. entry := UTF16PtrToString((*uint16)(unsafe.Pointer(blockp)))
  35. if len(entry) == 0 {
  36. break
  37. }
  38. env = append(env, entry)
  39. blockp += 2 * (uintptr(len(entry)) + 1)
  40. }
  41. return env, nil
  42. }
  43. func Unsetenv(key string) error {
  44. return syscall.Unsetenv(key)
  45. }