How would one go about pinging a computer on a local LAN in vb6?
Printable View
How would one go about pinging a computer on a local LAN in vb6?
VB Code:
'Place a Listbox, a command button, and a text box on your form. 'Call the command button cmdPing and the textbox txtPing Private Const IP_STATUS_BASE = 11000 Private Const IP_SUCCESS = 0 Private Const IP_BUF_TOO_SMALL = (11000 + 1) Private Const IP_DEST_NET_UNREACHABLE = (11000 + 2) Private Const IP_DEST_HOST_UNREACHABLE = (11000 + 3) Private Const IP_DEST_PROT_UNREACHABLE = (11000 + 4) Private Const IP_DEST_PORT_UNREACHABLE = (11000 + 5) Private Const IP_NO_RESOURCES = (11000 + 6) Private Const IP_BAD_OPTION = (11000 + 7) Private Const IP_HW_ERROR = (11000 + 8) Private Const IP_PACKET_TOO_BIG = (11000 + 9) Private Const IP_REQ_TIMED_OUT = (11000 + 10) Private Const IP_BAD_REQ = (11000 + 11) Private Const IP_BAD_ROUTE = (11000 + 12) Private Const IP_TTL_EXPIRED_TRANSIT = (11000 + 13) Private Const IP_TTL_EXPIRED_REASSEM = (11000 + 14) Private Const IP_PARAM_PROBLEM = (11000 + 15) Private Const IP_SOURCE_QUENCH = (11000 + 16) Private Const IP_OPTION_TOO_BIG = (11000 + 17) Private Const IP_BAD_DESTINATION = (11000 + 18) Private Const IP_ADDR_DELETED = (11000 + 19) Private Const IP_SPEC_MTU_CHANGE = (11000 + 20) Private Const IP_MTU_CHANGE = (11000 + 21) Private Const IP_UNLOAD = (11000 + 22) Private Const IP_ADDR_ADDED = (11000 + 23) Private Const IP_GENERAL_FAILURE = (11000 + 50) Private Const MAX_IP_STATUS = 11000 + 50 Private Const IP_PENDING = (11000 + 255) Private Const PING_TIMEOUT = 200 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 MAX_WSADescription = 256 Private Const MAX_WSASYSStatus = 128 Private Type ICMP_OPTIONS Ttl As Byte Tos As Byte Flags As Byte OptionsSize As Byte OptionsData As Long End Type Dim ICMPOPT As ICMP_OPTIONS Private Type ICMP_ECHO_REPLY Address As Long status As Long RoundTripTime As Long DataSize As Integer Reserved As Integer DataPointer As Long Options As ICMP_OPTIONS Data As String * 250 End Type Private Type HOSTENT hName As Long hAliases As Long hAddrType As Integer hLen As Integer hAddrList As Long End Type Private 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 Private Declare Function IcmpCreateFile Lib "icmp.dll" () As Long Private Declare Function IcmpCloseHandle Lib "icmp.dll" _ (ByVal IcmpHandle As Long) As Long Private Declare Function IcmpSendEcho Lib "icmp.dll" _ (ByVal IcmpHandle As Long, _ ByVal DestinationAddress As Long, _ ByVal RequestData As String, _ ByVal RequestSize As Integer, _ ByVal RequestOptions As Long, _ ReplyBuffer As ICMP_ECHO_REPLY, _ ByVal ReplySize As Long, _ ByVal Timeout As Long) As Long Private Declare Function WSAGetLastError Lib "WSOCK32.DLL" () As Long Private Declare Function WSAStartup Lib "WSOCK32.DLL" _ (ByVal wVersionRequired As Long, _ lpWSADATA As WSADATA) As Long Private Declare Function WSACleanup Lib "WSOCK32.DLL" () As Long Private Declare Function gethostname Lib "WSOCK32.DLL" _ (ByVal szHost As String, _ ByVal dwHostLen As Long) As Long Private Declare Function gethostbyname Lib "WSOCK32.DLL" _ (ByVal szHost As String) As Long Private Declare Sub RtlMoveMemory Lib "kernel32" _ (hpvDest As Any, _ ByVal hpvSource As Long, _ ByVal cbCopy As Long) Private Function AddressStringToLong(ByVal tmp As String) As Long Dim i As Integer Dim parts(1 To 4) As String i = 0 While InStr(tmp, ".") > 0 i = i + 1 parts(i) = Mid(tmp, 1, InStr(tmp, ".") - 1) tmp = Mid(tmp, InStr(tmp, ".") + 1) Wend i = i + 1 parts(i) = tmp If i <> 4 Then AddressStringToLong = 0 Exit Function End If AddressStringToLong = Val("&H" & Right("00" & Hex(parts(4)), 2) & _ Right("00" & Hex(parts(3)), 2) & _ Right("00" & Hex(parts(2)), 2) & _ Right("00" & Hex(parts(1)), 2)) End Function Private Function GetStatusCode(status As Long) As String Dim msg As String Select Case status Case IP_SUCCESS: msg = txtPing.Text & " is replying" Case IP_BUF_TOO_SMALL: msg = "IP buf too_small" Case IP_DEST_NET_UNREACHABLE: msg = "IP dest net unreachable" Case IP_DEST_HOST_UNREACHABLE: msg = "IP dest host unreachable" Case IP_DEST_PROT_UNREACHABLE: msg = "IP dest prot unreachable" Case IP_DEST_PORT_UNREACHABLE: msg = "IP dest port unreachable" Case IP_NO_RESOURCES: msg = "IP no resources" Case IP_BAD_OPTION: msg = "IP bad option" Case IP_HW_ERROR: msg = "IP hw_error" Case IP_PACKET_TOO_BIG: msg = "IP packet too_big" Case IP_REQ_TIMED_OUT: msg = "IP req timed out" Case IP_BAD_REQ: msg = "IP bad req" Case IP_BAD_ROUTE: msg = "IP bad route" Case IP_TTL_EXPIRED_TRANSIT: msg = "IP ttl expired transit" Case IP_TTL_EXPIRED_REASSEM: msg = "IP ttl expired reassem" Case IP_PARAM_PROBLEM: msg = "IP param_problem" Case IP_SOURCE_QUENCH: msg = "IP source quench" Case IP_OPTION_TOO_BIG: msg = "IP option too_big" Case IP_BAD_DESTINATION: msg = "IP bad destination" Case IP_ADDR_DELETED: msg = "IP addr deleted" Case IP_SPEC_MTU_CHANGE: msg = "IP spec mtu change" Case IP_MTU_CHANGE: msg = "IP mtu_change" Case IP_UNLOAD: msg = "IP unload" Case IP_ADDR_ADDED: msg = "IP addr added" Case IP_GENERAL_FAILURE: msg = "IP general failure" Case IP_PENDING: msg = "IP pending" Case PING_TIMEOUT: msg = "ping timeout" Case Else: msg = "unknown msg returned" End Select GetStatusCode = CStr(status) & " [ " & msg & " ]" End Function Private Function HiByte(ByVal wParam As Integer) HiByte = wParam \ &H100 And &HFF& End Function Private Function LoByte(ByVal wParam As Integer) LoByte = wParam And &HFF& End Function Private Function Ping(szAddress As String, ECHO As ICMP_ECHO_REPLY) As Long Dim hPort As Long Dim dwAddress As Long Dim sDataToSend As String Dim iOpt As Long sDataToSend = "Echo This" dwAddress = AddressStringToLong(szAddress) Call SocketsInitialize hPort = IcmpCreateFile() If IcmpSendEcho(hPort, _ dwAddress, _ sDataToSend, _ Len(sDataToSend), _ 0, _ ECHO, _ Len(ECHO), _ PING_TIMEOUT) Then Ping = ECHO.RoundTripTime Else Ping = ECHO.status * -1 End If Call IcmpCloseHandle(hPort) Call SocketsCleanup End Function Private Function SocketsCleanup() As Boolean Dim X As Long X = WSACleanup() If X <> 0 Then MsgBox "Windows Sockets error " & Trim$(Str$(X)) & _ " occurred in Cleanup.", vbExclamation SocketsCleanup = False Else SocketsCleanup = True End If End Function Private Function SocketsInitialize() As Boolean Dim WSAD As WSADATA Dim X As Integer Dim szLoByte As String, szHiByte As String, szBuf As String X = WSAStartup(WS_VERSION_REQD, WSAD) If X <> 0 Then MsgBox "Windows Sockets for 32 bit Windows " & _ "environments is not successfully responding." 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 szHiByte = Trim$(Str$(HiByte(WSAD.wVersion))) szLoByte = Trim$(Str$(LoByte(WSAD.wVersion))) szBuf = "Windows Sockets Version " & szLoByte & "." & szHiByte szBuf = szBuf & " is not supported by Windows " & _ "Sockets for 32 bit Windows environments." MsgBox szBuf, vbExclamation SocketsInitialize = False Exit Function End If If WSAD.wMaxSockets < MIN_SOCKETS_REQD Then szBuf = "This application requires a minimum of " & _ Trim$(Str$(MIN_SOCKETS_REQD)) & " supported sockets." MsgBox szBuf, vbExclamation SocketsInitialize = False Exit Function End If SocketsInitialize = True End Function Private Sub cmdPing_Click() Dim ECHO As ICMP_ECHO_REPLY Dim pos As Integer Dim i As Integer Call Ping(txtPing.Text, ECHO) For i = 1 To 3 List1.AddItem GetStatusCode(ECHO.status) Next List1.AddItem ECHO.Address List1.AddItem "Time: " & ECHO.RoundTripTime & " ms" List1.AddItem ECHO.DataSize & " bytes" If Left$(ECHO.Data, 1) <> Chr$(0) Then pos = InStr(ECHO.Data, Chr$(0)) List1.AddItem Left$(ECHO.Data, pos - 1) End If List1.AddItem ECHO.DataPointer End Sub
Or, ShellExecute
VB Code:
Option Explicit Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, _ ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long Private Const SW_SHOWNORMAL As Long = 1 Private Const SW_HIDE As Long = 0 Private Declare Function GetSystemDirectory Lib "kernel32" Alias "GetSystemDirectoryA" (ByVal lpBuffer As String, ByVal nSize As Long) As Long Dim sSave As String, Ret As Long Private Sub Form_Load() 'KPD-Team 1998 'URL: [url]http://www.allapi.net/[/url] 'E-Mail: [email][email protected][/email] Dim sSave As String, Ret As Long 'Create a buffer sSave = Space(255) 'Get the system directory Ret = GetSystemDirectory(sSave, 255) 'Remove all unnecessary chr$(0)'s sSave = Left$(sSave, Ret) End Sub Private Sub Command1_Click() ShellExecute Me.hwnd, "Open", sSave & "CMD.exe", " /c ping 192.168.1.101 c: > C:\Myfile.txt", "C:\", SW_SHOWNORMAL End Sub
Which saves it to a file.
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?
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.
I just tried Dave's code post, and I got an error with this:MyFile.txt displayed:VB Code:
Private Sub Command1_Click() ShellExecute Me.hwnd, "Open", sSave & "CMD.exe", " /c ping 192.168.1.101 c: > C:\Myfile.txt", "C:\", SW_SHOWNORMAL End Sub
Bad parameter c:
I took out the c: so it readand it worked just fine.VB Code:
Private Sub Command1_Click() ShellExecute Me.hwnd, "Open", sSave & "CMD.exe", " /c ping 192.168.1.101 > C:\Myfile.txt", "C:\", SW_SHOWNORMAL End Sub
Oops...sorry my bad. I thought that came with the example from allapi.netQuote:
Originally Posted by dglienna
No problem...I've posted stuff that had extra stuff in as well 'cause I forgot to take it out. :D
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.Quote:
Originally Posted by Psychotic
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 :D ), 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.
I used a Listbox and the code that I posted. Change what is in cmdPing to thisVB Code:
Private Sub cmdPing_Click() Dim ECHO As ICMP_ECHO_REPLY Dim pos As Integer Dim i As Integer Dim j As Integer Open "c:\ipping.txt" For Append As #1 For j = 0 To List1.ListCount - 1 Call Ping(List1.List(i), ECHO) For i = 1 To 3 Print #1, GetStatusCode(ECHO.status) Next Print #1, ECHO.Address Print #1, "Time: " & ECHO.RoundTripTime & " ms" Print #1, ECHO.DataSize & " bytes" If Left$(ECHO.Data, 1) <> Chr$(0) Then pos = InStr(ECHO.Data, Chr$(0)) Print #1, Left$(ECHO.Data, pos - 1) End If Print #1, ECHO.DataPointer Next Close #1
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.
Error at this line;"Compile error: User-defined type not defined".Quote:
Originally Posted by Hack
What should I do?
Quote:
Originally Posted by g-mie
VB Code:
[B]Public[/B] Type ICMP_ECHO_REPLY Address As Long status As Long RoundTripTime As Long DataSize As Integer Reserved As Integer DataPointer As Long Options As ICMP_OPTIONS Data As String * 250 End Type
Same error, but now the error at 'Options As ICMP_OPTIONS' line.
It's all in Post #2
ok guys.
thanks for the info. its helping me.