#Persistent
OnExit, ExitSub
NetworkListManager := ComObjCreate(CLSID_NetworkListManager := "{DCB00C01-570F-4A9B-8D69-199FDBA5723B}")
if (NetworkListManager.GetConnectivity & 0x40) or (NetworkListManager.GetConnectivity & 0x400)   ; NLM_CONNECTIVITY_IPV4_INTERNET or NLM_CONNECTIVITY_IPV6_INTERNET
{
   InternetExist := 1
   msgbox internet ok
}
else
{
   InternetExist := 0
   msgbox no internet
}
Events := [3 ;QueryInterface
          ,1 ;AddRef
          ,1 ;Release
          ,2] ;ConnectivityChanged
VarSetCapacity(CallbackTable, A_PtrSize*Events.MaxIndex(), 0)
For Index, ParameterCount In Events
   NumPut(RegisterCallback("EventHandler", "Fast", ParameterCount, A_Index-1), CallbackTable, A_PtrSize*(A_Index-1))
pSink := DllCall("GlobalAlloc", "uint", 0, "ptr", A_PtrSize+4, "ptr")
NumPut(&CallbackTable, pSink+0, 0)
NumPut(InternetExist, pSink+0, A_PtrSize, "uint")
ConnectionPointContainer := ComObjQuery(NetworkListManager, IID_IConnectionPointContainer := "{B196B284-BAB4-101A-B69C-00AA00341D07}")
VarSetCapacity(CLSID, 16, 0)
DllCall("ole32\CLSIDFromString", "wstr", IID_INetworkListManagerEvents := "{DCB00001-570F-4A9B-8D69-199FDBA5723B}", "ptr", &CLSID)
DllCall(NumGet(NumGet(ConnectionPointContainer + 0) + A_PtrSize*4), "ptr", ConnectionPointContainer, "ptr", &CLSID, "ptr*", ConnectionPoint)   ; IConnectionPointContainer::FindConnectionPoint
ObjRelease(ConnectionPointContainer)
DllCall(NumGet(NumGet(ConnectionPoint + 0) + A_PtrSize*5), "ptr", ConnectionPoint, "ptr", psink, "uint*", nCookie)   ; IConnectionPoint::Advise
return


; https://www.autohotkey.com/boards/viewtopic.php?f=6&t=113844
EventHandler(this, guid = "", ppvObject = "")
{
   static IID_IUnknown, p1, p2, refCount := 1, InternetExist
   if !IID_IUnknown
   {
      VarSetCapacity(p1, 16, 0)
      DllCall("ole32\CLSIDFromString", "wstr", IID_IUnknown := "{00000000-0000-0000-C000-000000000046}", "ptr", &p1)
      VarSetCapacity(p2, 16, 0)
      DllCall("ole32\CLSIDFromString", "wstr", IID_INetworkListManagerEvents := "{DCB00001-570F-4A9B-8D69-199FDBA5723B}", "ptr", &p2)
      InternetExist := NumGet(this+0, A_PtrSize, "uint")
   }
   If (A_EventInfo = 0)
   {
      if DllCall("ole32\IsEqualGUID", "ptr", guid, "ptr", &p1) or DllCall("ole32\IsEqualGUID", "ptr", guid, "ptr", &p2)
      {
         NumPut(this, ppvObject+0)
         refCount++
         return 0
      }
      else
      {
         NumPut(0, ppvObject+0)
         return 0x80004002
      }
   }
   else if (A_EventInfo = 1)
   {
      refCount++
      return refCount
   }
   else if (A_EventInfo = 2)
   {
      if (refCount > 0)
      {
         refCount--
         if (refCount = 0)
            DllCall("GlobalFree", "ptr", this, "ptr")
      }
      return refCount
   }
   else if (A_EventInfo = 3)   ; ConnectivityChanged
   {
      if (guid & 0x40) or (guid & 0x400)   ; NLM_CONNECTIVITY_IPV4_INTERNET or NLM_CONNECTIVITY_IPV6_INTERNET
      {
         if !InternetExist
         {
            InternetExist := 1
            msgbox internet appears
         }
      }
      else
      {
         if InternetExist
         {
            InternetExist := 0
            msgbox internet disappears
         }
      }
      return 0
   }
}

ExitSub:
DllCall(NumGet(NumGet(ConnectionPoint + 0) + A_PtrSize*6), "ptr", ConnectionPoint, "uint", nCookie)   ; IConnectionPoint::Unadvise
ObjRelease(ConnectionPoint)
ObjRelease(pSink)
ExitApp

 

声明:站内资源为整理优化好的代码上传分享与学习研究,如果是开源代码基本都会标明出处,方便大家扩展学习路径。请不要恶意搬运,破坏站长辛苦整理维护的劳动成果。本站为爱好者分享站点,所有内容不作为商业行为。如若本站上传内容侵犯了原著者的合法权益,请联系我们进行删除下架。