通过Go编写到Windows注册表的方法

huangapple go评论89阅读模式
英文:

Write to Windows Registry via Go

问题

我需要通过Go语言写入Windows注册表,但是找不到相应的机制。

我发现https://github.com/lxn/go-winapi在_advapi32.go_文件中提供了对注册表的只读访问。我已经提交了一个错误报告,请求能够写入注册表的功能,这样它就可以成为未来某个人的愿望清单或任务清单。

我认为这个功能可能在现有的Go库中甚至不存在,需要创建它。如果它已经存在,我在这里询问。如果它不存在,但你有兴趣增强_go-winapi_或其他类似的库,请尽管去做。或者如果你知道写入注册表的机制,分享这些信息,这样我(或其他人)可以将其作为补丁添加到_go-winapi_库中,将会非常感激。

如果没有其他办法,如果我能够将这个功能添加到_go-winapi_或类似的库中,我会回到这里回答这个问题(如果没有其他人回答)。

英文:

I need to write to the Windows Registry via Go, but am unable to find the mechanism to do so.

I have found that https://github.com/lxn/go-winapi provides read-only access to the Registry in the advapi32.go file. I have filed a bug report requesting the ability to write to the Registry as well so it makes it into a wishlist or tasklist for someone in the future.

I think that this ability may not even exist in an existing Go library and that it needs to be created. On the chance that it already exists, I'm asking here. If it doesn't exist but you are interested in enhancing go-winapi or another similar library, please do. Or if you know the mechanics behind writing to the registry, sharing that information so that I (or someone else) can work that into a patch for the go-winapi library would certainly be appreciated.

If nothing else, I'll come back here to answer this (if no one else does) if I'm able to get this added to go-winapi or a similar library myself.

答案1

得分: 3

这是一些示例:

https://github.com/axgle/go/tree/master/regedit

英文:

here is some sample

https://github.com/axgle/go/tree/master/regedit

答案2

得分: 3

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:
                // MSDN上关于转义反斜杠的注释是特定于C语言的。
                // API只接受普通的NUL终止字符串,不需要特殊的转义。
                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
}
英文:

Found at bitbucket.org/kardianos/service/src/service_windows.go, there is working Windows Registry code along with (bonus!) Windows Service management code, too. Here is a sample of that code:

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
}

答案3

得分: 0

另一个选择是使用WMI来读写注册表,这也可以通过Go来访问。

WMIC: 使用注册表描述了如何通过Windows的WMIC命令行工具来实现。这可以适应任何可以与WMI进行接口的编程环境。

我将保留user1391070的答案作为标记的答案,但这可能对需要使用WMI的人有所帮助。

英文:

Another option is to use WMI to read/write the registry, which is also accessible via Go.

WMIC: Work with the registry describes how to go about doing that via the WMIC command line tool in Windows. That can be adapted for use in any programming environment that can interface with WMI.

I'm leaving user1391070's answer as the flagged answer, but this might help someone that needs to go the WMI route.

huangapple
  • 本文由 发表于 2012年5月15日 14:46:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/10595476.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定