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 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 UE5#if WITH_EDITOR导致打包的功能不可用
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
  • ¥20 yolov5自定义Prune报错,如何解决?
  • ¥15 电磁场的matlab仿真
  • ¥15 mars2d在vue3中的引入问题
  • ¥50 h5唤醒支付宝并跳转至向小荷包转账界面
  • ¥15 算法题:数的划分,用记忆化DFS做WA求调
  • ¥15 chatglm-6b应用到django项目中,模型加载失败