Results 1 to 15 of 15

Thread: Get IP address and computer name

  1. #1

    Thread Starter
    Fanatic Member daimous's Avatar
    Join Date
    Aug 2005
    Posts
    657

    Get IP address and computer name

    hi! how can i get the Ip address and name of the computer where the application is running? thanks in advance!

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Get IP address and computer name

    Use the GetComputerName API.

    VB Code:
    1. Private Declare Function GetComputerName Lib "kernel32.dll" Alias "GetComputerNameA" (ByVal lpBuffer As String, ByRef nSize As Long) As Long
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  3. #3

    Thread Starter
    Fanatic Member daimous's Avatar
    Join Date
    Aug 2005
    Posts
    657

    Re: Get IP address and computer name

    thanks for that..but how can i display the ipaddress?

  4. #4
    Just Married shakti5385's Avatar
    Join Date
    Mar 2006
    Location
    Udaipur,Rajasthan(INDIA)
    Posts
    3,747

    Thumbs up Re: Get IP address and computer name

    VB Code:
    1. Option Explicit
    2.  
    3. Private Const WS_VERSION_REQD = &H101
    4. Private Const WS_VERSION_MAJOR = WS_VERSION_REQD \ &H100 And &HFF&
    5. Private Const WS_VERSION_MINOR = WS_VERSION_REQD And &HFF&
    6. Private Const MIN_SOCKETS_REQD = 1
    7. Private Const SOCKET_ERROR = -1
    8. Private Const WSADescription_Len = 256
    9. Private Const WSASYS_Status_Len = 128
    10.  
    11. Private Type HOSTENT
    12.     hName As Long
    13.     hAliases As Long
    14.     hAddrType As Integer
    15.     hLength As Integer
    16.     hAddrList As Long
    17. End Type
    18.  
    19. Private Type WSADATA
    20.     wversion As Integer
    21.     wHighVersion As Integer
    22.     szDescription(0 To WSADescription_Len) As Byte
    23.     szSystemStatus(0 To WSASYS_Status_Len) As Byte
    24.     iMaxSockets As Integer
    25.     iMaxUdpDg As Integer
    26.     lpszVendorInfo As Long
    27. End Type
    28.  
    29. Private Declare Function WSAGetLastError Lib "WSOCK32.DLL" () As Long
    30. Private Declare Function WSAStartup Lib "WSOCK32.DLL" (ByVal wVersionRequired As Integer, lpWSAData As WSADATA) As Long
    31. Private Declare Function WSACleanup Lib "WSOCK32.DLL" () As Long
    32.  
    33. Private Declare Function gethostname Lib "WSOCK32.DLL" (ByVal hostname$, ByVal HostLen As Long) As Long
    34. Private Declare Function gethostbyname Lib "WSOCK32.DLL" (ByVal hostname$) As Long
    35. Private Declare Sub RtlMoveMemory Lib "KERNEL32" (hpvDest As Any, ByVal hpvSource&, ByVal cbCopy&)
    36. Function hibyte(ByVal wParam As Integer)
    37.     hibyte = wParam \ &H100 And &HFF&
    38. End Function
    39.  
    40. Function lobyte(ByVal wParam As Integer)
    41.     lobyte = wParam And &HFF&
    42. End Function
    43.  
    44. ' Return the local host's name.
    45. Private Function LocalHostName() As String
    46. Dim hostname As String * 256
    47.  
    48.     If gethostname(hostname, 256) = SOCKET_ERROR Then
    49.         LocalHostName = "<Error>"
    50.     Else
    51.         LocalHostName = Trim$(hostname)
    52.     End If
    53. End Function
    54. Private Sub InitializeSockets()
    55. Dim WSAD As WSADATA
    56. Dim iReturn As Integer
    57. Dim sLowByte As String, sHighByte As String, sMsg As String
    58.  
    59.     iReturn = WSAStartup(WS_VERSION_REQD, WSAD)
    60.  
    61.     If iReturn <> 0 Then
    62.         MsgBox "Winsock.dll is not responding."
    63.         End
    64.     End If
    65.  
    66.     If lobyte(WSAD.wversion) < WS_VERSION_MAJOR Or (lobyte(WSAD.wversion) = _
    67.         WS_VERSION_MAJOR And hibyte(WSAD.wversion) < WS_VERSION_MINOR) Then
    68.  
    69.         sHighByte = Trim$(Str$(hibyte(WSAD.wversion)))
    70.         sLowByte = Trim$(Str$(lobyte(WSAD.wversion)))
    71.         sMsg = "Windows Sockets version " & sLowByte & "." & sHighByte
    72.         sMsg = sMsg & " is not supported by winsock.dll "
    73.         MsgBox sMsg
    74.         End
    75.     End If
    76.  
    77.     'iMaxSockets is not used in winsock 2. So the following check is only
    78.     'necessary for winsock 1. If winsock 2 is requested,
    79.     'the following check can be skipped.
    80.  
    81.     If WSAD.iMaxSockets < MIN_SOCKETS_REQD Then
    82.         sMsg = "This application requires a minimum of "
    83.         sMsg = sMsg & Trim$(Str$(MIN_SOCKETS_REQD)) & " supported sockets."
    84.         MsgBox sMsg
    85.         End
    86.     End If
    87.  
    88. End Sub
    89.  
    90. Private Sub CleanupSockets()
    91. Dim lReturn As Long
    92.  
    93.     lReturn = WSACleanup()
    94.  
    95.     If lReturn <> 0 Then
    96.         MsgBox "Socket error " & Trim$(Str$(lReturn)) & " occurred in Cleanup "
    97.         End
    98.     End If
    99.  
    100. End Sub
    101.  
    102. Private Sub cmdGetIPAddress_Click()
    103.     txtIPAddress.Text = IPAddressFromHostName(txtHostName.Text)
    104. End Sub
    105.  
    106. Private Sub Form_Load()
    107.     ' Initialize the sockets library.
    108.     InitializeSockets
    109.  
    110.     ' Display the local host's name.
    111.     txtHostName = LocalHostName()
    112. End Sub
    113. Private Sub Form_Unload(Cancel As Integer)
    114.     ' Clean up the sockets library.
    115.     CleanupSockets
    116. End Sub
    117. ' Return this host's IP address.
    118. Private Function IPAddressFromHostName(ByVal hostname As String) As String
    119. Dim hostent_addr As Long
    120. Dim host As HOSTENT
    121. Dim hostip_addr As Long
    122. Dim temp_ip_address() As Byte
    123. Dim i As Integer
    124. Dim ip_address As String
    125. Dim result As String
    126.  
    127.     hostent_addr = gethostbyname(hostname)
    128.     If hostent_addr = 0 Then
    129.         IPAddressFromHostName = "<error>"
    130.         Exit Function
    131.     End If
    132.  
    133.     RtlMoveMemory host, hostent_addr, LenB(host)
    134.     RtlMoveMemory hostip_addr, host.hAddrList, 4
    135.  
    136.     ' Get multiple pieces of the IP address
    137.     ' if machine is multi-homed.
    138.     Do
    139.         ReDim temp_ip_address(1 To host.hLength)
    140.         RtlMoveMemory temp_ip_address(1), hostip_addr, host.hLength
    141.  
    142.         For i = 1 To host.hLength
    143.             ip_address = ip_address & temp_ip_address(i) & "."
    144.         Next
    145.  
    146.         ip_address = Mid$(ip_address, 1, Len(ip_address) - 1)
    147.         result = result & ip_address & vbCrLf
    148.         ip_address = ""
    149.  
    150.         host.hAddrList = host.hAddrList + LenB(host.hAddrList)
    151.         RtlMoveMemory hostip_addr, host.hAddrList, 4
    152.     Loop While (hostip_addr <> 0)
    153.  
    154.     ' Remove the last vbCrLf.
    155.     If Len(result) > 0 Then result = Left$(result, Len(result) - Len(vbCrLf))
    156.  
    157.     IPAddressFromHostName = result
    158. End Function





    'make a form draw2 follow and paste above code
    'Command Button Name cmdGetIPadd
    'Textbox1 name txtHostName
    'Textbox2 name txtIPAddress


    'make a form draw2 follow and paste above code
    'Command Button Name cmdGetIPadd
    'Textbox1 name txtHostName
    'Textbox2 name txtIPAddress

  5. #5

    Thread Starter
    Fanatic Member daimous's Avatar
    Join Date
    Aug 2005
    Posts
    657

    Re: Get IP address and computer name

    Thanks for that..but the code is too complicated...is there easier way to retrive the ip address and name of the computer?..i think the idea/code of RobDog888 is more easier,hope it will work, but how can i display the ip address and name RobDog888?

  6. #6
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Get IP address and computer name

    Hers an example of how to use it. this is just for getting the machine name.

    http://vbforums.com/showpost.php?p=1447037&postcount=3
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  7. #7
    Fanatic Member schoolbusdriver's Avatar
    Join Date
    Jan 2006
    Location
    O'er yonder
    Posts
    1,020

    Re: Get IP address and computer name

    shakti5385 - Command Button Name "cmdGetIPadd" should be "cmdGetIPAddress"

  8. #8
    Just Married shakti5385's Avatar
    Join Date
    Mar 2006
    Location
    Udaipur,Rajasthan(INDIA)
    Posts
    3,747

    Thumbs down Re: Get IP address and computer name

    Quote Originally Posted by schoolbusdriver
    shakti5385 - Command Button Name "cmdGetIPadd" should be "cmdGetIPAddress"

    thanks
    but remember there is need of command button

    if mistake in code then pass argument

  9. #9
    Fanatic Member schoolbusdriver's Avatar
    Join Date
    Jan 2006
    Location
    O'er yonder
    Posts
    1,020

    Re: Get IP address and computer name

    Sorry The code is fine. You can also do this:-
    VB Code:
    1. Private Sub Form_Load()
    2.    InitializeSockets ' Initialize the sockets library.
    3.    txtHostName = LocalHostName() ' Display the local host name.
    4.    txtIPAddress.Text = IPAddressFromHostName(txtHostName.Text)' Show IP.
    5. End Sub

  10. #10
    Just Married shakti5385's Avatar
    Join Date
    Mar 2006
    Location
    Udaipur,Rajasthan(INDIA)
    Posts
    3,747

    Re: Get IP address and computer name

    its enough thanks

  11. #11
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Get IP address and computer name

    Why not use good ole Winsock
    VB Code:
    1. Text1.Text = Winsock1.LocalIp

  12. #12
    PowerPoster
    Join Date
    Feb 2006
    Location
    East of NYC, USA
    Posts
    5,691

    Re: Get IP address and computer name

    One thing to bear in mind - all of those get the local address - if the computer is behind a router the address will be 10.x.x.x, 172.18.x.x or 192.x.x.x, regardless of the external address.
    The most difficult part of developing a program is understanding the problem.
    The second most difficult part is deciding how you're going to solve the problem.
    Actually writing the program (translating your solution into some computer language) is the easiest part.

    Please indent your code and use [HIGHLIGHT="VB"] [/HIGHLIGHT] tags around it to make it easier to read.

    Please Help Us To Save Ana

  13. #13
    New Member
    Join Date
    Dec 2019
    Posts
    3

    Re: Get IP address and computer name

    if i have 2 ip address for a host name how to take only one IP ?..
    Last edited by subramanian.hari; Jul 10th, 2021 at 01:03 PM.

  14. #14
    New Member
    Join Date
    Dec 2019
    Posts
    3

    Re: Get IP address and computer name

    if i have 2 ipaddress for 1 hostname how to fetch only one?..

  15. #15
    PowerPoster wqweto's Avatar
    Join Date
    May 2011
    Location
    Sofia, Bulgaria
    Posts
    6,183

    Re: Get IP address and computer name

    Quote Originally Posted by subramanian.hari View Post
    if i have 2 ipaddress for 1 hostname how to fetch only one?..
    Take both and discard second.

    cheers,
    </wqw>

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