Sign in to follow this  
Followers 0
mrowwy

@IPAddress Issues

7 posts in this topic

I'm having a problem with @IPAddress1 & @IPAddress2 where it seems like @IPAddress1 refers to the first active IP address but isn't necessarily tied to a specific network adapter.

I have a script that checks the IP address of each at each index then posts them to a listview, however if I disconnect the first NIC, the IP address that was listed at @IPAddress2 is now shows at @IPAddress1.

Which also results in @IPAddress2 reporting "0.0.0.0" for its IP.

Is this something I should expect to happen?

Is there a way to get the address of a specific NIC easily?

#include <GUIConstantsEx.au3>
#include <ListViewConstants.au3>
 
Opt("GUIOnEventMode", 1)  ; Change to OnEvent mode 
$mainwindow = GUICreate("IP Check", 240, 120)
 
GUISetOnEvent($GUI_EVENT_CLOSE, "CLOSEClicked")
 
$ip1 = @IPAddress1
$ip2 = @IPAddress2
 
$listview = GUICtrlCreateListView("| IP 1 | IP 2", 10, 10, 220, 100, $LVSCW_AUTOSIZE)
$listviewitem = GUICtrlCreateListViewItem(" | " & $ip1 & " | " & $ip2, $listview)
 
GUISetState(@SW_SHOW)
 
While 1
   If @IPAddress1 <> $ip1 Or @IPAddress2 <> $ip2 Then
 GUICtrlSetData($listviewitem, " | " & @IPaddress1 & " | " & @IPAddress2)
 $ip1 = @IPAddress1
 $ip2 = @IPAddress2
   Else
   Sleep(1000)
   EndIf
WEnd
 
Func CLOSEClicked()
  ;Note: at this point @GUI_CTRLID would equal $GUI_EVENT_CLOSE,
  ;@GUI_WINHANDLE will be either $mainwindow or $dummywindow
  If @GUI_WINHANDLE = $mainwindow Then
    Exit
  EndIf 
EndFunc

Share this post


Link to post
Share on other sites



quick and dirty: call ipconfig, redirect output to text file, parse it.

not hard, provided you know what is your NIC name.

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

If all you are looking for is IP in relation to adapter name, try this out.

#include <array.au3>

$aTest = GetNics()
_ArrayDisplay($aTest)

Func GetNics()
    Local $NETWORK_REG_KEY = "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Network\{4D36E972-E325-11CE-BFC1-08002BE10318}\"
    Local $tagIP_ADDRESS_STRING = "char IPAddress[16];"
    Local $tagIP_MASK_STRING = "char IPMask[16];"
    Local $tagIP_ADDR_STRING = "ptr Next;" & $tagIP_ADDRESS_STRING & $tagIP_MASK_STRING & "DWORD Context;"
    Local $tagIP_ADAPTER_INFO = "ptr Next; DWORD ComboIndex; char AdapterName[260];char Description[132]; UINT AddressLength; BYTE Address[8]; dword Index; UINT Type;" & _
            " UINT DhcpEnabled; ptr CurrentIpAddress; ptr IpAddressListNext; char IpAddressListADDRESS[16]; char IpAddressListMASK[16]; DWORD IpAddressListContext; " & _
            "ptr GatewayListNext; char GatewayListADDRESS[16]; char GatewayListMASK[16]; DWORD GatewayListContext; " & _
            "ptr DhcpServerNext; char DhcpServerADDRESS[16]; char DhcpServerMASK[16]; DWORD DhcpServerContext; " & _
            "int HaveWins; " & _
            "ptr PrimaryWinsServerNext; char PrimaryWinsServerADDRESS[16]; char PrimaryWinsServerMASK[16]; DWORD PrimaryWinsServerContext; " & _
            "ptr SecondaryWinsServerNext; char SecondaryWinsServerADDRESS[16]; char SecondaryWinsServerMASK[16]; DWORD SecondaryWinsServerContext; " & _
            "DWORD LeaseObtained; DWORD LeaseExpires;"
    Local $dll = DllOpen("Iphlpapi.dll")
    Local $ret = DllCall($dll, "dword", "GetAdaptersInfo", "ptr", 0, "dword*", 0)
    Local $adapterBuffer = DllStructCreate("byte[" & $ret[2] & "]")
    Local $adapterBuffer_pointer = DllStructGetPtr($adapterBuffer)
    Local $return = DllCall($dll, "dword", "GetAdaptersInfo", "ptr", $adapterBuffer_pointer, "dword*", $ret[2])
    Local $adapter = DllStructCreate($tagIP_ADAPTER_INFO, $adapterBuffer_pointer)
    If Not @error Then
        Dim $aAdapters[100][3]
        $i = 0
        Do
            $aAdapters[$i][0] = DllStructGetData($adapter, "Description")
            $aAdapters[$i][1] = RegRead($NETWORK_REG_KEY & DllStructGetData($adapter, "AdapterName") & "\Connection", "Name")
            $aAdapters[$i][2] = DllStructGetData(DllStructCreate($tagIP_ADDR_STRING, DllStructGetPtr($adapter, "IpAddressListNext")), "IPAddress")
            $i += 1
            $ptr = DllStructGetData($adapter, "Next")
            $adapter = DllStructCreate($tagIP_ADAPTER_INFO, $ptr)
        Until @error
        ReDim $aAdapters[$i][3]
    EndIf
    Return $aAdapters
