staros/os_windows.go

67 lines
1.4 KiB
Go
Raw Permalink Normal View History

//go:build windows
2020-02-11 10:53:25 +08:00
// +build windows
package staros
import (
2020-06-08 14:52:16 +08:00
"syscall"
2020-02-11 10:53:25 +08:00
"time"
2020-06-08 14:52:16 +08:00
"unsafe"
2020-02-11 10:53:25 +08:00
"b612.me/wincmd"
"b612.me/win32api"
)
// StartTime 开机时间
func StartTime() time.Time {
data, _ := win32api.GetTickCount()
date := float64(time.Now().Unix())
unix := date - float64(data)/1000
max := (unix - float64(int64(unix))) * 1000000000
return time.Unix(int64(unix), int64(max))
}
// IsRoot 当前是否是管理员用户
func IsRoot() bool {
return wincmd.Isas()
}
2020-06-08 14:52:16 +08:00
func DiskUsage(path string) (disk DiskStatus) {
disk, _ = DiskUsageE(path)
return
}
2020-06-08 14:52:16 +08:00
func DiskUsageE(path string) (disk DiskStatus, err error) {
if path == "" {
path = "."
2020-06-08 14:52:16 +08:00
}
lpFreeBytesAvailable := int64(0)
lpTotalNumberOfBytes := int64(0)
lpTotalNumberOfFreeBytes := int64(0)
path16, err := syscall.UTF16PtrFromString(path)
if err != nil {
return
}
r1, _, callErr := syscall.NewLazyDLL("kernel32.dll").NewProc("GetDiskFreeSpaceExW").Call(
uintptr(unsafe.Pointer(path16)),
2020-06-08 14:52:16 +08:00
uintptr(unsafe.Pointer(&lpFreeBytesAvailable)),
uintptr(unsafe.Pointer(&lpTotalNumberOfBytes)),
uintptr(unsafe.Pointer(&lpTotalNumberOfFreeBytes)),
)
if r1 == 0 {
err = callErr
return
}
2020-06-08 14:52:16 +08:00
disk.Free = uint64(lpTotalNumberOfFreeBytes)
disk.Used = uint64(lpTotalNumberOfBytes - lpTotalNumberOfFreeBytes)
disk.All = uint64(lpTotalNumberOfBytes)
disk.Available = uint64(lpFreeBytesAvailable)
return
}
2020-07-17 09:56:23 +08:00
func CpuUsage(sleep time.Duration) float64 {
return 0
}