Get currently connected network name

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
DataLife
Posts: 447
Joined: 29 Sep 2013, 19:52

Get currently connected network name

04 Oct 2017, 00:11

I need to retrieve the name of the currently connected network. (wired or wireless)

I found the following code that gets the SSID of the wireless network, but I have not been able to determine how to get the wired network name.

Code: Select all

MsgBox % getssid()

exitapp

getssid()
	{	
		filedelete, %a_temp%\wlan.tmp
		runwait, %comspec% /c netsh wlan show interfaces >>%a_temp%\wlan.tmp,, hide
		FileRead, Var, %a_temp%\wlan.tmp
		filedelete, %a_temp%\wlan.tmp
		RegExMatch(Var, "mi)SSID\s*:\s*([^\r]+)",ssid)
		stringtrimleft,ssid, ssid,25
		;stringtrimright,ssid, ssid,1
		if ssid =
			return "ethernet"
		else 
			return ssid
	}
I have studied the netsh command and can not find what I am looking for.

"netsh lan show interfaces" requires the Wired Autoconfig Service to be running and then it only shows the name of the wired interface not the network name it is connected to.

Any help would be greatly appreciated.
Check out my scripts. (MyIpChanger) (ClipBoard Manager) (SavePictureAs)
All my scripts are tested on Windows 10, AutoHotkey 32 bit Ansi unless otherwise stated.
User avatar
jNizM
Posts: 3183
Joined: 30 Sep 2013, 01:33
Contact:

Re: Get currently connected network name

04 Oct 2017, 02:39

See this (unfinished) example:

Code: Select all

; ===============================================================================================================================
; The GetNetworkParams function retrieves network parameters for the local computer.
; ===============================================================================================================================

GetAdaptersInfo()
{
	if (DllCall("iphlpapi.dll\GetAdaptersInfo", "ptr", 0, "uint*", size) = 111)
		if !(VarSetCapacity(buf, size, 0))
			throw Exception("Memory allocation failed for IP_ADAPTER_INFO structures")

	if (DllCall("iphlpapi.dll\GetAdaptersInfo", "ptr", &buf, "uint*", size) != 0)
		throw Exception("Call to GetAdaptersInfo failed with error: " A_LastError, -1)

	addr := &buf, offs := A_PtrSize, IP_ADAPTER_INFO := {}
	while (addr)
	{
		IP_ADAPTER_INFO[A_Index, "ComboIndex"]          := NumGet(addr+0, offs, "uint"),           offs += 4
		IP_ADAPTER_INFO[A_Index, "AdapterName"]         := StrGet(addr + offs, 260, "cp0"),        offs += 260
		IP_ADAPTER_INFO[A_Index, "Description"]         := StrGet(addr + offs, 132, "cp0"),        offs += 132
		IP_ADAPTER_INFO[A_Index, "AddressLength"]       := NumGet(addr+0, offs, "uint"),           offs += 4
		loop % IP_ADAPTER_INFO[A_Index].AddressLength
			mac .= Format("{:02x}",                        NumGet(addr+0, offs + A_Index - 1, "uchar")) "-"
		IP_ADAPTER_INFO[A_Index, "Address"]             := SubStr(mac, 1, -1), mac := "",          offs += 8
		IP_ADAPTER_INFO[A_Index, "Index"]               := NumGet(addr+0, offs, "uint"),           offs += 4
		IP_ADAPTER_INFO[A_Index, "Type"]                := NumGet(addr+0, offs, "uint"),           offs += 4
		IP_ADAPTER_INFO[A_Index, "DhcpEnabled"]         := NumGet(addr+0, offs, "uint"),           offs += A_PtrSize
		IP_ADAPTER_INFO[A_Index, "CurrentIpAddress"]    := NumGet(addr+0, offs, "uptr"),           offs += A_PtrSize
		IP_ADAPTER_INFO[A_Index, "IpAddressList"]       := StrGet(addr + offs + A_PtrSize, "cp0"), offs += A_PtrSize + 32 + A_PtrSize
		IP_ADAPTER_INFO[A_Index, "GatewayList"]         := StrGet(addr + offs + A_PtrSize, "cp0"), offs += A_PtrSize + 32 + A_PtrSize
		IP_ADAPTER_INFO[A_Index, "DhcpServer"]          := StrGet(addr + offs + A_PtrSize, "cp0"), offs += A_PtrSize + 32 + A_PtrSize
		IP_ADAPTER_INFO[A_Index, "HaveWins"]            := NumGet(addr+0, offs, "int"),            offs += A_PtrSize
		IP_ADAPTER_INFO[A_Index, "PrimaryWinsServer"]   := StrGet(addr + offs + A_PtrSize, "cp0"), offs += A_PtrSize + 32 + A_PtrSize
		IP_ADAPTER_INFO[A_Index, "SecondaryWinsServer"] := StrGet(addr + offs + A_PtrSize, "cp0"), offs += A_PtrSize + 32 + A_PtrSize
		IP_ADAPTER_INFO[A_Index, "LeaseObtained"]       := NumGet(addr+0, offs, "int"),            offs += A_PtrSize
		IP_ADAPTER_INFO[A_Index, "LeaseExpires"]        := NumGet(addr+0, offs, "int")
		addr := NumGet(addr+0, "uptr"), offs := A_PtrSize
	}
	return IP_ADAPTER_INFO
}

