golang---获取windows系统相关信息

2021-05-28 17:08

阅读:416

标签:ack   ati   connect   variable   back   turn   file   range   json   

package main

import (
	"fmt"
	"net"
	"runtime"
	"strings"
	"syscall"
	"time"
	"unsafe"

	"github.com/StackExchange/wmi"
)

var (
	advapi = syscall.NewLazyDLL("Advapi32.dll")
	kernel = syscall.NewLazyDLL("Kernel32.dll")
)

func main() {
	fmt.Printf("开机时长:%s\n", GetStartTime())
	fmt.Printf("当前用户:%s\n", GetUserName())
	fmt.Printf("当前系统:%s\n", runtime.GOOS)
	fmt.Printf("系统版本:%s\n", GetSystemVersion())
	fmt.Printf("%s\n", GetBiosInfo())
	fmt.Printf("Motherboard:\t%s\n", GetMotherboardInfo())

	fmt.Printf("CPU:\t%s\n", GetCpuInfo())
	fmt.Printf("Memory:\t%s\n", GetMemory())
	fmt.Printf("Disk:\t%v\n", GetDiskInfo())
	fmt.Printf("Interfaces:\t%v\n", GetIntfs())
}

//开机时间
func GetStartTime() string {
	GetTickCount := kernel.NewProc("GetTickCount")
	r, _, _ := GetTickCount.Call()
	if r == 0 {
		return ""
	}
	ms := time.Duration(r * 1000 * 1000)
	return ms.String()
}

//当前用户名
func GetUserName() string {
	var size uint32 = 128
	var buffer = make([]uint16, size)
	user := syscall.StringToUTF16Ptr("USERNAME")
	domain := syscall.StringToUTF16Ptr("USERDOMAIN")
	r, err := syscall.GetEnvironmentVariable(user, &buffer[0], size)
	if err != nil {
		return ""
	}
	buffer[r] = ‘@‘
	old := r + 1
	if old >= size {
		return syscall.UTF16ToString(buffer[:r])
	}
	r, err = syscall.GetEnvironmentVariable(domain, &buffer[old], size-old)
	return syscall.UTF16ToString(buffer[:old+r])
}

//系统版本
func GetSystemVersion() string {
	version, err := syscall.GetVersion()
	if err != nil {
		return ""
	}
	return fmt.Sprintf("%d.%d (%d)", byte(version), uint8(version>>8), version>>16)
}

type diskusage struct {
	Path  string `json:"path"`
	Total uint64 `json:"total"`
	Free  uint64 `json:"free"`
}

func usage(getDiskFreeSpaceExW *syscall.LazyProc, path string) (diskusage, error) {
	lpFreeBytesAvailable := int64(0)
	var info = diskusage{Path: path}
	diskret, _, err := getDiskFreeSpaceExW.Call(
		uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(info.Path))),
		uintptr(unsafe.Pointer(&lpFreeBytesAvailable)),
		uintptr(unsafe.Pointer(&(info.Total))),
		uintptr(unsafe.Pointer(&(info.Free))))
	if diskret != 0 {
		err = nil
	}
	return info, err
}

//硬盘信息
func GetDiskInfo() (infos []diskusage) {
	GetLogicalDriveStringsW := kernel.NewProc("GetLogicalDriveStringsW")
	GetDiskFreeSpaceExW := kernel.NewProc("GetDiskFreeSpaceExW")
	lpBuffer := make([]byte, 254)
	diskret, _, _ := GetLogicalDriveStringsW.Call(
		uintptr(len(lpBuffer)),
		uintptr(unsafe.Pointer(&lpBuffer[0])))
	if diskret == 0 {
		return
	}
	for _, v := range lpBuffer {
		if v >= 65 && v 

  输出结果:

开机时长:27h3m55.042s

当前用户:saryli@saryli-PC

当前系统:windows

系统版本:6.1 (7601)

BIOS Date: 04/08/14 13:08:33 Ver: 04.06.05

Motherboard: Z97 GAMING 5 (MS-7917)

CPU: Num:8 Arch:AMD64

Memory: 16264

Disk: [{C: 127928365056 69651759104} {D: 858994503680 799115214848} {E: 536871956480 445582901248} {F: 249998340096 249896194048} {G: 1141402234880 896679825408} {H: 963427430400 881289777152} {J: 128033222656 118774480896}]

Interfaces: [{Local Area Connection [192.168.6.45/24 192.168.6.100/24 192.168.6.102/24] [fe80::91f7:d107:7d92:e971/64]} {VMware Network Adapter VMnet1 [192.168.46.1/24] [fe80::9c79:462d:6831:8c27/64]} {VMware Network Adapter VMnet8 [192.168.153.1/24] [fe80::8055:efa0:615c:4814/64]} {Loopback Pseudo-Interface 1 [127.0.0.1/8] [::1/128]} {isatap.{DEAC948E-CFD8-4CF7-9B40-C1AB75D37536} [] [fe80::5efe:c0a8:62d/128 fe80::5efe:c0a8:664/128 fe80::5efe:c0a8:666/128]} {isatap.localdomain [] [fe80::5efe:c0a8:2e01/128 fe80::5efe:c0a8:9901/128]}]

 

golang---获取windows系统相关信息

标签:ack   ati   connect   variable   back   turn   file   range   json   

原文地址:https://www.cnblogs.com/saryli/p/11119033.html


评论


亲,登录后才可以留言!