windows: add token environment functions

This brings the x/sys package into parity with the capabilities provided
indirectly in CL 176619, and adds a helper to make it useful.

Change-Id: I81f2d205bbb0c2b2c530b2bd991c1e6ff30cc94e
Reviewed-on: https://go-review.googlesource.com/c/sys/+/176620
Run-TryBot: Jason Donenfeld <Jason@zx2c4.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Alex Brainman <alex.brainman@gmail.com>
This commit is contained in:
Jason A. Donenfeld
2019-05-15 12:05:40 +00:00
committed by Alex Brainman
parent f91f9b37d0
commit 06a5c49444
3 changed files with 68 additions and 2 deletions
+33 -1
View File
@@ -37,6 +37,7 @@ func errnoErr(e syscall.Errno) error {
var (
modadvapi32 = NewLazySystemDLL("advapi32.dll")
modkernel32 = NewLazySystemDLL("kernel32.dll")
moduserenv = NewLazySystemDLL("userenv.dll")
modshell32 = NewLazySystemDLL("shell32.dll")
modmswsock = NewLazySystemDLL("mswsock.dll")
modcrypt32 = NewLazySystemDLL("crypt32.dll")
@@ -45,7 +46,6 @@ var (
modiphlpapi = NewLazySystemDLL("iphlpapi.dll")
modsecur32 = NewLazySystemDLL("secur32.dll")
modnetapi32 = NewLazySystemDLL("netapi32.dll")
moduserenv = NewLazySystemDLL("userenv.dll")
modwtsapi32 = NewLazySystemDLL("wtsapi32.dll")
procRegisterEventSourceW = modadvapi32.NewProc("RegisterEventSourceW")
@@ -128,6 +128,8 @@ var (
procFreeEnvironmentStringsW = modkernel32.NewProc("FreeEnvironmentStringsW")
procGetEnvironmentVariableW = modkernel32.NewProc("GetEnvironmentVariableW")
procSetEnvironmentVariableW = modkernel32.NewProc("SetEnvironmentVariableW")
procCreateEnvironmentBlock = moduserenv.NewProc("CreateEnvironmentBlock")
procDestroyEnvironmentBlock = moduserenv.NewProc("DestroyEnvironmentBlock")
procSetFileTime = modkernel32.NewProc("SetFileTime")
procGetFileAttributesW = modkernel32.NewProc("GetFileAttributesW")
procSetFileAttributesW = modkernel32.NewProc("SetFileAttributesW")
@@ -1300,6 +1302,36 @@ func SetEnvironmentVariable(name *uint16, value *uint16) (err error) {
return
}
func CreateEnvironmentBlock(block **uint16, token Token, inheritExisting bool) (err error) {
var _p0 uint32
if inheritExisting {
_p0 = 1
} else {
_p0 = 0
}
r1, _, e1 := syscall.Syscall(procCreateEnvironmentBlock.Addr(), 3, uintptr(unsafe.Pointer(block)), uintptr(token), uintptr(_p0))
if r1 == 0 {
if e1 != 0 {
err = errnoErr(e1)
} else {
err = syscall.EINVAL
}
}
return
}
func DestroyEnvironmentBlock(block *uint16) (err error) {
r1, _, e1 := syscall.Syscall(procDestroyEnvironmentBlock.Addr(), 1, uintptr(unsafe.Pointer(block)), 0, 0)
if r1 == 0 {
if e1 != 0 {
err = errnoErr(e1)
} else {
err = syscall.EINVAL
}
}
return
}
func SetFileTime(handle Handle, ctime *Filetime, atime *Filetime, wtime *Filetime) (err error) {
r1, _, e1 := syscall.Syscall6(procSetFileTime.Addr(), 4, uintptr(handle), uintptr(unsafe.Pointer(ctime)), uintptr(unsafe.Pointer(atime)), uintptr(unsafe.Pointer(wtime)), 0, 0)
if r1 == 0 {