Trigger subroutines from another script

Get help with using AutoHotkey and its commands and hotkeys
User avatar
Kanji
Posts: 4
Joined: 17 Feb 2020, 20:12

Trigger subroutines from another script

25 Feb 2020, 13:16

Hi everyone, hi read a lot of documentation, but I'm still confused.
I've a script which checks if my pc is connected to the internet or not. I want it to trigger a subroutine in another script when there's no connection. Something like this:

Code: Select all

#SingleInstance, Force

IsInternetConnected()
{
  static sz := A_IsUnicode ? 408 : 204, addrToStr := "Ws2_32\WSAAddressToString" (A_IsUnicode ? "W" : "A")
  VarSetCapacity(wsaData, 408)
  if DllCall("Ws2_32\WSAStartup", "UShort", 0x0202, "Ptr", &wsaData)
    return false
  if DllCall("Ws2_32\GetAddrInfoW", "wstr", "dns.msftncsi.com", "wstr", "http", "ptr", 0, "ptr*", results)
  {
    DllCall("Ws2_32\WSACleanup")
    return false
  }
  ai_family := NumGet(results+4, 0, "int")    ;address family (ipv4 or ipv6)
  ai_addr := Numget(results+16, 2*A_PtrSize, "ptr")   ;binary ip address
  ai_addrlen := Numget(results+16, 0, "ptr")   ;length of ip
  DllCall(addrToStr, "ptr", ai_addr, "uint", ai_addrlen, "ptr", 0, "str", wsaData, "uint*", 204)
  DllCall("Ws2_32\FreeAddrInfoW", "ptr", results)
  DllCall("Ws2_32\WSACleanup")
  http := ComObjCreate("WinHttp.WinHttpRequest.5.1")

  if (ai_family = 2 && wsaData = "131.107.255.255:80")
  {
    http.Open("GET", "http www.msftncsi.com /ncsi.txt")  Broken Link for safety
  }
  else if (ai_family = 23 && wsaData = "[fd3e:4f5a:5b81::1]:80")
  {
    http.Open("GET", "http ipv6.msftncsi.com /ncsi.txt")  Broken Link for safety
  }
  else
  {
    return false
  }
  http.Send()
  return (http.ResponseText = "Microsoft NCSI") ;ncsi.txt will contain exactly this text
}

a = 0 ; Toggle Connection ON
b = 0 ; Toggle Connection OFF
loop{
	Sleep 300
	if (IsInternetConnected() = 1 && a = 0)
		{
			GoSub, Subroutine in script2
			a++ ; 
			b = 0 ;
		}
	else if (IsInternetConnected() = 0 && b = 0)
		{
			GoSub, Subroutine in script2
			b++ ; 
			a = 0 ; 
		}
}
Hope someone can help me :D
User avatar
boiler
Posts: 5663
Joined: 21 Dec 2014, 02:44

Re: Trigger subroutines from another script

25 Feb 2020, 15:07

One way is to use SendMessage in one script and OnMessage in the other.
Rohwedder
Posts: 3269
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Trigger subroutines from another script

26 Feb 2020, 02:49

Hallo,
use only one script and instead of the (silly) loop this timer:

Code: Select all

SetTimer, Connection, 300
Return
Connection:
IF (IsInternetConnected() <> Connection)
{
	Connection := !Connection
	GoSub, Subroutine
}
Return
Your loop called IsInternetConnected() twice on each pass.
User avatar
Jovannb
Posts: 244
Joined: 17 Jun 2014, 02:44
Location: Austria

Re: Trigger subroutines from another script

26 Feb 2020, 12:54

Hi,

look at this https://www.autohotkey.com/boards/viewtopic.php?t=20763, that is what you are looking for.

J.B.
AHK-Release 1.1.30.03 Ansi 32-bit, Win10 (1903 18362.295) 64-bit, german

Return to “Ask For Help”

Who is online

Users browsing this forum: markhavemann, pblpbl, prasad383 and 32 guests