EndFunc   ;==>GetNics

 

NOTE: >All thanks to the work done by ProgAndy in this thread.

Edited by danwilli
1 person likes this

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

If all you are looking for is IP in relation to adapter name, try this out.

#include <array.au3>

$aTest = GetNics()
_ArrayDisplay($aTest)

Func GetNics()
    Local $NETWORK_REG_KEY = "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Network\{4D36E972-E325-11CE-BFC1-08002BE10318}\"
    Local $tagIP_ADDRESS_STRING = "char IPAddress[16];"
    Local $tagIP_MASK_STRING = "char IPMask[16];"
    Local $tagIP_ADDR_STRING = "ptr Next;" & $tagIP_ADDRESS_STRING & $tagIP_MASK_STRING & "DWORD Context;"
    Local $tagIP_ADAPTER_INFO = "ptr Next; DWORD ComboIndex; char AdapterName[260];char Description[132]; UINT AddressLength; BYTE Address[8]; dword Index; UINT Type;" & _
            " UINT DhcpEnabled; ptr CurrentIpAddress; ptr IpAddressListNext; char IpAddressListADDRESS[16]; char IpAddressListMASK[16]; DWORD IpAddressListContext; " & _
            "ptr GatewayListNext; char GatewayListADDRESS[16]; char GatewayListMASK[16]; DWORD GatewayListContext; " & _
            "ptr DhcpServerNext; char DhcpServerADDRESS[16]; char DhcpServerMASK[16]; DWORD DhcpServerContext; " & _
            "int HaveWins; " & _
            "ptr PrimaryWinsServerNext; char PrimaryWinsServerADDRESS[16]; char PrimaryWinsServerMASK[16]; DWORD PrimaryWinsServerContext; " & _
            "ptr SecondaryWinsServerNext; char SecondaryWinsServerADDRESS[16]; char SecondaryWinsServerMASK[16]; DWORD SecondaryWinsServerContext; " & _
            "DWORD LeaseObtained; DWORD LeaseExpires;"
    Local $dll = DllOpen("Iphlpapi.dll")
    Local $ret = DllCall($dll, "dword", "GetAdaptersInfo", "ptr", 0, "dword*", 0)
    Local $adapterBuffer = DllStructCreate("byte[" & $ret[2] & "]")
    Local $adapterBuffer_pointer = DllStructGetPtr($adapterBuffer)
    Local $return = DllCall($dll, "dword", "GetAdaptersInfo", "ptr", $adapterBuffer_pointer, "dword*", $ret[2])
    Local $adapter = DllStructCreate($tagIP_ADAPTER_INFO, $adapterBuffer_pointer)
    If Not @error Then
        Dim $aAdapters[100][3]
        $i = 0
        Do
            $aAdapters[$i][0] = DllStructGetData($adapter, "Description")
            $aAdapters[$i][1] = RegRead($NETWORK_REG_KEY & DllStructGetData($adapter, "AdapterName") & "\Connection", "Name")
            $aAdapters[$i][2] = DllStructGetData(DllStructCreate($tagIP_ADDR_STRING, DllStructGetPtr($adapter, "IpAddressListNext")), "IPAddress")
            $i += 1
            $ptr = DllStructGetData($adapter, "Next")
            $adapter = DllStructCreate($tagIP_ADAPTER_INFO, $ptr)
        Until @error
        ReDim $aAdapters[$i][3]
    EndIf
    Return $aAdapters
