[FUNCTION] Net_GetInterfaceInfo

Post your working scripts, libraries and tools.
User avatar
cyruz
Posts: 348
Joined: 30 Sep 2013, 13:31

[FUNCTION] Net_GetInterfaceInfo

12 Dec 2022, 10:50

Hi guys,

just a small function I wrote for a side project. Tested on Windows 10.

Code: Select all

; ----------------------------------------------------------------------------------------------------------------------
; Function .....: Net_GetInterfaceInfo
; Description ..: Get network interfaces index and GUID.
; Return .......: Object with layout { id: "xxx", guid: "xxx" } or 0 if no adapters are found.
; AHK Version ..: AHK v2
; Author .......: Cyruz  (http://ciroprincipe.info)
; License ......: WTFPL - http://www.wtfpl.net/txt/copying/
; Changelog ....: Dec. 12, 2022 - v0.1 - First version.
; Remarks.......: This function works only on IPv4 adapters.
; Info .........: https://learn.microsoft.com/en-us/windows/win32/api/iphlpapi/nf-iphlpapi-getinterfaceinfo
; ----------------------------------------------------------------------------------------------------------------------
Net_GetInterfaceInfo()
{
    If DllCall("Iphlpapi.dll\GetInterfaceInfo", "Ptr",Buffer(1), "UInt*",&dwLen:=0, "UInt") != 122
        Throw OsError("Can't get required buffer size.")
    If (nRet := DllCall("Iphlpapi.dll\GetInterfaceInfo", "Ptr",cBuf:=Buffer(dwLen), "UInt*",&dwLen, "UInt")) != 0
        Throw OsError(nRet)

    If (nNum := NumGet(cBuf, 0, "UInt")) == 0
        Return 0
        
    aInterfaces := []
    Loop nNum
    {
        ; typedef struct _IP_INTERFACE_INFO {                 Minimum 264 bytes
        ;   LONG                 NumAdapters;                 4 bytes (x86/x64)
        ;   IP_ADAPTER_INDEX_MAP Adapter[1];                  Array of structures (minimum 1 element)
        ; } IP_INTERFACE_INFO, *PIP_INTERFACE_INFO;
        ;
        ; typedef struct _IP_ADAPTER_INDEX_MAP {              260 bytes
        ;   ULONG Index;                                      4 bytes (x86/x64)
        ;   WCHAR Name[MAX_ADAPTER_NAME];                     2 bytes * (MAX_ADAPTER_NAME = 128)
        ; } IP_ADAPTER_INDEX_MAP, *PIP_ADAPTER_INDEX_MAP;
        
        ; GUID additional offset is 32 because the bytes from 4 to 32 are "\DEVICE\TCPIP_" so we omit them.
        nOffset := 4+260*(A_Index-1)       
        aInterfaces.Push({id: NumGet(cBuf, nOffset, "UInt"), guid: StrGet(cBuf.Ptr+nOffset+32,, "UTF-16")})
    }
    
    Return aInterfaces
}
2022.12.13: Some renaming and added info.
Last edited by cyruz on 14 Dec 2022, 18:12, edited 2 times in total.
ABCza on the old forum.
My GitHub.
User avatar
jNizM
Posts: 3183
Joined: 30 Sep 2013, 01:33
Contact:

Re: [FUNCTION] NetLib_GetInterfaceInfo

13 Dec 2022, 02:36

Thanks cyruz
Did something similar with GetAdaptersInfo (viewtopic.php?f=83&t=89720)
[AHK] v2.0.5 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile
User avatar
cyruz
Posts: 348
Joined: 30 Sep 2013, 13:31

Re: [FUNCTION] NetLib_GetInterfaceInfo

13 Dec 2022, 13:40

jNizM wrote:
13 Dec 2022, 02:36
Thanks cyruz
Did something similar with GetAdaptersInfo (viewtopic.php?f=83&t=89720)
Hey jNizM! Thanks for the link, I didn't notice it before in the forum.
ABCza on the old forum.
My GitHub.

Return to “Scripts and Functions (v2)”

Who is online

Users browsing this forum: No registered users and 48 guests