doulu1020 2012-05-15 06:46
浏览 103
已采纳

通过Go写入Windows注册表

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.

  • 写回答

3条回答 默认 最新

  • dow98764 2012-05-31 11:52
    关注

    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
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题