Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Using Ping in VB6.0 ?

2,320 views
Skip to first unread message

KG

unread,
Nov 4, 1998, 3:00:00 AM11/4/98
to
Hi,

Can somebody tell me how I can ping to another computer just to see
if that computer is still on the network ?

How can I do this in VB50 without using "ping.exe" ??

May thanks,
Karel

Ricky Harjes

unread,
Nov 4, 1998, 3:00:00 AM11/4/98
to
Check out
www.mvps.org/vbnet/

--
Ricky
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
ICQ# 4730537 EGN# 16191
kharjes at dgi dot net
http://www.geocities.com/~howdy
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
Please Reply To The Newsgroups
KG wrote in message <71p9qk$kgr$1...@news3.Belgium.EU.net>...

Rajeesh V.K.

unread,
Nov 11, 1998, 3:00:00 AM11/11/98
to KG
Karel,
Did  you try VB Shell commnad,
you can ping to another computer and check whether the computer is in network by using following code

z = Shell("ping.exe 169.18.6.20", vbHide) 'use your own IP Address

if z> 0 then
    'computer is still on network
end if

--
RAJEESH .V.K.
http://members.tripod.com/~rajeesh/

Bob Butler

unread,
Nov 11, 1998, 3:00:00 AM11/11/98
to
Rajeesh V.K. wrote in message <364902ED...@hotmail.com>...

>Karel,
>Did you try VB Shell commnad,
>you can ping to another computer and check whether the computer is in
>network by using following code
>
>z = Shell("ping.exe 169.18.6.20", vbHide) 'use your own IP Address
>
>if z> 0 then
> 'computer is still on network
>end if