EndFunc   ;==>GetNics

NOTE: >All thanks to the work done by ProgAndy in this thread.

 

This was massively helpful! Thank you.

I'm still a bit new at this, how would I go about adding the correlating Subnet Masks to a 4th column?

I've tried to expand the array to [100][4] and include another column:

$aAdapters[$i][3] = DllStructGetData(DllStructCreate($tagIP_ADDR_STRING, DllStructGetPtr($adapter, "IpAddressListMASK")), "IPAddress")

 

However, what is returned is "0.0" for disconnected adapters and is missing the first octet for connected ones "255.255.0".

What am I missing?

Thanks again!

Edited by mrowwy

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

This was massively helpful! Thank you.

I'm still a bit new at this, how would I go about adding the correlating Subnet Masks to a 4th column?

Thanks again!

#include <array.au3>

$aTest = GetNics()
_ArrayDisplay($aTest)

Func GetNics()
    Local $NETWORK_REG_KEY = "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Network\{4D36E972-E325-11CE-BFC1-08002BE10318}\"
    Local $tagIP_ADDRESS_STRING = "char IPAddress[16];"
    Local $tagIP_MASK_STRING = "char IPMask[16];"
    Local $tagIP_ADDR_STRING = "ptr Next;" & $tagIP_ADDRESS_STRING & $tagIP_MASK_STRING & "DWORD Context;"
    Local $tagIP_ADAPTER_INFO = "ptr Next; DWORD ComboIndex; char AdapterName[260];char Description[132]; UINT AddressLength; BYTE Address[8]; dword Index; UINT Type;" & _
            " UINT DhcpEnabled; ptr CurrentIpAddress; ptr IpAddressListNext; char IpAddressListADDRESS[16]; char IpAddressListMASK[16]; DWORD IpAddressListContext; " & _
            "ptr GatewayListNext; char GatewayListADDRESS[16]; char GatewayListMASK[16]; DWORD GatewayListContext; " & _
            "ptr DhcpServerNext; char DhcpServerADDRESS[16]; char DhcpServerMASK[16]; DWORD DhcpServerContext; " & _
            "int HaveWins; " & _
            "ptr PrimaryWinsServerNext; char PrimaryWinsServerADDRESS[16]; char PrimaryWinsServerMASK[16]; DWORD PrimaryWinsServerContext; " & _
            "ptr SecondaryWinsServerNext; char SecondaryWinsServerADDRESS[16]; char SecondaryWinsServerMASK[16]; DWORD SecondaryWinsServerContext; " & _
            "DWORD LeaseObtained; DWORD LeaseExpires;"
    Local $dll = DllOpen("Iphlpapi.dll")
    Local $ret = DllCall($dll, "dword", "GetAdaptersInfo", "ptr", 0, "dword*", 0)
    Local $adapterBuffer = DllStructCreate("byte[" & $ret[2] & "]")
    Local $adapterBuffer_pointer = DllStructGetPtr($adapterBuffer)
    Local $return = DllCall($dll, "dword", "GetAdaptersInfo", "ptr", $adapterBuffer_pointer, "dword*", $ret[2])
    Local $adapter = DllStructCreate($tagIP_ADAPTER_INFO, $adapterBuffer_pointer)
    If Not @error Then
        Dim $aAdapters[100][4]
        $i = 0
        Do
            $aAdapters[$i][0] = DllStructGetData($adapter, "Description")
            $aAdapters[$i][1] = RegRead($NETWORK_REG_KEY & DllStructGetData($adapter, "AdapterName") & "\Connection", "Name")
            $aAdapters[$i][2] = DllStructGetData(DllStructCreate($tagIP_ADDR_STRING, DllStructGetPtr($adapter, "IpAddressListNext")), "IPAddress")
            $aAdapters[$i][3] = DllStructGetData(DllStructCreate($tagIP_ADDR_STRING, DllStructGetPtr($adapter, "IpAddressListNext")), "IPMask")
            $i += 1
            $ptr = DllStructGetData($adapter, "Next")
            $adapter = DllStructCreate($tagIP_ADAPTER_INFO, $ptr)
        Until @error
        ReDim $aAdapters[$i][4]
    EndIf
    Return $aAdapters
