gpt4 book ai didi

c++ - 系统调用 GetLastError() 不返回错误

转载 作者:数据小太阳 更新时间:2023-10-29 03:24:18 24 4
gpt4 key购买 nike

在 golang 中 syscall.GetLastError() 不返回最后一个错误。看下面的例子

if handle := _OpenSCManager(machineNamePtr, databaseNamePtr, desiredAccess); handle == nil {
if err := syscall.GetLastError(); err != nil {
return InvalidServiceDatabaseHandleHandle, ServiceErrno(err.(syscall.Errno))
}
}

err 始终为 nil。假设 machineNamePtr 是一台不存在的机器。使用 C++ 测试了相同的代码,GetLastError() 抛出 RPC 服务器不可用。那为什么不 go 呢?

编辑

_OpenSCManager 是用go generate 生成的。//sys _OpenSCManager(machineName *uint16, databaseName *uint16, desiredAccesss ServiceAccessRight) (句柄 ServiceDatabaseHandle) = advapi32.OpenSCManagerW

func _OpenSCManager(machineName *uint16, databaseName *uint16, desiredAcces ServiceAccessRight) (handle ServiceDatabaseHandle) {
r0, _, _ := syscall.Syscall(procOpenSCManagerW.Addr(), 3, uintptr(unsafe.Pointer(machineName)), uintptr(unsafe.Pointer(databaseName)), uintptr(desiredAcces))
handle = ServiceDatabaseHandle(r0)
return

最佳答案

所以我终于成功了。首先,我尝试返回一个具有以下签名的错误变量 //sys _OpenSCManager(machineName *uint16, databaseName *uint16, desiredAcces ServiceAccessRight) (handle ServiceDatabaseHandle, lasterror error) = advapi32.OpenSCManagerW。但是 go generate 总是抛出 Only last windows error is allowed as second return value...。但是,如果将其更改为 //sys ... (handle ServiceDatabaseHandle, err error) = advapi32.OpenSCManagerW 它会成功生成代码。所以你必须明确地写err error。有人知道为什么吗?所以现在函数看起来像

func _OpenSCManager(machineName *uint16, databaseName *uint16, desiredAcces ServiceAccessRight) (handle ServiceDatabaseHandle, err error) {
r0, _, e1 := syscall.Syscall(procOpenSCManagerW.Addr(), 3, uintptr(unsafe.Pointer(machineName)), uintptr(unsafe.Pointer(databaseName)), uintptr(desiredAcces))
handle = ServiceDatabaseHandle(r0)
if handle == 0 {
if e1 != 0 {
err = errnoErr(e1)
} else {
err = syscall.EINVAL
}
}
return
}

它返回一个错误。因此无需调用 GetLastError()

关于c++ - 系统调用 GetLastError() 不返回错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46337816/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com