Jump to content

_GetLocalIP() function


hm2k
 Share

Recommended Posts

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)
Link to comment
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.
Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...