473,847 Members | 1,851 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to perform a broadcast ping or IcmpSendEcho in VB.NET using icmp.dll?

Ed
I've attached some VB.NET code I've hacked together (some taken from
MS examples & newsgroup postings) that will perform a ping or
IcmpSendEcho using the icmp.dll (see this for more info:
http://support.microsoft.com/default...b;en-us;170591 ).

The problem I have is in order to perform a discovery/ping of an
entire subnet (192.168.1.* for instance) I have to do a FOR loop to
itterate through all of the addresses. That it seems VERY inefficient
to me.

I originally tried to use the IcmpSendEcho function in IPHLPAPI.DLL
version 5.0.2195.6602 (see these for more info:

http://msdn.microsoft.com/library/de..._functions.asp

http://msdn.microsoft.com/library/en...mpsendecho.asp
)

because the documentation for the IcmpSendEcho in the Platform SDK IP
Helper indicated that replybuffer was an array of ICMP_ECHO_REPLY
structures (and I was hoping to be able to perform a broadcast ping
and have all of the results in this array). However, it doesn't
actually seem to support this function...I got an error indicating
that my program couldn't find the entry point, and upon reviewing the
..DLL I didn't see that function listed in it. So, I reverted to using
the function in icmp.dll.

Essentially, I'd like to be able to ping 255.255.255.255 (the
broadcast IP address) a single time and get a list of all of the
successful pings. I'd appreciate any/all help with this. Does anyone
know if there are any plans to add ICMP and similar networking support
to the .NET Framework in the future (or if there's currently support
for this that I don't know about)?

Thanks!

-Ed

--------------------------------------------------
Option Strict On
Imports System.Net
Imports System.Net.Sock ets
Imports System.Runtime. InteropServices

Public Class IPHlp

Private Const ICMP_SUCCESS As Long = 0
Private Const ICMP_STATUS_BUF FER_TO_SMALL As Int32 = 11001
'Buffer Too Small
Private Const ICMP_STATUS_DES TINATION_NET_UN REACH As Int32 = 11002
'Destination Net Unreachable
Private Const ICMP_STATUS_DES TINATION_HOST_U NREACH As Int32 =
11003 ' Destination Host Unreachable
Private Const ICMP_STATUS_DES TINATION_PROTOC OL_UNREACH As Int32 =
11004 'Destination Protocol Unreachable
Private Const ICMP_STATUS_DES TINATION_PORT_U NREACH As Int32 =
11005 'Destination Port Unreachable
Private Const ICMP_STATUS_NO_ RESOURCE As Int32 = 11006 'No
Resources
Private Const ICMP_STATUS_BAD _OPTION As Int32 = 11007 'Bad Option
Private Const ICMP_STATUS_HAR DWARE_ERROR As Int32 = 11008
'Hardware Error
Private Const ICMP_STATUS_LAR GE_PACKET As Int32 = 11009 'Packet
Too Big
Private Const ICMP_STATUS_REQ UEST_TIMED_OUT As Int32 = 11010
'Request Timed Out
Private Const ICMP_STATUS_BAD _REQUEST As Int32 = 11011 'Bad
Request
Private Const ICMP_STATUS_BAD _ROUTE As Int32 = 11012 'Bad Route
Private Const ICMP_STATUS_TTL _EXPIRED_TRANSI T As Int32 = 11013
'TimeToLive Expired Transit
Private Const ICMP_STATUS_TTL _EXPIRED_REASSE MBLY As Int32 = 11014
'TimeToLive Expired Reassembly
Private Const ICMP_STATUS_PAR AMETER As Int32 = 11015 'Parameter
Problem
Private Const ICMP_STATUS_SOU RCE_QUENCH As Int32 = 11016 'Source
Quench
Private Const ICMP_STATUS_OPT ION_TOO_BIG As Int32 = 11017 'Option
Too Big
Private Const ICMP_STATUS_BAD _DESTINATION As Int32 = 11018 'Bad
Destination
Private Const ICMP_STATUS_NEG OTIATING_IPSEC As Int32 = 11032
'Negotiating IPSEC
Private Const ICMP_STATUS_GEN ERAL_FAILURE As Int32 = 11050
'General Failure

Public Function Ping(ByVal sIP As String, _
Optional ByRef IP As IPAddress = Nothing, _
Optional ByRef Reply As String = "", _
Optional ByRef RoundTrip As Int32 = 0) As Boolean

IP = Dns.GetHostByNa me(sIP).Address List(0)
Return Ping(IP, Reply, RoundTrip)

End Function
Public Function Ping(ByVal IP As IPAddress, _
Optional ByRef Reply As String = "", _
Optional ByRef RoundTrip As Int32 = 0) As Boolean

Dim ICMPHandle As IntPtr
Dim iIP As Int32
Dim sData As String
Dim oICMPOptions As New IPHlp.ICMP_OPTI ONS
Dim ICMPReply As ICMP_ECHO_REPLY
Dim iReplies As Int32

Ping = False

ICMPHandle = IcmpCreateFile

iIP = System.BitConve rter.ToInt32(IP .GetAddressByte s, 0)
sData = "x" 'Placeholder?
oICMPOptions.Tt l = 255

iReplies = IcmpSendEcho(IC MPHandle, iIP, sData, sData.Length,
oICMPOptions, ICMPReply, Marshal.SizeOf( ICMPReply), 30)

Reply = EvaluatePingRes ponse(ICMPReply .Status)
RoundTrip = ICMPReply.Round TripTime

If ICMPReply.Statu s = 0 Then
Ping = True
End If

ICMPHandle = Nothing
IP = Nothing
oICMPOptions = Nothing
ICMPReply = Nothing

End Function
Public Function EvaluatePingRes ponse(ByVal PingResponse As Long)
As String

Select Case PingResponse

'success
Case ICMP_SUCCESS
EvaluatePingRes ponse = "Success!"

'some error occurred
Case ICMP_STATUS_BUF FER_TO_SMALL
EvaluatePingRes ponse = "Buffer Too Small"
Case ICMP_STATUS_DES TINATION_NET_UN REACH
EvaluatePingRes ponse = "Destinatio n Net Unreachable"
Case ICMP_STATUS_DES TINATION_HOST_U NREACH
EvaluatePingRes ponse = "Destinatio n Host Unreachable"
Case ICMP_STATUS_DES TINATION_PROTOC OL_UNREACH
EvaluatePingRes ponse = "Destinatio n Protocol
Unreachable"
Case ICMP_STATUS_DES TINATION_PORT_U NREACH
EvaluatePingRes ponse = "Destinatio n Port Unreachable"
Case ICMP_STATUS_NO_ RESOURCE
EvaluatePingRes ponse = "No Resources"
Case ICMP_STATUS_BAD _OPTION
EvaluatePingRes ponse = "Bad Option"
Case ICMP_STATUS_HAR DWARE_ERROR
EvaluatePingRes ponse = "Hardware Error"
Case ICMP_STATUS_LAR GE_PACKET
EvaluatePingRes ponse = "Packet Too Big"
Case ICMP_STATUS_REQ UEST_TIMED_OUT
EvaluatePingRes ponse = "Request Timed Out"
Case ICMP_STATUS_BAD _REQUEST
EvaluatePingRes ponse = "Bad Request"
Case ICMP_STATUS_BAD _ROUTE
EvaluatePingRes ponse = "Bad Route"
Case ICMP_STATUS_TTL _EXPIRED_TRANSI T
EvaluatePingRes ponse = "TimeToLive Expired Transit"
Case ICMP_STATUS_TTL _EXPIRED_REASSE MBLY
EvaluatePingRes ponse = "TimeToLive Expired Reassembly"
Case ICMP_STATUS_PAR AMETER
EvaluatePingRes ponse = "Parameter Problem"
Case ICMP_STATUS_SOU RCE_QUENCH
EvaluatePingRes ponse = "Source Quench"
Case ICMP_STATUS_OPT ION_TOO_BIG
EvaluatePingRes ponse = "Option Too Big"
Case ICMP_STATUS_BAD _DESTINATION
EvaluatePingRes ponse = "Bad Destination"
Case ICMP_STATUS_NEG OTIATING_IPSEC
EvaluatePingRes ponse = "Negotiatin g IPSEC"
Case ICMP_STATUS_GEN ERAL_FAILURE
EvaluatePingRes ponse = "General Failure"
'unknown error occurred
Case Else
EvaluatePingRes ponse = "Unknown Response"
End Select

End Function

Declare Auto Function IcmpCreateFile Lib "ICMP.DLL" () As IntPtr

Declare Auto Function IcmpSendEcho Lib "ICMP.DLL" _
(ByVal IcmpHandle As IntPtr, _
ByVal DestinationAddr ess As Integer, _
ByVal RequestData As String, _
ByVal RequestSize As Integer, _
ByRef RequestOptions As ICMP_OPTIONS, _
ByRef ReplyBuffer As ICMP_ECHO_REPLY , _
ByVal ReplySize As Integer, _
ByVal Timeout As Integer) As Integer

<StructLayout(L ayoutKind.Seque ntial, CharSet:=CharSe t.Ansi)> _
Public Structure ICMP_OPTIONS
Public Ttl As Byte
Public Tos As Byte
Public Flags As Byte
Public OptionsSize As Byte
Public OptionsData As IntPtr
End Structure

<StructLayout(L ayoutKind.Seque ntial, CharSet:=CharSe t.Ansi)> _
Public Structure ICMP_ECHO_REPLY
'Public Address As Integer
Public Address As Integer
Public Status As Integer
Public RoundTripTime As Integer
Public DataSize As Short
Public Reserved As Short
Public DataPtr As IntPtr
Public Options As ICMP_OPTIONS
<MarshalAs(Unma nagedType.ByVal TStr, SizeConst:=250) > _
Public Data As String
End Structure

End Class
--------------------------------------------------
Nov 20 '05 #1
0 7791

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2271
by: Jason Rodman | last post by:
I have downloaded every example on how to create a ping utility in .Net in both VB and C#, but have been disappointed with the results. I have YET to find an example that returns consistent results that are even close the the actual ping command line utility. For example: If i would ping the same server over and over, some pings would come back in 0 milliseconds, others would come back in 15, but never in between. It would always jump...
2
10205
by: Tim C | last post by:
Is there a way to do a Ping from C# if you are not logged in as administrator on W2k? I am trying to do a Ping from C# running under ASP.NET. I have found several examples on the web, mostly derived from Lance Olson's code. The examples use raw sockets and I get a permissions error: "An attempt was made to access a socket in a way
2
5608
by: Eric Wenger | last post by:
I'm pretty new to C# and I'm trying to do what I believe should be a simple thing. I want to modify a program that is using the System.Net.NetworkInformation.Ping class to have some custom behavior. For example I would like to do a "TCP-ping" where I try to connect to a TCP port on a remote machine. It seems obvious to me that I should try to derive a new Ping class (TCPPing) from the existing class so that I can use its interfaces. ...
0
1629
by: Patrice de Boisgrollier | last post by:
Hello I'm having trouble under windows 2000 dealing with programming a ping in a Visual C++ 6 environment My problem is that my code is based on a code sampl from Microsoft that is found in the MSDN library Under Windows 98 there is no problem and everythin works fine but I have a problem in Windows 2000 and XP as well with the following function : bread = recvfrom(sockRaw
3
2238
by: Rudolf | last post by:
Is there a way in VB.Net to implement ping functionality without using raw sockets? I cannot use raw sockets because it has been locked/disabled for security reasons. Thanks Rudolf
8
18400
by: Nico Grubert | last post by:
Hi there, I could not find any "ping" Class or Handler in python (2.3.5) to ping a machine. I just need to "ping" a machine to see if its answering. What's the best way to do it? Kind regards, Nico
4
13207
by: =?Utf-8?B?QWxleCBLLg==?= | last post by:
Hi all I need a simple program that allows me to check if an IP address is pingable. I am not going to send/receive anything to the remote host, just check if it is visible. Something like this: private bool CheckIPAddress(string address) { if (Ping(address))
7
6547
by: Linus Cohen | last post by:
Hi all, I'm a newbie to python and programming in general, so I wanted a simple project to start off. What I'm trying to do here is write a python command-line ping program, much like the Unix and Windows ping programs. I've got this much worked out already: class ping def PING(IP, pings, size): and that's where I stop, because I realize I have no idea how to make
1
2730
by: Mauro \Baba\ Mascia | last post by:
Hi, this is my question: I want to know if several switch (about 50) in a big lan are up and then know their MAC addresses to do a list that contains host name, ip and mac. I know only the range of their IP addresses (the host name it's simply to know using socket.gethostn. The first idea it's to ping all ip, parse the response and then execute the command "arp -a" and parse the response. However this way depends on the operating...
0
9886
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10983
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10647
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10706
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10338
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7882
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7056
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
2
4119
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3164
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.