recreate A_IPAddressXXX variables as a function

Get help with using AutoHotkey and its commands and hotkeys
User avatar
jeeswg
Posts: 6904
Joined: 19 Dec 2016, 01:58
Location: UK

recreate A_IPAddressXXX variables as a function

06 Oct 2018, 20:46

AHK v2 has replaced the 4 variables A_IPAddress1 to A_IPAddress4 with a SysGetIPAddresses() function.
AutoHotkey v2 alpha (UPDATES) - Page 3 - AutoHotkey Community
https://autohotkey.com/boards/viewtopic ... 06#p242306

So I'm seeking to backport this function to AHK v1. To recreate it.
commands as functions (AHK v2 functions for AHK v1) - AutoHotkey Community
https://autohotkey.com/boards/viewtopic ... 37&t=29689

This is the code I will probably use:

Code: Select all

MsgBox, % SysGetIPAddresses().Length()
MsgBox, % SysGetIPAddresses().1

SysGetIPAddresses()
{
	local IPAddress, List
	List := []
	Loop 4
	{
		IPAddress := A_IPAddress%A_Index%
		if !(IPAddress = "0.0.0.0")
			List.Push(IPAddress)
	}
	return List
}
However, I started trying to recreate the functionality via DllCall, as I saw that the source code was fairly small, and that theoretically there might be more than 4 IP addresses.

There's also this, as another reason:
AutoHotkey via DllCall: AutoHotkey functions as custom functions - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=7&t=37871

I based my code on these links (and the MSDN pages for Winapi structs/functions):
Pop3 Mail Check Filter (no Gui) - Scripts and Functions - AutoHotkey Community
https://autohotkey.com/board/topic/4403 ... er-no-gui/
#exclude for Excluding Components - Suggestions - AutoHotkey Community
https://autohotkey.com/board/topic/7000 ... omponents/
Replaced A_IPAddressX with SysGetIPAddresses(). · Lexikos/[email protected] · GitHub
https://github.com/Lexikos/AutoHotkey_L ... a22b2aa642

