douyujun0152 2014-04-08 22:29
浏览 297
已采纳

使用Golang获取Windows空闲时间(GetLastInputInfo或类似内容)

Is there an example or method of getting a Windows system's idle time using Go?
I've been looking at the documentation at the Golang site but I think I'm missing how to access (and use) the API to get system information including the idle time.

  • 写回答

2条回答 默认 最新

  • dongtan7351 2014-04-08 23:54
    关注

    Go's website is hardcoded to show the documentation for the standard library packages on Linux. You will need to get godoc and run it yourself:

    go get golang.org/x/tools/cmd/godoc
    godoc --http=:6060
    

    then open http://127.0.0.1:6060/ in your web browser.

    Of note is package syscall, which provides facilities for accessing functions in DLLs, including UTF-16 helpers and callback generation functions.

    Doing a quick recursive search of the Go tree says it doesn't have an API for GetLastInputInfo() in particular, so unless I'm missing something, you should be able to call that function from the DLL directly:

    user32 := syscall.MustLoadDLL("user32.dll") // or NewLazyDLL() to defer loading
    getLastInputInfo := user32.MustFindProc("GetLastInputInfo") // or NewProc() if you used NewLazyDLL()
    // or you can handle the errors in the above if you want to provide some alternative
    r1, _, err := getLastInputInfo.Call(uintptr(arg))
    // err will always be non-nil; you need to check r1 (the return value)
    if r1 == 0 { // in this case
        panic("error getting last input info: " + err.Error())
    }
    

    Your case involves a structure. As far as I know, you can just recreate the structure flat (keeping fields in the same order), but you must convert any int fields in the original to int32, otherwise things will break on 64-bit Windows. Consult the Windows Data Types page on MSDN for the appropriate type equivalents. In your case, this would be

    var lastInputInfo struct {
        cbSize uint32
        dwTime uint32
    }
    

    Because this (like so many structs in the Windows API) has a cbSize field that requires you to initialize it with the size of the struct, we must do so too:

    lastInputInfo.cbSize = uint32(unsafe.Sizeof(lastInputInfo))
    

    Now we just need to pass a pointer to that lastInputInfo variable to the function:

    r1, _, err := getLastInputInfo.Call(
        uintptr(unsafe.Pointer(&lastInputInfo)))
    

    and just remember to import syscall and unsafe.

    All args to DLL/LazyDLL.Call() are uintptr, as is the r1 return. The _ return is never used on Windows (it has to do with the ABI used).


    Since I went over most of what you need to know to use the Windows API in Go that you can't gather from reading the syscall docs, I will also say (and this is irrelevant to the above question) that if a function has both ANSI and Unicode versions, you should use the Unicode versions (W suffix) and the UTF-16 conversion functions in package syscall for best results.

    I think that's all the info you (or anyone, for that matter) will need to use the Windows API in Go programs.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 数学建模,尽量用matlab回答,论文格式
  • ¥15 昨天挂载了一下u盘,然后拔了
  • ¥30 win from 窗口最大最小化,控件放大缩小,闪烁问题
  • ¥20 易康econgnition精度验证
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能