Jump to content

Recommended Posts

j0kky

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

Edited by j0kky
  • Like 4

Share this post


Link to post
Share on other sites
UEZ

Nice work j0kky.  :thumbsup:

Br,

UEZ


Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Share this post


Link to post
Share on other sites
helpus

i got this error after i tick IPv6 & click show

1jitl.png

Share this post


Link to post
Share on other sites
j0kky

Which Operative System and Service Pack do you have?

EDIT:

And what happens if you click on "both" and then on show? Is it the same output of "IPv4"?

Edited by j0kky

Share this post


Link to post
Share on other sites
Spider001

hey

latest autoit

windows xp sp3 pro

ipv4

post-87546-0-09023100-1423388026_thumb.j

Share this post


Link to post
Share on other sites
j0kky

@Spider001: thank you for the feedback, there is an error in _WinApi_GetString documentation:

 

If the pointer is incorrect, _WinAPI_GetString() does not set the @error flag but always returns an empty string.

False, if the pointer is incorrect or the buffer is empty, it sets @error to 10, example:

_WinAPI_GetString(Ptr(0))
_WinAPI_GetString(Ptr(Random(1, 10000)))

always return an empty string and set @error to 10.

I've modified suitably the UDF in the first post :)

Test it and report any bug, please!

Share this post


Link to post
Share on other sites
Spider001

no error's

but i don't now if this is normal no data on the last listview's ?

post-87546-0-38711500-1423474047_thumb.j

Share this post


Link to post
Share on other sites
j0kky

Finally I solved the issue with Windows XP:

WSAAddressToString on WinXP doesn't like the Null pointer as fourth parameter:

$aRet = DllCall($hWs2_32, "int", "WSAAddressToString", "ptr", $plpSockaddr, "int", $iSockaddrLength, "ptr", Null, "ptr", Null, "dword*", 0)

so, this is the workaround:

$aRet = DllCall($hWs2_32, "int", "WSAAddressToString", "ptr", $plpSockaddr, "int", $iSockaddrLength, "ptr", Null, "byte*", Null, "dword*", 0)

I've edited the first post with the correct UDF and the new example script.

Thanks to Spider001 for testing my scripts :)

Test it and report any bug in this topic, please!

