winapi - Using Golang to get Windows idle time (GetLastInputInfo or similar) -
is there example or method of getting windows system's idle time using go?
i've been looking @ documentation @ golang site think i'm missing how access (and use) api system information including idle time.
go's website hardcoded show documentation standard library packages on linux. need godoc , run yourself:
go golang.org/x/tools/cmd/godoc godoc --http=:6060
then open http://127.0.0.1:6060/
in web browser.
of note package syscall
, provides facilities accessing functions in dlls, including utf-16 helpers , callback generation functions.
doing quick recursive search of go tree says doesn't have api getlastinputinfo()
in particular, unless i'm missing something, should able call function dll directly:
user32 := syscall.mustloaddll("user32.dll") // or newlazydll() defer loading getlastinputinfo := user32.mustfindproc("getlastinputinfo") // or newproc() if used newlazydll() // or can handle errors in above if want provide alternative r1, _, err := getlastinputinfo.call(uintptr(arg)) // err non-nil; need check r1 (the return value) if r1 == 0 { // in case panic("error getting last input info: " + err.error()) }
your case involves structure. far know, can recreate structure flat (keeping fields in same order), must convert int
fields in original int32
, otherwise things break on 64-bit windows. consult windows data types page on msdn appropriate type equivalents. in case, be
var lastinputinfo struct { cbsize uint32 dwtime uint32 }
because (like many structs in windows api) has cbsize
field requires initialize size of struct, must too:
lastinputinfo.cbsize = uint32(unsafe.sizeof(lastinputinfo))
now need pass pointer lastinputinfo
variable function:
r1, _, err := getlastinputinfo.call( uintptr(unsafe.pointer(&lastinputinfo)))
and remember import syscall
, unsafe
.
all args dll/lazydll.call()
uintptr
, r1
return. _
return never used on windows (it has abi used).
since went on of need know use windows api in go can't gather reading syscall
docs, (and irrelevant above question) if function has both ansi , unicode versions, should use unicode versions (w
suffix) , utf-16 conversion functions in package syscall
best results.
i think that's info (or anyone, matter) need use windows api in go programs.
Comments
Post a Comment