borderAndreaVB free resources for Visual Basic developersborder
borderAndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2014 Andrea Tincaniborder

AndreaVB | Forum | News | Downloads | Register | Help | Member List | Statistics | Search | PM | Profile

Print This Topic
Previous Topic (How to retrive active network connection using vb.net)Next Topic (How to capture Screenshot in other Computer) New Topic New Poll Post Reply
AndreaVB Forum : Network : Silent Ping
Poster Message
hg363
Level: Guest


icon Silent Ping

Can any one supply me with some VB6 code to perform a silent PING.

By silent I mean, no DOS screens to appear and the PING is performed in the background without the user being aware.

The code also needs to capture the output and I do not require any thing fancy, just some simple code to PING from one PC to another, on a network.

The code does not need Time-To-Live, number of replies etc etc, it just needs to be able to establish if another PC is on the network.

If you can help, please send me an example my email address is

h.ghelani1@rbh.nthames.nhs.uk

25-02-2004 at 08:05 AM
| Quote Reply
stickleprojects
Level: Moderator


Registered: 09-09-2002
Posts: 1036
icon Re: Silent Ping

Hi hg.
2 things:
1. What research have you done so far?
2. This is a forum, where other people can learn from (and correct) the advice you are given. Bypassing this for personal e-mails does not take advantage of the knowledge of the many people online, and you may miss out on their experience.
Kieron


____________________________
Build it better, faster, quicker, easier.. then fix it (non-offical MS mission statement)

28-02-2004 at 01:05 AM
View Profile Send Email to User Show All Posts | Quote Reply
Lycaon
Level: Guest

icon Re: Silent Ping

Throw the following code into a module or class (or form if you change the constants to private), then simply use:

Dim Result As Integer

Result = Ping("www.google.com")

If Ping < 0 Then MsgBox "Ping failed, error: " & Result

A reply of -1 means the ping timed out, -2 indicates any other error.

Default timeout to wait for a reply is 2 seconds (2000 ms) but this can be overridden.

Option Explicit

Const SOCKET_ERROR = 0
Const MAX_IP = 10

Private Type WSAdata
    wVersion As Integer
    wHighVersion As Integer
    szDescription(0 To 255) As Byte
    szSystemStatus(0 To 128) As Byte
    iMaxSockets As Integer
    iMaxUdpDg As Integer
    lpVendorInfo As Long
End Type

Private Type Hostent
    h_name As Long
    h_aliases As Long
    h_addrtype As Integer
    h_length As Integer
    h_addr_list As Long
End Type

Private Type IP_OPTION_INFORMATION
    TTL As Byte
    Tos As Byte
    Flags As Byte
    OptionsSize As Long
    OptionsData As String * 128
End Type

Private Type IP_ECHO_REPLY
    Address(0 To 3) As Byte
    Status As Long
    RoundTripTime As Long
    DataSize As Integer
    Reserved As Integer
    data As Long
    Options As IP_OPTION_INFORMATION
End Type

Private Declare Function GetHostByName Lib "wsock32.dll" Alias "gethostbyname" (ByVal HostName As String) As Long
Private Declare Function WSAStartup Lib "wsock32.dll" (ByVal wVersionRequired&, lpWSAdata As WSAdata) As Long
Private Declare Function WSACleanup Lib "wsock32.dll" () As Long
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (hpvDest As Any, hpvSource As Any, ByVal cbCopy As Long)
Private Declare Function IcmpCreateFile Lib "icmp.dll" () As Long
Private Declare Function IcmpCloseHandle Lib "icmp.dll" (ByVal HANDLE As Long) As Boolean
Private Declare Function IcmpSendEcho Lib "ICMP" (ByVal IcmpHandle As Long, ByVal DestAddress As Long, ByVal RequestData As String, ByVal RequestSize As Integer, RequestOptns As IP_OPTION_INFORMATION, ReplyBuffer As IP_ECHO_REPLY, ByVal ReplySize As Long, ByVal TimeOut As Long) As Boolean

Public Function Ping(sAddr As String, Optional Timeout As Integer = 2000) As Integer
Dim hFile As Long, lpWSAdata As WSAdata
Dim hHostent As Hostent, AddrList As Long
Dim Address As Long, rIP As String
Dim OptInfo As IP_OPTION_INFORMATION
Dim EchoReply As IP_ECHO_REPLY

