473,729 Members | 2,309 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 7779

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

Similar topics

3
2264
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
10200
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
5603
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
1624
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
2236
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
18392
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
13200
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
6540
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
2718
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
8761
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9281
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...
0
9142
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...
0
8148
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6722
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
6022
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();...
0
4525
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4795
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2163
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.