I am trying to call the ping command from a VB app to test if a web server
is down. I'm basically executing a command line thru CreateProcess and
piping the results to a text file:
This isn't working. pingresults.txt never gets written. If I start a
comand window session (Start|Run|cmd) and then execute the same command,
everything is cool. But, if I do Start|Run|ping www.somesite.com
>pingresults.txt, the command window flashes up but nothing is written to
the output file.
I've also tried calling the executable directly
(c:\winnt\system32\ping.exe), but nothing seems to work. Does anyone know
how I need to call ping thru CreateProcess?
Thanks
MJS
BTW, I am running Win2000 Server.
03-28-2001, 11:06 AM
Mattias Sjögren
Re: calling ping thru vb
Michael,
>Does anyone know how I need to call ping thru CreateProcess?
The piping is handled by cmd.exe or command.com, so your command line
should be
You know, I though about that (calling cmd.exe) while I was at lunch, but it
looks like a moot point now. Didn't know there were dedicated functions for
ICMP. I thought about doing the ping thru the WinSock control but thought
it would too much of a headache to learn the ICMP message format.
Thanks.
MJS
"Mattias Sjögren" <mattias.dont.want.spam@mvps.org> wrote in message
news:3ac20a91.24713145@news.devx.com...
> Michael,
>
> >Does anyone know how I need to call ping thru CreateProcess?
>
> The piping is handled by cmd.exe or command.com, so your command line
> should be
>
> Environ$("COMSPEC") & " /c ping www.somesite.com > pingresult.txt"
>
>
> You can also bypass the command processor and set up the pipes
> yourself:
>
> http://www.msjogren.net/dotnet/eng/s..._cmdoutput.asp
>
>
> And finally, you can do the pinging yourself and not depend on
> ping.exe:
>
> http://www.mvps.org/vbnet/code/netwo...byhostname.htm
>
>
> Mattias
>
> ====================================
> Mattias Sjögren - mattias @ mvps.org
> http://www.msjogren.net/dotnet/
>
> CodeHound - The Software Developer's Search Engine
> http://www.codehound.com
03-28-2001, 01:13 PM
Michael Shutt
Re: calling ping thru vb
You know, I though about that (calling cmd.exe) while I was at lunch, but it
looks like a moot point now. Didn't know there were dedicated functions for
ICMP. I thought about doing the ping thru the WinSock control but thought
it would too much of a headache to learn the ICMP message format.
Thanks.
MJS
"Mattias Sjögren" <mattias.dont.want.spam@mvps.org> wrote in message
news:3ac20a91.24713145@news.devx.com...
> Michael,
>
> >Does anyone know how I need to call ping thru CreateProcess?
>
> The piping is handled by cmd.exe or command.com, so your command line
> should be
>
> Environ$("COMSPEC") & " /c ping www.somesite.com > pingresult.txt"
>
>
> You can also bypass the command processor and set up the pipes
> yourself:
>
> http://www.msjogren.net/dotnet/eng/s..._cmdoutput.asp
>
>
> And finally, you can do the pinging yourself and not depend on
> ping.exe:
>
> http://www.mvps.org/vbnet/code/netwo...byhostname.htm
>
>
> Mattias
>
> ====================================
> Mattias Sjögren - mattias @ mvps.org
> http://www.msjogren.net/dotnet/
>
> CodeHound - The Software Developer's Search Engine
> http://www.codehound.com
04-05-2001, 02:25 AM
Piyush Gupta
Re: calling ping thru vb
try this code :
'
'//
'// Structures
'//
Private Type ICMP_OPTIONS
Ttl As Byte
Tos As Byte
Flags As Byte
OptionsSize As Byte
OptionsData As Long
End Type
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
'//
'// Win32s
'//
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, 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)
Dim szHost As String * 256
Dim lpHost As Long
Dim HOST As HOSTENT
Dim dwIPAddr As Long
Dim tmpIPAddr() As Byte
Dim i As Integer
Dim szIPAddr As String
If Not SocketsInitialize() Then
GetIPAddress = ""
Exit Function
End If
If gethostname(szHost, 256) = SOCKET_ERROR Then
GetIPAddress = ""
MsgBox "Windows Sockets error " & Str(WSAGetLastError()) & " has
occurred. Unable to successfully get Host Name.", vbExclamation
SocketsCleanup
Exit Function
End If
If lpHost = 0 Then
GetIPAddress = ""
MsgBox "Windows Sockets are not responding. Unable to successfully
get Host Name.", vbExclamation
SocketsCleanup
Exit Function
End If
End Function
Public Function GetIPHostName() As String
Dim szHost As String * 256
If Not SocketsInitialize() Then
GetIPHostName = ""
Exit Function
End If
If gethostname(szHost, 256) = SOCKET_ERROR Then
GetIPHostName = ""
MsgBox "Windows Sockets error " & Str(WSAGetLastError()) & " has
occurred. Unable to successfully get Host Name.", vbExclamation
SocketsCleanup
Exit Function
End If
hPort = IcmpCreateFile()
x = IcmpSendEcho(hPort, dwAddress, szMessage, Len(szMessage), 0, ECHO,
Len(ECHO), PING_TIMEOUT)
If x = 0 Then
Ping = ECHO.status * -1
Else
' the ping succeeded,
' .Status will be 0
' .RoundTripTime is the time in ms for the ping to complete, .Data
is the data returned (NULL terminated)
' .Address is the Ip address that actually replied
' .DataSize is the size of the string in .Data
Ping = ECHO.RoundTripTime
End If
x = IcmpCloseHandle(hPort)
End Function
Function AddressStringToLong(szAddress As String) As Long
Dim szTmp As String, i As Integer, Octets(1 To 4) As String
szTmp = szAddress
i = 0
While InStr(szTmp, ".") > 0
i = i + 1
Octets(i) = Mid(szTmp, 1, InStr(szTmp, ".") - 1)
szTmp = Mid(szTmp, InStr(szTmp, ".") + 1)
Wend
i = i + 1
Octets(i) = szTmp
If i <> 4 Then
AddressStringToLong = 0
Exit Function
End If
End Function
Public 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
Public 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.", vbExclamation
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
Public Function DetermineIP(szTmp As String) As Boolean
On Error GoTo Handler
Dim i As Integer
Dim nOctet As Integer
While InStr(szTmp, ".") > 0
i = i + 1
nOctet = Mid(szTmp, 1, InStr(szTmp, ".") - 1)
szTmp = Mid(szTmp, InStr(szTmp, ".") + 1)
Wend
If (i <> 3) Then
DetermineIP = False
Else
DetermineIP = True
End If
Exit Function
Handler:
DetermineIP = False
End Function
"Michael Shutt" <mshutt@mediaone.net> wrote in message
news:3ac20772$1@news.devx.com...
> I am trying to call the ping command from a VB app to test if a web server
> is down. I'm basically executing a command line thru CreateProcess and
> piping the results to a text file:
>
> ping www.somesite.com >pingresults.txt
>
> This isn't working. pingresults.txt never gets written. If I start a
> comand window session (Start|Run|cmd) and then execute the same command,
> everything is cool. But, if I do Start|Run|ping www.somesite.com
> >pingresults.txt, the command window flashes up but nothing is written to
> the output file.
>
> I've also tried calling the executable directly
> (c:\winnt\system32\ping.exe), but nothing seems to work. Does anyone know
> how I need to call ping thru CreateProcess?
>
> Thanks
>
> MJS
>
> BTW, I am running Win2000 Server.
>
>
04-05-2001, 02:25 AM
Piyush Gupta
Re: calling ping thru vb
try this code :
'
'//
'// Structures
'//
Private Type ICMP_OPTIONS
Ttl As Byte
Tos As Byte
Flags As Byte
OptionsSize As Byte
OptionsData As Long
End Type
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
'//
'// Win32s
'//
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, 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)
Dim szHost As String * 256
Dim lpHost As Long
Dim HOST As HOSTENT
Dim dwIPAddr As Long
Dim tmpIPAddr() As Byte
Dim i As Integer
Dim szIPAddr As String
If Not SocketsInitialize() Then
GetIPAddress = ""
Exit Function
End If
If gethostname(szHost, 256) = SOCKET_ERROR Then
GetIPAddress = ""
MsgBox "Windows Sockets error " & Str(WSAGetLastError()) & " has
occurred. Unable to successfully get Host Name.", vbExclamation
SocketsCleanup
Exit Function
End If
If lpHost = 0 Then
GetIPAddress = ""
MsgBox "Windows Sockets are not responding. Unable to successfully
get Host Name.", vbExclamation
SocketsCleanup
Exit Function
End If
End Function
Public Function GetIPHostName() As String
Dim szHost As String * 256
If Not SocketsInitialize() Then
GetIPHostName = ""
Exit Function
End If
If gethostname(szHost, 256) = SOCKET_ERROR Then
GetIPHostName = ""
MsgBox "Windows Sockets error " & Str(WSAGetLastError()) & " has
occurred. Unable to successfully get Host Name.", vbExclamation
SocketsCleanup
Exit Function
End If
hPort = IcmpCreateFile()
x = IcmpSendEcho(hPort, dwAddress, szMessage, Len(szMessage), 0, ECHO,
Len(ECHO), PING_TIMEOUT)
If x = 0 Then
Ping = ECHO.status * -1
Else
' the ping succeeded,
' .Status will be 0
' .RoundTripTime is the time in ms for the ping to complete, .Data
is the data returned (NULL terminated)
' .Address is the Ip address that actually replied
' .DataSize is the size of the string in .Data
Ping = ECHO.RoundTripTime
End If
x = IcmpCloseHandle(hPort)
End Function
Function AddressStringToLong(szAddress As String) As Long
Dim szTmp As String, i As Integer, Octets(1 To 4) As String
szTmp = szAddress
i = 0
While InStr(szTmp, ".") > 0
i = i + 1
Octets(i) = Mid(szTmp, 1, InStr(szTmp, ".") - 1)
szTmp = Mid(szTmp, InStr(szTmp, ".") + 1)
Wend
i = i + 1
Octets(i) = szTmp
If i <> 4 Then
AddressStringToLong = 0
Exit Function
End If
End Function
Public 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
Public 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.", vbExclamation
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
Public Function DetermineIP(szTmp As String) As Boolean
On Error GoTo Handler
Dim i As Integer
Dim nOctet As Integer
While InStr(szTmp, ".") > 0
i = i + 1
nOctet = Mid(szTmp, 1, InStr(szTmp, ".") - 1)
szTmp = Mid(szTmp, InStr(szTmp, ".") + 1)
Wend
If (i <> 3) Then
DetermineIP = False
Else
DetermineIP = True
End If
Exit Function
Handler:
DetermineIP = False
End Function
"Michael Shutt" <mshutt@mediaone.net> wrote in message
news:3ac20772$1@news.devx.com...
> I am trying to call the ping command from a VB app to test if a web server
> is down. I'm basically executing a command line thru CreateProcess and
> piping the results to a text file:
>
> ping www.somesite.com >pingresults.txt
>
> This isn't working. pingresults.txt never gets written. If I start a
> comand window session (Start|Run|cmd) and then execute the same command,
> everything is cool. But, if I do Start|Run|ping www.somesite.com
> >pingresults.txt, the command window flashes up but nothing is written to
> the output file.
>
> I've also tried calling the executable directly
> (c:\winnt\system32\ping.exe), but nothing seems to work. Does anyone know
> how I need to call ping thru CreateProcess?
>
> Thanks
>
> MJS
>
> BTW, I am running Win2000 Server.
>
>