Getting network interface name

Just starting out? Need help? Post your questions and find answers here.
fryquez
Enthusiast
Enthusiast
Posts: 362
Joined: Mon Dec 21, 2015 8:12 pm

Re: Getting network interface name

Post by fryquez »

You should use the registry key "CurrentControlSet" instead of "ControlSet001".

ControlSet001 could be a configuration that have not been booted since years, and maybe contains outdated information.
User avatar
Dreamland Fantasy
Enthusiast
Enthusiast
Posts: 335
Joined: Fri Jun 11, 2004 9:35 pm
Location: Glasgow, UK
Contact:

Re: Getting network interface name

Post by Dreamland Fantasy »

fryquez wrote:You should use the registry key "CurrentControlSet" instead of "ControlSet001".

ControlSet001 could be a configuration that have not been booted since years, and maybe contains outdated information.
Thanks for the tip! :)

Kind regards,

Francis
User avatar
Michael Vogel
Addict
Addict
Posts: 2666
Joined: Thu Feb 09, 2006 11:27 pm
Contact:

Re: Getting network interface name

Post by Michael Vogel »

Cool, does anyone know if the operational state could be checked also easily (by Win-API calls if possible)? This can be done using a code like here, but that one is "more I ever wanted" because I need to check if any interface is up on a machine only...
firace
Addict
Addict
Posts: 899
Joined: Wed Nov 09, 2011 8:58 am

Re: Getting network interface name

Post by firace »

Windows-only, non API solution.

Very quick and dirty, but might give a starting point.
First run "netsh interface ip show interface" in a command prompt to identify the interface index you want to look at.

Code: Select all

Procedure.s outputOf(externalCommand.s)   ;; easier handling of one-liner output
  
  externalCommand = ReplaceString(externalCommand,"'",Chr(34))
  
  o.s = ReadProgramString(RunProgram("cmd" , "/c " + externalCommand, "", #PB_Program_Open|#PB_Program_Read|#PB_Program_Hide) ) 
  
  ProcedureReturn o
EndProcedure


OpenConsole("Connection state detector")

PrintN( OutputOf("netsh interface ip show interface 12 | find 'State  '" ))    ;;;   change 12 by your interface index

Delay(900)
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

Re: Getting network interface name

Post by Dude »

Michael Vogel wrote:Cool, does anyone know if the operational state could be checked also easily
From the command line:

Code: Select all

wmic nic where netenabled=true get netconnectionID 
My output:

Code: Select all

Wireless Network Connection
VirtualBox Host-Only Network
More WMIC options here: https://blogs.technet.microsoft.com/ask ... c-queries/

You'll need to clean up the output with Trim() and FindString() a lot. ;)
User avatar
Michael Vogel
Addict
Addict
Posts: 2666
Joined: Thu Feb 09, 2006 11:27 pm
Contact:

Re: Getting network interface name

Post by Michael Vogel »

Thanks, both are good ideas - I don't want to use external programs because I'd like to check the network state permanently, but anyhow your approaches are nice.
WMIC would be the better choice for me - I have written a management tool using this protocol to control PC's (set IP addresses, start/kill processes, etc.) remotely.

Here's the compact code for checking, if any network interface is operational, virtual interfaces are ignored (see "virtu" at the end of the code):

Code: Select all

EnableExplicit

; Define Network Adapter

	#MIB_IF_TYPE_OTHER=1
	#MIB_IF_TYPE_ETHERNET=6
	#MIB_IF_TYPE_TOKENRING=9
	#MIB_IF_TYPE_FDDI=15
	#MIB_IF_TYPE_PPP=23
	#MIB_IF_TYPE_LOOPBACK=24
	#MIB_IF_TYPE_SLIP=28
	#MIB_IF_TYPE_80211=71

	Structure IP_ADDR_STRING1
		*NextAdapter.IP_ADDR_STRING1
		;NextAdapter.i
		IpAddress.a[16];IP_ADDRESS_STRING
		IpMask.a[16];IP_ADDRESS_STRING
		Context.i
	EndStructure

	Structure IP_ADAPTER_INFO1
		NextAdapter.i
		ComboIndex.l
		AdapterName.a[260] ;MAX_ADAPTER_NAME_LENGTH + 4
		Description.a[132] ;MAX_ADAPTER_DESCRIPTION_LENGTH + 4
		AddressLength.l
		Address.a[8] ;MAX_ADAPTER_ADDRESS_LENGTH
		index.l
		Type.l
		DhcpEnabled.i
		CurrentIpAddress.i
		IpAddressList.IP_ADDR_STRING1
		GatewayList.IP_ADDR_STRING1
		DhcpServer.IP_ADDR_STRING1
		HaveWINS.i
		PrimaryWinsServer.IP_ADDR_STRING1
		SecondaryWinsServer.IP_ADDR_STRING1
		LeaseObtained.i
		LeaseExpires.i
	EndStructure

	Structure MIB_IFROW2
		StructureUnion
			Value.q
			Value2.q
		EndStructureUnion
		InterfaceIndex.l
		InterfaceGuid.a[16]
		Alias.w[257]
		Description.w[257]
		PhysicalAddressLength.l
		PhysicalAddress.a[32]
		PermanentPhysicalAddress.a[32]
		Mtu.l
		Type.l
		TunnelType.l;
		MediaType.l
		PhysicalMediumType.l
		AccessType.l
		DirectionType.l
		InterfaceAndOperStatusFlags.l
		OperStatus.l
		AdminStatus.l
		MediaConnectState.l
		NetworkGuid.a[16]
		ConnectionType.q
		TransmitLinkSpeed.q
		ReceiveLinkSpeed.q
		InOctets.q
		InUcastPkts.q
		InNUcastPkts.q
		InDiscards.q
		InErrors.q
		InUnknownProtos.q
		InUcastOctets.q
		InMulticastOctets.q
		InBroadcastOctets.q
		OutOctets.q
		OutUcastPkts.q
		OutNUcastPkts.q
		OutDiscards.q
		OutErrors.q
		OutUcastOctets.q
		OutMulticastOctets.q
		OutBroadcastOctets.q
		OutQLen.q
	EndStructure

	Structure MIB_IFROW
		wszName.a[512]
		dwIndex.l
		dwType.l
		dwMtu.l
		dwSpeed.l
		dwPhysAddrLen.l
		bPhysAddr.a[8]
		dwAdminStatus.l
		dwOperStatus.l
		dwLastChange.l
		dwInOctets.l
		dwInUcastPkts.l
		dwInNUcastPkts.l
		dwInDiscards.l
		dwInErrors.l
		dwInUnknownProtos.l
		dwOutOctets.l
		dwOutUcastPkts.l
		dwOutNUcastPkts.l
		dwOutDiscards.l
		dwOutErrors.l
		dwOutQLen.l
		dwDescrLen.l
		bDescr.a[256]
	EndStructure

	Structure MIB_IFTABLE1
		dwNumEntries.l
		table.MIB_IFROW[256]
	EndStructure

	Structure NetWorkInfo
		index.l
		HostName.s
		DomainName.s
		DNSIPAdd.s[11]
		nod.s
		ScopeID.s
		DNSE.s
		ProxyE.s
		RoutE.s
		Conx.s
		AdapterName.s[11]
		Type.s[11]
		Speed.s[11]
		sMTU.s[11]
		packsS.s[11]
		bytesS.s[11]
		packsR.s[11]
		bytesR.s[11]
		status.s[11]
		Array IPAddr.s(11,256)
		Array SubMask.s(11,256)
		Addr.s[11]
		Indx.s[11]
		DHCPE.s[11]
		DHCPIPAddr.s[11]
		DHCPIPMask.s[11]
		DHCPLObt.s[11]
		DHCPLExp.s[11]
		GateIPAddress.s[11]
		GateIPMask.s[11]
		HaveWINS.s[11]
		PWINSIPAddress.s[11]
		PWINSIPMask.s[11]
		SWINSIPAddress.s[11]
		SWINSIPMask.s[11]
		HWInterface.s[11]
		FilterInterface.s[11]
		HasConntectorPresent.s[11]
		PortAuthenticated.s[11]
		MediaConnected.s[11]
		Paused.s[11]
		LowPower.s[11]
		EndPoint.s[11]
	EndStructure

	Global MIB_IFTABLE.MIB_IFTABLE1
	Global LANInfo.NetWorkInfo


	Procedure.s GetRegString(hKey.l,strPath.s,strValue.s,RegType.l=2)

		Protected KeyHand.l
		Protected datatype.l
		Protected lResult.l
		Protected Dim strBuf.a(1)
		Protected lDataBufSize.l

		Protected intZeroPos.l
		Protected tempV.l
		Protected mm.l
		Protected lValueType
		Protected *Buffer

		RegOpenKeyEx_(hKey,strPath,0,1,@KeyHand)
		lResult=RegQueryValueEx_(KeyHand,strValue,0,@lValueType,0,@lDataBufSize)
		If lDataBufSize > 0
			*Buffer=AllocateMemory(lDataBufSize)
		Else
			ProcedureReturn
		EndIf

		lResult=RegQueryValueEx_(KeyHand,@strValue,0,@RegType,*Buffer,@lDataBufSize)
		If lResult=#ERROR_SUCCESS
			ProcedureReturn PeekS(*Buffer,-1,#PB_Unicode)
		EndIf

	EndProcedure
	Procedure CheckInternet()

		Protected CardsFound
		Protected lErrors
		Protected lBufferPos.l
		Protected lBufferLength
		Protected lSize
		Protected GetIfTables
		Protected s.s

		lErrors=GetAdaptersInfo_(0,@lBufferLength)
		CardsFound.i=lBufferLength/SizeOf(IP_ADAPTER_INFO1)
		Protected Dim IP_ADAPTER_INFO.IP_ADAPTER_INFO1(CardsFound)

		lErrors=GetAdaptersInfo_(@IP_ADAPTER_INFO(0),@lBufferLength)

		If lErrors <> #ERROR_SUCCESS
			ProcedureReturn #False
		EndIf

		lSize=SizeOf(MIB_IFTABLE1)
		lErrors=GetIfTable_(@MIB_IFTABLE,@lSize,0)

		If MIB_IFTABLE\dwNumEntries <> 0
			GetIfTables.i=MIB_IFTABLE\dwNumEntries; we need to know how many are the tables to retrieve them.
		EndIf

		Protected i,ii
		Protected If_entry1.MIB_IFROW
		Protected If_entry2.MIB_IFROW2
		Protected Lib_iphlpapi
		Protected GetIfEntry
		Protected Dim If_entry.MIB_IFROW2(GetIfTables-1)
		Protected Dim tmpMACs.s(GetIfTables)

		If OSVersion() >= #PB_OS_Windows_Vista
			Lib_iphlpapi=OpenLibrary(#PB_Any,"iphlpapi.dll")
			If Lib_iphlpapi<> 0
				GetIfEntry=GetFunction(Lib_iphlpapi,"GetIfEntry2")
			EndIf
			CloseLibrary(Lib_iphlpapi)
			For ii=1 To GetIfTables
				If_entry2\InterfaceIndex=ii
				CallFunctionFast(GetIfEntry,@If_entry2)
				CopyMemory(@If_entry2,@If_entry(ii-1),SizeOf(MIB_IFROW2))
				ClearStructure(@If_entry2,MIB_IFROW2)
			Next
		Else
			For ii=1 To GetIfTables
				IF_entry1\dwIndex=ii
				GetIfEntry_(@If_entry1)
				CopyMemory(@If_entry1\bDescr,@If_entry(ii-1)\Description,256)
				CopyMemory(@If_entry1\bPhysAddr,@If_entry(ii-1)\PhysicalAddress,8)
				If_entry(ii-1)\AdminStatus=If_entry1\dwAdminStatus
				If_entry(ii-1)\InterfaceIndex=If_entry1\dwIndex
				If_entry(ii-1)\OperStatus=If_entry1\dwOperStatus
			Next
		EndIf

		For ii =0 To GetIfTables-1
			s=""
			For i=0 To If_entry(ii)\PhysicalAddressLength -1
				s+RSet(Hex(PeekA(@if_entry(ii)\PhysicalAddress[i])),2,"0") + ":"
			Next
			tmpMACs(ii)=Left(s,Len(s)-1)
		Next
		For ii=0 To CardsFound-1
			s=""
			For i=0 To IP_ADAPTER_INFO(ii)\AddressLength-1
				s+RSet(Hex(PeekA(@IP_ADAPTER_INFO(ii)\Address[i])),2,"0") + ":"
			Next
			LANInfo\Addr[ii]=Left(s,Len(s)-1)
		Next


		For i=0 To CardsFound-1
			With IP_ADAPTER_INFO(i)

				; 			Select \Type
				; 			Case #MIB_IF_TYPE_OTHER: LANInfo\Type[i]="Other"
				; 			Case #MIB_IF_TYPE_ETHERNET: LANInfo\Type[i]="Ethernet"
				; 			Case #MIB_IF_TYPE_TOKENRING: LANInfo\Type[i]="Tokenring"
				; 			Case #MIB_IF_TYPE_FDDI: LANInfo\Type[i]="FDDI"
				; 			Case #MIB_IF_TYPE_PPP: LANInfo\Type[i]="PPP"
				; 			Case #MIB_IF_TYPE_LOOPBACK: LANInfo\Type[i]="Loopback"
				; 			Case #MIB_IF_TYPE_SLIP: LANInfo\Type[i]="Slip"
				; 			Case #MIB_IF_TYPE_80211: LANInfo\Type[i]="802.11 wireless network"
				; 			Default: LANInfo\Type[i]="Unknown " + Str(\Type)
				; 			EndSelect

				For ii=0 To GetIfTables-1
					;If PeekS(@If_entry(ii)\Description,-1,#PB_Unicode) = LANInfo\AdapterName[i]
					If tmpMACs(ii)=LANInfo\Addr[i] And If_entry(ii)\InterfaceIndex=IP_ADAPTER_INFO(i)\index
						LANInfo\AdapterName[i]=PeekS(@\Description,-1,#PB_Ascii)
						If LANInfo\AdapterName[i]=""
							LANInfo\AdapterName[i]=PeekS(@If_entry(ii)\Description,-1,#PB_Unicode)
							If LANInfo\AdapterName[i]=""
								s=PeekS(@IP_ADAPTER_INFO(i)\AdapterName,-1,#PB_Ascii)
								For ii=0 To 100
									If GetRegString(#HKEY_LOCAL_MACHINE,"SOFTWARE\Microsoft\Windows NT\CurrentVErsion\NetworkCards\" + Str(ii),"ServiceName")=s
										LANInfo\AdapterName[i]=GetRegString(#HKEY_LOCAL_MACHINE,"SOFTWARE\Microsoft\Windows NT\CurrentVErsion\NetworkCards\" + Str(ii),"Description")
									EndIf
								Next
							EndIf
						EndIf

						If OSVersion() >= #PB_OS_Windows_Vista
							LANInfo\status[i]=StringField("Oper.Disc.Test.Unkw.BeOp.UnAv.-.-.Down",If_entry(ii)\OperStatus,".")

						Else
							#MIB_IF_OPER_STATUS_OPERATIONAL=5
							If If_entry(ii)\OperStatus<=#MIB_IF_OPER_STATUS_OPERATIONAL
								LANInfo\status[i]=StringField("NoOp.Unrc.Disc.BeCo.Conn.Oper",If_entry(ii)\OperStatus+1,".")
							Else
								LANInfo\status[i]="Unkw"
							EndIf
						EndIf
					EndIf
				Next
			EndWith
		Next

		i=CardsFound
		ii=0
		While i
			i-1
			If LANInfo\Addr[i]
				lErrors=Bool(FindString(LCase(LANInfo\AdapterName[i]),"virtu"))
				Debug LANInfo\status[i]+Mid(" *",lErrors+1,1)+" - "+LANInfo\AdapterName[i]
				If lErrors=0
					If LANInfo\status[i]="Oper"
						ii+1
					EndIf
				EndIf
			EndIf
		Wend

		ProcedureReturn ii

	EndProcedure

; EndDefine

Debug CheckInternet()
Post Reply