My code works, but I only had 1 IP address, so I couldn't test for further addresses.
I present it here in case others can test it/fix it for additional IP addresses.
(I'll probably stick with the simpler function above though, for my backport library.)
Btw do other users have multiple IP addresses? Ever more than 4? Thanks.

Code: Select all

q:: ;recreate A_IPAddressXXX variables
;IP_ADDRESS_SIZE := 32
VarSetCapacity(WSADATA, A_PtrSize=8?408:400, 0)
if DllCall("ws2_32\WSAStartup", UShort,0x101, Ptr,&WSADATA)
	return
VarSetCapacity(vHostName, 256, 0)
DllCall("ws2_32\gethostname", Ptr,&vHostName, Int,256)
MsgBox, % StrGet(&vHostName, "CP0")
if ((pHOSTENT := DllCall("ws2_32\gethostbyname", Ptr,&vHostName, "Cdecl Ptr")) != 0)
{
	Loop, 1
	{
		vOffset:=(A_Index-1)*4
		pTemp := NumGet(pHOSTENT+(A_PtrSize=8?24:12))
		if (pTempIP := NumGet(pTemp+vOffset*2))
		{
			vIPAddress := ""
			Loop, 4
				vIPAddress .= NumGet(pTempIP+vOffset, A_Index-1, "UChar") "."
			vIPAddress := SubStr(vIPAddress, 1, -1)
		}
		else
			break
	}
}
DllCall("ws2_32\WSACleanup")
MsgBox, % vIPAddress "`r`n" A_IPAddress1
return
Last edited by jeeswg on 08 Oct 2018, 10:52, edited 3 times in total.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
CyL0N
Posts: 210
Joined: 27 Sep 2018, 09:58

Re: recreate A_IPAddressXXX variables

06 Oct 2018, 22:24

Sup dude, so i got 6 active adapters, WiFi,Hotspot,VM,Tunngle,ProtonVPN & Ethernet, with 4being visible on the A_IPAddress1-4, increased inner loop to 24 but it only showed all 5 but not the sixth IP.
live ? long & prosper : regards
User avatar
jeeswg
Posts: 6904
Joined: 19 Dec 2016, 01:58
Location: UK

Re: recreate A_IPAddressXXX variables

06 Oct 2018, 23:36

- Thanks very much for your post.
- What do you use to inform you that you have 6 IP addresses?
- Are they returned in the same order as the AHK variables?
- Which one is missing?
- Is it anything to do with hostname perhaps? Thanks.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
CyL0N
Posts: 210
Joined: 27 Sep 2018, 09:58

Re: recreate A_IPAddressXXX variables

07 Oct 2018, 04:37

jeeswg wrote:- Thanks very much for your post.
- What do you use to inform you that you have 6 IP addresses?
- Are they returned in the same order as the AHK variables?
- Which one is missing?
- Is it anything to do with hostname perhaps? Thanks.
-Glad to help
-ipconfig |findstr /i ipv4
-Both ipconfig and you script above return the same order as A_IPAddress<1-4> variables
-The oldest connection is always missing,while the last made connection is at the top
-i don't think so,if i remove one connection, i can see the one that's omitted so it goes to reason that can't be it... i'm running 32bitUnicode ahk and x64 OS if that's helpful at all...
live ? long & prosper : regards
User avatar
jeeswg
Posts: 6904
Joined: 19 Dec 2016, 01:58
Location: UK

Re: recreate A_IPAddressXXX variables

07 Oct 2018, 06:44

- Interesting, thanks.
- You could download the latest version of AutoHotkey v2 to see if it gives 6 or 5 IPs:
AutoHotkey v2
https://autohotkey.com/v2/

Code: Select all

MsgBox(SysGetIPAddresses().Length())
MsgBox(SysGetIPAddresses().1)
vOutput := ""
for vKey, vValue in SysGetIPAddresses()
	vOutput .= vKey " " vValue "`r`n"
MsgBox(vOutput)
- I'm new to this IP-related code, so there could be any number of issues with my script above, for example, I should like to change/confirm the pointer variable names (variables beginning with 'p').

[EDIT:] Also check the comments here:
SysGetIPAddresses - Syntax & Usage | AutoHotkey v2
https://lexikos.github.io/v2/docs/comma ... resses.htm
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
CyL0N
Posts: 210
Joined: 27 Sep 2018, 09:58

Re: recreate A_IPAddressXXX variables

08 Oct 2018, 04:35

So progress, i rebooted and it showed me all 6 addresses,though i have no clue why...i hadn't rebooted for about two weeks though...

As for ahk_v2 SysGetIPAddresses() is reported as unknown, i tested it on the version i had and the latest version AutoHotkey_2.0-a099-ca386dc.zip 2018-08-22 05:11 3.2M,am i missing something... i prefix all v2 scripts as .ahk2 so there's no way it was executed by ahkv1 and ofcourse i drag & dropped it just in case... And i was assuming the SysGetIPAddresses() referenced isn't the one above but built into the latest ahkv2.
live ? long & prosper : regards
User avatar
jeeswg
Posts: 6904
Joined: 19 Dec 2016, 01:58
Location: UK

Re: recreate A_IPAddressXXX variables

08 Oct 2018, 09:12

- This link appears to point to the old version, 2.0-a099, at the moment, although I believe I downloaded the new version, 2.0-a100, from this link:
AutoHotkey v2
https://autohotkey.com/v2/
- And I can't see it here:
Index of /download/2.0
https://autohotkey.com/download/2.0/
- Maybe something happened during the forum maintenance.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
User avatar
nnnik
Posts: 4317
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: recreate A_IPAddressXXX variables

08 Oct 2018, 09:55

The issue with your attempt at recreating this is that ip adresses may change while the script is running.
So unless you call some sort of update code you will have old addresses.
That means that your attempt at recreating this is bound to fail.
People will have to call update whenever they access one of the variables anyways.
You could also SetTimer - however that will leave unwanted side effects that you cannot prevent.
Recommends AHK Studio
User avatar
jeeswg
Posts: 6904
Joined: 19 Dec 2016, 01:58
Location: UK

Re: recreate A_IPAddressXXX variables

08 Oct 2018, 10:07

- I based the code on the source code. What would you propose to do to update the list?
- What did you have in mind when you mentioned SetTimer? The idea of the A_IPAddressXXX variables and the new function is to get the addresses once. (And to run them again to get an up-to-date list.)
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
User avatar
nnnik
Posts: 4317
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: recreate A_IPAddressXXX variables

08 Oct 2018, 10:17

Well as far as I understand it you want to recreate A_IPAddress1-4. They update automatically.
If one wants to recreate the behavior of A_IpAddress1-4 they also need to recreate this part of their behavior.
Using your code you would ave to call update() everytime the code uses any A_IPAdress variable.
People are probably better off using the function and not using this new implementation.

There is no way for automatically updating variables. Thats why there is no way to rebuild A_IpAddress1-4. These things are dead now.
Recommends AHK Studio
User avatar
jeeswg
Posts: 6904
Joined: 19 Dec 2016, 01:58
Location: UK

Re: recreate A_IPAddressXXX variables

08 Oct 2018, 10:26

- The OP states that I'm trying to backport a function, not A_ variables.
- Only built-in A_ variables can 'update', that's clear. (Built-in A_ variables are really functions that look like variables.)
- If I turned the code above into a function, each time you ran it, it would 'update' (get the latest IP addresses), or would it not? Does the code need to be changed?
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
Helgef
Posts: 4031
Joined: 17 Jul 2016, 01:02
Contact:

Re: recreate A_IPAddressXXX variables

08 Oct 2018, 10:35

Hello :wave:
The thread title is a bit misleading.

Code: Select all

local IPAddress
List := []
:arrow:

Code: Select all

local 
IPAddress
List := []
Cheers.

Edit, :thumbup:
Last edited by Helgef on 08 Oct 2018, 10:59, edited 1 time in total.
User avatar
jeeswg
Posts: 6904
Joined: 19 Dec 2016, 01:58
Location: UK

Re: recreate A_IPAddressXXX variables

08 Oct 2018, 10:49

- Thanks Helgef, I made it 'local IPAddress, List'. I would use 'local' by itself but that would make the functions fail on older AHK versions. I'm going to bring in #Warn to my scripts to detect undeclared variables, but it will be a big undertaking to update all my function libraries.
- Re. the title.
Before:
recreate A_IPAddressXXX variables
After:
recreate A_IPAddressXXX variables as a function

- @nnnik: You must understand that other people can also understand complex ideas on the forum.
- You could have just said: 'just checking, do you realise that custom variables can't update'? I had no idea what you were trying to say. And it wasn't clear that you were completely changing the subject, I just wanted to know about any possible bugs in the DllCall code. I thought you were implying a bug in the code, and so you ended up wasting a lot of my time.
- Your statements, the way they've been worded, can lead to confusion. I would edit your above comment:
If one wants to recreate the behavior of A_IpAddress1-4 they also need to recreate this part of their behavior. [(The ability of an A_ variable to update.) But this is impossible without C++ source code changes.]
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
User avatar
nnnik
Posts: 4317
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: recreate A_IPAddressXXX variables as a function

08 Oct 2018, 15:11

I wasnt changing the topic at all. I simply misunderstood your topic.
Recommends AHK Studio
User avatar
jeeswg
Posts: 6904
Joined: 19 Dec 2016, 01:58
Location: UK

Re: recreate A_IPAddressXXX variables as a function

31 Oct 2018, 20:51

- @CyL0N: I tried to create a function, if you'd like to test it. Cheers.
- It was hard to find good information, if anyone has any good links. I.e. does **h_addr_list point to an array of pointers, with each pointer pointing to a 4-byte IP address?

Code: Select all

;AHK v2
q:: ;recreate A_IPAddressXXX variables as a function
vOutput := ""

;AHK v1
;Loop, 4
;	vOutput .= A_Index " " A_IPAddress%A_Index% "`r`n"
;vOutput .= "`r`n"

;AHK v2
for vKey, vValue in SysGetIPAddresses()
	vOutput .= vKey " " vValue "`r`n"

;recreated function
vOutput .= "`r`n"
for vKey, vValue in JEE_SysGetIPAddresses()
	vOutput .= vKey " " vValue "`r`n"

MsgBox(vOutput)
return

;==================================================

;gethostbyname macro | Microsoft Docs
;https://docs.microsoft.com/en-us/windows/desktop/api/wsipv6ok/nf-wsipv6ok-gethostbyname
;hostent | Microsoft Docs
;https://docs.microsoft.com/en-gb/windows/desktop/api/winsock/ns-winsock-hostent

JEE_SysGetIPAddresses()
{
	local
	oArray := []
	;IP_ADDRESS_SIZE := 32
	VarSetCapacity(WSADATA, A_PtrSize=8?408:400, 0)
	if DllCall("ws2_32\WSAStartup", UShort,0x101, Ptr,&WSADATA)
		return
	VarSetCapacity(vHostName, 256, 0)
	DllCall("ws2_32\gethostname", Ptr,&vHostName, Int,256)
	;MsgBox, % StrGet(&vHostName, "CP0")
	if ((pHOSTENT := DllCall("ws2_32\gethostbyname", Ptr,&vHostName, "Cdecl Ptr")) != 0)
	{
		;vSize := NumGet(pHOSTENT+(A_PtrSize=8?18:10), "Short") ;h_length
		ppHAddrList := NumGet(pHOSTENT+(A_PtrSize=8?24:12)) ;**h_addr_list
		Loop 1000
		{
			vOffset := (A_Index-1)*A_PtrSize
			if !(pIP := NumGet(ppHAddrList+vOffset))
				break
			vIPAddress := ""
			Loop 4
				vIPAddress .= NumGet(pIP+0, A_Index-1, "UChar") "."
			vIPAddress := SubStr(vIPAddress, 1, -1)
			oArray.Push(vIPAddress)
		}
	}
	DllCall("ws2_32\WSACleanup")
	return oArray
}

;==================================================
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
CyL0N
Posts: 210
Joined: 27 Sep 2018, 09:58

Re: recreate A_IPAddressXXX variables as a function

01 Nov 2018, 04:48

Both work flawlessly,i updated to the latest version before trying it...


1 192.168.42.72
2 192.168.42.34
3 192.168.2.1
4 192.168.43.152
5 192.168.56.1

1 192.168.42.72
2 192.168.42.34
3 192.168.2.1
4 192.168.43.152
5 192.168.56.1
live ? long & prosper : regards
User avatar
jeeswg
Posts: 6904
Joined: 19 Dec 2016, 01:58
Location: UK

Re: recreate A_IPAddressXXX variables as a function

01 Nov 2018, 04:55

- Thanks very much CyL0N.
- I would welcome anyone familiar with this sort of stuff to check the code or suggest any links.
- Btw it's always good to test x64 and x32. Cheers.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
CyL0N
Posts: 210
Joined: 27 Sep 2018, 09:58

Re: recreate A_IPAddressXXX variables as a function

02 Nov 2018, 02:40

yup, tested on both.
live ? long & prosper : regards
User avatar
Drugwash
Posts: 739
Joined: 29 May 2014, 21:07
Location: Ploieşti, Romania

Re: recreate A_IPAddressXXX variables as a function

02 Nov 2018, 05:08

How about IPv6 addresses…? If I do ipconfig /all I get a few IPv6 tunneling pseudo-interfaces. Does SysGetIPAddresses() do that in AHKv2?

Also, CyL0N, you appear to only have internal IPs - all the 192.168.x.x are internal LAN IPs not external (web). But ipconfig doesn't show the real external IP(s) though, at least not for me on a 3G modem; I need to use third-party tools or web sites to find the real external IP.
For example, ipconfig shows me 10.112.x.x for the PPP adapter (USB 3G modem) but rejetto's HFS shows the real external IP as 82.137.x.x. This may be a corner case though.
I've deleted my CloudMe account because of GDPR - the now legal base for privacy invasion and data theft.
CyL0N
Posts: 210
Joined: 27 Sep 2018, 09:58

Re: recreate A_IPAddressXXX variables as a function

02 Nov 2018, 08:49

Drugwash wrote:
02 Nov 2018, 05:08
How about IPv6 addresses…? If I do ipconfig /all I get a few IPv6 tunneling pseudo-interfaces. Does SysGetIPAddresses() do that in AHKv2?

Also, CyL0N, you appear to only have internal IPs - all the 192.168.x.x are internal LAN IPs not external (web). But ipconfig doesn't show the real external IP(s) though, at least not for me on a 3G modem; I need to use third-party tools or web sites to find the real external IP.
For example, ipconfig shows me 10.112.x.x for the PPP adapter (USB 3G modem) but rejetto's HFS shows the real external IP as 82.137.x.x. This may be a corner case though.
Actually they're all online,only 192.168.2.1 is my WLAN hotspot, the rest: Tunngle Virtual Adapter(online LAN gaming), WiFi router connection,Ethernet connection to ADSL modem & another is a USB tether to my 3G hotspot on my phone which I use when I need fastest browsing speed... And yea non have a public IP as I'm also behind my ISP's NAT where I kid you not my IP on the NAT right now is 192.168.134.232.

192.168.*.* is no longer exclusive LAN territory it seems...NAT's also give that out in addition to routers.




https://lexikos.github.io/v2/docs/comma ... resses.htm
SysGetIPAddresses() does not support IPV6, at least according to docs on latest ahkv2.


Cheers.
live ? long & prosper : regards

Return to “Ask For Help”

Who is online

Users browsing this forum: boiler, Odlanir, pn4265 and 234 guests