• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 244
  • Last Modified:

Find IP of me

I am looking for the VB6 code to determine the IP  address of the local machine
Thanks,
John
0
JohnFra
Asked:
JohnFra
1 Solution
 
BrownheadCommented:
This is for the external IP address

'//Code\\
'This code requires an INet ciontrol named INet
Private Function GetIP() as String
GetIP = Split(Split(INet.OpenURL("http://www.whatismyip.com"), "displaycopy('")(1), "'")(0)
End Function
'\\Code//

And to get your Local IP Address

'//Code\\
'This requires a WinSock control named Sck
Private Function GetIPLocal() as String
GetIPLocal=Sck.LocalIP
End Function
'\\Code//
0
 
BrianGEFF719Commented:
Here is an example that doesn't require INET Controls:

http://vbnet.mvps.org/index.html?code/internet/getpublicip.htm
0
 
JackOfPHCommented:
here is a complete tutorial using winsock.
http://www.vbip.com/winsock-api/default.asp
you will learn things about it. It`s a nice complete tutorial. I hope it helps you.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
AbhishekSharmaCommented:
Get HostName/IP Using API ==>

Place all the code in a form with a command1 command button. It will produce two msgboxes, one with the hostname and the other with the ip.

Private Const WS_VERSION_REQD = &H101
  Private Const WS_VERSION_MAJOR = WS_VERSION_REQD \ &H100 And &HFF&
  Private Const WS_VERSION_MINOR = WS_VERSION_REQD And &HFF&
  Private Const MIN_SOCKETS_REQD = 1
  Private Const SOCKET_ERROR = -1
  Private Const WSADescription_Len = 256
  Private Const WSASYS_Status_Len = 128

  Private Type HOSTENT
      hName As Long
      hAliases As Long
      hAddrType As Integer
      hLength As Integer
      hAddrList As Long
  End Type

  Private Type WSADATA
      wversion As Integer
      wHighVersion As Integer
      szDescription(0 To WSADescription_Len) As Byte
      szSystemStatus(0 To WSASYS_Status_Len) As Byte
      iMaxSockets As Integer
      iMaxUdpDg As Integer
      lpszVendorInfo As Long
  End Type

  Private Declare Function WSAGetLastError Lib "WSOCK32.DLL" () As Long
  Private Declare Function WSAStartup Lib "WSOCK32.DLL" (ByVal _
  wVersionRequired as Integer, lpWSAData As WSADATA) As Long
  Private Declare Function WSACleanup Lib "WSOCK32.DLL" () As Long
 
  Private Declare Function gethostname Lib "WSOCK32.DLL" (ByVal hostname$,_
  ByVal HostLen as Long) as long
  Private Declare Function gethostbyname Lib "WSOCK32.DLL" (ByVal _
  hostname$) As Long
  Private Declare Sub RtlMoveMemory Lib "KERNEL32" (hpvDest As Any, ByVal_
  hpvSource&, ByVal cbCopy&)

  Function hibyte(ByVal wParam As Integer)

      hibyte = wParam \ &H100 And &HFF&

  End Function

  Function lobyte(ByVal wParam As Integer)

      lobyte = wParam And &HFF&

  End Function

  Sub SocketsInitialize()
  Dim WSAD As WSADATA
  Dim iReturn As Integer
  Dim sLowByte As String, sHighByte As String, sMsg As String

      iReturn = WSAStartup(WS_VERSION_REQD, WSAD)

      If iReturn <> 0 Then
          MsgBox "Winsock.dll is not responding."
          End
      End If

      If lobyte(WSAD.wversion) <WS_VERSION_MAJOR Or (lobyte(WSAD.wversion) = _
          WS_VERSION_MAJOR And hibyte(WSAD.wversion) < WS_VERSION_MINOR) Then

          sHighByte = Trim$(Str$(hibyte(WSAD.wversion)))
          sLowByte = Trim$(Str$(lobyte(WSAD.wversion)))
          sMsg = "Windows Sockets version " & sLowByte & "." & sHighByte
          sMsg = sMsg & " is not supported by winsock.dll "
          MsgBox sMsg
          End
      End If

      'iMaxSockets is not used in winsock 2. So the following check is only
      'necessary for winsock 1. If winsock 2 is requested,
      'the following check can be skipped.

      If WSAD.iMaxSockets < MIN_SOCKETS_REQD Then
          sMsg = "This application requires a minimum of "
          sMsg = sMsg & Trim$(Str$(MIN_SOCKETS_REQD)) & " supported sockets."
          MsgBox sMsg
          End
      End If

  End Sub

  Sub SocketsCleanup()
  Dim lReturn As Long

      lReturn = WSACleanup()

      If lReturn <> 0 Then
          MsgBox "Socket error " & Trim$(Str$(lReturn)) & " occurred in Cleanup "
          End
      End If

  End Sub

  Sub Form_Load()

      SocketsInitialize

  End Sub

  Private Sub Form_Unload(Cancel As Integer)

      SocketsCleanup

  End Sub

  Private Sub Command1_click()
  Dim hostname As String * 256
  Dim hostent_addr As Long
  Dim host As HOSTENT
  Dim hostip_addr As Long
  Dim temp_ip_address() As Byte
  Dim i As Integer
  Dim ip_address As String

      If gethostname(hostname, 256) = SOCKET_ERROR Then
          MsgBox "Windows Sockets error " & Str(WSAGetLastError())
          Exit Sub
      Else
          hostname = Trim$(hostname)
      End If

      hostent_addr = gethostbyname(hostname)

      If hostent_addr = 0 Then
          MsgBox "Winsock.dll is not responding."
          Exit Sub
      End If

      RtlMoveMemory host, hostent_addr, LenB(host)
      RtlMoveMemory hostip_addr, host.hAddrList, 4

      MsgBox hostname

      'get all of the IP address if machine is  multi-homed

      Do
          ReDim temp_ip_address(1 To host.hLength)
          RtlMoveMemory temp_ip_address(1), hostip_addr, host.hLength

          For i = 1 To host.hLength
              ip_address = ip_address & temp_ip_address(i) & "."
          Next
          ip_address = Mid$(ip_address, 1, Len(ip_address) - 1)

          MsgBox ip_address

          ip_address = ""
          host.hAddrList = host.hAddrList + LenB(host.hAddrList)
          RtlMoveMemory hostip_addr, host.hAddrList, 4
       Loop While (hostip_addr <> 0)

  End Sub


'****************************************************
'Code Copied from   http://www.developerfusion.co.uk/show/1851/
'****************************************************
0
 
BrianGEFF719Commented:
AbhishekSharma : That code will only provide the localhost name/address, its of no use to anyone who is behind a router.

-Brian
0
 
bmatumburaCommented:
Please send me your e-mail address at bmatumbura@hotmail.com then I can send you a set of classes I wrote that will show you all the private and public IP addresses on your system, including the Index, Subnet Mask, Broadcast Address etc.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now