for k, v in GetAdaptersInfo()
    MsgBox % v.Description
ExitApp
As soon I finish this example it can be found here -> AHK Net Examples (GitHub)
[AHK] v2.0.5 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile
User avatar
DataLife
Posts: 447
Joined: 29 Sep 2013, 19:52

Re: Get currently connected network name

04 Oct 2017, 07:51

jNizM, thanks for the quick reply.

Your example gives me the network interface names. I need the network name.

For instance when I am connected via wireless I can retrieve the SSID with the example code I provided.

When I am connected via wired only how can I get the network name?

The icon in my system tray says I am connected to "Windstream", which is my ISP.

How can I retrieve "windstream" in my case?
Check out my scripts. (MyIpChanger) (ClipBoard Manager) (SavePictureAs)
All my scripts are tested on Windows 10, AutoHotkey 32 bit Ansi unless otherwise stated.
User avatar
jNizM
Posts: 3183
Joined: 30 Sep 2013, 01:33
Contact:

Re: Get currently connected network name

04 Oct 2017, 08:31

For Wireless (WLAN) it should be this function -> WlanGetAvailableNetworkList (msdn)

Found this for ahk:
https://github.com/nkruzan/autohotkey-w ... r/WLAN.ahk
Maybe it needs to be a bit rewritten
[AHK] v2.0.5 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile
User avatar
DataLife
Posts: 447
Joined: 29 Sep 2013, 19:52

Re: Get currently connected network name

04 Oct 2017, 09:39

jNizM wrote:For Wireless (WLAN) it should be this function -> WlanGetAvailableNetworkList (msdn)

Found this for ahk:
https://github.com/nkruzan/autohotkey-w ... r/WLAN.ahk
Maybe it needs to be a bit rewritten
The WlanGetAvailableNetworkList function retrieves the list of available networks on a wireless LAN interface.

Both of these refer to the wlan (wireless lan).

I need the currently connected network name of the lan (wired lan).
Check out my scripts. (MyIpChanger) (ClipBoard Manager) (SavePictureAs)
All my scripts are tested on Windows 10, AutoHotkey 32 bit Ansi unless otherwise stated.
User avatar
DataLife
Posts: 447
Joined: 29 Sep 2013, 19:52

Re: Get currently connected network name

04 Oct 2017, 22:10

Upon further research it appears that each time the wired ethernet adapter connects to a network windows gives the network a name. For instance I just connected my laptop to a new wired network and the wired ethernet network name is "Network 19". If I were to connect it to another new network that my laptop has not been connected to then the network name would be "Network 20"

