'2个TextBox :
Text1 - 输入ip
Text2 - 显示结果
'1个Timer : Timer1
'1个CommandButton : Command1
Option Explicit
Private Const NORMAL_PRIORITY_CLASS = &H20&
Private Const STARTF_USESTDHANDLES = &H100&
Private Const STARTF_USESHOWWINDOW = &H1
Private Type SECURITY_ATTRIBUTES
nLength As Long
lpSecurityDescriptor As Long
bInheritHandle 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 Long
hStdInput As Long
hStdOutput As Long
hStdError As Long
End Type
Private Type PROCESS_INFORMATION
hProcess As Long
hThread As Long
dwProcessID As Long
dwThreadID As Long
End Type
Const SWP_NOMOVE = &H2
Const SWP_NOSIZE = &H1
Const FLAG = SWP_NOMOVE Or SWP_NOSIZE
Const HWND_TOPMOST = -1
Const HWND_NOTOPMOST = -2
Const HWND_TOP = 0
Const HWND_BOTTOM = 1
Dim Proc As PROCESS_INFORMATION '进程信息
Dim Start As STARTUPINFO '启动信息
Dim SecAttr As SECURITY_ATTRIBUTES '安全属性
Dim hReadPipe As Long '读取管道句柄
Dim hWritePipe As Long '写入管道句柄
Dim lngBytesRead As Long '读出数据的字节数
Dim strBuffer As String * 256 '读取管道的字符串buffer
Dim Command As String 'DOS命令
Dim ret As Long 'API函数返回值
Private Declare Function SetWindowPos Lib "user32" _
(ByVal hwnd As Long, ByVal hWndInsertAfter As Long, ByVal x As Long, _
ByVal y As Long, ByVal cx As Long, ByVal cy As Long, _
ByVal wFlags As Long) As Long
Private Declare Function CreatePipe Lib "kernel32" (phReadPipe As Long, phWritePipe As Long, lpPipeAttributes As SECURITY_ATTRIBUTES, ByVal nSize As Long) As Long
Private Declare Function CreateProcess Lib "kernel32" Alias "CreateProcessA" (ByVal lpApplicationName As String, ByVal lpCommandLine As String, lpProcessAttributes As SECURITY_ATTRIBUTES, lpThreadAttributes As SECURITY_ATTRIBUTES, 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 ReadFile Lib "kernel32" (ByVal hFile As Long, ByVal lpBuffer As String, ByVal nNumberOfBytesToRead As Long, lpNumberOfBytesRead As Long, ByVal lpOverlapped As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Sub Command1_Click()
If Not InitPipe Then
Exit Sub
Else
'init
Dim s As String
s = ReadPipe
'Me.Text2.Text = s
Me.Timer1.Enabled = True
End If
End Sub
Private Sub Form_Load()
Call SetWindowPos(Me.hwnd, HWND_TOPMOST, 0, 0, 0, 0, FLAG)
Me.Command1.Caption = "Start"
Me.Timer1.Enabled = False
Me.Timer1.Interval = 300
Me.Text1.Text = "222.210.27.114"
End Sub
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
ClosePipe
End Sub
Private Sub Timer1_Timer()
Dim strPipe As String
On Error Resume Next
strPipe = ReadPipe()
If Len(strPipe) > 0 Then
If InStr(1, strPipe, "time") > 0 Then
Dim lPosStart As Long
Dim lPosEnd As Long
Dim sMS As String
lPosStart = InStr(strPipe, "time=")
lPosEnd = InStr(strPipe, "ms")
sMS = Mid(strPipe, lPosStart + 5, lPosEnd - lPosStart - 5)
'Text2.Text = Now & "==============>" & vbCrLf & strPipe & vbCrLf & Text2.Text
Text2.Text = sMS & vbCrLf & Text2.Text
End If
End If
End Sub
Private Function InitPipe() As Boolean
'设置安全属性
With SecAttr
.nLength = LenB(SecAttr)
.bInheritHandle = True
.lpSecurityDescriptor = 0
End With
'创建管道
ret = CreatePipe(hReadPipe, hWritePipe, SecAttr, 0)
If ret = 0 Then
MsgBox "无法创建管道", vbExclamation, "错误"
GoTo ErrHdr
End If
'设置进程启动前的信息
With Start
.cb = LenB(Start)
.dwFlags = STARTF_USESHOWWINDOW Or STARTF_USESTDHANDLES
.hStdOutput = hWritePipe '设置输出管道
.hStdError = hWritePipe '设置错误管道
End With
'启动进程
Command = "c:\windows\system32\ping.exe -t " & Me.Text1.Text 'DOS进程以ipconfig.exe为例
ret = CreateProcess(vbNullString, Command, SecAttr, SecAttr, True, NORMAL_PRIORITY_CLASS, ByVal 0, vbNullString, Start, Proc)
If ret = 0 Then
MsgBox "无法启动新进程", vbExclamation, "错误"
ret = CloseHandle(hWritePipe)
ret = CloseHandle(hReadPipe)
GoTo ErrHdr
End If
If False Then
ErrHdr:
InitPipe = False
Exit Function
End If
InitPipe = True
End Function
Private Function ReadPipe() As String
Dim lpOutputs As String
'因为无需写入数据,所以先关闭写入管道。而且这里必须关闭此管道,否则将无法读取数据
ret = CloseHandle(hWritePipe)
'从输出管道读取数据,每次最多读取256字节
ret = ReadFile(hReadPipe, strBuffer, 256, lngBytesRead, ByVal 0)
lpOutputs = lpOutputs & Left(strBuffer, lngBytesRead)
ReadPipe = lpOutputs
End Function
Private Sub ClosePipe()
On Error Resume Next
'读取操作完成,关闭各句柄
ret = CloseHandle(Proc.hProcess)
ret = CloseHandle(Proc.hThread)
ret = CloseHandle(hReadPipe)
End Sub
温馨提示:内容为网友见解,仅供参考
用vb编写一个调用ping.exe程序。
Text1 - 输入ip Text2 - 显示结果 '1个Timer : Timer1 '1个CommandButton : Command1 Option Explicit Private Const NORMAL_PRIORITY_CLASS = &H20& Private Const STARTF_USESTDHANDLES = &H100& Private Const STARTF_USESHOWWINDOW = &H1 Private Type SECURITY_ATTRIBUTES nLength As Long l...
VB调用PING命令
Public Function Pings(strMachines As String) As Boolean aMachines = Split(strMachines, ";")For Each machine In aMachines Set objPing = GetObject("winmgmts:{impersonationLevel=impersonate}").ExecQuery("select * from Win32_PingStatus where address = '" & machine & "'")For Each o...
怎么在vb中调用ping命令,并将ping的过程显示在vb界面中,就是把运行...
添加一个文本框 Set objShell = CreateObject("WScript.Shell")Set objWshScriptExec = objShell.Exec("ping 192.168.6.242")Set objStdOut = objWshScriptExec.StdOut Do Until objStdOut.AtEndOfStream strLine = objStdOut.ReadLine Text1.Text = Text1.Text & strLine & vbNewLine Loop ...
如何在vb中使用ping?
Dim pid As Long pid = Shell("cmd.exe \/C Ping " & Text1.Text & " > c:\\r.txt", vbHide) ' 提示 Text2.Text = "正在执行Ping " & Text1.Text & " ..." '等待Shell执行结束 Dim hProc As Long hProc = OpenProcess(PROCESS_QUERY_INFORMATION, 0, pid) ...
vb怎么实现ping,并将ping结果搞到手。
Dim pid As Long pid = Shell("cmd.exe \/C Ping " & Text1.Text & " > c:\\r.txt", vbHide) ' 提示 Text2.Text = "正在执行Ping " & Text1.Text & " ..." '等待Shell执行结束 Dim hProc As Long hProc = OpenProcess(PROCESS_QUERY_INFORMATION, 0, pid) ...
VB如何调用ping命令来判断是否联网!?
Private Const MIN_SOCKETS_REQD As Long = 1 Private Const SOCKET_ERROR As Long = -1 Private Const INADDR_NONE As Long = &HFFFFFFFF Private Const MAX_WSADescription As Long = 256 Private Const MAX_WSASYSStatus As Long = 128 Public PingTime As Long Private Type ICMP_OPTIONS T...
VB 怎么实现ping
说明:不是调用cmd命令ping,完全是内置的。用法:PingIP("202.108.22.142", TTL(可选,默认10), TimeOut(可选,默认1000)),返回延时时长 注意:不能ping域名。可自定义TTL和超时时间。这个是改国外的,原版超级啰嗦。给精简了。原作者是谁已不得而知。以下是Ping 模块代码:Option Explicit '...
vb调用ping命令检测网络
Public Function CmdPing(ByVal strIp As String) As String Dim p As New Process '创建一个线程 p.StartInfo.FileName = "cmd.exe"p.StartInfo.UseShellExecute = False p.StartInfo.RedirectStandardInput = True p.StartInfo.RedirectStandardOutput = True p.StartInfo.RedirectStandardError = True...
每一分钟检测网络一次(ping )如不通结束进程,如果通启动进程.VBs\/bat\/...
'其中:"61.135.169.125"为默认不许修改 "cmd.exe"是需要结束的进程 "d:\\qq.exe"是需要打开的程序 10*60是10分钟后自动退出检测 Call p("61.135.169.125","cmd.exe","d:\\qq.exe",10*60)Sub p(ip,exname,exstart,t)tem_time=Int(timer)strs=array,48,,50,44,52,54,44...
用VB.NET怎么编一个ping的程序
Net.NetworkInformation.PingReply = m_ping.Send("192.168.1.1", 1000)'设置为自己要ping的ip地址 If m_PingReply.Status = Net.NetworkInformation.IPStatus.Success Then MsgBox(m_PingReply.RoundtripTime)'返回网络延迟 Else'返回不通的原因 MsgBox(m_PingReply.Status.ToString)End If ...