Results 1 to 4 of 4

Thread: ping a computer in vb 6.0, help

  1. #1
    Join Date
    Oct 2005
    Posts
    190

    ping a computer in vb 6.0, help

    Can anyone tell me how to ping local network from vb ? The local area network works on a similar lan with a pc name given to it. So for knowing whether the pc exists in the network or not I want to use a DOS command ping. Can anyone help me with giving detailed information about the programs? Thank you for any help
    I may b a dreamer, but I'm not the only one

  2. #2
    Join Date
    Jan 2006
    Posts
    2,257

    Re: ping a computer in vb 6.0, help

    If you want to ping with VB6 your computer, you can use ICMP to ping in Visual Basic

    • Start Microsoft Visual Basic 6.0.
    • In the New Project dialog box, click Standard EXE, and then click Open. The Visual Basic Design Editor opens, and Form1 is loaded.
    • To add a new module to this project, right-click Project1.vbp in Project Explorer, point to Add, and then click Module.
    • In the Add Module dialog box, click Open. The Module1.bas module is added to the project and opens in the Code Editor window.
    • At the top of the Code Editor window of Module1, add the following constants and function declaration:


    Code:
    Option Explicit
    
    'Icmp constants converted from
    'http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wmisdk/wmi/win32_pingstatus.asp
    
    Private Const ICMP_SUCCESS As Long = 0
    Private Const ICMP_STATUS_BUFFER_TO_SMALL = 11001                   'Buffer Too Small
    Private Const ICMP_STATUS_DESTINATION_NET_UNREACH = 11002           'Destination Net Unreachable
    Private Const ICMP_STATUS_DESTINATION_HOST_UNREACH = 11003          'Destination Host Unreachable
    Private Const ICMP_STATUS_DESTINATION_PROTOCOL_UNREACH = 11004      'Destination Protocol Unreachable
    Private Const ICMP_STATUS_DESTINATION_PORT_UNREACH = 11005          'Destination Port Unreachable
    Private Const ICMP_STATUS_NO_RESOURCE = 11006                       'No Resources
    Private Const ICMP_STATUS_BAD_OPTION = 11007                        'Bad Option
    Private Const ICMP_STATUS_HARDWARE_ERROR = 11008                    'Hardware Error
    Private Const ICMP_STATUS_LARGE_PACKET = 11009                      'Packet Too Big
    Private Const ICMP_STATUS_REQUEST_TIMED_OUT = 11010                 'Request Timed Out
    Private Const ICMP_STATUS_BAD_REQUEST = 11011                       'Bad Request
    Private Const ICMP_STATUS_BAD_ROUTE = 11012                         'Bad Route
    Private Const ICMP_STATUS_TTL_EXPIRED_TRANSIT = 11013               'TimeToLive Expired Transit
    Private Const ICMP_STATUS_TTL_EXPIRED_REASSEMBLY = 11014            'TimeToLive Expired Reassembly
    Private Const ICMP_STATUS_PARAMETER = 11015                         'Parameter Problem
    Private Const ICMP_STATUS_SOURCE_QUENCH = 11016                     'Source Quench
    Private Const ICMP_STATUS_OPTION_TOO_BIG = 11017                    'Option Too Big
    Private Const ICMP_STATUS_BAD_DESTINATION = 11018                   'Bad Destination
    Private Const ICMP_STATUS_NEGOTIATING_IPSEC = 11032                 'Negotiating IPSEC
    Private Const ICMP_STATUS_GENERAL_FAILURE = 11050                   'General Failure
    
    Public Const WINSOCK_ERROR = "Windows Sockets not responding correctly."
    Public Const INADDR_NONE As Long = &HFFFFFFFF
    Public Const WSA_SUCCESS = 0
    Public Const WS_VERSION_REQD As Long = &H101
    
    'Clean up sockets.
    'http://support.microsoft.com/default.aspx?scid=kb;EN-US;q154512
    
    Private Declare Function WSACleanup Lib "WSOCK32.DLL" () As Long
    
    'Open the socket connection.
    'http://support.microsoft.com/default.aspx?scid=kb;EN-US;q154512
    Private Declare Function WSAStartup Lib "WSOCK32.DLL" (ByVal wVersionRequired As Long, lpWSADATA As WSADATA) As Long
    
    'Create a handle on which Internet Control Message Protocol (ICMP) requests can be issued.
    'http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wcesdkr/htm/_wcesdk_icmpcreatefile.asp
    Private Declare Function IcmpCreateFile Lib "icmp.dll" () As Long
    
    'Convert a string that contains an (Ipv4) Internet Protocol dotted address into a correct address.
    'http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winsock/wsapiref_4esy.asp
    Private Declare Function inet_addr Lib "WSOCK32.DLL" (ByVal cp As String) As Long
    
    'Close an Internet Control Message Protocol (ICMP) handle that IcmpCreateFile opens.
    'http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wcesdkr/htm/_wcesdk_icmpclosehandle.asp
    
    Private Declare Function IcmpCloseHandle Lib "icmp.dll" (ByVal IcmpHandle As Long) As Long
    
    'Information about the Windows Sockets implementation
    'http://support.microsoft.com/default.aspx?scid=kb;EN-US;q154512
    Private Type WSADATA
       wVersion As Integer
       wHighVersion As Integer
       szDescription(0 To 256) As Byte
       szSystemStatus(0 To 128) As Byte
       iMaxSockets As Long
       iMaxUDPDG As Long
       lpVendorInfo As Long
    End Type
    
    'Send an Internet Control Message Protocol (ICMP) echo request, and then return one or more replies.
    'http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wcetcpip/htm/cerefIcmpSendEcho.asp
    Private Declare Function IcmpSendEcho Lib "icmp.dll" _
       (ByVal IcmpHandle As Long, _
        ByVal DestinationAddress As Long, _
        ByVal RequestData As String, _
        ByVal RequestSize As Long, _
        ByVal RequestOptions As Long, _
        ReplyBuffer As ICMP_ECHO_REPLY, _
        ByVal ReplySize As Long, _
        ByVal Timeout As Long) As Long
     
    'This structure describes the options that will be included in the header of an IP packet.
    'http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wcetcpip/htm/cerefIP_OPTION_INFORMATION.asp
    Private Type IP_OPTION_INFORMATION
       Ttl             As Byte
       Tos             As Byte
       Flags           As Byte
       OptionsSize     As Byte
       OptionsData     As Long
    End Type
    
    'This structure describes the data that is returned in response to an echo request.
    'http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wcesdkr/htm/_wcesdk_icmp_echo_reply.asp
    Public Type ICMP_ECHO_REPLY
       address         As Long
       Status          As Long
       RoundTripTime   As Long
       DataSize        As Long
       Reserved        As Integer
       ptrData                 As Long
       Options        As IP_OPTION_INFORMATION
       Data            As String * 250
    End Type
    
    '-- Ping a string representation of an IP address.
    ' -- Return a reply.
    ' -- Return long code.
    Public Function ping(sAddress As String, Reply As ICMP_ECHO_REPLY) As Long
    
    Dim hIcmp As Long
    Dim lAddress As Long
    Dim lTimeOut As Long
    Dim StringToSend As String
    
    'Short string of data to send
    StringToSend = "hello"
    
    'ICMP (ping) timeout
    lTimeOut = 1000 'ms
    
    'Convert string address to a long representation.
    lAddress = inet_addr(sAddress)
    
    If (lAddress <> -1) And (lAddress <> 0) Then
            
        'Create the handle for ICMP requests.
        hIcmp = IcmpCreateFile()
        
        If hIcmp Then
            'Ping the destination IP address.
            Call IcmpSendEcho(hIcmp, lAddress, StringToSend, Len(StringToSend), 0, Reply, Len(Reply), lTimeOut)
    
            'Reply status
            ping = Reply.Status
            
            'Close the Icmp handle.
            IcmpCloseHandle hIcmp
        Else
            Debug.Print "failure opening icmp handle."
            ping = -1
        End If
    Else
        ping = -1
    End If
    
    End Function
    
    'Clean up the sockets.
    'http://support.microsoft.com/default.aspx?scid=kb;EN-US;q154512
    Public Sub SocketsCleanup()
       
       WSACleanup
        
    End Sub
    
    'Get the sockets ready.
    'http://support.microsoft.com/default.aspx?scid=kb;EN-US;q154512
    Public Function SocketsInitialize() As Boolean
    
       Dim WSAD As WSADATA
    
       SocketsInitialize = WSAStartup(WS_VERSION_REQD, WSAD) = ICMP_SUCCESS
    
    End Function
    
    'Convert the ping response to a message that you can read easily from constants.
    'For more information about these constants, visit the following Microsoft Web site: 
    'http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wmisdk/wmi/win32_pingstatus.asp
    
    Public Function EvaluatePingResponse(PingResponse As Long) As String
    
      Select Case PingResponse
        
      'Success
      Case ICMP_SUCCESS: EvaluatePingResponse = "Success!"
                
      'Some error occurred
      Case ICMP_STATUS_BUFFER_TO_SMALL:    EvaluatePingResponse = "Buffer Too Small"
      Case ICMP_STATUS_DESTINATION_NET_UNREACH: EvaluatePingResponse = "Destination Net Unreachable"
      Case ICMP_STATUS_DESTINATION_HOST_UNREACH: EvaluatePingResponse = "Destination Host Unreachable"
      Case ICMP_STATUS_DESTINATION_PROTOCOL_UNREACH: EvaluatePingResponse = "Destination Protocol Unreachable"
      Case ICMP_STATUS_DESTINATION_PORT_UNREACH: EvaluatePingResponse = "Destination Port Unreachable"
      Case ICMP_STATUS_NO_RESOURCE: EvaluatePingResponse = "No Resources"
      Case ICMP_STATUS_BAD_OPTION: EvaluatePingResponse = "Bad Option"
      Case ICMP_STATUS_HARDWARE_ERROR: EvaluatePingResponse = "Hardware Error"
      Case ICMP_STATUS_LARGE_PACKET: EvaluatePingResponse = "Packet Too Big"
      Case ICMP_STATUS_REQUEST_TIMED_OUT: EvaluatePingResponse = "Request Timed Out"
      Case ICMP_STATUS_BAD_REQUEST: EvaluatePingResponse = "Bad Request"
      Case ICMP_STATUS_BAD_ROUTE: EvaluatePingResponse = "Bad Route"
      Case ICMP_STATUS_TTL_EXPIRED_TRANSIT: EvaluatePingResponse = "TimeToLive Expired Transit"
      Case ICMP_STATUS_TTL_EXPIRED_REASSEMBLY: EvaluatePingResponse = "TimeToLive Expired Reassembly"
      Case ICMP_STATUS_PARAMETER: EvaluatePingResponse = "Parameter Problem"
      Case ICMP_STATUS_SOURCE_QUENCH: EvaluatePingResponse = "Source Quench"
      Case ICMP_STATUS_OPTION_TOO_BIG: EvaluatePingResponse = "Option Too Big"
      Case ICMP_STATUS_BAD_DESTINATION: EvaluatePingResponse = "Bad Destination"
      Case ICMP_STATUS_NEGOTIATING_IPSEC: EvaluatePingResponse = "Negotiating IPSEC"
      Case ICMP_STATUS_GENERAL_FAILURE: EvaluatePingResponse = "General Failure"
                
      'Unknown error occurred
      Case Else: EvaluatePingResponse = "Unknown Response"
            
      End Select
    
    End Function
    Note Watch for line wrapping when you copy this code into your project. This code provides the functions to access the Icmp.dll file. See the inline comments for a description of the constants, the declarations, and the function calls.

    • In Project Explorer, right-click Form1, and then click View Object. Form1 loads in the Designer window.
    • Double-click the Form1 body. The Code Editor window opens with the following code:


    Code:
    Private Sub Form_Load()
    
    End Sub
    • Replace the code in the Form_Load method with the following code:


    Code:
    Private Sub Form_Load()
    
       Dim Reply As ICMP_ECHO_REPLY
       Dim lngSuccess As Long
       Dim strIPAddress As String
       
       'Get the sockets ready.
       If SocketsInitialize() Then
          
        'Address to ping
        strIPAddress = "192.168.1.1"
        
        'Ping the IP that is passing the address and get a reply.
        lngSuccess = ping(strIPAddress, Reply)
          
        'Display the results.
        Debug.Print "Address to Ping: " & strIPAddress
        Debug.Print "Raw ICMP code: " & lngSuccess
        Debug.Print "Ping Response Message : " & EvaluatePingResponse(lngSuccess)
        Debug.Print "Time : " & Reply.RoundTripTime & " ms"
          
        'Clean up the sockets.
        SocketsCleanup
          
       Else
       
       'Winsock error failure, initializing the sockets.
       Debug.Print WINSOCK_ERROR
       
       End If
       
    End Sub
    • Locate the following lines of code:


    • 'Address to ping
    • strIPAddress = "192.168.1.1"


    Change the strIPAddress to an IP address you want to ping.
    • To save the project and the files that are associated with the project, click Save Project on the File menu.
    • Notice that the results of the code are written to the Immediate window. To verify that the Immediate window is open, click Immediate Window on the View menu.
    • To test this code, press F5. The results are displayed in the Immediate window. The results are similar to the following results:


    • Address to Ping: 192.168.1.1
    • Raw ICMP code: 0
    • Ping Response Message : Success!
    • Time : 10 ms


    Note To ping a domain name, you must first translate the domain name to an address. For more information go to this link.
    With great power comes great responsibility - Spiderman's Uncle

    The Greatest Sig Ever

  3. #3
    Join Date
    Jun 2006
    Posts
    623

    Re: ping a computer in vb 6.0, help

    I found the VB Ping script, it does exactly what you want, you will need a file containing a list of the hostnames you want to ping and a log file will be written.

    Code:
    '------------------------------------------------------
    'Script designed to run a ping test on hostnames in a text
    'file, and then return only the live hostnames into a log
    'file.
    '------------------------------------------------------
    Const ForReading = 1
    Const ForAppending = 8
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objTextFile = objFSO.OpenTextFile _
    ("disabledcomputers.txt", ForReading)
    Set objLogFile = objFSO.OpenTextFile _
    ("pingtest-log.txt", ForAppending, True)
    objLogFile.WriteLine("Test started on: " & Now)
    Do Until objTextFile.AtEndOfStream
    strNextLine = objTextFile.Readline
    arrTargets = Split(strNextLine , ",")
    For Each strNextLine in arrTargets
    Set objShell = CreateObject("WScript.Shell")
    Set objExec = objShell.Exec("ping.exe -n 2 -w 1000 " & strNextLine)
    strPingResults = LCase(objExec.StdOut.ReadAll)
    If InStr(strPingResults, "reply from") Then
    objLogFile.WriteLine(VbCrLf & strNextLine & " reponded to ping.")
    Else
    End If
    Next
    Loop
    objLogFile.WriteLine(VbCrLf & "Test ended on: " & Now)

  4. #4
    Join Date
    Jan 2006
    Posts
    605

    Re: ping a computer in vb 6.0, help

    If you want to see if computer exists on the network via a vb code then just copy paste the code below, works flawlessly:

    Function PingComputer(ByVal sComputer As String) As Boolean
    Dim Ping As New Net.NetworkInformation.Ping
    Dim PingOptions As New Net.NetworkInformation.PingOptions
    Dim PingReply As Net.NetworkInformation.PingReply
    PingOptions.Ttl = 64
    Try
    PingReply = Ping.Send(sComputer, 200)
    Catch e As Net.NetworkInformation.PingException
    Return False
    End Try
    If PingReply.Status = Net.NetworkInformation.IPStatus.Success Then
    Return True
    Else
    Return False
    End If
    Return False
    End Function

Similar Threads

  1. Wireless cannot ping / ethernet can ping
    By Nereus in forum Networking & Security
    Replies: 6
    Last Post: 14-08-2010, 05:56 AM
  2. Cannot ping my home computer via router from outside my network
    By Sachit in forum Technology & Internet
    Replies: 3
    Last Post: 22-06-2009, 06:35 PM
  3. Replies: 3
    Last Post: 05-02-2009, 02:27 PM
  4. Cannot Ping the other computer
    By RMG in forum Networking & Security
    Replies: 2
    Last Post: 23-01-2009, 11:45 AM
  5. I can ping with IPv6. I can not ping with IPv4.
    By Eric Gamess in forum Vista Help
    Replies: 2
    Last Post: 04-05-2008, 01:31 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,926,111.92161 seconds with 17 queries