Results 1 to 2 of 2

Thread: Finding the IP with VB?

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2001
    Posts
    421

    Question Finding the IP with VB?

    How can I get the IP address using VB? I was thinking GetSysInfo but that didn't work, any ideas? Thanks.
    [vbcode]
    ' comment
    Rem remark
    [/vbcode]

  2. #2
    PowerPoster MidgetsBro's Avatar
    Join Date
    Oct 2000
    Location
    Apparently, Internet.com
    Posts
    3,125
    I think this should do it:
    VB Code:
    1. 'in a module
    2. Public Const WS_VERSION_REQD = &H101
    3. Public Const WS_VERSION_MAJOR = WS_VERSION_REQD \ &H100 And &HFF&
    4. Public Const WS_VERSION_MINOR = WS_VERSION_REQD And &HFF&
    5. Public Const MIN_SOCKETS_REQD = 1
    6. Public Const SOCKET_ERROR = -1
    7. Public Const WSADescription_Len = 256
    8. Public Const WSASYS_Status_Len = 128
    9.  
    10. Public Type HOSTENT
    11.     hName As Long
    12.     hAliases As Long
    13.     hAddrType As Integer
    14.     hLength As Integer
    15.     hAddrList As Long
    16. End Type
    17.  
    18. Public Type WSADATA
    19.     wversion As Integer
    20.     wHighVersion As Integer
    21.     szDescription(0 To WSADescription_Len) As Byte
    22.     szSystemStatus(0 To WSASYS_Status_Len) As Byte
    23.     iMaxSockets As Integer
    24.     iMaxUdpDg As Integer
    25.     lpszVendorInfo As Long
    26. End Type
    27.  
    28. Public Declare Function WSAGetLastError Lib "WSOCK32.DLL" () As Long
    29. Public Declare Function WSAStartup Lib "WSOCK32.DLL" (ByVal wVersionRequired&, lpWSAData As WSADATA) As Long
    30. Public Declare Function WSACleanup Lib "WSOCK32.DLL" () As Long
    31. Public Declare Function gethostname Lib "WSOCK32.DLL" (ByVal hostname$, ByVal HostLen As Long) As Long
    32. Public Declare Function gethostbyname Lib "WSOCK32.DLL" (ByVal hostname$) As Long
    33. Public Declare Sub RtlMoveMemory Lib "kernel32" (hpvDest As Any, ByVal hpvSource&, ByVal cbCopy&)
    34.  
    35. Function hibyte(ByVal wParam As Integer)
    36.     hibyte = wParam \ &H100 And &HFF&
    37. End Function
    38.  
    39.  
    40. Function lobyte(ByVal wParam As Integer)
    41.     lobyte = wParam And &HFF&
    42. End Function
    43.  
    44.  
    45. Sub SocketsInitialize()
    46.     Dim WSAD As WSADATA
    47.     Dim iReturn As Integer
    48.     Dim sLowByte As String, sHighByte As String, sMsg As String
    49.     iReturn = WSAStartup(WS_VERSION_REQD, WSAD)
    50.  
    51.  
    52.     If iReturn <> 0 Then
    53.         MsgBox "Winsock.dll Error."
    54.         End
    55.     End If
    56.     If lobyte(WSAD.wversion) < WS_VERSION_MAJOR Or (lobyte(WSAD.wversion) = _
    57.         WS_VERSION_MAJOR And hibyte(WSAD.wversion) < WS_VERSION_MINOR) Then
    58.         sHighByte = Trim$(Str$(hibyte(WSAD.wversion)))
    59.         sLowByte = Trim$(Str$(lobyte(WSAD.wversion)))
    60.         sMsg = "Windows Sockets version " & sLowByte & "." & sHighByte
    61.         'sMsg = sMsg & " winsock.dll tarafindan desteklenmiyor. "
    62.         MsgBox sMsg
    63.         End
    64.     End If
    65.  
    66. End Sub
    67.  
    68. Public Function CurrentIP(ReturnExternalIP As Boolean)
    69.     Dim hostname As String * 256
    70.     Dim hostent_addr As Long
    71.     Dim host As HOSTENT
    72.     Dim hostip_addr As Long
    73.     Dim temp_ip_address() As Byte
    74.     Dim i As Integer
    75.     Dim ip_address As String
    76.     Dim IP As String
    77.  
    78.     If gethostname(hostname, 256) = SOCKET_ERROR Then
    79.         MsgBox "Windows Socket Error " & Str(WSAGetLastError())
    80.         Exit Function
    81.     Else
    82.         hostname = Trim$(hostname)
    83.     End If
    84.     hostent_addr = gethostbyname(hostname)
    85.  
    86.  
    87.     If hostent_addr = 0 Then
    88.         MsgBox "Winsock.dll error."
    89.         Exit Function
    90.     End If
    91.     RtlMoveMemory host, hostent_addr, LenB(host)
    92.     RtlMoveMemory hostip_addr, host.hAddrList, 4
    93.    
    94.     Do
    95.         ReDim temp_ip_address(1 To host.hLength)
    96.         RtlMoveMemory temp_ip_address(1), hostip_addr, host.hLength
    97.  
    98.  
    99.         For i = 1 To host.hLength
    100.             ip_address = ip_address & temp_ip_address(i) & "."
    101.         Next
    102.         ip_address = Mid$(ip_address, 1, Len(ip_address) - 1)
    103.        
    104.         Internal = TheIP        ' Send ONLY the External IP to the CurrentIP Function
    105.         EXTERNAL = ip_address   ' Send the External IP to the  function parameter External
    106.         TheIP = ip_address      ' Send LAN IP to the function para Internal
    107.      
    108.         ip_address = ""
    109.         host.hAddrList = host.hAddrList + LenB(host.hAddrList)
    110.         RtlMoveMemory hostip_addr, host.hAddrList, 4
    111.     Loop While (hostip_addr <> 0)
    112.    
    113.    
    114. If ReturnExternalIP = True Then
    115.     CurrentIP = EXTERNAL
    116. Else
    117.     CurrentIP = Internal
    118. End If
    119. End Function
    120.  
    121. Sub SocketsCleanup()
    122.     Dim lReturn As Long
    123.     lReturn = WSACleanup()
    124.  
    125.  
    126.     If lReturn <> 0 Then
    127.         MsgBox "Socket Error " & Trim$(Str$(lReturn)) & " occurred In Cleanup "
    128.         End
    129.     End If
    130. End Sub
    131.  
    132. 'on the form
    133. Private Sub Command1_Click()
    134.     SocketsInitialize
    135.     YourIP = CurrentIP(True)
    136.     msgbox YourIP
    137. End Sub

    This will return you true, external IP address. If you have two network cards, or a network card and a dial-up adapter, then you need to use the above. Otherwise, you can use this:

    VB Code:
    1. 'add a winsock control: Winsock1
    2. msgbox winsock1.LocalIP
    Last edited by MidgetsBro; Jun 21st, 2001 at 02:51 PM.
    <removed by admin>

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