Search the Community
Showing results for tags 'winapi_getadaptersaddresses'.
-
Hi guys, there is no UDF wrappers for GetAdaptersAddresses API... till now Here you are: ; #FUNCTION# ==================================================================================================================== ; Name...........: _WinAPI_GetAdaptersAddresses ; Description ...: Retrieves the addresses associated with the adapters on the local computer. ; Syntax.........: _WinAPI_GetAdaptersAddresses($iFamily = 2, $iShowAll = 1, $iShowTunnel = 0) ; Parameters ....: $iFamily - IP type. ; |0 - Both ; |2 - IPv4 (default) ; |23 - IPv6 ; $iShowAll - Presentation mode ; |0 - Short: only Unicast address(es) and Friendly Name columns are returned ; |1 - Long (default): all columns are returned but Anycast and Multicast columns return only the first address (it is not an array) ; |2 - Complete: all columns are returned, Anycast and Multicast columns return an array with the complete list of addresses ; $iShowTunnel - Shows or not tunnel interfaces ; |0 - Don't show (default) ; |1 - Show ; Return values .: On success it returns a bidimensional array: on rows there is the adapter list, these are the columns ; |0 - Preferred unicast address(es), it returns an array of addresses only if $iFamily == 0 ; |1 - Friendly name ; |2 - Anycast addresses: if $iShowAll == 1 it returns the first anycast address of the list, ; if $iShowAll == 2 it returns an array with the complete list of anycast addresses ; |3 - Multicast addresses: if $iShowAll == 1 it returns the first multicast address of the list, ; if $iShowAll == 2 it returns an array with the complete list of multicast addresses ; |4 - DNS Server 1 address ; |5 - DNS Server 2 address ; |6 - Adapter Name ; |7 - Description ; |8 - MAC address ; |9 - Interface Type ; for example $array[2][8] represents the MAC address of the third adapter ; ; On failure it returns 0 and sets @error to non zero (these values are useful only for debugging reasons): ; |1 - incorrect parameters ; |2 - DllOpen error ; |3 - DllCall error ; |4 - _WinAPI_GetString error ; |5 - Query for IPv6 addresses and IPv6 is not installed on the OS ; Author ........: j0kky ; Modified ......: 1.1.3 13/02/15 ; Remarks .......: This function is fully compatible with all OS from Windows XP onwards, there can be some problem using this function ; with $iFamily == 23 if the OS is prior to Windows XP SP1 ; Link ..........: https://msdn.microsoft.com/en-us/library/windows/desktop/aa365915(v=vs.85).aspx ; =============================================================================================================================== #include <WinAPIMisc.au3> Func _WinAPI_GetAdaptersAddresses($iFamily = 2, $iShowAll = 1, $iShowTunnel = 0) If Not ($iFamily == 0 Or $iFamily == 2 Or $iFamily == 23) Or _ Not ($iShowAll == 0 Or $iShowAll == 1 Or $iShowAll == 2) Or _ Not ($iShowTunnel == 0 Or $iShowTunnel == 1) Then Return SetError(1, 0, 0) Local $aRet If $iFamily == 23 Then $aRet = DllCall("Ws2_32.dll", "int", "WSCEnumProtocols", "ptr", Null, "ptr", Null, "dword*", Null, "ptr", Null) Local $tlpProtocolBuffer = DllStructCreate("byte[" & $aRet[3] & "]") Local $plpProtocolBuffer = DllStructGetPtr($tlpProtocolBuffer) $aRet = DllCall("Ws2_32.dll", "int", "WSCEnumProtocols", "ptr", Null, "ptr", $plpProtocolBuffer, "dword*", $aRet[3], "ptr", Null) Local $tagWSAPROTOCOL_INFOW = "dword dwServiceFlags1; dword dwServiceFlags2; dword dwServiceFlags3; dword dwServiceFlags4; dword dwServiceFlags5; byte ProviderId[16]; " & _ "dword dwCatalogEntryId; " & _ "STRUCT; int ChainLen; dword ChainEntries[7]; ENDSTRUCT;" & _ ;ProtocolChain "int iVersion; int iAddressFamily; int iMaxSockAddr; int iMinSockAddr; int iSocketType; int iProtocol; int iProtocolMaxOffset; int iNetworkByteOrder; " & _ "int iSecurityScheme; dword dwMessageSize; dword dwProviderReserved; wchar szProtocol[256]" Local $tWSAPROTOCOL_INFOW, $size For $i = 0 To $aRet[0] - 1 If $i == 0 Then $tWSAPROTOCOL_INFOW = DllStructCreate($tagWSAPROTOCOL_INFOW, $plpProtocolBuffer) Else $tWSAPROTOCOL_INFOW = DllStructCreate($tagWSAPROTOCOL_INFOW, $plpProtocolBuffer + $size) EndIf If DllStructGetData($tWSAPROTOCOL_INFOW, "iAddressFamily") == 23 Then ExitLoop If $i == ($aRet[0] - 1) Then Return SetError(5, 0, 0) ;if IPv6 is not installed on the system $size += DllStructGetSize($tWSAPROTOCOL_INFOW) Next EndIf $hIphlpapi = DllOpen("Iphlpapi.dll") If @error Then Return SetError(2, 0, 0) $aRet = DllCall($hIphlpapi, "ULONG", "GetAdaptersAddresses", "ULONG", $iFamily, "ULONG", 0, "ptr", Null, "ptr", Null, "dword*", 0) If @error Then DllClose($hIphlpapi) Return SetError(3, 0, 0) EndIf Local $tbuffer_AdapterAddresses = DllStructCreate("byte[" & $aRet[5] & "]") Local $pbuffer_AdapterAddresses = DllStructGetPtr($tbuffer_AdapterAddresses) DllCall($hIphlpapi, "ULONG", "GetAdaptersAddresses", "ULONG", $iFamily, "ULONG", 0, "ptr", Null, "ptr", $pbuffer_AdapterAddresses, "dword*", $aRet[5]) If @error Then DllClose($hIphlpapi) Return SetError(3, 0, 0) EndIf DllClose($hIphlpapi) Local Const $tagIP_ADAPTER_ADDRESSES = "ulong Length; dword IfIndex; ptr Next; ptr AdapterName; ptr FirstUnicastAddress; ptr FirstAnycastAddress; " & _ "ptr FirstMulticastAddress; ptr FirstDnsServerAddress; ptr DnsSuffix; ptr Description; ptr FriendlyName; byte PhysicalAddress[8]; dword PhysicalAddressLength; " & _ "dword Flags; dword Mtu; dword IfType; int OperStatus" ;valid on Windows XP #cs "dword Ipv6IfIndex; dword ZoneIndices[16]; ptr FirstPrefix; " & _ ;added on WinXP SP1 "uint64 TransmitLinkSpeed; uint64 ReceiveLinkSpeed; ptr FirstWinsServerAddress; ptr FirstGatewayAddress; ulong Ipv4Metric; ulong Ipv6Metric; uint64 Luid; " & _ "STRUCT; ptr Dhcpv4ServerlpSockaddr; int Dhcpv4ServeriSockaddrLength; ENDSTRUCT; " & _ ;Dhcpv4Server "uint CompartmentId; byte NetworkGuid[16]; int ConnectionType; int TunnelType; " & _ "STRUCT; ptr Dhcpv6ServerlpSockaddr; int Dhcpv6ServeriSockaddrLength; ENDSTRUCT; " & _ ;Dhcpv6Server "byte Dhcpv6ClientDuid[130]; ulong Dhcpv6ClientDuidLength; ulong Dhcpv6Iaid; " & _ ;added on WinVista "ptr FirstDnsSuffix" ;added on WinVista SP1 #ce Local Const $tagIP_ADAPTER_UNICAST_ADDRESS = "ulong Length; dword Flags; ptr Next; " & _ "STRUCT; ptr AddresslpSockaddr; int AddressiSockaddrLength; ENDSTRUCT; " & _ ;Address "int PrefixOrigin; int SuffixOrigin; int DadState; ulong ValidLifetime; ulong PreferredLifetime; ulong LeaseLifetime" #cs "byte OnLinkPrefixLength" ;added on WinVista #ce Local $tIP_ADAPTER_ADDRESSES = DllStructCreate($tagIP_ADAPTER_ADDRESSES, $pbuffer_AdapterAddresses) Local $nAdapter = 0, $pUnicastAddress, $tIP_ADAPTER_UNICAST_ADDRESS, $plpSockaddr, $iSockaddrLength, $tbuffer_lpszAddressString, $pbuffer_lpszAddressString Local Const $IfOperStatusUp = 1, $IF_TYPE_SOFTWARE_LOOPBACK = 24, $IF_TYPE_TUNNEL = 131, $IpDadStatePreferred = 4, $pNull = Ptr(0) If $iShowAll Then Local $nColumns = 10 Local Const $tagIP_ADAPTER_ANYCAST_ADDRESS = "ulong Length; dword Flags; ptr Next; " & _ "STRUCT; ptr AddresslpSockaddr; int AddressiSockaddrLength; ENDSTRUCT" ;Address Local Const $tagIP_ADAPTER_MULTICAST_ADDRESS = $tagIP_ADAPTER_ANYCAST_ADDRESS Local Const $tagIP_ADAPTER_DNS_SERVER_ADDRESS = "ulong Length; dword Reserved; ptr Next; " & _ "STRUCT; ptr AddresslpSockaddr; int AddressiSockaddrLength; ENDSTRUCT" ;Address Local $pAnycastAddress, $tIP_ADAPTER_ANYCAST_ADDRESS, $pMulticastAddress, $tIP_ADAPTER_MULTICAST_ADDRESS, $pDnsServerAddress, $tADAPTER_DNS_SERVER_ADDRESS, $sMAC Local Const $IF_TYPE_ETHERNET_CSMACD = 6, $IF_TYPE_IEEE80211 = 71, $IF_TYPE_IEEE1394 = 144 Else Local $nColumns = 2 EndIf Local $aResult[$nAdapter][$nColumns] $hWs2_32 = DllOpen("Ws2_32.dll") If @error Then Return SetError(2, 0, 0) TCPStartup() Do If DllStructGetData($tIP_ADAPTER_ADDRESSES, "OperStatus") == $IfOperStatusUp And _ Not (DllStructGetData($tIP_ADAPTER_ADDRESSES, "IfType") == $IF_TYPE_SOFTWARE_LOOPBACK) And _ (Not (DllStructGetData($tIP_ADAPTER_ADDRESSES, "IfType") == $IF_TYPE_TUNNEL) Or $iShowTunnel) Then ;if it's connected and it is not a loopback and it is a tunnel interface (only if $iShowTunnel == 1) $nAdapter += 1 ReDim $aResult[$nAdapter][$nColumns] ;Unicast address(es) $pUnicastAddress = DllStructGetData($tIP_ADAPTER_ADDRESSES, "FirstUnicastAddress") $tIP_ADAPTER_UNICAST_ADDRESS = DllStructCreate($tagIP_ADAPTER_UNICAST_ADDRESS, $pUnicastAddress) If Not $iFamily Then Local $aAddresses[0], $iCounter = -1 Do ;write the PREFERRED address for this adapter, it writes both IPV4 and IPV6 preferred addresses in an array if $iFamily == 0 If DllStructGetData($tIP_ADAPTER_UNICAST_ADDRESS, "DadState") == $IpDadStatePreferred Then $plpSockaddr = DllStructGetData($tIP_ADAPTER_UNICAST_ADDRESS, "AddresslpSockaddr") $iSockaddrLength = DllStructGetData($tIP_ADAPTER_UNICAST_ADDRESS, "AddressiSockaddrLength") $aRet = DllCall($hWs2_32, "int", "WSAAddressToString", "ptr", $plpSockaddr, "int", $iSockaddrLength, "ptr", Null, "byte*", Null, "dword*", 0) If @error Then DllClose($hWs2_32) Return SetError(3, 0, 0) EndIf $tbuffer_lpszAddressString = DllStructCreate("wchar[" & $aRet[5] & "]") $pbuffer_lpszAddressString = DllStructGetPtr($tbuffer_lpszAddressString) $aRet = DllCall($hWs2_32, "int", "WSAAddressToStringW", "ptr", $plpSockaddr, "int", $iSockaddrLength, "ptr", Null, "ptr", $pbuffer_lpszAddressString, "dword*", $aRet[5]) If @error Then DllClose($hWs2_32) Return SetError(3, 0, 0) EndIf If $iFamily Then $aResult[$nAdapter - 1][0] = _WinAPI_GetString($pbuffer_lpszAddressString) If @error And @error <> 10 Then DllClose($hWs2_32) Return SetError(4, 0, 0) EndIf ExitLoop Else $iCounter = UBound($aAddresses) ReDim $aAddresses[$iCounter + 1] $aAddresses[$iCounter] = _WinAPI_GetString($pbuffer_lpszAddressString) If @error And @error <> 10 Then DllClose($hWs2_32) Return SetError(4, 0, 0) EndIf EndIf EndIf $pUnicastAddress = DllStructGetData($tIP_ADAPTER_UNICAST_ADDRESS, "Next") If Not ($pUnicastAddress == $pNull) Then $tIP_ADAPTER_UNICAST_ADDRESS = DllStructCreate($tagIP_ADAPTER_UNICAST_ADDRESS, $pUnicastAddress) EndIf Until $pUnicastAddress == $pNull If Not $iFamily Then If $iCounter < 0 Then ReDim $aAddresses[1] ;if there is no preferred unicast addresses for this adapter, I don't know if it can happens $aResult[$nAdapter - 1][0] = $aAddresses EndIf ;FriendlyName $aResult[$nAdapter - 1][1] = _WinAPI_GetString(DllStructGetData($tIP_ADAPTER_ADDRESSES, "FriendlyName")) If @error And @error <> 10 Then DllClose($hWs2_32) Return SetError(4, 0, 0) EndIf If $iShowAll Then ;Anycast address, it shows only the first one if $iShowAll == 1 $pAnycastAddress = DllStructGetData($tIP_ADAPTER_ADDRESSES, "FirstAnycastAddress") If Not ($pAnycastAddress == $pNull) Then $tIP_ADAPTER_ANYCAST_ADDRESS = DllStructCreate($tagIP_ADAPTER_ANYCAST_ADDRESS, $pAnycastAddress) If $iShowAll == 2 Then Local $aAddresses[0] While $pMulticastAddress <> $pNull $plpSockaddr = DllStructGetData($tIP_ADAPTER_ANYCAST_ADDRESS, "AddresslpSockaddr") $iSockaddrLength = DllStructGetData($tIP_ADAPTER_ANYCAST_ADDRESS, "AddressiSockaddrLength") $aRet = DllCall($hWs2_32, "int", "WSAAddressToString", "ptr", $plpSockaddr, "int", $iSockaddrLength, "ptr", Null, "byte*", Null, "dword*", 0) If @error Then DllClose($hWs2_32) Return SetError(3, 0, 0) EndIf $tbuffer_lpszAddressString = DllStructCreate("wchar[" & $aRet[5] & "]") $pbuffer_lpszAddressString = DllStructGetPtr($tbuffer_lpszAddressString) $aRet = DllCall($hWs2_32, "int", "WSAAddressToStringW", "ptr", $plpSockaddr, "int", $iSockaddrLength, "ptr", Null, "ptr", $pbuffer_lpszAddressString, "dword*", $aRet[5]) If @error Then DllClose($hWs2_32) Return SetError(3, 0, 0) EndIf If $iShowAll == 2 Then $iCounter = UBound($aAddresses) ReDim $aAddresses[$iCounter + 1] $aAddresses[$iCounter] = _WinAPI_GetString($pbuffer_lpszAddressString) If @error And @error <> 10 Then DllClose($hWs2_32) Return SetError(4, 0, 0) EndIf Else $aResult[$nAdapter - 1][2] = _WinAPI_GetString($pbuffer_lpszAddressString) If @error And @error <> 10 Then DllClose($hWs2_32) Return SetError(4, 0, 0) EndIf ExitLoop EndIf $pAnycastAddress = DllStructGetData($tIP_ADAPTER_ANYCAST_ADDRESS, "Next") If Not ($pAnycastAddress == $pNull) Then $tIP_ADAPTER_ANYCAST_ADDRESS = DllStructCreate($tagIP_ADAPTER_ANYCAST_ADDRESS, $pAnycastAddress) EndIf WEnd If $iShowAll == 2 Then If $aAddresses[0] Then $aResult[$nAdapter - 1][2] = $aAddresses EndIf EndIf ;Multicast address, it shows only the first one if $iShowAll == 1 $pMulticastAddress = DllStructGetData($tIP_ADAPTER_ADDRESSES, "FirstMulticastAddress") If Not ($pMulticastAddress == $pNull) Then $tIP_ADAPTER_MULTICAST_ADDRESS = DllStructCreate($tagIP_ADAPTER_MULTICAST_ADDRESS, $pMulticastAddress) If $iShowAll == 2 Then Local $aAddresses[0] While $pMulticastAddress <> $pNull $plpSockaddr = DllStructGetData($tIP_ADAPTER_MULTICAST_ADDRESS, "AddresslpSockaddr") $iSockaddrLength = DllStructGetData($tIP_ADAPTER_MULTICAST_ADDRESS, "AddressiSockaddrLength") $aRet = DllCall($hWs2_32, "int", "WSAAddressToString", "ptr", $plpSockaddr, "int", $iSockaddrLength, "ptr", Null, "byte*", Null, "dword*", 0) If @error Then DllClose($hWs2_32) Return SetError(3, 0, 0) EndIf $tbuffer_lpszAddressString = DllStructCreate("wchar[" & $aRet[5] & "]") $pbuffer_lpszAddressString = DllStructGetPtr($tbuffer_lpszAddressString) $aRet = DllCall($hWs2_32, "int", "WSAAddressToStringW", "ptr", $plpSockaddr, "int", $iSockaddrLength, "ptr", Null, "ptr", $pbuffer_lpszAddressString, "dword*", $aRet[5]) If @error Then DllClose($hWs2_32) Return SetError(3, 0, 0) EndIf If $iShowAll == 2 Then $iCounter = UBound($aAddresses) ReDim $aAddresses[$iCounter + 1] $aAddresses[$iCounter] = _WinAPI_GetString($pbuffer_lpszAddressString) If @error And @error <> 10 Then DllClose($hWs2_32) Return SetError(4, 0, 0) EndIf Else $aResult[$nAdapter - 1][3] = _WinAPI_GetString($pbuffer_lpszAddressString) If @error And @error <> 10 Then DllClose($hWs2_32) Return SetError(4, 0, 0) EndIf ExitLoop EndIf $pMulticastAddress = DllStructGetData($tIP_ADAPTER_MULTICAST_ADDRESS, "Next") If Not ($pMulticastAddress == $pNull) Then $tIP_ADAPTER_MULTICAST_ADDRESS = DllStructCreate($tagIP_ADAPTER_MULTICAST_ADDRESS, $pMulticastAddress) EndIf WEnd If $iShowAll == 2 Then If $aAddresses[0] Then $aResult[$nAdapter - 1][3] = $aAddresses EndIf EndIf ;DnsServer 1 and 2 $pDnsServerAddress = DllStructGetData($tIP_ADAPTER_ADDRESSES, "FirstDnsServerAddress") If Not ($pDnsServerAddress == $pNull) Then $tADAPTER_DNS_SERVER_ADDRESS = DllStructCreate($tagIP_ADAPTER_DNS_SERVER_ADDRESS, $pDnsServerAddress) For $iCounter = 4 To 5 $plpSockaddr = DllStructGetData($tADAPTER_DNS_SERVER_ADDRESS, "AddresslpSockaddr") $iSockaddrLength = DllStructGetData($tADAPTER_DNS_SERVER_ADDRESS, "AddressiSockaddrLength") $aRet = DllCall($hWs2_32, "int", "WSAAddressToString", "ptr", $plpSockaddr, "int", $iSockaddrLength, "ptr", Null, "byte*", Null, "dword*", 0) If @error Then DllClose($hWs2_32) Return SetError(3, 0, 0) EndIf $tbuffer_lpszAddressString = DllStructCreate("wchar[" & $aRet[5] & "]") $pbuffer_lpszAddressString = DllStructGetPtr($tbuffer_lpszAddressString) $aRet = DllCall($hWs2_32, "int", "WSAAddressToStringW", "ptr", $plpSockaddr, "int", $iSockaddrLength, "ptr", Null, "ptr", $pbuffer_lpszAddressString, "dword*", $aRet[5]) If @error Then DllClose($hWs2_32) Return SetError(3, 0, 0) EndIf $aResult[$nAdapter - 1][$iCounter] = _WinAPI_GetString($pbuffer_lpszAddressString) If @error And @error <> 10 Then DllClose($hWs2_32) Return SetError(4, 0, 0) EndIf $pDnsServerAddress = DllStructGetData($tADAPTER_DNS_SERVER_ADDRESS, "Next") If Not ($pDnsServerAddress == $pNull) Then $tADAPTER_DNS_SERVER_ADDRESS = DllStructCreate($tagIP_ADAPTER_DNS_SERVER_ADDRESS, $pDnsServerAddress) Else ExitLoop EndIf Next EndIf ;AdapterName $aResult[$nAdapter - 1][6] = _WinAPI_GetString(DllStructGetData($tIP_ADAPTER_ADDRESSES, "AdapterName"), False) If @error And @error <> 10 Then DllClose($hWs2_32) Return SetError(4, 0, 0) EndIf ;Description $aResult[$nAdapter - 1][7] = _WinAPI_GetString(DllStructGetData($tIP_ADAPTER_ADDRESSES, "Description")) If @error And @error <> 10 Then DllClose($hWs2_32) Return SetError(4, 0, 0) EndIf ;MAC $sMAC = String(DllStructGetData($tIP_ADAPTER_ADDRESSES, "PhysicalAddress")) $aResult[$nAdapter - 1][8] = StringRegExpReplace( _ StringTrimRight(StringTrimLeft($sMAC, 2), _ ;0x StringLen($sMAC) - ((DllStructGetData($tIP_ADAPTER_ADDRESSES, "PhysicalAddressLength") * 2) + 2)), _ "(.{2})(?!$)", "$1-") ;from 0xG4V4B5B4V2N2000000 to G4-V4-B5-B4-V2-N2 ;IfType Switch DllStructGetData($tIP_ADAPTER_ADDRESSES, "IfType") Case $IF_TYPE_ETHERNET_CSMACD Switch @OSVersion Case "WIN_XP", "WIN_XPe", "WIN_2003" $aResult[$nAdapter - 1][9] = "Ethernet or wireless network interface" Case Else $aResult[$nAdapter - 1][9] = "Ethernet network interface" EndSwitch Case $IF_TYPE_IEEE80211 $aResult[$nAdapter - 1][9] = "Wireless network interface" Case $IF_TYPE_TUNNEL $aResult[$nAdapter - 1][9] = "Tunnel type encapsulation network interface" Case $IF_TYPE_IEEE1394 $aResult[$nAdapter - 1][9] = "Firewire network interface" Case Else $aResult[$nAdapter - 1][9] = "Other type of network interface" EndSwitch EndIf EndIf $pbuffer_AdapterAddresses = DllStructGetData($tIP_ADAPTER_ADDRESSES, "Next") If Not ($pbuffer_AdapterAddresses == $pNull) Then $tIP_ADAPTER_ADDRESSES = DllStructCreate($tagIP_ADAPTER_ADDRESSES, $pbuffer_AdapterAddresses) EndIf Until $pbuffer_AdapterAddresses == $pNull TCPShutdown() DllClose($hWs2_32) Return $aResult EndFunc ;==>_WinAPI_GetAdaptersAddresses It is really useful because it can retrieve all informations needed for every adapters and it works with IPv6 too (opposite of GetAdaptersInfo, that supports only IPv4). I've made it compatible with every OS (from Windows XP). Test it and report any bug in this topic, please! I've attached an example about how it can be used, enjoy it EDIT: Last UDF version: 13th February 2015 GetAdaptersAddresses.au3
- 10 replies
-
- winapi
- getadaptersaddresses
-
(and 1 more)
Tagged with: