VB and Ping 
Author Message
 VB and Ping

Is it possible to ping a device on the network to see if it is available, using
VB?


Sat, 08 May 2004 21:27:23 GMT  
 VB and Ping
Check Randy Birch's VBNet site http://www.mvps.org/vbnet/

It's the first entry in his Top 10 The most populate code pages last week
(and has been for weeks!)

--
Doug Steele, Microsoft Access MVP
http://I.Am/DougSteele


Quote:
> Is it possible to ping a device on the network to see if it is available,
using
> VB?



Sun, 09 May 2004 07:53:14 GMT  
 VB and Ping
It sure is!!!

Use a few API calls to redirect output from the command line... Copy
the following text into a form's code window, add two textboxes to the
form called Text1 and Text2, set the Muliline property of Text2 to
true. Also add a command button called Command1:

'------------------------------------------------------------
'| Code starts here!
'V-----------------------------------------------------------
Option Explicit
Private Declare Function CreatePipe Lib "kernel32" (phReadPipe As
Long, phWritePipe As Long, lpPipeAttributes As SECURITY_ATTRIBUTES,
ByVal nSize As Long) As Long
Private Declare Sub GetStartupInfo Lib "kernel32" Alias
"GetStartupInfoA" (lpStartupInfo As STARTUPINFO)
Private Declare Function CreateProcess Lib "kernel32" Alias
"CreateProcessA" (ByVal lpApplicationName As String, ByVal
lpCommandLine As String, lpProcessAttributes As Any,
lpThreadAttributes As Any, ByVal bInheritHandles As Long, ByVal
dwCreationFlags As Long, lpEnvironment As Any, ByVal
lpCurrentDriectory As String, lpStartupInfo As STARTUPINFO,
lpProcessInformation As PROCESS_INFORMATION) As Long
Private Declare Function SetWindowText Lib "user32" Alias
"SetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String) As
Long
Private Declare Function ReadFile Lib "kernel32" (ByVal hFile As Long,
lpBuffer As Any, ByVal nNumberOfBytesToRead As Long,
lpNumberOfBytesRead As Long, lpOverlapped As Any) As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA"
(ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam
As Any) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As
Long) As Long

Private Type SECURITY_ATTRIBUTES
  nLength As Long
  lpSecurityDescriptor As Long
  bInheritHandle As Long
End Type

Private Type PROCESS_INFORMATION
  hProcess As Long
  hThread As Long
  dwProcessId As Long
  dwThreadId As Long
End Type

Private Type STARTUPINFO
  cb As Long
  lpReserved As Long
  lpDesktop As Long
  lpTitle As Long
  dwX As Long
  dwY As Long
  dwXSize As Long
  dwYSize As Long
  dwXCountChars As Long
  dwYCountChars As Long
  dwFillAttribute As Long
  dwFlags As Long
  wShowWindow As Integer
  cbReserved2 As Integer
  lpReserved2 As Byte
  hStdInput As Long
  hStdOutput As Long
  hStdError As Long
End Type

Private Type OVERLAPPED
    ternal As Long
    ternalHigh As Long
    offset As Long
    OffsetHigh As Long
    hEvent As Long
End Type

Private Const STARTF_USESHOWWINDOW = &H1
Private Const STARTF_USESTDHANDLES = &H100
Private Const SW_HIDE = 0
Private Const EM_SETSEL = &HB1
Private Const EM_REPLACESEL = &HC2

Private Sub Command1_Click()
  Command1.Enabled = False
  Redirect Text1.Text, Text2
  Command1.Enabled = True
End Sub
Private Sub Form_Load()
    Text1.Text = "ping"
End Sub
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
  If Command1.Enabled = False Then Cancel = True
End Sub

Sub Redirect(cmdLine As String, objTarget As Object)
  Dim i%, t$
  Dim pa As SECURITY_ATTRIBUTES
  Dim pra As SECURITY_ATTRIBUTES
  Dim tra As SECURITY_ATTRIBUTES
  Dim pi As PROCESS_INFORMATION
  Dim sui As STARTUPINFO
  Dim hRead As Long
  Dim hWrite As Long
  Dim bRead As Long
  Dim lpBuffer(1024) As Byte
  pa.nLength = Len(pa)
  pa.lpSecurityDescriptor = 0
  pa.bInheritHandle = True

  pra.nLength = Len(pra)
  tra.nLength = Len(tra)

  If CreatePipe(hRead, hWrite, pa, 0) <> 0 Then
    sui.cb = Len(sui)
    GetStartupInfo sui
    sui.hStdOutput = hWrite
    sui.hStdError = hWrite
    sui.dwFlags = STARTF_USESHOWWINDOW Or STARTF_USESTDHANDLES
    sui.wShowWindow = SW_HIDE
    If CreateProcess(vbNullString, cmdLine, pra, tra, True, 0, Null,
vbNullString, sui, pi) <> 0 Then
      SetWindowText objTarget.hwnd, ""
      Do
        Erase lpBuffer()
        If ReadFile(hRead, lpBuffer(0), 1023, bRead, ByVal 0&) Then
          SendMessage objTarget.hwnd, EM_SETSEL, -1, 0
          SendMessage objTarget.hwnd, EM_REPLACESEL, False,
lpBuffer(0)
          DoEvents
        Else
          Exit Do
        End If
        CloseHandle hWrite
      Loop
      CloseHandle hRead
    End If
  End If
End Sub
'^----------------------------------------------------
'| Code ends here!
'-----------------------------------------------------

Now run the exe and in Text1 type the ping command followed by the IP
address/hostname and see the results appear in Text2!!!

Enjoy!

-Ben

Quote:

> Is it possible to ping a device on the network to see if it is available, using
> VB?



Wed, 12 May 2004 00:34:17 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. How to make a VB program PING automatically

2. VB and Ping protocol?

3. PING, Is there an API (or other method) to call ping from VB

4. Ping writing ping component voor asp

5. "PING FROM VB 5" - "PING desde VB 5"

6. "PING FROM VB 5" - "PING desde VB 5"

7. Ping in Vb.net

8. ICMP in VB .NET? (ICMP=Ping)

9. Ping in VB.net

10. How to ping with VB.NET

11. pinging websites via vb.net

12. TraceRoute PINGs from VB

 

 
Powered by phpBB® Forum Software