Network 19, Network 20 or whatever windows decides to name the new network is what I am needing my script to get.
As I mentioned in my opening post I can retrieve the SSID of the currently connected WiFi network, but I need to get the Network name of the WIRED (Ethernet) connection. (Network 19 in the example below)
Attachments
NetworkConnections.PNG
I need this info.
NetworkConnections.PNG (9.7 KiB) Viewed 6364 times
Check out my scripts. (MyIpChanger) (ClipBoard Manager) (SavePictureAs)
All my scripts are tested on Windows 10, AutoHotkey 32 bit Ansi unless otherwise stated.
User avatar
jNizM
Posts: 3183
Joined: 30 Sep 2013, 01:33
Contact:

Re: Get currently connected network name

05 Oct 2017, 01:24

Since im in a domain, I cannot test it like you. So please test if it is the return you wants:

Code: Select all

GetNetworkParamsDomainName()
{
    if (DllCall("iphlpapi\GetNetworkParams", "ptr", 0, "uint*", size) = 111) {
        VarSetCapacity(buf, size, 0)
        if (DllCall("iphlpapi\GetNetworkParams", "ptr", &buf, "uint*", size) = 0)
            return StrGet(&buf + 132, 132, "cp0")
    }
    return 0
}

MsgBox % GetNetworkParamsDomainName()
If not you need to look into registry:
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\NetworkList\Profiles -> {some id} -> ProfileName
[AHK] v2.0.5 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile
qwerty12
Posts: 468
Joined: 04 Mar 2016, 04:33
Contact:

Re: Get currently connected network name

05 Oct 2017, 04:34

Derived from a script here, one of the strings it displays in the MsgBox might be what you're after:

Code: Select all

#NoEnv
VarSetCapacity(adapterGuid, 16), VarSetCapacity(adapterGuidStr, 140), adapters := GetAdaptersAddresses()

for INetwork in ComObjCreate("{DCB00C01-570F-4A9B-8D69-199FDBA5723B}").GetNetworks(1) { ; NLM_ENUM_NETWORK_CONNECTED
	profileName := INetwork.GetName()
	for k, v in INetwork.GetNetworkConnections() {
		try if ((INetworkConnection := ComObjQuery(k, "{DCB00005-570F-4A9B-8D69-199FDBA5723B}"))) {
			if (DllCall(NumGet(NumGet(INetworkConnection+0)+12*A_PtrSize), "Ptr", INetworkConnection, "Ptr", &adapterGuid) == 0) { ; ::GetAdapterId
				if (DllCall("ole32\StringFromGUID2", "Ptr", &adapterGuid, "WStr", adapterGuidStr, "Int", 68)) {
					adapterName := adapters[adapterGuidStr].Description
					interfaceAlias := adapters[adapterGuidStr].FriendlyName
				}
			}
			ObjRelease(INetworkConnection)
		}
	}
	MsgBox % profileName . "`n" . adapterName . "`n" . interfaceAlias
}

; just me: https://autohotkey.com/boards/viewtopic.php?t=18768
GetAdaptersAddresses()
{
	; initial call to GetAdaptersAddresses to get the size needed
	If (DllCall("iphlpapi.dll\GetAdaptersAddresses", "UInt", 2, "UInt", 0, "Ptr", 0, "Ptr", 0, "UIntP", Size) = 111) ; ERROR_BUFFER_OVERFLOW
		If !(VarSetCapacity(Buf, Size, 0))
			Return "Memory allocation failed for IP_ADAPTER_ADDRESSES struct"

	; second call to GetAdapters Addresses to get the actual data we want
	If (DllCall("iphlpapi.dll\GetAdaptersAddresses", "UInt", 2, "UInt", 0, "Ptr", 0, "Ptr", &Buf, "UIntP", Size) != 0) ; NO_ERROR
		Return "Call to GetAdaptersAddresses failed with error: " . A_LastError

	Addr := &Buf
	Adapters := {}
	While (Addr) {
		AdapterName := StrGet(NumGet(Addr + 8, A_PtrSize, "Uptr"), "CP0")
		Description := StrGet(NumGet(Addr + 8, A_PtrSize * 7, "UPtr"), "UTF-16")
		FriendlyName := StrGet(NumGet(Addr + 8, A_PtrSize * 8, "UPtr"), "UTF-16")
		Adapters[AdapterName] := {Description: Description, FriendlyName: FriendlyName}
		Addr := NumGet(Addr + 8, "UPtr") ; *Next
	}
	Return Adapters
}
EDIT: Oh thanks, jNizM
Last edited by qwerty12 on 05 Oct 2017, 05:14, edited 1 time in total.
User avatar
jNizM
Posts: 3183
Joined: 30 Sep 2013, 01:33
Contact:

