Results 1 to 16 of 16

Thread: Ping

  1. #1

    Thread Starter
    Addicted Member Psychotic's Avatar
    Join Date
    May 2004
    Posts
    164

    Ping

    How would one go about pinging a computer on a local LAN in vb6?
    -Psychotic-

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

    Re: Ping

    VB Code:
    1. 'Place a Listbox, a command button, and a text box on your form.
    2. 'Call the command button cmdPing and the textbox txtPing
    3.  
    4. Private Const IP_STATUS_BASE = 11000
    5. Private Const IP_SUCCESS = 0
    6. Private Const IP_BUF_TOO_SMALL = (11000 + 1)
    7. Private Const IP_DEST_NET_UNREACHABLE = (11000 + 2)
    8. Private Const IP_DEST_HOST_UNREACHABLE = (11000 + 3)
    9. Private Const IP_DEST_PROT_UNREACHABLE = (11000 + 4)
    10. Private Const IP_DEST_PORT_UNREACHABLE = (11000 + 5)
    11. Private Const IP_NO_RESOURCES = (11000 + 6)
    12. Private Const IP_BAD_OPTION = (11000 + 7)
    13. Private Const IP_HW_ERROR = (11000 + 8)
    14. Private Const IP_PACKET_TOO_BIG = (11000 + 9)
    15. Private Const IP_REQ_TIMED_OUT = (11000 + 10)
    16. Private Const IP_BAD_REQ = (11000 + 11)
    17. Private Const IP_BAD_ROUTE = (11000 + 12)
    18. Private Const IP_TTL_EXPIRED_TRANSIT = (11000 + 13)
    19. Private Const IP_TTL_EXPIRED_REASSEM = (11000 + 14)
    20. Private Const IP_PARAM_PROBLEM = (11000 + 15)
    21. Private Const IP_SOURCE_QUENCH = (11000 + 16)
    22. Private Const IP_OPTION_TOO_BIG = (11000 + 17)
    23. Private Const IP_BAD_DESTINATION = (11000 + 18)
    24. Private Const IP_ADDR_DELETED = (11000 + 19)
    25. Private Const IP_SPEC_MTU_CHANGE = (11000 + 20)
    26. Private Const IP_MTU_CHANGE = (11000 + 21)
    27. Private Const IP_UNLOAD = (11000 + 22)
    28. Private Const IP_ADDR_ADDED = (11000 + 23)
    29. Private Const IP_GENERAL_FAILURE = (11000 + 50)
    30. Private Const MAX_IP_STATUS = 11000 + 50
    31. Private Const IP_PENDING = (11000 + 255)
    32. Private Const PING_TIMEOUT = 200
    33. Private Const WS_VERSION_REQD = &H101
    34. Private Const WS_VERSION_MAJOR = WS_VERSION_REQD \ &H100 And &HFF&
    35. Private Const WS_VERSION_MINOR = WS_VERSION_REQD And &HFF&
    36. Private Const MIN_SOCKETS_REQD = 1
    37. Private Const SOCKET_ERROR = -1
    38. Private Const MAX_WSADescription = 256
    39. Private Const MAX_WSASYSStatus = 128
    40.  
    41. Private Type ICMP_OPTIONS
    42.     Ttl             As Byte
    43.     Tos             As Byte
    44.     Flags           As Byte
    45.     OptionsSize     As Byte
    46.     OptionsData     As Long
    47. End Type
    48.  
    49. Dim ICMPOPT As ICMP_OPTIONS
    50.  
    51. Private Type ICMP_ECHO_REPLY
    52.     Address         As Long
    53.     status          As Long
    54.     RoundTripTime   As Long
    55.     DataSize        As Integer
    56.     Reserved        As Integer
    57.     DataPointer     As Long
    58.     Options         As ICMP_OPTIONS
    59.     Data            As String * 250
    60. End Type
    61.  
    62. Private Type HOSTENT
    63.     hName As Long
    64.     hAliases As Long
    65.     hAddrType As Integer
    66.     hLen As Integer
    67.     hAddrList As Long
    68. End Type
    69.  
    70. Private Type WSADATA
    71.     wVersion As Integer
    72.     wHighVersion As Integer
    73.     szDescription(0 To MAX_WSADescription) As Byte
    74.     szSystemStatus(0 To MAX_WSASYSStatus) As Byte
    75.     wMaxSockets As Integer
    76.     wMaxUDPDG As Integer
    77.     dwVendorInfo As Long
    78. End Type
    79.  
    80. Private Declare Function IcmpCreateFile Lib "icmp.dll" () As Long
    81.  
    82. Private Declare Function IcmpCloseHandle Lib "icmp.dll" _
    83.    (ByVal IcmpHandle As Long) As Long
    84.    
    85. Private Declare Function IcmpSendEcho Lib "icmp.dll" _
    86.    (ByVal IcmpHandle As Long, _
    87.     ByVal DestinationAddress As Long, _
    88.     ByVal RequestData As String, _
    89.     ByVal RequestSize As Integer, _
    90.     ByVal RequestOptions As Long, _
    91.     ReplyBuffer As ICMP_ECHO_REPLY, _
    92.     ByVal ReplySize As Long, _
    93.     ByVal Timeout As Long) As Long
    94.    
    95. Private Declare Function WSAGetLastError Lib "WSOCK32.DLL" () As Long
    96.  
    97. Private Declare Function WSAStartup Lib "WSOCK32.DLL" _
    98.    (ByVal wVersionRequired As Long, _
    99.     lpWSADATA As WSADATA) As Long
    100.    
    101. Private Declare Function WSACleanup Lib "WSOCK32.DLL" () As Long
    102.  
    103. Private Declare Function gethostname Lib "WSOCK32.DLL" _
    104.    (ByVal szHost As String, _
    105.     ByVal dwHostLen As Long) As Long
    106.    
    107. Private Declare Function gethostbyname Lib "WSOCK32.DLL" _
    108.    (ByVal szHost As String) As Long
    109.    
    110. Private Declare Sub RtlMoveMemory Lib "kernel32" _
    111.    (hpvDest As Any, _
    112.     ByVal hpvSource As Long, _
    113.     ByVal cbCopy As Long)
    114.  
    115. Private Function AddressStringToLong(ByVal tmp As String) As Long
    116.  
    117.    Dim i As Integer
    118.    Dim parts(1 To 4) As String
    119.    
    120.    i = 0
    121.  
    122.    While InStr(tmp, ".") > 0
    123.       i = i + 1
    124.       parts(i) = Mid(tmp, 1, InStr(tmp, ".") - 1)
    125.       tmp = Mid(tmp, InStr(tmp, ".") + 1)
    126.    Wend
    127.    
    128.    i = i + 1
    129.    parts(i) = tmp
    130.    
    131.    If i <> 4 Then
    132.       AddressStringToLong = 0
    133.       Exit Function
    134.    End If
    135.  
    136.    AddressStringToLong = Val("&H" & Right("00" & Hex(parts(4)), 2) & _
    137.                          Right("00" & Hex(parts(3)), 2) & _
    138.                          Right("00" & Hex(parts(2)), 2) & _
    139.                          Right("00" & Hex(parts(1)), 2))
    140.    
    141. End Function
    142.  
    143. Private Function GetStatusCode(status As Long) As String
    144.  
    145.    Dim msg As String
    146.  
    147.    Select Case status
    148.       Case IP_SUCCESS:               msg = txtPing.Text & " is replying"
    149.       Case IP_BUF_TOO_SMALL:         msg = "IP buf too_small"
    150.       Case IP_DEST_NET_UNREACHABLE:  msg = "IP dest net unreachable"
    151.       Case IP_DEST_HOST_UNREACHABLE: msg = "IP dest host unreachable"
    152.       Case IP_DEST_PROT_UNREACHABLE: msg = "IP dest prot unreachable"
    153.       Case IP_DEST_PORT_UNREACHABLE: msg = "IP dest port unreachable"
    154.       Case IP_NO_RESOURCES:          msg = "IP no resources"
    155.       Case IP_BAD_OPTION:            msg = "IP bad option"
    156.       Case IP_HW_ERROR:              msg = "IP hw_error"
    157.       Case IP_PACKET_TOO_BIG:        msg = "IP packet too_big"
    158.       Case IP_REQ_TIMED_OUT:         msg = "IP req timed out"
    159.       Case IP_BAD_REQ:               msg = "IP bad req"
    160.       Case IP_BAD_ROUTE:             msg = "IP bad route"
    161.       Case IP_TTL_EXPIRED_TRANSIT:   msg = "IP ttl expired transit"
    162.       Case IP_TTL_EXPIRED_REASSEM:   msg = "IP ttl expired reassem"
    163.       Case IP_PARAM_PROBLEM:         msg = "IP param_problem"
    164.       Case IP_SOURCE_QUENCH:         msg = "IP source quench"
    165.       Case IP_OPTION_TOO_BIG:        msg = "IP option too_big"
    166.       Case IP_BAD_DESTINATION:       msg = "IP bad destination"
    167.       Case IP_ADDR_DELETED:          msg = "IP addr deleted"
    168.       Case IP_SPEC_MTU_CHANGE:       msg = "IP spec mtu change"
    169.       Case IP_MTU_CHANGE:            msg = "IP mtu_change"
    170.       Case IP_UNLOAD:                msg = "IP unload"
    171.       Case IP_ADDR_ADDED:            msg = "IP addr added"
    172.       Case IP_GENERAL_FAILURE:       msg = "IP general failure"
    173.       Case IP_PENDING:               msg = "IP pending"
    174.       Case PING_TIMEOUT:             msg = "ping timeout"
    175.       Case Else:                     msg = "unknown  msg returned"
    176.    End Select
    177.    
    178.    GetStatusCode = CStr(status) & "   [ " & msg & " ]"
    179.    
    180. End Function
    181.  
    182. Private Function HiByte(ByVal wParam As Integer)
    183.     HiByte = wParam \ &H100 And &HFF&
    184. End Function
    185.  
    186. Private Function LoByte(ByVal wParam As Integer)
    187.     LoByte = wParam And &HFF&
    188. End Function
    189.  
    190. Private Function Ping(szAddress As String, ECHO As ICMP_ECHO_REPLY) As Long
    191.  
    192.    Dim hPort As Long
    193.    Dim dwAddress As Long
    194.    Dim sDataToSend As String
    195.    Dim iOpt As Long
    196.    
    197.    sDataToSend = "Echo This"
    198.    dwAddress = AddressStringToLong(szAddress)
    199.    
    200.    Call SocketsInitialize
    201.    hPort = IcmpCreateFile()
    202.    
    203.    If IcmpSendEcho(hPort, _
    204.                    dwAddress, _
    205.                    sDataToSend, _
    206.                    Len(sDataToSend), _
    207.                    0, _
    208.                    ECHO, _
    209.                    Len(ECHO), _
    210.                    PING_TIMEOUT) Then
    211.    
    212.          Ping = ECHO.RoundTripTime
    213.    Else
    214.          Ping = ECHO.status * -1
    215.    End If
    216.                        
    217.    Call IcmpCloseHandle(hPort)
    218.    Call SocketsCleanup
    219.    
    220. End Function
    221.  
    222. Private Function SocketsCleanup() As Boolean
    223.  
    224.     Dim X As Long
    225.    
    226.     X = WSACleanup()
    227.    
    228.     If X <> 0 Then
    229.         MsgBox "Windows Sockets error " & Trim$(Str$(X)) & _
    230.                " occurred in Cleanup.", vbExclamation
    231.         SocketsCleanup = False
    232.     Else
    233.         SocketsCleanup = True
    234.     End If
    235.    
    236. End Function
    237.  
    238. Private Function SocketsInitialize() As Boolean
    239.  
    240.     Dim WSAD As WSADATA
    241.     Dim X As Integer
    242.     Dim szLoByte As String, szHiByte As String, szBuf As String
    243.    
    244.     X = WSAStartup(WS_VERSION_REQD, WSAD)
    245.    
    246.     If X <> 0 Then
    247.         MsgBox "Windows Sockets for 32 bit Windows " & _
    248.                "environments is not successfully responding."
    249.         SocketsInitialize = False
    250.         Exit Function
    251.     End If
    252.    
    253.     If LoByte(WSAD.wVersion) < WS_VERSION_MAJOR Or _
    254.        (LoByte(WSAD.wVersion) = WS_VERSION_MAJOR And _
    255.         HiByte(WSAD.wVersion) < WS_VERSION_MINOR) Then
    256.        
    257.         szHiByte = Trim$(Str$(HiByte(WSAD.wVersion)))
    258.         szLoByte = Trim$(Str$(LoByte(WSAD.wVersion)))
    259.         szBuf = "Windows Sockets Version " & szLoByte & "." & szHiByte
    260.         szBuf = szBuf & " is not supported by Windows " & _
    261.                           "Sockets for 32 bit Windows environments."
    262.         MsgBox szBuf, vbExclamation
    263.         SocketsInitialize = False
    264.         Exit Function
    265.        
    266.     End If
    267.    
    268.     If WSAD.wMaxSockets < MIN_SOCKETS_REQD Then
    269.         szBuf = "This application requires a minimum of " & _
    270.                  Trim$(Str$(MIN_SOCKETS_REQD)) & " supported sockets."
    271.         MsgBox szBuf, vbExclamation
    272.         SocketsInitialize = False
    273.         Exit Function
    274.     End If
    275.    
    276.     SocketsInitialize = True
    277.        
    278. End Function
    279.  
    280. Private Sub cmdPing_Click()
    281. Dim ECHO As ICMP_ECHO_REPLY
    282.    Dim pos As Integer
    283.    Dim i As Integer
    284.    Call Ping(txtPing.Text, ECHO)
    285.  
    286.   For i = 1 To 3
    287.    List1.AddItem GetStatusCode(ECHO.status)
    288.   Next
    289.    List1.AddItem ECHO.Address
    290.    List1.AddItem "Time: " & ECHO.RoundTripTime & " ms"
    291.    List1.AddItem ECHO.DataSize & " bytes"
    292.    
    293.    If Left$(ECHO.Data, 1) <> Chr$(0) Then
    294.       pos = InStr(ECHO.Data, Chr$(0))
    295.       List1.AddItem Left$(ECHO.Data, pos - 1)
    296.    End If
    297.  
    298.    List1.AddItem ECHO.DataPointer
    299. End Sub

  3. #3
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Ping

    Or, ShellExecute

    VB Code:
    1. Option Explicit
    2.  
    3. Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, _
    4. ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
    5.  
    6. Private Const SW_SHOWNORMAL As Long = 1
    7. Private Const SW_HIDE As Long = 0
    8.  
    9. Private Declare Function GetSystemDirectory Lib "kernel32" Alias "GetSystemDirectoryA" (ByVal lpBuffer As String, ByVal nSize As Long) As Long
    10.  
    11. Dim sSave As String, Ret As Long
    12.  
    13. Private Sub Form_Load()
    14.     'KPD-Team 1998
    15.     'URL: [url]http://www.allapi.net/[/url]
    16.     'E-Mail: [email][email protected][/email]
    17.     Dim sSave As String, Ret As Long
    18.     'Create a buffer
    19.     sSave = Space(255)
    20.     'Get the system directory
    21.     Ret = GetSystemDirectory(sSave, 255)
    22.     'Remove all unnecessary chr$(0)'s
    23.     sSave = Left$(sSave, Ret)
    24. End Sub
    25.  
    26. Private Sub Command1_Click()
    27.     ShellExecute Me.hwnd, "Open", sSave & "CMD.exe", " /c ping 192.168.1.101 c: > C:\Myfile.txt", "C:\", SW_SHOWNORMAL
    28. End Sub

    Which saves it to a file.

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

    Re: Ping

    Actually Dave, I kind of like that short, sweet and to the purpose way.

    I know it came from allapi.net, but what is the purpose of including GetSystemDirectory do you suppose?

  5. #5
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Ping

    I was going to take out the reference, because I have modified it to suit the purpose many times. I'm sure that it was needed for the original use of ShellExecute, but it isn't needed here. I had a different example that opened the txt file, but grabbed this one instead, and modified it.

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

    Re: Ping

    I just tried Dave's code post, and I got an error with this:
    VB Code:
    1. Private Sub Command1_Click()
    2.     ShellExecute Me.hwnd, "Open", sSave & "CMD.exe", " /c ping 192.168.1.101 c: > C:\Myfile.txt", "C:\", SW_SHOWNORMAL
    3. End Sub
    MyFile.txt displayed:

    Bad parameter c:

    I took out the c: so it read
    VB Code:
    1. Private Sub Command1_Click()
    2.     ShellExecute Me.hwnd, "Open", sSave & "CMD.exe", " /c ping 192.168.1.101 > C:\Myfile.txt", "C:\", SW_SHOWNORMAL
    3. End Sub
    and it worked just fine.

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

    Re: Ping

    Quote Originally Posted by dglienna
    I was going to take out the reference, because I have modified it to suit the purpose many times. I'm sure that it was needed for the original use of ShellExecute, but it isn't needed here. I had a different example that opened the txt file, but grabbed this one instead, and modified it.
    Oops...sorry my bad. I thought that came with the example from allapi.net

    No problem...I've posted stuff that had extra stuff in as well 'cause I forgot to take it out.

  8. #8

    Thread Starter
    Addicted Member Psychotic's Avatar
    Join Date
    May 2004
    Posts
    164

    Re: Ping

    Which one of these two would be faster to ping a large number of addresses? sorry, currently at work so I can't test them at the moment.
    -Psychotic-

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

    Re: Ping

    Quote Originally Posted by Psychotic
    Which one of these two would be faster to ping a large number of addresses? sorry, currently at work so I can't test them at the moment.
    Both of them will only ping one address at a time. You would have to make some modifications in order for it to ping more than one.

    I could do that for you, but I need a little more information.

    Where would this list of address be stored? A listbox? A db table?

    I can take what Dave did (at least I can in theory....haven't actually tried it yet ), and put it through a loop so it would write all results off to a text file, but I need to know where the code will be looking for the addresses.

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

    Re: Ping

    I used a Listbox and the code that I posted. Change what is in cmdPing to this
    VB Code:
    1. Private Sub cmdPing_Click()
    2. Dim ECHO As ICMP_ECHO_REPLY
    3.    Dim pos As Integer
    4.    Dim i As Integer
    5.    Dim j As Integer
    6. Open "c:\ipping.txt" For Append As #1
    7. For j = 0 To List1.ListCount - 1
    8.    Call Ping(List1.List(i), ECHO)
    9.  
    10.   For i = 1 To 3
    11.    Print #1, GetStatusCode(ECHO.status)
    12.   Next
    13.    Print #1, ECHO.Address
    14.    Print #1, "Time: " & ECHO.RoundTripTime & " ms"
    15.    Print #1, ECHO.DataSize & " bytes"
    16.    
    17.    If Left$(ECHO.Data, 1) <> Chr$(0) Then
    18.       pos = InStr(ECHO.Data, Chr$(0))
    19.       Print #1, Left$(ECHO.Data, pos - 1)
    20.    End If
    21.  
    22.    Print #1, ECHO.DataPointer
    23. Next
    24. Close #1

  11. #11

    Thread Starter
    Addicted Member Psychotic's Avatar
    Join Date
    May 2004
    Posts
    164

    Re: Ping

    Ok, I want to make an app that is just like the one I currently have but with a few minor changes.

    I want the user (me) to input a range of ip addresses to scan and the app will produce a list of ips that are in use and hopefully the MAC address's of the devices connected as well as some other information.

    I don't want to input a range of 100 ips and have to wait 15 minutes so the faster the better.
    -Psychotic-

  12. #12
    Addicted Member g-mie's Avatar
    Join Date
    Jan 2004
    Location
    EarTh
    Posts
    212

    Re: Ping

    Quote Originally Posted by Hack
    VB Code:
    1. Dim ECHO As ICMP_ECHO_REPLY
    Error at this line;"Compile error: User-defined type not defined".
    What should I do?

  13. #13
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: Ping

    Quote Originally Posted by g-mie
    Error at this line;"Compile error: User-defined type not defined".
    What should I do?
    VB Code:
    1. [B]Public[/B] Type ICMP_ECHO_REPLY
    2.     Address         As Long
    3.     status          As Long
    4.     RoundTripTime   As Long
    5.     DataSize        As Integer
    6.     Reserved        As Integer
    7.     DataPointer     As Long
    8.     Options         As ICMP_OPTIONS
    9.     Data            As String * 250
    10. End Type

  14. #14
    Addicted Member g-mie's Avatar
    Join Date
    Jan 2004
    Location
    EarTh
    Posts
    212

    Re: Ping

    Same error, but now the error at 'Options As ICMP_OPTIONS' line.

  15. #15
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Ping

    It's all in Post #2

  16. #16
    Addicted Member g-mie's Avatar
    Join Date
    Jan 2004
    Location
    EarTh
    Posts
    212

    Re: Ping

    ok guys.
    thanks for the info. its helping me.

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