Edited by j0kky
  • Like 1

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Similar Content

    • VADemon
      By VADemon
      I've encountered a problem with a single file where I cannot retrieve it's Date-time. So far my code has worked well for over 30 files, but this one is a mystery I cannot debug myself due to insufficient Au3 knowledge.
      In line 11 "_Date_Time_FileTimeToArray" is called and for this particular file it sets the @error to 10. I don't know what that error code means, but it's not set by the _Date functions themselves I think.
      Overall, it could be a problem caused by any of the functions below, how can I properly debug this? / Does anybody know a what's causing this?
      _WinAPI_CreateFile() / _Date_Time_GetFileTime() / _Date_Time_FileTimeToArray()
      Func _SetFileTimes($sFilePath) Local $monthNumber[13] = ["", "January", "February", "March", "April", "May", "Juny", "July", "August", "September", "October", "November", "December"] Local $dayNumber[7] = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"] Local $fHandle = _WinAPI_CreateFile($sFilePath, 2, 2) ; read-only ; may NOT return a valid date for some reason! TODO Local $fTagFILETIME = _Date_Time_GetFileTime($fHandle) _WinAPI_CloseHandle($fHandle) ; This will return an empty array if theres no valid date $fModTime = _Date_Time_FileTimeToArray($fTagFILETIME[2]) ; last Modified if @error <> 10 then Local $year = $fModTime[2] Local $month = $fModTime[0] Local $day = $fModTime[1] Local $hour = $fModTime[3] Local $min = $fModTime[4] Local $sec = $fModTime[5] Local $ms = $fModTime[6] Local $weekday = $fModTime[7] Global $prettyTimestamp = StringFormat("%s, %s %d, %04d %02d:%02d:%02d", $dayNumber[$weekday], $monthNumber[$month], $day, $year, $hour, $min, $sec) Global $uploadDate = StringFormat("%04d-%02d-%02d", $year, $month, $day) $fModTime = _Date_Time_FileTimeToArray(_Date_Time_FileTimeToLocalFileTime($fTagFILETIME[2])) ; last Modified Local $year = $fModTime[2] Local $month = $fModTime[0] Local $day = $fModTime[1] Local $hour = $fModTime[3] Local $min = $fModTime[4] Local $sec = $fModTime[5] Local $ms = $fModTime[6] Local $weekday = $fModTime[7] ; GetUnixTime accounts for Local time, hence feed it local time Global $unixTimestamp = _GetUnixTime($year &"/"& $month &"/"& $day &" "& $hour&":"& $min &":"& $sec) else Global $prettyTimestamp = "N/A" Global $uploadDate = "" Global $unixTimestamp = "N/A" endif endfunc  
      _GetUnixTime returned the year 1601 start date, showing that $fModTime is probably equal 0. (But Why?)
      The file reports these dates in Explorer, it's on local NTFS drive:
      Created: ‎‎Wednesday, ‎31. ‎Januar ‎2018, ‏‎18:55:02
      Modified: ‎Wednesday, ‎10. ‎Januar ‎2018, ‏‎12:39:23
      Accessed: ‎Wednesday, ‎10. ‎Januar ‎2018, ‏‎12:39:23
    • astrionn
      By astrionn
      Hi Guys,
       
      I`m trying to record with my webcam in 5s intervalls and do some stuff between starting to record and stoping.
      I took code from here: https://www.autoitscript.com/forum/topic/27925-webcam-example/
      and took what looked relevant to me.
      I use 3 different files:
       Is called WMS.au3 and contains all the constants for the dll calls is called rec.au3 and does all necessary dll calls to start a webcam recording to a file Is called stopcam.exe and does the dll calls to stop recording into a file WMS.au3  :
      $WM_CAP_START = 0x400 $WM_CAP_UNICODE_START = $WM_CAP_START +100 $WM_CAP_PAL_SAVEA = $WM_CAP_START + 81 $WM_CAP_PAL_SAVEW = $WM_CAP_UNICODE_START + 81 $WM_CAP_UNICODE_END = $WM_CAP_PAL_SAVEW $WM_CAP_ABORT = $WM_CAP_START + 69 $WM_CAP_DLG_VIDEOCOMPRESSION = $WM_CAP_START + 46 $WM_CAP_DLG_VIDEODISPLAY = $WM_CAP_START + 43 $WM_CAP_DLG_VIDEOFORMAT = $WM_CAP_START + 41 $WM_CAP_DLG_VIDEOSOURCE = $WM_CAP_START + 42 $WM_CAP_DRIVER_CONNECT = $WM_CAP_START + 10 $WM_CAP_DRIVER_DISCONNECT = $WM_CAP_START + 11 $WM_CAP_DRIVER_GET_CAPS = $WM_CAP_START + 14 $WM_CAP_DRIVER_GET_NAMEA = $WM_CAP_START + 12 $WM_CAP_DRIVER_GET_NAMEW = $WM_CAP_UNICODE_START + 12 $WM_CAP_DRIVER_GET_VERSIONA = $WM_CAP_START + 13 $WM_CAP_DRIVER_GET_VERSIONW = $WM_CAP_UNICODE_START + 13 $WM_CAP_EDIT_COPY = $WM_CAP_START + 30 $WM_CAP_END = $WM_CAP_UNICODE_END $WM_CAP_FILE_ALLOCATE = $WM_CAP_START + 22 $WM_CAP_FILE_GET_CAPTURE_FILEA = $WM_CAP_START + 21 $WM_CAP_FILE_GET_CAPTURE_FILEW = $WM_CAP_UNICODE_START + 21 $WM_CAP_FILE_SAVEASA = $WM_CAP_START + 23 $WM_CAP_FILE_SAVEASW = $WM_CAP_UNICODE_START + 23 $WM_CAP_FILE_SAVEDIBA = $WM_CAP_START + 25 $WM_CAP_FILE_SAVEDIBW = $WM_CAP_UNICODE_START + 25 $WM_CAP_FILE_SET_CAPTURE_FILEA = $WM_CAP_START + 20 $WM_CAP_FILE_SET_CAPTURE_FILEW = $WM_CAP_UNICODE_START + 20 $WM_CAP_FILE_SET_INFOCHUNK = $WM_CAP_START + 24 $WM_CAP_GET_AUDIOFORMAT = $WM_CAP_START + 36 $WM_CAP_GET_CAPSTREAMPTR = $WM_CAP_START + 1 $WM_CAP_GET_MCI_DEVICEA = $WM_CAP_START + 67 $WM_CAP_GET_MCI_DEVICEW = $WM_CAP_UNICODE_START + 67 $WM_CAP_GET_SEQUENCE_SETUP = $WM_CAP_START + 65 $WM_CAP_GET_STATUS = $WM_CAP_START + 54 $WM_CAP_GET_USER_DATA = $WM_CAP_START + 8 $WM_CAP_GET_VIDEOFORMAT = $WM_CAP_START + 44 $WM_CAP_GRAB_FRAME = $WM_CAP_START + 60 $WM_CAP_GRAB_FRAME_NOSTOP = $WM_CAP_START + 61 $WM_CAP_PAL_AUTOCREATE = $WM_CAP_START + 83 $WM_CAP_PAL_MANUALCREATE = $WM_CAP_START + 84 $WM_CAP_PAL_OPENA = $WM_CAP_START + 80 $WM_CAP_PAL_OPENW = $WM_CAP_UNICODE_START + 80 $WM_CAP_PAL_PASTE = $WM_CAP_START + 82 $WM_CAP_SEQUENCE = $WM_CAP_START + 62 $WM_CAP_SEQUENCE_NOFILE = $WM_CAP_START + 63 $WM_CAP_SET_AUDIOFORMAT = $WM_CAP_START + 35 $WM_CAP_SET_CALLBACK_CAPCONTROL = $WM_CAP_START + 85 $WM_CAP_SET_CALLBACK_ERRORA = $WM_CAP_START + 2 $WM_CAP_SET_CALLBACK_ERRORW = $WM_CAP_UNICODE_START + 2 $WM_CAP_SET_CALLBACK_FRAME = $WM_CAP_START + 5 $WM_CAP_SET_CALLBACK_STATUSA = $WM_CAP_START + 3 $WM_CAP_SET_CALLBACK_STATUSW = $WM_CAP_UNICODE_START + 3 $WM_CAP_SET_CALLBACK_VIDEOSTREAM = $WM_CAP_START + 6 $WM_CAP_SET_CALLBACK_WAVESTREAM = $WM_CAP_START + 7 $WM_CAP_SET_CALLBACK_YIELD = $WM_CAP_START + 4 $WM_CAP_SET_MCI_DEVICEA = $WM_CAP_START + 66 $WM_CAP_SET_MCI_DEVICEW = $WM_CAP_UNICODE_START + 66 $WM_CAP_SET_OVERLAY = $WM_CAP_START + 51 $WM_CAP_SET_PREVIEW = $WM_CAP_START + 50 $WM_CAP_SET_PREVIEWRATE = $WM_CAP_START + 52 $WM_CAP_SET_SCALE = $WM_CAP_START + 53 $WM_CAP_SET_SCROLL = $WM_CAP_START + 55 $WM_CAP_SET_SEQUENCE_SETUP = $WM_CAP_START + 64 $WM_CAP_SET_USER_DATA = $WM_CAP_START + 9 $WM_CAP_SET_VIDEOFORMAT = $WM_CAP_START + 45 $WM_CAP_SINGLE_FRAME = $WM_CAP_START + 72 $WM_CAP_SINGLE_FRAME_CLOSE = $WM_CAP_START + 71 $WM_CAP_SINGLE_FRAME_OPEN = $WM_CAP_START + 70 $WM_CAP_STOP = $WM_CAP_START + 68 rec.au3  :
       
      #include <WMS.au3> #include <GUIConstants.au3> if not FileExists(@ScriptDir&"\vid\"&string(@MON)&"\"&string(@mday)) Then DirCreate(@ScriptDir&"\vid\"&"\"&string(@MON)&"\"&string(@mday)) EndIf $moviefile = @ScriptDir&"\vid"&"\"&string(@MON)&"\"&string(@mday)&"\test.avi" $avi = DllOpen("avicap32.dll") $user = DllOpen("user32.dll") FileWrite(@scriptdir&"\clop.clop",$user) $Main = GUICreate("Camera",350,270) $moviefile=@ScriptDir&"\test.avi" $cap = DllCall($avi, "int", "capCreateCaptureWindow", "str", "cap", "int", BitOR($WS_CHILD,$WS_VISIBLE), "int", 15, "int", 15, "int", 320, "int", 240, "hwnd", $Main, "int", 1) FileWrite(@scriptdir&"\clip.clip",$cap[0]) run(@scriptdir&"\stopcam.exe") DllCall($user, "int", "SendMessage", "hWnd", $cap[0], "int", $WM_CAP_DRIVER_CONNECT, "int", 0, "int", 0) DllCall($user, "int", "SendMessage", "hWnd", $cap[0], "int", $WM_CAP_SET_SCALE, "int", 1, "int", 0) DllCall($user, "int", "SendMessage", "hWnd", $cap[0], "int", $WM_CAP_SET_OVERLAY, "int", 1, "int", 0) DllCall($user, "int", "SendMessage", "hWnd", $cap[0], "int", $WM_CAP_SET_PREVIEW, "int", 1, "int", 0) DllCall($user, "int", "SendMessage", "hWnd", $cap[0], "int", $WM_CAP_SET_PREVIEWRATE, "int", 1, "int", 0) DllCall($user, "int", "SendMessage", "hWnd", $cap[0], "int", $WM_CAP_FILE_SET_CAPTURE_FILEA, "int", 0, "str", $moviefile) DllCall($user, "int", "SendMessage", "hWnd", $cap[0], "int", $WM_CAP_SEQUENCE, "int", 0, "int", 0) Stopcam.exe is the compiled version of this:
      #include "WMS.au3" #include <GUIConstants.au3> $cap = Int(FileRead(@scriptdir&"\clip.clip")) $user = int(fileread(@scriptdir&"\clop.clop")) Sleep(10000) DllCall($user, "int", "SendMessage", "hWnd", $cap, "int", $WM_CAP_STOP, "int", 0, "int", 0) DllCall($user, "int", "SendMessage", "hWnd", $cap, "int", $WM_CAP_END, "int", 0, "int", 0) DllCall($user, "int", "SendMessage", "hWnd", $cap, "int", $WM_CAP_DRIVER_DISCONNECT, "int", 0, "int", 0) DllClose($user) FileDelete(@scriptdir&"\clip.clip") FileDelete(@scriptdir&"\clop.clop")  
      Unfortunately this is not working as I thought it would be working.
      Does someone see what I´m doing wrong?
      It should record the webcam for about 10 seconds ( a little less) and save that to an .avi file.
      It does not stop the recording , I have to stop the process manually...
      Secondly, which confuses me more is that it creates the test.avi not in the location is specified in the script... its stored in the ScriptDir for whatever reason haha
      I hope you can help me
       
      ~HappyCoding
    • badcoder123
      By badcoder123
      I was looking around the help file and I came across _GDIPlus_GraphicsDrawRect and _WinAPI_DrawLine and I seem to be having the same "problem" where every time it draws into a window and you lose or gain focus it has to redraw itself. Is there anyway around this? 
      #include <WindowsConstants.au3> #include <WinAPI.au3> #Include <GDIPlus.au3> _GDIPlus_Startup() Run("notepad.exe") $hWnd = WinWait("Untitled") $hGraphic = _GDIPlus_GraphicsCreateFromHWND($hWnd) $Color = 0xFFFF0000 $hPen = _GDIPlus_PenCreate($Color, 2) For $i = 1 To 10 _GDIPlus_GraphicsDrawRect($hGraphic, 200, 200, 25 ,25, $hPen) ToolTip($i) Sleep(1000) Next _WinAPI_RedrawWindow($hWnd, 0, 0, $RDW_INVALIDATE + $RDW_ALLCHILDREN) _GDIPlus_GraphicsDispose($hGraphic) _GDIPlus_PenDispose($hPen) _GDIPlus_Shutdown()  
    • ScriptJunky
      By ScriptJunky
      I noticed a lack of a constants file for _WinAPI_GetSystemMetrics() so I made this for anyone who wants to add it to their library. Enjoy!  (file attached below)
      #include-once ; #INDEX# ======================================================================================================================= ; Title .........: WinAPI GetSystemMetrics Constants ; AutoIt Version : 3.3.14.5 ; Language ......: English ; Description ...: Constants for _WinAPI_GetSystemMetrics(). ; Author(s) .....: ScriptJunky ; =============================================================================================================================== ; #CONSTANTS# =================================================================================================================== ; _WinAPI_GetSystemMetrics() Global Const $ARRANGE = 56 Global Const $CLEANBOOT = 67 Global Const $CMONITORS = 80 Global Const $CMOUSEBUTTONS = 43 Global Const $CONVERTIBLESLATEMODE = 0x2003 Global Const $CXBORDER = 5 Global Const $CXCURSOR = 13 Global Const $CXDLGFRAME = 7 Global Const $CXDOUBLECLK = 36 Global Const $CXDRAG = 68 Global Const $CXEDGE = 45 Global Const $CXFIXEDFRAME = 7 Global Const $CXFOCUSBORDER = 83 Global Const $CXFRAME = 32 Global Const $CXFULLSCREEN = 16 Global Const $CXHSCROLL = 21 Global Const $CXHTHUMB = 10 Global Const $CXICON = 11 Global Const $CXICONSPACING = 38 Global Const $CXMAXIMIZED = 61 Global Const $CXMAXTRACK = 59 Global Const $CXMENUCHECK = 71 Global Const $CXMENUSIZE = 54 Global Const $CXMIN = 28 Global Const $CXMINIMIZED = 57 Global Const $CXMINSPACING = 47 Global Const $CXMINTRACK = 34 Global Const $CXPADDEDBORDER = 92 Global Const $CXSCREEN = 0 Global Const $CXSIZE = 30 Global Const $CXSIZEFRAME = 32 Global Const $CXSMICON = 49 Global Const $CXSMSIZE = 52 Global Const $CXVIRTUALSCREEN = 78 Global Const $CXVSCROLL = 2 Global Const $CYBORDER = 6 Global Const $CYCAPTION = 4 Global Const $CYCURSOR = 14 Global Const $CYDLGFRAME = 8 Global Const $CYDOUBLECLK = 37 Global Const $CYDRAG = 69 Global Const $CYEDGE = 46 Global Const $CYFIXEDFRAME = 8 Global Const $CYFOCUSBORDER = 84 Global Const $CYFRAME = 33 Global Const $CYFULLSCREEN = 17 Global Const $CYHSCROLL = 3 Global Const $CYICON = 12 Global Const $CYICONSPACING = 39 Global Const $CYKANJIWINDOW = 18 Global Const $CYMAXIMIZED = 62 Global Const $CYMAXTRACK = 60 Global Const $CYMENU = 15 Global Const $CYMENUCHECK = 72 Global Const $CYMENUSIZE = 55 Global Const $CYMIN = 29 Global Const $CYMINIMIZED = 58 Global Const $CYMINSPACING = 48 Global Const $CYMINTRACK = 35 Global Const $CYSCREEN = 1 Global Const $CYSIZE = 31 Global Const $CYSIZEFRAME = 33 Global Const $CYSMCAPTION = 51 Global Const $CYSMICON = 50 Global Const $CYSMSIZE = 53 Global Const $CYVIRTUALSCREEN = 79 Global Const $CYVSCROLL = 20 Global Const $CYVTHUMB = 9 Global Const $DBCSENABLED = 42 Global Const $DEBUG = 22 Global Const $DIGITIZER = 94 Global Const $IMMENABLED = 82 Global Const $MAXIMUMTOUCHES = 95 Global Const $MEDIACENTER = 87 Global Const $MENUDROPALIGNMENT = 40 Global Const $MIDEASTENABLED = 74 Global Const $MOUSEPRESENT = 19 Global Const $MOUSEHORIZONTALWHEELPRESENT = 91 Global Const $MOUSEWHEELPRESENT = 75 Global Const $NETWORK = 63 Global Const $PENWINDOWS = 41 Global Const $REMOTECONTROL = 0x2001 Global Const $REMOTESESSION = 0x1000 Global Const $SAMEDISPLAYFORMAT = 81 Global Const $SECURE = 44 Global Const $SERVERR = 289 Global Const $SHOWSOUNDS = 70 Global Const $SHUTTINGDOWN = 0x2000 Global Const $SLOWMACHINE = 73 Global Const $STARTER = 88 Global Const $SWAPBUTTON = 23 Global Const $TABLETPC = 86 Global Const $XVIRTUALSCREEN = 76 Global Const $YVIRTUALSCREEN = 77  
      WinAPISystemMetricsConstants.au3
    • chacoya121
      By chacoya121
      can someone plz explain how WinAPI work and some example script plz
×