From a3da73489b46009df3c17b08cf4f8ed43b4ed204 Mon Sep 17 00:00:00 2001 From: WAKAYAMA Shirou Date: Tue, 22 Apr 2014 22:10:13 +0900 Subject: [PATCH] implement Boot_time on windows. But something is wrong. the returned value is not correct. --- common_windows.go | 5 +++++ cpu_windows.go | 5 ----- host_windows.go | 24 +++++++++++++++++++++++- 3 files changed, 28 insertions(+), 6 deletions(-) diff --git a/common_windows.go b/common_windows.go index 5ba012e..2f981d1 100644 --- a/common_windows.go +++ b/common_windows.go @@ -9,3 +9,8 @@ import ( var ( modKernel32 = syscall.NewLazyDLL("kernel32.dll") ) + +type FILETIME struct { + DwLowDateTime uint32 + DwHighDateTime uint32 +} diff --git a/cpu_windows.go b/cpu_windows.go index 70134c7..c34a116 100644 --- a/cpu_windows.go +++ b/cpu_windows.go @@ -12,11 +12,6 @@ var ( procGetSystemTimes = modkernel32.NewProc("GetSystemTimes") ) -type FILETIME struct { - DwLowDateTime uint32 - DwHighDateTime uint32 -} - func Cpu_times() ([]CPU_TimesStat, error) { ret := make([]CPU_TimesStat, 0) diff --git a/host_windows.go b/host_windows.go index 3ed1f78..45e0d29 100644 --- a/host_windows.go +++ b/host_windows.go @@ -6,6 +6,12 @@ import ( "github.com/mitchellh/go-ps" "os" "syscall" + "unsafe" +) + +var ( + procGetSystemTimeAsFileTime = modKernel32.NewProc("GetSystemTimeAsFileTime") + procGetTickCount = modKernel32.NewProc("GetTickCount") ) func HostInfo() (HostInfoStat, error) { @@ -39,8 +45,24 @@ func HostInfo() (HostInfoStat, error) { } func Boot_time() (int64, error) { + var lpSystemTimeAsFileTime FILETIME - return 0, nil + r, _, _ := procGetSystemTimeAsFileTime.Call(uintptr(unsafe.Pointer(&lpSystemTimeAsFileTime))) + if r == 0 { + return 0, syscall.GetLastError() + } + + // TODO: This calc is wrong. + ll := (uint32(lpSystemTimeAsFileTime.DwHighDateTime))<<32 + lpSystemTimeAsFileTime.DwLowDateTime + pt := (uint64(ll) - 116444736000000000) / 10000000 + + u, _, _ := procGetTickCount.Call() + if u == 0 { + return 0, syscall.GetLastError() + } + uptime := uint64(u) / 1000 + + return int64(pt - uptime), nil } func Users() ([]UserStat, error) {