package main
import (
"fmt"
"github.com/kbinani/win"
"unsafe"
)
func main(){
dnss := getNetworkParams()
fmt.Println(dnss)
}
func getNetworkParams() []string {
dns := []string{}
info := win.FIXED_INFO_W2KSP1{}
size := uint32(unsafe.Sizeof(info))
r := win.GetNetworkParams(&info, &size)
if r == 0 {
for ai := &info.DnsServerList; ai != nil; ai = ai.Next {
d := fmt.Sprintf("%v.%v.%v.%v", ai.Context&0xFF, (ai.Context>>8)&0xFF, (ai.Context>>16)&0xFF, (ai.Context>>24)&0xFF)
dns = append(dns, d)
}
} else if r == win.ValueOverflow {
newBuffers := make([]byte, size)
netParams := (win.PFIXED_INFO)(unsafe.Pointer(&newBuffers[0]))
win.GetNetworkParams(netParams, &size)
for ai := &netParams.DnsServerList; ai != nil; ai = ai.Next {
d := fmt.Sprintf("%v.%v.%v.%v", ai.Context&0xFF, (ai.Context>>8)&0xFF, (ai.Context>>16)&0xFF, (ai.Context>>24)&0xFF)
dns = append(dns, d)
}
}
return dns
}
运行结果:
[114.114.114.114]