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
}