Re: Get currently connected network name

05 Oct 2017, 05:02

Thank you for your input qwerty12 =)
[AHK] v2.0.5 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile
User avatar
DataLife
Posts: 447
Joined: 29 Sep 2013, 19:52

Re: Get currently connected network name

05 Oct 2017, 09:06

jNizM wrote:Since im in a domain, I cannot test it like you. So please test if it is the return you wants:

Code: Select all

GetNetworkParamsDomainName()
{
    if (DllCall("iphlpapi\GetNetworkParams", "ptr", 0, "uint*", size) = 111) {
        VarSetCapacity(buf, size, 0)
        if (DllCall("iphlpapi\GetNetworkParams", "ptr", &buf, "uint*", size) = 0)
            return StrGet(&buf + 132, 132, "cp0")
    }
    return 0
}

MsgBox % GetNetworkParamsDomainName()
If not you need to look into registry:
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\NetworkList\Profiles -> {some id} -> ProfileName
This returns a blank message box.

I was aware of the registry location but the "some id" is a stumbling block.
Check out my scripts. (MyIpChanger) (ClipBoard Manager) (SavePictureAs)
All my scripts are tested on Windows 10, AutoHotkey 32 bit Ansi unless otherwise stated.
User avatar
DataLife
Posts: 447
Joined: 29 Sep 2013, 19:52

Re: Get currently connected network name

05 Oct 2017, 09:10

qwerty12 wrote:Derived from a script here, one of the strings it displays in the MsgBox might be what you're after:

Code: Select all

#NoEnv
VarSetCapacity(adapterGuid, 16), VarSetCapacity(adapterGuidStr, 140), adapters := GetAdaptersAddresses()

for INetwork in ComObjCreate("{DCB00C01-570F-4A9B-8D69-199FDBA5723B}").GetNetworks(1) { ; NLM_ENUM_NETWORK_CONNECTED
	profileName := INetwork.GetName()
	for k, v in INetwork.GetNetworkConnections() {
		try if ((INetworkConnection := ComObjQuery(k, "{DCB00005-570F-4A9B-8D69-199FDBA5723B}"))) {
			if (DllCall(NumGet(NumGet(INetworkConnection+0)+12*A_PtrSize), "Ptr", INetworkConnection, "Ptr", &adapterGuid) == 0) { ; ::GetAdapterId
				if (DllCall("ole32\StringFromGUID2", "Ptr", &adapterGuid, "WStr", adapterGuidStr, "Int", 68)) {
					adapterName := adapters[adapterGuidStr].Description
					interfaceAlias := adapters[adapterGuidStr].FriendlyName
				}
			}
			ObjRelease(INetworkConnection)
		}
	}
	MsgBox % profileName . "`n" . adapterName . "`n" . interfaceAlias
}

; just me: https://autohotkey.com/boards/viewtopic.php?t=18768
GetAdaptersAddresses()
{
	; initial call to GetAdaptersAddresses to get the size needed
	If (DllCall("iphlpapi.dll\GetAdaptersAddresses", "UInt", 2, "UInt", 0, "Ptr", 0, "Ptr", 0, "UIntP", Size) = 111) ; ERROR_BUFFER_OVERFLOW
		If !(VarSetCapacity(Buf, Size, 0))
			Return "Memory allocation failed for IP_ADAPTER_ADDRESSES struct"

	; second call to GetAdapters Addresses to get the actual data we want
	If (DllCall("iphlpapi.dll\GetAdaptersAddresses", "UInt", 2, "UInt", 0, "Ptr", 0, "Ptr", &Buf, "UIntP", Size) != 0) ; NO_ERROR
		Return "Call to GetAdaptersAddresses failed with error: " . A_LastError

	Addr := &Buf
	Adapters := {}
	While (Addr) {
		AdapterName := StrGet(NumGet(Addr + 8, A_PtrSize, "Uptr"), "CP0")
		Description := StrGet(NumGet(Addr + 8, A_PtrSize * 7, "UPtr"), "UTF-16")
		FriendlyName := StrGet(NumGet(Addr + 8, A_PtrSize * 8, "UPtr"), "UTF-16")
		Adapters[AdapterName] := {Description: Description, FriendlyName: FriendlyName}
		Addr := NumGet(Addr + 8, "UPtr") ; *Next
	}
	Return Adapters
}
EDIT: Oh thanks, jNizM
After approximately 15 seconds this script ends and does not display the message box.
Check out my scripts. (MyIpChanger) (ClipBoard Manager) (SavePictureAs)
All my scripts are tested on Windows 10, AutoHotkey 32 bit Ansi unless otherwise stated.
qwerty12
Posts: 468
Joined: 04 Mar 2016, 04:33
Contact:

