首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Excel VBA Ping a设备

Excel VBA Ping a设备
EN

Stack Overflow用户
提问于 2016-01-08 22:03:20
回答 1查看 1.9K关注 0票数 1

我一直在四处寻找一种方法来ping网络上的设备而不进行炮击(我真的不想让用户看到一些东西试图ping只得到结果),我想像下面这样的过程。

代码语言:javascript
复制
Sub pingdevice(myip As String)
Dim Pingable As Boolean

   'Code here to ping device using myip variable and return result true or false to pingable variable

   If Pingable = True Then
      'Do Something
   Else
      msgbox "Device not pingable"
   End IF
End Sub
EN

回答 1

Stack Overflow用户

发布于 2016-01-08 22:38:39

请不要介意为同样的事情后的任何人找到我下面的答案代码

代码语言:javascript
复制
Option Explicit

Private Declare Function IcmpCreateFile Lib "icmp.dll" () As Long

Private Declare Function inet_addr Lib "WSOCK32.DLL" (ByVal cp As String) 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 Long, _
    ByVal RequestOptions As Long, _
    ReplyBuffer As ICMP_ECHO_REPLY, _
    ByVal ReplySize As Long, _
    ByVal timeout As Long) As Long

Private Type IP_OPTION_INFORMATION
   Ttl             As Byte
   Tos             As Byte
   Flags           As Byte
   OptionsSize     As Byte
   OptionsData     As Long
End Type

Public Type ICMP_ECHO_REPLY
   address         As Long
   Status          As Long
   RoundTripTime   As Long
   DataSize        As Long
   Reserved        As Integer
   ptrData                 As Long
   Options        As IP_OPTION_INFORMATION
   data            As String * 250
End Type

Public Function Ping(strAddress As String, Reply As ICMP_ECHO_REPLY) As Boolean

Dim hIcmp As Long
Dim lngAddress As Long
Dim lngTimeOut As Long
Dim strSendText As String

'Short string of data to send
strSendText = "blah"

' timeout value in ms
lngTimeOut = 1000

'Convert string address to a long
lngAddress = inet_addr(strAddress)

If (lngAddress <> -1) And (lngAddress <> 0) Then

    hIcmp = IcmpCreateFile()

    If hIcmp <> 0 Then
        'Ping the destination IP
        Call IcmpSendEcho(hIcmp, lngAddress, strSendText, Len(strSendText), 0, Reply, Len(Reply), lngTimeOut)

        'Reply status
        Ping = (Reply.Status = 0)

        'Close the Icmp handle.
        IcmpCloseHandle hIcmp
    Else
        Ping = False
    End If
Else
    Ping = False
End If

End Function

Sub TestPinger()
   Dim pingable As Boolean, lngStatus As ICMP_ECHO_REPLY
   pingable = Ping("192.168.1.101", lngStatus)
   MsgBox pingable
End Sub
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34678896

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档