在Go(Golang)中枚举注册表值

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

Enumerating Registry Values in Go (Golang)

问题

我正在尝试使用Go语言枚举Windows注册表中的值列表,但遇到了一些问题。我尝试了两种方法:一种是使用Go提供的syscall库调用RegEnumValue,另一种是使用lxn的Windows API包装器。在这两种情况下,我都遇到了相同的问题。以下是我目前使用的代码(当前使用的是lxn的win库):

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的唯一解释是“参数不正确”。

有没有人有任何想法可以指导我解决这个问题?

英文:

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)

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?

答案1

得分: 6

也许有人会觉得这个有用:

const regKey = `SOFTWARE\Microsoft\Windows NT\CurrentVersion`

func getSettingsFromRegistry() (settings map[string]string, error) {
    settings = make(map[string]string)

    k, err := registry.OpenKey(registry.LOCAL_MACHINE, regKey, registry.QUERY_VALUE)
    if err != nil {

        return fmt.Errorf("打开注册表键 %q 失败: %w", regKey, err)
    }
    defer k.Close()

    params, err := k.ReadValueNames(0)
    if err != nil {
        return fmt.Errorf("读取值名称 %q 失败: %w", psaConfRegistry32, err)
    }

    for _, param := range params {
        val, err := getRegistryValueAsString(k, param)
        if err != nil {
            return fmt.Errorf("获取字符串值 %q 失败: %w", k, err)
        }
        settings[param] = val
    }

    self.Log.Printf("%#v\n", settings)
    return
}

func getRegistryValueAsString(key registry.Key, subKey string) (string, error) {
    valString, _, err := key.GetStringValue(subKey)
    if err == nil {
        return valString, nil
    }
    valStrings, _, err := key.GetStringsValue(subKey)
    if err == nil {
        return strings.Join(valStrings, "\n"), nil
    }
    valBinary, _, err := key.GetBinaryValue(subKey)
    if err == nil {
        return string(valBinary), nil
    }
    valInteger, _, err := key.GetIntegerValue(subKey)
    if err == nil {
        return strconv.FormatUint(valInteger, 10), nil
    }

    return "", fmt.Errorf("无法获取子键 %q 的类型", subKey)
}

REG_BINARY 值将会像 "\x01\x00\x00..." 这样。

英文:

Maybe someone found this useful:

const regKey = `SOFTWARE\Microsoft\Windows NT\CurrentVersion`

func getSettingsFromRegistry() (settings map[string]string, error) {
    settings = make(map[string]string)

    k, err := registry.OpenKey(registry.LOCAL_MACHINE, regKey, registry.QUERY_VALUE)
    if err != nil {
        
        return fmt.Errorf("open registry key %q: %w", regKey, err)
    }
    defer k.Close()

    params, err := k.ReadValueNames(0)
    if err != nil {
        return fmt.Errorf("read value names %q: %w", psaConfRegistry32, err)
    }

    for _, param := range params {
        val, err := getRegistryValueAsString(k, param)
        if err != nil {
            return fmt.Errorf("get value as string %q: %w", k, err)
        }
        settings[param] = val
    }

    self.Log.Printf("%#v\n", settings)
    return
}

func getRegistryValueAsString(key registry.Key, subKey string) (string, error) {
    valString, _, err := key.GetStringValue(subKey)
    if err == nil {
        return valString, nil
    }
    valStrings, _, err := key.GetStringsValue(subKey)
    if err == nil {
        return strings.Join(valStrings, "\n"), nil
    }
    valBinary, _, err := key.GetBinaryValue(subKey)
    if err == nil {
        return string(valBinary), nil
    }
    valInteger, _, err := key.GetIntegerValue(subKey)
    if err == nil {
        return strconv.FormatUint(valInteger, 10), nil
    }

    return "", fmt.Errorf("failed to get type for sub key %q", subKey)
}

REG_BINARY values will looks like "\x01\x00\x00..."

答案2

得分: 5

Golang子版块的一位成员指出,我实际上没有为传递给RegEnumValue的缓冲区分配任何内存。因此,我已经将上面的示例更正如下:

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,可以获取有关注册表键的信息,以便为键中最大名称和值分配正确数量的字节。

英文:

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)

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.

huangapple
  • 本文由 发表于 2013年8月25日 11:43:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/18425465.html
匿名

发表评论

匿名网友

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

确定