EndFunc   ;==>GetNics
Edited by danwilli
1 person likes this

Share this post


Link to post
Share on other sites
#include <array.au3>

$aTest = GetNics()
_ArrayDisplay($aTest)

Func GetNics()
    Local $NETWORK_REG_KEY = "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Network\{4D36E972-E325-11CE-BFC1-08002BE10318}\"
    Local $tagIP_ADDRESS_STRING = "char IPAddress[16];"
    Local $tagIP_MASK_STRING = "char IPMask[16];"
    Local $tagIP_ADDR_STRING = "ptr Next;" & $tagIP_ADDRESS_STRING & $tagIP_MASK_STRING & "DWORD Context;"
    Local $tagIP_ADAPTER_INFO = "ptr Next; DWORD ComboIndex; char AdapterName[260];char Description[132]; UINT AddressLength; BYTE Address[8]; dword Index; UINT Type;" & _
            " UINT DhcpEnabled; ptr CurrentIpAddress; ptr IpAddressListNext; char IpAddressListADDRESS[16]; char IpAddressListMASK[16]; DWORD IpAddressListContext; " & _
            "ptr GatewayListNext; char GatewayListADDRESS[16]; char GatewayListMASK[16]; DWORD GatewayListContext; " & _
            "ptr DhcpServerNext; char DhcpServerADDRESS[16]; char DhcpServerMASK[16]; DWORD DhcpServerContext; " & _
            "int HaveWins; " & _
            "ptr PrimaryWinsServerNext; char PrimaryWinsServerADDRESS[16]; char PrimaryWinsServerMASK[16]; DWORD PrimaryWinsServerContext; " & _
            "ptr SecondaryWinsServerNext; char SecondaryWinsServerADDRESS[16]; char SecondaryWinsServerMASK[16]; DWORD SecondaryWinsServerContext; " & _
            "DWORD LeaseObtained; DWORD LeaseExpires;"
    Local $dll = DllOpen("Iphlpapi.dll")
    Local $ret = DllCall($dll, "dword", "GetAdaptersInfo", "ptr", 0, "dword*", 0)
    Local $adapterBuffer = DllStructCreate("byte[" & $ret[2] & "]")
    Local $adapterBuffer_pointer = DllStructGetPtr($adapterBuffer)
    Local $return = DllCall($dll, "dword", "GetAdaptersInfo", "ptr", $adapterBuffer_pointer, "dword*", $ret[2])
    Local $adapter = DllStructCreate($tagIP_ADAPTER_INFO, $adapterBuffer_pointer)
    If Not @error Then
        Dim $aAdapters[100][4]
        $i = 0
        Do
            $aAdapters[$i][0] = DllStructGetData($adapter, "Description")
            $aAdapters[$i][1] = RegRead($NETWORK_REG_KEY & DllStructGetData($adapter, "AdapterName") & "\Connection", "Name")
            $aAdapters[$i][2] = DllStructGetData(DllStructCreate($tagIP_ADDR_STRING, DllStructGetPtr($adapter, "IpAddressListNext")), "IPAddress")
            $aAdapters[$i][3] = DllStructGetData(DllStructCreate($tagIP_ADDR_STRING, DllStructGetPtr($adapter, "IpAddressListNext")), "IPMask")
            $i += 1
            $ptr = DllStructGetData($adapter, "Next")
            $adapter = DllStructCreate($tagIP_ADAPTER_INFO, $ptr)
        Until @error
        ReDim $aAdapters[$i][4]
    EndIf
    Return $aAdapters
EndFunc   ;==>GetNics

 

Thanks danwilli!

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
Sign in to follow this  
Followers 0