<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
	<channel>
		<title><![CDATA[Серый форум &mdash; AHK: Оповещание об отключении/появлении интернета]]></title>
		<link>http://forum.script-coding.com/viewtopic.php?id=17643</link>
		<atom:link href="http://forum.script-coding.com/extern.php?action=feed&amp;tid=17643&amp;type=rss" rel="self" type="application/rss+xml" />
		<description><![CDATA[Недавние сообщения в теме «AHK: Оповещание об отключении/появлении интернета».]]></description>
		<lastBuildDate>Fri, 10 Mar 2023 12:34:20 +0000</lastBuildDate>
		<generator>PunBB</generator>
		<item>
			<title><![CDATA[AHK: Оповещание об отключении/появлении интернета]]></title>
			<link>http://forum.script-coding.com/viewtopic.php?pid=157071#p157071</link>
			<description><![CDATA[<p><a href="https://learn.microsoft.com/en-us/previous-versions/windows/desktop/mpc/network-awareness-on-windows-vista-and-windows-7">https://learn.microsoft.com/en-us/previ … -windows-7</a><br />В скрипте отлавливаются события INetworkListManagerEvents.<br /><a href="https://learn.microsoft.com/en-us/windows/win32/api/netlistmgr/nn-netlistmgr-inetworklistmanagerevents">https://learn.microsoft.com/en-us/windo … agerevents</a><br /></p><div class="codebox"><pre><code>#Persistent
OnExit, ExitSub
NetworkListManager := ComObjCreate(CLSID_NetworkListManager := &quot;{DCB00C01-570F-4A9B-8D69-199FDBA5723B}&quot;)
if (NetworkListManager.GetConnectivity &amp; 0x40) or (NetworkListManager.GetConnectivity &amp; 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(&quot;EventHandler&quot;, &quot;Fast&quot;, ParameterCount, A_Index-1), CallbackTable, A_PtrSize*(A_Index-1))
pSink := DllCall(&quot;GlobalAlloc&quot;, &quot;uint&quot;, 0, &quot;ptr&quot;, A_PtrSize+4, &quot;ptr&quot;)
NumPut(&amp;CallbackTable, pSink+0, 0)
NumPut(InternetExist, pSink+0, A_PtrSize, &quot;uint&quot;)
ConnectionPointContainer := ComObjQuery(NetworkListManager, IID_IConnectionPointContainer := &quot;{B196B284-BAB4-101A-B69C-00AA00341D07}&quot;)
VarSetCapacity(CLSID, 16, 0)
DllCall(&quot;ole32\CLSIDFromString&quot;, &quot;wstr&quot;, IID_INetworkListManagerEvents := &quot;{DCB00001-570F-4A9B-8D69-199FDBA5723B}&quot;, &quot;ptr&quot;, &amp;CLSID)
DllCall(NumGet(NumGet(ConnectionPointContainer + 0) + A_PtrSize*4), &quot;ptr&quot;, ConnectionPointContainer, &quot;ptr&quot;, &amp;CLSID, &quot;ptr*&quot;, ConnectionPoint)   ; IConnectionPointContainer::FindConnectionPoint
ObjRelease(ConnectionPointContainer)
DllCall(NumGet(NumGet(ConnectionPoint + 0) + A_PtrSize*5), &quot;ptr&quot;, ConnectionPoint, &quot;ptr&quot;, psink, &quot;uint*&quot;, nCookie)   ; IConnectionPoint::Advise
return



EventHandler(this, guid = &quot;&quot;, ppvObject = &quot;&quot;)
{
   static IID_IUnknown, p1, p2, refCount := 1, InternetExist
   if !IID_IUnknown
   {
      VarSetCapacity(p1, 16, 0)
      DllCall(&quot;ole32\CLSIDFromString&quot;, &quot;wstr&quot;, IID_IUnknown := &quot;{00000000-0000-0000-C000-000000000046}&quot;, &quot;ptr&quot;, &amp;p1)
      VarSetCapacity(p2, 16, 0)
      DllCall(&quot;ole32\CLSIDFromString&quot;, &quot;wstr&quot;, IID_INetworkListManagerEvents := &quot;{DCB00001-570F-4A9B-8D69-199FDBA5723B}&quot;, &quot;ptr&quot;, &amp;p2)
      InternetExist := NumGet(this+0, A_PtrSize, &quot;uint&quot;)
   }
   If (A_EventInfo = 0)
   {
      if DllCall(&quot;ole32\IsEqualGUID&quot;, &quot;ptr&quot;, guid, &quot;ptr&quot;, &amp;p1) or DllCall(&quot;ole32\IsEqualGUID&quot;, &quot;ptr&quot;, guid, &quot;ptr&quot;, &amp;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 &gt; 0)
      {
         refCount--
         if (refCount = 0)
            DllCall(&quot;GlobalFree&quot;, &quot;ptr&quot;, this, &quot;ptr&quot;)
      }
      return refCount
   }
   else if (A_EventInfo = 3)   ; ConnectivityChanged
   {
      if (guid &amp; 0x40) or (guid &amp; 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), &quot;ptr&quot;, ConnectionPoint, &quot;uint&quot;, nCookie)   ; IConnectionPoint::Unadvise
ObjRelease(ConnectionPoint)
objrelease(pSink)
ExitApp</code></pre></div><p>Код для ahk v2 от <strong>teadrinker</strong>:<br /></p><div class="codebox"><pre><code>#Requires AutoHotkey v2

Persistent
ConnectionWatcher := INetworkListManagerEvents(WatchInternetConnection)
WatchInternetConnection(ConnectionWatcher.IsConnection)

WatchInternetConnection(isConnection) { ; when the internet goes down, a notification appears
    static wnd := &#039;&#039;, ExStyles := (WS_EX_NOACTIVATE := 0x8000000) | (WS_EX_TRANSPARENT := 0x00000020)
    if !wnd {
        wnd := Gui(&#039;+LastFound -Caption +AlwaysOnTop +Owner +E&#039; . ExStyles)
        WinSetTransparent 210
        wnd.BackColor := &#039;Red&#039;
        wnd.SetFont(&#039;s20&#039;, &#039;Calibri&#039;)
        wnd.MarginX := wnd.MarginY := 20
        wnd.AddText(, &#039;No Internet connection!&#039;)
    }
    wnd.%isConnection ? &#039;Hide&#039; : &#039;Show&#039;%(isConnection ? unset : &#039;NA y0&#039;)
}

class INetworkListManagerEvents
{
    ipv4 := NLM_CONNECTIVITY_IPV4_INTERNET := 0x0040
    ipv6 := NLM_CONNECTIVITY_IPV6_INTERNET := 0x0400
    CLSID_NetworkListManager      := &#039;{DCB00C01-570F-4A9B-8D69-199FDBA5723B}&#039;
    IID_INetworkListManagerEvents := &#039;{DCB00001-570F-4A9B-8D69-199FDBA5723B}&#039;
    IID_IConnectionPointContainer := &#039;{B196B284-BAB4-101A-B69C-00AA00341D07}&#039;
    IID_IUnknown                  := &#039;{00000000-0000-0000-C000-000000000046}&#039;

    __New(callback) {
        this.callback := callback
        this.CreateVTable()

        this.NetworkListManager := ComObject(this.CLSID_NetworkListManager)
        IConnectionPointContainer := ComObjQuery(this.NetworkListManager, this.IID_IConnectionPointContainer)

        DllCall(&#039;Ole32\CLSIDFromString&#039;, &#039;Str&#039;, this.IID_INetworkListManagerEvents, &#039;Ptr&#039;, CLSID := Buffer(16))
        ComCall(FindConnectionPoint := 4, IConnectionPointContainer, &#039;Ptr&#039;, CLSID,
                &#039;PtrP&#039;, IConnectionPoint := ComValue(VT_UNKNOWN := 0xD, pIConnectionPoint := 0))
        
        ComCall(Advise := 5, IConnectionPoint, &#039;Ptr&#039;, this.vtable.ptr, &#039;UIntP&#039;, &amp;cookie := 0)
        this.IConnectionPoint := IConnectionPoint, this.cookie := cookie
    }

    IsConnection =&gt; ( NLM_CONNECTIVITY := this.NetworkListManager.GetConnectivity(),
                      NLM_CONNECTIVITY &amp; this.ipv4) || (NLM_CONNECTIVITY &amp; this.ipv6 )

    CreateVTable() {
        static S_OK := 0
             , Methods := [{name: &#039;QueryInterface&#039;     , paramCount: 3},
                           {name: &#039;AddRef&#039;             , paramCount: 1},
                           {name: &#039;Release&#039;            , paramCount: 1},
                           {name: &#039;ConnectivityChanged&#039;, paramCount: 2}]
        
        this.vtable := Buffer(A_PtrSize * (Methods.Length + 1))
        NumPut(&#039;Ptr&#039;, this.vtable.Ptr + A_PtrSize, this.vtable)
        for k, v in Methods {
            NumPut(&#039;Ptr&#039;, CallbackCreate(%v.name%, &#039;F&#039;, v.paramCount), this.vtable, A_PtrSize * k)
        }

        QueryInterface(self, riid, pObj) {
            DllCall(&#039;Ole32\StringFromGUID2&#039;, &#039;Ptr&#039;, riid, &#039;Ptr&#039;, buf := Buffer(78), &#039;Int&#039;, 39)
            str := StrGet(buf)
            if !(str = this.IID_IUnknown || str = this.IID_INetworkListManagerEvents)
                return E_NOINTERFACE := 0x80004002
            else {
                NumPut(&#039;Ptr&#039;, self, pObj)
                return S_OK
            }
        }

        AddRef(self)  =&gt; 1
        Release(self) =&gt; 1

        ConnectivityChanged(self, NLM_CONNECTIVITY) {
            isInternetConnection := (NLM_CONNECTIVITY &amp; this.ipv4) || (NLM_CONNECTIVITY &amp; this.ipv6)
            SetTimer this.callback.Bind(isInternetConnection), -10
            return S_OK
        }
    }

    __Delete() {
        ComCall(Unadvise := 6, this.IConnectionPoint, &#039;UInt&#039;, this.cookie)
        this.IConnectionPoint := &#039;&#039;
        Loop 4 {
            CallbackFree NumGet(this.vtable, A_PtrSize * A_Index, &#039;Ptr&#039;)
        }
    }
}</code></pre></div><p><a href="http://forum.script-coding.com/viewtopic.php?id=17603">Тема для обсуждения</a></p>]]></description>
			<author><![CDATA[null@example.com (Malcev)]]></author>
			<pubDate>Fri, 10 Mar 2023 12:34:20 +0000</pubDate>
			<guid>http://forum.script-coding.com/viewtopic.php?pid=157071#p157071</guid>
		</item>
	</channel>
</rss>