Re: Get currently connected network name

05 Oct 2017, 10:07

Perhaps just doing this might be enough, if the problem lies in the adapter stuff:

Code: Select all

for INetwork in ComObjCreate("{DCB00C01-570F-4A9B-8D69-199FDBA5723B}").GetNetworks(1)
	MsgBox % INetwork.GetName()
Trying with GetNetworks(3) for details on all the networks your computer knows about might at least net one result. But with no error message or anything like that, I don't even have a semblance of a clue about what's going wrong.

EDIT:
DataLife wrote:Absolutely perfect. This returns "Network 19" if I am connected via Wired and the SSID if I am connected Wireless

thank you very much.
You're welcome, DataLife.
Last edited by qwerty12 on 05 Oct 2017, 19:17, edited 1 time in total.
User avatar
DataLife
Posts: 447
Joined: 29 Sep 2013, 19:52

Re: Get currently connected network name

05 Oct 2017, 10:29

qwerty12 wrote:Perhaps just doing this might be enough, if the problem lies in the adapter stuff:

Code: Select all

for INetwork in ComObjCreate("{DCB00C01-570F-4A9B-8D69-199FDBA5723B}").GetNetworks(1)
	MsgBox % INetwork.GetName()
Trying with GetNetworks(3) for details on all the networks your computer knows about might at least net one result. But with no error message or anything like that, I don't even have a semblance of a clue about what's going wrong.
Absolutely perfect. This returns "Network 19" if I am connected via Wired and the SSID if I am connected Wireless

thank you very much.
Check out my scripts. (MyIpChanger) (ClipBoard Manager) (SavePictureAs)
All my scripts are tested on Windows 10, AutoHotkey 32 bit Ansi unless otherwise stated.
hasantr
Posts: 933
Joined: 05 Apr 2016, 14:18
Location: İstanbul

Re: Get currently connected network name

14 May 2020, 05:53

Can I get the list of devices connected to the Modem using this method?

Edit:

It is enough to write to cmd command line.

Code: Select all

arp -a
Still, I would love to find out if there is a different way.
b0dhimind
Posts: 18
Joined: 20 May 2015, 11:41

Re: Get currently connected network name

15 Dec 2022, 15:02

I've been using this small piece of goodness from here and after Windows 10's most recent update... I'm getting an error.
Any thoughts? I don't understand com objects well enough but it sounds like windows changed the interface and I'll have to look for a new way to identify the current connected wifi name. :'(

Error: 0x8002802B - Element not found.

Code: Select all

for INetwork in ComObjCreate("{DCB00C01-570F-4A9B-8D69-199FDBA5723B}").GetNetworks(1)
WifiPrevious := INetwork.GetName()
mitasamodel
Posts: 3
Joined: 22 Aug 2021, 09:28

Re: Get currently connected network name

19 Dec 2022, 14:07

b0dhimind wrote:
15 Dec 2022, 15:02
I've been using this small piece of goodness from here and after Windows 10's most recent update... I'm getting an error.
Any thoughts? I don't understand com objects well enough but it sounds like windows changed the interface and I'll have to look for a new way to identify the current connected wifi name. :'(

Error: 0x8002802B - Element not found.
Same issue. Did anyone fine/create a solution?
teadrinker
Posts: 4326
Joined: 29 Mar 2015, 09:41
Contact:

