登录
首页 >  Golang >  Go问答

如何优雅地从“syscall.Syscall()”的使用中迁移?

来源:stackoverflow

时间:2024-03-26 15:42:38 322浏览 收藏

syscall 软件包已被弃用,其建议替代方案无法提供类似于 syscall.syscall() 的功能。要优雅地迁移,可以利用 mkwinsyscall 工具生成自定义系统调用代码。该工具自动处理 uintptr 和字符串转换,简化了系统调用实现,并确保指针在调用过程中保持有效。

问题内容

syscall 软件包已弃用。假设我有以下代码,我想将其迁移到未弃用的代码:

somegoobject := &struct{int; float32}{5, 45.4}
syscall.syscall6(someproc.addr(), 1, uintptr(unsafe.pointer(somegoobject)), 0, 0, 0, 0, 0)

其中 someproc 的类型为 *syscall.lazyproc (windows)。

syscall 文档建议使用的 sys 子存储库不再提供类似于 syscall.syscall 的功能,如果那里没有实现所需的功能,人们可能会尝试通过以下方式解决问题,并且相信工作已经完成:

someproc.call(uintptr(unsafe.pointer(somegoobject)))

现在 someproc 的类型为 *windows.lazyproc

但是,如果我们这样做,我们将无法获得 syscall.syscall(和朋友)的保证之一,因为 lazyproc.call() 未在汇编中实现:

(4) 调用 syscall.syscall 时将指针转换为 uintptr。 syscall 包中的 syscall 函数传递它们的 uintptr 参数 直接连接到操作系统,然后操作系统可能会根据 调用的详细信息,将其中一些重新解释为指针。那是, 系统调用实现隐式转换某些 从 uintptr 返回到指针的参数。 如果指针参数必须转换为 uintptr 才能用作 参数,该转换必须出现在调用表达式本身中: syscall.syscall(sys_read, uintptr(fd), uintptr(unsafe.pointer(p)), uintptr(n)) 编译器处理转换为 uintptr 的指针 调用在汇编中实现的函数的参数列表 安排保留引用的分配对象(如果有) 即使从类型来看,在调用完成之前也不会移动 单独来看,在该过程中似乎不再需要该对象 称呼。 为了让编译器识别此模式,必须出现转换 在参数列表中: // 无效:uintptr 不能存储在变量中 // 在系统调用期间隐式转换回指针之前。 u := uintptr(不安全.pointer(p)) syscall.syscall(sys_read, uintptr(fd), u, uintptr(n))

(取自https://golang.org/pkg/unsafe/)

在我看来,唯一安全的方法是使用 c 来分配内存。然而,这意味着我们需要复制数据并增加需要编写的代码量。

someObject := (*struct{int; float32})(C.calloc(1, unsafe.Sizeof(struct{int; float32}{}))) // Alloc
*someObject = struct{int; float32}{123, 456.789} // Fill with desired data
someProc.Call(uintptr(unsafe.Pointer(someObject))) // The actual call
C.free(unsafe.Pointer(someObject)) // Clean up

有更好的方法吗?


解决方案


我认为处理这个问题的正确方法是使用 mkwinsyscall 工具。 您可以像这样创建一个 go 文件:

package main
//go:generate mkwinsyscall -output zmsi.go msi.go
//sys msiopendatabase(dbpath string, persist int, db *windows.handle) (e error) = msi.msiopendatabasew

然后运行gogenerate,你会得到一个像这样的结果文件(删除了一些部分 为简洁起见):

func MsiOpenDatabase(dbPath string, persist int, db *windows.Handle) (e error) {
    var _p0 *uint16
    _p0, e = syscall.UTF16PtrFromString(dbPath)
    if e != nil {
        return
    }
    return _MsiOpenDatabase(_p0, persist, db)
}

func _MsiOpenDatabase(dbPath *uint16, persist int, db *windows.Handle) (e error) {
    r0, _, _ := syscall.Syscall(procMsiOpenDatabaseW.Addr(), 3, uintptr(unsafe.Pointer(dbPath)), uintptr(persist), uintptr(unsafe.Pointer(db)))
    if r0 != 0 {
        e = syscall.Errno(r0)
    }
    return
}

如您所见,它也会自动处理字符串转换 作为系统调用代码,甚至错误处理。

终于介绍完啦!小伙伴们,这篇关于《如何优雅地从“syscall.Syscall()”的使用中迁移?》的介绍应该让你收获多多了吧!欢迎大家收藏或分享给更多需要学习的朋友吧~golang学习网公众号也会发布Golang相关知识,快来关注吧!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>