huh? That will tell you only if Shell had an error or returned a process ID
> 0. It says nothing about the result of the PING. You could use:
z = Shell(Environ$("comspec" & " /c ping.exe 169.18.6.20 > pingout.tmp",
vbHide)
Then wait for the PING process to finish and parse the contents of the
'pingout.tmp' file, but that's the only way Shell would be of use.

To do it in code you must call the routines in ICMP.DLL and for an example
of that see http://home.earthlink.net/~butlerbob/vb/network/tcpip/ping.htm


Frank Weber

unread,
Nov 12, 1998, 3:00:00 AM11/12/98
to

Bob Butler schrieb:

> To do it in code you must call the routines in ICMP.DLL and for an example
> of that see http://home.earthlink.net/~butlerbob/vb/network/tcpip/ping.htm

Nice piece of code !!!
Very helpful to me, too !!!
Is there a way to make this routine do also a DNS-Lookup (resolving the full
name when entering an ipadress) ????

best regards

Frank

Bob Butler

unread,
Nov 12, 1998, 3:00:00 AM11/12/98
to
Frank Weber wrote in message <364AF0BB...@TIC.thyssen.com>...

>Bob Butler schrieb:
>> To do it in code you must call the routines in ICMP.DLL and for an
example
>> of that see
http://home.earthlink.net/~butlerbob/vb/network/tcpip/ping.htm
>
>Is there a way to make this routine do also a DNS-Lookup (resolving the
full
>name when entering an ipadress) ????


If you mean that literally (user enters IP, you display hostname) then
that's a reverse-DNS lookup. Usually the user enters a name and you need to
translate it to IP. The regular DNS lookup is there in the NameLookup
function but I haven't ever looked at doing a reverse lookup. It would
meanusing the GetHostByAddr cal instead of GetHostByName and should be
possible with relatively few tweaks to that code.


Frank Weber

unread,
Nov 13, 1998, 3:00:00 AM11/13/98
to

Bob Butler schrieb:

> If you mean that literally (user enters IP, you display hostname) then
> that's a reverse-DNS lookup. Usually the user enters a name and you need to
> translate it to IP. The regular DNS lookup is there in the NameLookup
> function but I haven't ever looked at doing a reverse lookup. It would
> meanusing the GetHostByAddr cal instead of GetHostByName and should be
> possible with relatively few tweaks to that code.

Sorry !!! I'm not an expert in calling .DLL funcions (yet). ;-)

Anybody got some spare time to implement the GetHostByAddr in this code ??

By the way: are there any problems to expect when running this routine under
VB5.0, is it
necessary to use VB6.0 ???

Bob Butler

unread,
Nov 13, 1998, 3:00:00 AM11/13/98
to
Frank Weber wrote in message <364C07BB...@TIC.thyssen.com>...
>Bob Butler schrieb:

>> meanusing the GetHostByAddr cal instead of GetHostByName and should be
>> possible with relatively few tweaks to that code.
>Sorry !!! I'm not an expert in calling .DLL funcions (yet). ;-)


I started looking at it last night but I haven't been able to get
GetHostByAddr working. Some quick searches through dejanews indicates that
there may be some strange issues with the byte order of the IP address to
send to the call so I'll have to do some digging. I will be ading this to
the code but can't guarantee when I'll have enough time to figure it out.
With luck, maybe over the weekend.


Bob Butler

unread,
Nov 13, 1998, 3:00:00 AM11/13/98
to
Frank Weber wrote in message <364C07BB...@TIC.thyssen.com>...
>Bob Butler schrieb:
<cut>

>Anybody got some spare time to implement the GetHostByAddr in this code ??
>
>By the way: are there any problems to expect when running this routine
under
>VB5.0, is it
>necessary to use VB6.0 ???


I figured out my problem (I had copied some code from an old VB3 app and
forgot to update the Declare for 32-bit... just a stupid oversight on my
part). Once I did that the gethostbyaddr call started working fine. I
updated the page so that the NameLookup function now accepts a string with
either a hostname or an IP (standard dotted decimal format) and performs the
translation either direction.

http://home.earthlink.net/~butlerbob/vb/network/tcpip/ping.htm


Frank Weber

unread,
Nov 16, 1998, 3:00:00 AM11/16/98
to

Bob Butler schrieb:

> I figured out my problem (I had copied some code from an old VB3 app and
> forgot to update the Declare for 32-bit... just a stupid oversight on my
> part). Once I did that the gethostbyaddr call started working fine. I
> updated the page so that the NameLookup function now accepts a string with
> either a hostname or an IP (standard dotted decimal format) and performs the
> translation either direction.
>
> http://home.earthlink.net/~butlerbob/vb/network/tcpip/ping.htm

SMOOOOOOOTHHHHHH!!!!!!

Great stuff !!! Works fine on NT4.0 and W95 !!! Thank you , Bob !!!!

Once the routine ist started, can it be interrupted on a keypress (i.e. pinging
the whole subnet 1-255 in a loop and interrupt by pressing ESC) ??

Where can I find a documentation about all the routines included in the ICMP.DLL
(I want to figure out other options by myself) ???

best regards

Frank


Frank Weber

unread,
Nov 30, 1998, 3:00:00 AM11/30/98
to

Bob Butler schrieb:

> I figured out my problem (I had copied some code from an old VB3 app and
> forgot to update the Declare for 32-bit... just a stupid oversight on my
> part). Once I did that the gethostbyaddr call started working fine. I
> updated the page so that the NameLookup function now accepts a string with
> either a hostname or an IP (standard dotted decimal format) and performs the
> translation either direction.
>
> http://home.earthlink.net/~butlerbob/vb/network/tcpip/ping.htm

One more question:

This routine delivers one ipadress and one hostname, which changes do I have to
make to the NameLookup subfunction to make it give back ALL hostnames (i.e. our
mailservers have up to 10 names) and all ipadresses (some routers have more than
one) of the specified host ???

Here is the complete NameLookup subfunction:

Function NameLookup(strName As String) As String
' routine to convert hostname to IP
' this routine actually gets all known aliases
' and IP addresses but only returns the first IP
Dim x As Long ' scratch
Dim nbytes As Long
Dim strTarget As String ' null-delimited hostname
Dim lngHostEnt As Long ' address of hostent structure
Dim lngHEName As Long ' address of name pointer
Dim lngHEAlias As Long ' address of alias pointer
Dim lngHEAddress As Long ' address of address pointer
Dim lngIPpointer As Long ' address of IP address
Dim lngAPointer As Long ' address of Alias

Dim intAliasCount As Long
Dim intAddressCount As Long
Dim strIP() As String
Dim strAlias() As String
Dim strAddress() As String

'default values
intAliasCount = 0
intAddressCount = 0
NameLookup = ""
strTarget = strName & vbNullChar
Debug.Print "Resolve: " & strName
lngHostEnt = gethostbyname(strTarget) ' do actual winsock call
If lngHostEnt = 0 Then
NameLookup = 0
Exit Function ' failed!
End If
lngHEName = lngHostEnt ' set pointer addresses
lngHEAlias = lngHostEnt + 4
lngHEAddress = lngHostEnt + 12
' convert addresses of pointers to the pointers...
CopyMemory lngHEName, ByVal lngHEName, 4
CopyMemory lngHEAlias, ByVal lngHEAlias, 4
CopyMemory lngHEAddress, ByVal lngHEAddress, 4

' Get resolved hostname
nbytes = lstrlen(ByVal lngHEName)
If nbytes > 0 Then
strName = Space$(nbytes)
CopyMemory ByVal strName, ByVal lngHEName, nbytes
Debug.Print "Full name: " & strName
End If

' get all IP addresses
CopyMemory lngIPpointer, ByVal lngHEAddress, 4
Do While lngIPpointer ' end-of-list is null pointer
ReDim Preserve strAddress(intAddressCount + 1)
strAddress(intAddressCount) = Space$(4)
CopyMemory ByVal strAddress(intAddressCount), ByVal lngIPpointer, 4
Debug.Print "IP address " & CStr(intAddressCount) & _
": " & IPToText(strAddress(intAddressCount))
CopyMemory ByVal lngHEAddress, 0&, 4 ' null for next call
intAddressCount = intAddressCount + 1
' move to next IP
lngHEAddress = lngHEAddress + 4
CopyMemory lngIPpointer, ByVal lngHEAddress, 4
Loop

' get any/all aliases
CopyMemory lngAPointer, ByVal lngHEAlias, 4
Do While lngAPointer ' end-of-list is a null
ReDim Preserve strAlias(intAliasCount + 1)
nbytes = lstrlen(ByVal lngAPointer)
strAlias(intAliasCount) = Space$(nbytes)
CopyMemory ByVal strAlias(intAliasCount), ByVal lngAPointer, nbytes
Debug.Print "Alias " & CStr(intAliasCount) & ": " & _
strAlias(intAliasCount)
CopyMemory ByVal lngHEAlias, 0&, 4
intAliasCount = intAliasCount + 1
' move to next IP
lngHEAlias = lngHEAlias + 4
CopyMemory lngAPointer, ByVal lngHEAlias, 4
Loop

If intAddressCount > 0 Then
' success
NameLookup = IPToText(strAddress(0))
Else
NameLookup = "" ' weird!
End If
End Function

Bob Butler

unread,
Nov 30, 1998, 3:00:00 AM11/30/98
to
Frank Weber wrote in message <3662663C...@TIC.thyssen.com>...
<cut>
>> http://home.earthlink.net/~butlerbob/vb/network/tcpip/ping.htm

>This routine delivers one ipadress and one hostname, which changes do I
have to
>make to the NameLookup subfunction to make it give back ALL hostnames (i.e.
our
>mailservers have up to 10 names) and all ipadresses (some routers have more
than
>one) of the specified host ???
<cut>

> Dim intAliasCount As Long
> Dim intAddressCount As Long
> Dim strIP() As String
> Dim strAlias() As String
> Dim strAddress() As String


You are correct that the routine returns only the primary information but if
you look you'll see that it actually parses all of the data into the arrays
shown above. If you modify the function to return an array or to update
module-level or global arrays instead of these local ones then you will have
all the info. Most of the cde on my web pages is not intended to be used
as-is but rather to demonstrate ways to accomplish specific tasks which
people can then modify & extend as needed.


0 new messages