CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: Get Ip Address

  1. #1
    Join Date
    Jun 1999
    Location
    Georgia
    Posts
    24

    Get Ip Address

    I am looking for a way to retrieve the local machines IP address with out getting it out of the registry.

    Is there an API that I can use

    Any suggestions would be greatly appreciated
    tcompe


  2. #2
    Join Date
    Aug 1999
    Location
    US, Florida
    Posts
    817

    Re: Get Ip Address

    Here I pulled this of somewhere on the internet, it's not mine...
    1) Make a Command1 commandbutton
    2) Make a Text1 textbox
    3) Make a module
    place this code into a module

    option Explicit

    public Const MAX_WSADescription = 256
    public Const MAX_WSASYSStatus = 128
    public Const ERROR_SUCCESS as Long = 0
    public Const WS_VERSION_REQD as Long = &H101
    public Const WS_VERSION_MAJOR as Long = WS_VERSION_REQD \ &H100 And &HFF&
    public Const WS_VERSION_MINOR as Long = WS_VERSION_REQD And &HFF&
    public Const MIN_SOCKETS_REQD as Long = 1
    public Const SOCKET_ERROR as Long = -1

    public Type HOSTENT
    hName as Long
    hAliases as Long
    hAddrType as Integer
    hLen as Integer
    hAddrList as Long
    End Type

    public Type WSADATA
    wVersion as Integer
    wHighVersion as Integer
    szDescription(0 to MAX_WSADescription) as Byte
    szSystemStatus(0 to MAX_WSASYSStatus) as Byte
    wMaxSockets as Integer
    wMaxUDPDG as Integer
    dwVendorInfo as Long
    End Type


    public Declare Function WSAGetLastError Lib "WSOCK32.DLL" () as Long

    public Declare Function WSAStartup Lib "WSOCK32.DLL" _
    (byval wVersionRequired as Long, lpWSADATA as WSADATA) as Long

    public Declare Function WSACleanup Lib "WSOCK32.DLL" () as Long

    public Declare Function gethostname Lib "WSOCK32.DLL" _
    (byval szHost as string, byval dwHostLen as Long) as Long

    public Declare Function gethostbyname Lib "WSOCK32.DLL" _
    (byval szHost as string) as Long

    public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" _
    (hpvDest as Any, byval hpvSource as Long, byval cbCopy as Long)
    public Function GetIPAddress() as string

    Dim sHostName as string * 256
    Dim lpHost as Long
    Dim HOST as HOSTENT
    Dim dwIPAddr as Long
    Dim tmpIPAddr() as Byte
    Dim i as Integer
    Dim sIPAddr as string

    If Not SocketsInitialize() then
    GetIPAddress = ""
    Exit Function
    End If
    If gethostname(sHostName, 256) = SOCKET_ERROR then
    GetIPAddress = ""
    MsgBox "Windows Sockets error " & Str$(WSAGetLastError()) & _
    " has occurred. Unable to successfully get Host Name."
    SocketsCleanup
    Exit Function
    End If
    sHostName = Trim$(sHostName)
    lpHost = gethostbyname(sHostName)

    If lpHost = 0 then
    GetIPAddress = ""
    MsgBox "Windows Sockets are not responding. " & _
    "Unable to successfully get Host Name."
    SocketsCleanup
    Exit Function
    End If
    CopyMemory HOST, lpHost, len(HOST)
    CopyMemory dwIPAddr, HOST.hAddrList, 4
    ReDim tmpIPAddr(1 to HOST.hLen)
    CopyMemory tmpIPAddr(1), dwIPAddr, HOST.hLen
    for i = 1 to HOST.hLen
    sIPAddr = sIPAddr & tmpIPAddr(i) & "."
    next
    GetIPAddress = mid$(sIPAddr, 1, len(sIPAddr) - 1)

    SocketsCleanup

    End Function
    public Function GetIPHostName() as string

    Dim sHostName as string * 256

    If Not SocketsInitialize() then
    GetIPHostName = ""
    Exit Function
    End If

    If gethostname(sHostName, 256) = SOCKET_ERROR then
    GetIPHostName = ""
    MsgBox "Windows Sockets error " & Str$(WSAGetLastError()) & _
    " has occurred. Unable to successfully get Host Name."
    SocketsCleanup
    Exit Function
    End If

    GetIPHostName = Left$(sHostName, InStr(sHostName, Chr(0)) - 1)
    SocketsCleanup

    End Function
    public Function HiByte(byval wParam as Integer)

    HiByte = wParam \ &H100 And &HFF&

    End Function
    public Function LoByte(byval wParam as Integer)

    LoByte = wParam And &HFF&

    End Function
    public Sub SocketsCleanup()

    If WSACleanup() <> ERROR_SUCCESS then
    MsgBox "Socket error occurred in Cleanup."
    End If

    End Sub

    public Function SocketsInitialize() as Boolean

    Dim WSAD as WSADATA
    Dim sLoByte as string
    Dim sHiByte as string

    If WSAStartup(WS_VERSION_REQD, WSAD) <> ERROR_SUCCESS then
    MsgBox "The 32-bit Windows Socket is not responding."
    SocketsInitialize = false
    Exit Function
    End If


    If WSAD.wMaxSockets < MIN_SOCKETS_REQD then
    MsgBox "This application requires a minimum of " & _
    CStr(MIN_SOCKETS_REQD) & " supported sockets."

    SocketsInitialize = false
    Exit Function
    End If


    If LoByte(WSAD.wVersion) < WS_VERSION_MAJOR Or _
    (LoByte(WSAD.wVersion) = WS_VERSION_MAJOR And _
    HiByte(WSAD.wVersion) < WS_VERSION_MINOR) then

    sHiByte = CStr(HiByte(WSAD.wVersion))
    sLoByte = CStr(LoByte(WSAD.wVersion))

    MsgBox "Sockets version " & sLoByte & "." & sHiByte & _
    " is not supported by 32-bit Windows Sockets."

    SocketsInitialize = false
    Exit Function

    End If
    SocketsInitialize = true
    End Function



    place this code into command1_click

    Text1 = GetIPAddress









  3. #3
    Join Date
    Sep 1999
    Location
    Red Wing, MN USA
    Posts
    312

    Re: Get Ip Address

    Alternatively..

    Stick a Winsock Control on Your Form and Read the LocalIP Property.

    Aaron Young
    Analyst Programmer
    [email protected]
    [email protected]
    Aaron Young
    Senior Programmer Analyst (Red Wing Software)
    Certified AllExperts Expert

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured