You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
staros/os_windows.go

61 lines
1.4 KiB
Go

// +build windows
package staros
import (
"log"
"syscall"
"time"
"unsafe"
"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()
}
func DiskUsage(path string) (disk DiskStatus) {
kernel32, err := syscall.LoadLibrary("Kernel32.dll")
if err != nil {
log.Panic(err)
}
defer syscall.FreeLibrary(kernel32)
GetDiskFreeSpaceEx, err := syscall.GetProcAddress(syscall.Handle(kernel32), "GetDiskFreeSpaceExW")
if err != nil {
log.Panic(err)
}
lpFreeBytesAvailable := int64(0)
lpTotalNumberOfBytes := int64(0)
lpTotalNumberOfFreeBytes := int64(0)
syscall.Syscall6(uintptr(GetDiskFreeSpaceEx), 4,
uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr("C:"))),
uintptr(unsafe.Pointer(&lpFreeBytesAvailable)),
uintptr(unsafe.Pointer(&lpTotalNumberOfBytes)),
uintptr(unsafe.Pointer(&lpTotalNumberOfFreeBytes)), 0, 0)
disk.Free = uint64(lpTotalNumberOfFreeBytes)
disk.Used = uint64(lpTotalNumberOfBytes - lpTotalNumberOfFreeBytes)
disk.All = uint64(lpTotalNumberOfBytes)
disk.Available = uint64(lpFreeBytesAvailable)
return
}
func CpuUsage(sleep time.Duration) float64 {
return 0
}