Guest User

Untitled

a guest
Oct 19th, 2017
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.02 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4. "fmt"
  5. "syscall"
  6. "unsafe"
  7. )
  8.  
  9. var (
  10. wininet, _ = syscall.LoadLibrary("wininet.dll")
  11. getConnectedState, _ = syscall.GetProcAddress(wininet, "InternetGetConnectedState")
  12. )
  13.  
  14. const (
  15. INTERNET_CONNECTION_MODEM = 0x01 // 本地系统使用调制解调器连接到互联网。
  16. INTERNET_CONNECTION_LAN = 0x02 // 本地系统使用局域网连接到互联网。
  17. INTERNET_CONNECTION_PROXY = 0x04 // 本地系统使用代理服务器连接到Internet。
  18. INTERNET_CONNECTION_MODEM_BUSY = 0x08 // 不再使用
  19. INTERNET_RAS_INSTALLED = 0x10 // 本地系统已安装RAS。
  20. INTERNET_CONNECTION_OFFLINE = 0x20 // 本地系统处于离线模式。
  21. INTERNET_CONNECTION_CONFIGURED = 0x40 // 本地系统具有到互联网的有效连接,但它可能或可能不是当前连接的。
  22. )
  23.  
  24. func main() {
  25. defer syscall.FreeLibrary(wininet)
  26.  
  27. var nargs uintptr = 2
  28. flags := int32(0)
  29. r1, _, err := syscall.Syscall(
  30. uintptr(getConnectedState),
  31. nargs,
  32. uintptr(unsafe.Pointer(&flags)),
  33. 0,
  34. 0,
  35. )
  36.  
  37. if err != 0 {
  38. fmt.Printf("Error: %s\n", err)
  39. return
  40. }
  41.  
  42. if r1 == 1 {
  43. switch flags {
  44. case INTERNET_CONNECTION_MODEM:
  45. fmt.Println("本地系统使用调制解调器连接到互联网。")
  46. case INTERNET_CONNECTION_LAN:
  47. fmt.Println("本地系统使用局域网连接到互联网。")
  48. case INTERNET_CONNECTION_PROXY:
  49. fmt.Println("本地系统使用代理服务器连接到Internet。")
  50. case INTERNET_CONNECTION_MODEM_BUSY:
  51. fmt.Println("不再使用")
  52. case INTERNET_RAS_INSTALLED:
  53. fmt.Println("本地系统已安装RAS。")
  54. case INTERNET_CONNECTION_OFFLINE:
  55. fmt.Println("本地系统处于离线模式。")
  56. case INTERNET_CONNECTION_CONFIGURED:
  57. fmt.Println("本地系统具有到互联网的有效连接,但它可能或可能不是当前连接的。")
  58. case INTERNET_CONNECTION_LAN + INTERNET_RAS_INSTALLED:
  59. fmt.Println("在线:0x12")
  60. default:
  61. fmt.Printf("flags: %#x\n", flags)
  62. }
  63. } else {
  64. // 拔掉网线
  65. fmt.Println("没有连网: 检查网络设置")
  66. }
  67.  
  68. }
Add Comment
Please, Sign In to add comment