dongyi1441 2013-08-25 03:43 采纳率: 100%
浏览 282
已采纳

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

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?

  • 写回答

2条回答 默认 最新

  • duan032225 2013-08-25 06:44
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!