Sign in to follow this  
Followers 0
hm2k

_GetLocalIP() function

3 posts in this topic

Sometimes @IPAddress1 @IPAddress2 @IPAddress3 @IPAddress4 aren't enough.

I created this function to get a local IP address of a network interface.

You can specify a variable that will offset the result to another interface.

Func _GetLocalIP($offset=1)
    Local $ip, $k, $i=0, $r=1
    $k='HKLM\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces'
    While($r)
        $i=$i+1
        $r=RegEnumKey($k,$i)
        If (@error <> 0) Then
            $r=0
        EndIf
        $ip=RegRead($k & '\' & $r,'IPAddress')
        If (($ip) And ($ip = '0.0.0.0')) Then
            $ip=RegRead($k & '\' & $r,'DhcpIPAddress')
        EndIf
        If (($ip) And Not ($ip = '0.0.0.0')) Then
            If ($i == $offset) Then
                Return $ip
            EndIf
        EndIf
    WEnd
    SetError(1)
    Return -1
EndFunc

;Testing
$ip=_GetLocalIP()
InputBox('Testing', 'Your Local IP Address...', $ip)

Share this post


Link to post
Share on other sites



Sometimes @IPAddress1 @IPAddress2 @IPAddress3 @IPAddress4 aren't enough.

I created this function to get a local IP address of a network interface.

You can specify a variable that will offset the result to another interface.

Func _GetLocalIP($offset=1)
    Local $ip, $k, $i=0, $r=1
    $k='HKLM\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces'
    While($r)
        $i=$i+1
        $r=RegEnumKey($k,$i)
        If (@error <> 0) Then
            $r=0
        EndIf
        $ip=RegRead($k & '\' & $r,'IPAddress')
        If (($ip) And ($ip = '0.0.0.0')) Then
            $ip=RegRead($k & '\' & $r,'DhcpIPAddress')
        EndIf
        If (($ip) And Not ($ip = '0.0.0.0')) Then
            If ($i == $offset) Then
                Return $ip
            EndIf
        EndIf
    WEnd
    SetError(1)
    Return -1
EndFunc

;Testing
$ip=_GetLocalIP()
InputBox('Testing', 'Your Local IP Address...', $ip)
How do you know what offset to use? I need to use 2 to get my IP, or at least I do at the moment.

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

Share this post


Link to post
Share on other sites

What does 1 return? 1 should return the first network interface IP address instance, 2 should return the second.

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