枚举Go中的注册表值(Golang) [英] Enumerating Registry Values in Go (Golang)

查看:198
本文介绍了枚举Go中的注册表值(Golang)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用Go枚举Windows注册表中的值列表,但我遇到了一些麻烦。我尝试了两种方法:使用Go提供的 syscall 库调用 RegEnumValue ,以及< href =https://github.com/lxn/win>使用lxn的Windows API封装器。在这两种情况下,我都有同样的问题。这是我使用的代码(目前使用lxn的win库):

I'm trying to enumerate over a list of values in the Windows registry using Go, but I'm running into some trouble. I've tried two approaches: using both the Go-provided syscall library to call into RegEnumValue, as well as using a Windows API wrapper by lxn. In both cases, I'm having the same issue. This is the code I'm using (which is currently using the win library from lxn):

var root win.HKEY
rootpath, _ := syscall.UTF16PtrFromString("HARDWARE\\DEVICEMAP\\SERIALCOMM")
fmt.Println(win.RegOpenKeyEx(win.HKEY_LOCAL_MACHINE, rootpath, 0, win.KEY_READ, &root))

var name_length uint32 = 72
var name *uint16
var key_type uint32
var lpData *byte
var lpDataLength uint32 = 72
var zero_uint uint32 = 0
fmt.Println(win.RegEnumValue(root, zero_uint, name, &name_length, nil, &key_type, lpData, &lpDataLength))

win.RegCloseKey(root)

在这种情况下, RegEnumValue 始终返回代码87,其中 MSDN只有解释是参数不正确。

In this case, RegEnumValue always returns code 87, which MSDN's only explanation is "The parameter is incorrect."

有没有人有任何想法可以指引我正确的方向对于这个?

Does anyone have any ideas that can point me in the right direction for this?

推荐答案

Golang的子reddit成员指出,我实际上没有分配任何内存给传递的缓冲区进入 RegEnumValue 。因此,我已将上述示例更正为以下内容:

A member of the Golang sub-reddit pointed out that I was not actually allocating any memory to the buffers passed in to RegEnumValue. As such, I've corrected the above example to the following:

var name_length uint32 = 72
var key_type uint32
var lpDataLength uint32 = 72
var zero_uint uint32 = 0
name := make([]uint16, 72)
lpData := make([]byte, 72)

win.RegEnumValue(root, zero_uint, &name[0], &name_length, nil, &key_type, &lpData[0], &lpDataLength)

显然,72的幻数应该可以替换为其他值。还有另外一种称为 RegQueryInfoKey 的方法,它可以检索有关注册表项的信息,以便为键中的最大名称和值分配正确的字节数。

Obviously, the "magic number" of 72 should probably be replaced with something else. There is another method called RegQueryInfoKey that can retrieve information about the registry key to allocate the correct number of bytes for the largest name and value in the key.

这篇关于枚举Go中的注册表值(Golang)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