在bitbucket.org/kardianos/service/src/service_windows.go中找到,还有可用的 Windows 注册表代码以及(奖励!)Windows 服务管理代码。这是该代码的示例:
var regSetKeyValueExProc = advapi.MustFindProc("RegSetValueExW")
func regSetKeyValue(h syscall.Handle, keyName string, data interface{}) error {
var dataPtr, dataLen, dataType uintptr
switch v := data.(type) {
case uint32:
dataPtr, dataLen = uintptr(unsafe.Pointer(&v)), 4
dataType = _REG_DWORD
case string:
// The comment on MSDN regarding escaping back-slashes, are c-lang specific.
// The API just takes a normal NUL terminated string, no special escaping required.
dataPtr, dataLen = StringToUTF16PtrLen(v)
dataType = _REG_SZ
}
r0, _, e1 := regSetKeyValueExProc.Call(
uintptr(h),
uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(keyName))),
0,
dataType,
dataPtr,
dataLen,
)
if r0 != 0 {
return e1
}
return nil
}