dongzan1970 2015-11-14 13:42
浏览 169
已采纳

Golang:如何在没有cgo的情况下调用win32 API?

I'm trying to call GetUserNameEx from secur32.dll like this:

dll, err := syscall.LoadDLL("secur32.dll")
if err != nil {
    log.Fatal(err)
}
defer dll.Release()

GetUserNameEx, err := dll.FindProc("GetUserNameExW")
if err != nil {
    log.Fatal(err)
}
arr := make([]uint8, 256)
var size uint
GetUserNameEx.Call(3, uintptr(unsafe.Pointer(&arr[0])), uintptr(unsafe.Pointer(&size)))
fmt.Println(arr)
fmt.Println(size)

This code compiles fine, but GetUserNameEx.Call() will fail. I don't know why I cannot get UserName. Could anyone help me?

  • 写回答

1条回答 默认 最新

  • douju8782 2015-11-14 14:49
    关注

    size is an in-out parameter. When you make the call, you have to set it to the size of the buffer (arr). Also its type is PULONG, so in Go use uint32. Windows PULONG type is a pointer to a ULONG (which has range 0..4294967295). See source.

    Also Call() returns 3 values:

    func (p *Proc) Call(a ...uintptr) (r1, r2 uintptr, lastErr error)
    

    Store the returned lastErr and print it. Would you have done so, you would find the error earlier:

    _, _, lastErr := GetUserNameEx.Call(
        3, uintptr(unsafe.Pointer(&arr[0])), uintptr(unsafe.Pointer(&size)))
    
    fmt.Println(lastErr)
    

    Prints:

    More data is available.
    

    This means more data is available than what fits into the buffer you pass - or rather - by the size you indicated with the in-out parameter size (you passed 0 as the size).

    Working code (note the division by 2 due to unicode and minus 1 for the terminating '\0' byte / character for the size calculation):

    arr := make([]uint8, 256)
    var size uint32 = uint32(len(arr)) / 2 - 1
    _, _, lastErr := GetUserNameEx.Call(
        3, uintptr(unsafe.Pointer(&arr[0])), uintptr(unsafe.Pointer(&size)))
    
    fmt.Println(lastErr)
    fmt.Println(string(arr))
    fmt.Println(arr)
    fmt.Println(size)
    

    In this case lastErr will be:

    The operation completed successfully.
    

    To properly handle error:

    The returned error is always non-nil, constructed from the result of GetLastError. Callers must inspect the primary return value to decide whether an error occurred (according to the semantics of the specific function being called) before consulting the error. The error will be guaranteed to contain syscall.Errno.

    Example:

    r1, _, lastErr := GetUserNameEx.Call(
        3, uintptr(unsafe.Pointer(&arr[0])), uintptr(unsafe.Pointer(&size)))
    
    if r1 == 0 {
        fmt.Println("ERROR:", lastErr.Error())
        return
    }
    // No error, proceed to print/use arr
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 爬取1-112页所有帖子的标题但是12页后要登录后才能 我使用selenium模拟登录 账号密码输入后 会报错 不知道怎么弄了
  • ¥30 关于用python写支付宝扫码付异步通知收不到的问题
  • ¥50 vue组件中无法正确接收并处理axios请求
  • ¥15 隐藏系统界面pdf的打印、下载按钮
  • ¥15 MATLAB联合adams仿真卡死如何解决(代码模型无问题)
  • ¥15 基于pso参数优化的LightGBM分类模型
  • ¥15 安装Paddleocr时报错无法解决
  • ¥15 python中transformers可以正常下载,但是没有办法使用pipeline
  • ¥50 分布式追踪trace异常问题
  • ¥15 人在外地出差,速帮一点点