Re: Get currently connected network name

19 Dec 2022, 17:17

For me this works:

Code: Select all

CLSID_NetworkListManager := "{DCB00C01-570F-4A9B-8D69-199FDBA5723B}"
NLM_ENUM_NETWORK_CONNECTED := 1

connected := ""
NetworkListManager := ComObjCreate(CLSID_NetworkListManager)
for NetWork in NetworkListManager.GetNetworks(NLM_ENUM_NETWORK_CONNECTED) {
   INetWork := ComObjValue(NetWork)
   ; INetwork::GetName
   DllCall(NumGet(NumGet(INetWork + 0) + A_PtrSize*7), "Ptr", INetWork, "PtrP", pWStr)
   connected .= (connected = "" ? "" : "`n") . StrGet(pWStr, "UTF-16")
}
NetworkListManager := NetWork := ""
MsgBox, % connected
conracer
Posts: 6
Joined: 26 Jul 2020, 11:51

Re: Get currently connected network name

19 Dec 2022, 21:04

@teadrinker That seems not to be the right one because in my case I wan't to disconnect and reconnect to the SSID without disabling and reenabling the whole WiFi-Adapter. And for connecting to a WiFi-Network, the SSID is not enough, you (only) need the profile name:

My solution before "0x8002802B" (maybe incorrect in some cases):

Code: Select all

GetWifiProfileName() {
	; Inspired from https://www.autohotkey.com/boards/viewtopic.php?p=174272#p174272
	for INetwork in ComObjCreate("{DCB00C01-570F-4A9B-8D69-199FDBA5723B}").GetNetworks(1)
		profile:=INetwork.GetDescription()
	return profile
}
my current solution:

Code: Select all

GetWifiProfileName() { ; Should work in all Windows languages
	ClipSaved:=ClipboardAll
	Runwait %comspec% /c netsh wlan show interface | clip,,hide
	; I'm curious, if there is an elegant RegEx for this
	profile:=SubStr(Clipboard,InStr(Clipboard,":",,0,2)+2,InStr(Clipboard,"`n",,InStr(Clipboard,":",,0,2))-StrLen(Clipboard)-3)
	Clipboard:=ClipSaved
	return profile
}

profile:=GetWifiProfileName()
RunWait %comspec% /c
	(LTrim Join&
	@echo off
	echo disconnecting WiFi...
	netsh wlan disconnect
	echo reconnecting WiFi-Profile "%profile%"...
	netsh wlan connect name="%profile%"
	echo.
	pause
	)
conracer
Posts: 6
Joined: 26 Jul 2020, 11:51

Re: Get currently connected network name

19 Dec 2022, 21:56

Sorry @teadrinker, I just found a better solution on your codebase:

Code: Select all

CLSID_NetworkListManager := "{DCB00C01-570F-4A9B-8D69-199FDBA5723B}"
NLM_ENUM_NETWORK_CONNECTED := 1

connected := ""
NetworkListManager := ComObjCreate(CLSID_NetworkListManager)
for NetWork in NetworkListManager.GetNetworks(NLM_ENUM_NETWORK_CONNECTED) {
   INetWork := ComObjValue(NetWork)
   ; INetwork::GetDescription
   DllCall(NumGet(NumGet(INetWork + 0) + A_PtrSize*9), "Ptr", INetWork, "PtrP", pWStr)
   connected .= (connected = "" ? "" : "`n") . StrGet(pWStr, "UTF-16")
}
NetworkListManager := NetWork := ""
MsgBox, % connected
I just changed the "7" to a "9"! :oops:
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Get currently connected network name

20 Dec 2022, 08:20

weird error, some unpaid intern at microsoft must have broken something(localization, probably) on the entire IDispatch interface. not even invoking it from native code is working (DISP_E_MEMBERNOTFOUND 0x8002003), so this definitely isnt autohotkey's doing
anyway, u need to DllCall("oleaut32\SysFreeString", "Ptr", pWStr) any BSTRs u get back from calling the raw interfaces

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Anput, mcd, Nerafius, RandomBoy, Rohwedder and 102 guests