Call WSAStartup(&H101, lpWSAdata)

If GetHostByName(sAddr + String(64 - Len(sAddr), 0)) <> SOCKET_ERROR Then
    CopyMemory hHostent.h_name, ByVal GetHostByName(sAddr + String(64 - Len(sAddr), 0)), Len(hHostent)
    CopyMemory AddrList, ByVal hHostent.h_addr_list, 4
    CopyMemory Address, ByVal AddrList, 4
End If

hFile = IcmpCreateFile()

If hFile = 0 Then
    Ping = -2 ' MsgBox "Unable to Create File Handle"
    Exit Function
End If

OptInfo.TTL = 255

If IcmpSendEcho(hFile, Address, String(32, "A"), 32, OptInfo, EchoReply, Len(EchoReply) + 8, Timeout) Then
    rIP = CStr(EchoReply.Address(0)) + "." + CStr(EchoReply.Address(1)) + "." + CStr(EchoReply.Address(2)) + "." + CStr(EchoReply.Address(3))
Else
    Ping = -1 ' MsgBox "Timeout"
End If

If EchoReply.Status = 0 Then
    Ping = EchoReply.RoundTripTime
Else
    Ping = -3
End If

IcmpCloseHandle hFile
WSACleanup

End Function

29-06-2004 at 09:30 PM
| Quote Reply
puck18
Level: Trainee

Registered: 25-10-2004
Posts: 1
icon Re: Silent Ping

Hi Lycaon

I tried your module, but I could not use it for testing my company network if I tried google it worked but not our server from the company. aswell as it did not work for any ipadress I tried to use.

Reply 0 what is ment by it did it succeed or failed aswell, as usually I would say 0 is failed

greetz
puck18

[Edited by puck18 on 25-10-2004 at 03:29 PM GMT]

25-10-2004 at 02:15 PM
View Profile Send Email to User Show All Posts | Quote Reply
djjwp
Level: Trainee

Registered: 15-05-2005
Posts: 2
icon Re: Silent Ping

Good question...

I'm in the market for the same use and this is what I figured out.

-3 means request timed out and 0 and above is the average reply time to the ping.  

0 is a great reply time and the higher the number, the slower the average reply time was.

[Edited by djjwp on 15-05-2005 at 01:06 AM GMT]

15-05-2005 at 05:41 AM
View Profile Send Email to User Show All Posts Visit Homepage | Quote Reply
mmete
Level: Trainee

Registered: 26-09-2005
Posts: 1
icon Re: Silent Ping

This works fine, enjoy MMete

Function PingSilent(strComputer)

Set objPing = GetObject("winmgmts:{impersonationLevel=impersonate}")._
ExecQuery("select * from Win32_PingStatus where address = '"_
& strComputer & "'")
            
For Each objStatus in objPing
If IsNull(objStatus.StatusCode) or objStatus.StatusCode<>0 Then
'WScript.Echo("Computer " & machine & " is not reachable")
PingSilent = 0
Else
         'WScript.Echo("Computer " & machine & " is Live")
         PingSilent = 1
End If
Next

End Function

quote:
hg363 wrote:
Can any one supply me with some VB6 code to perform a silent PING.

By silent I mean, no DOS screens to appear and the PING is performed in the background without the user being aware.

The code also needs to capture the output and I do not require any thing fancy, just some simple code to PING from one PC to another, on a network.

The code does not need Time-To-Live, number of replies etc etc, it just needs to be able to establish if another PC is on the network.

If you can help, please send me an example my email address is

h.ghelani1@rbh.nthames.nhs.uk

26-09-2005 at 06:52 PM
View Profile Send Email to User Show All Posts | Quote Reply
coolcurrent4u
Level: Trainee

Registered: 20-04-2010
Posts: 1
icon Re: Silent Ping

This code does not ping ipv6 address, it just gives error returning 0

09-08-2010 at 07:15 AM
View Profile Send Email to User Show All Posts | Quote Reply
AndreaVB Forum : Network : Silent Ping
Previous Topic (How to retrive active network connection using vb.net)Next Topic (How to capture Screenshot in other Computer) New Topic New Poll Post Reply
Surf To:


Not Logged In? Username: Password: Lost your password?
borderAndreaVB free resources for Visual Basic developersborder
borderAndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2014 Andrea Tincaniborder