Solved

How To Get The MAC Addres From an IP Address or Computername inside a Webservice

Posted on 2004-04-14
17
2,114 Views
Last Modified: 2007-12-19
I am writing a webservice which takes in a MAC address, IP addres, or computername. I check the string and convert IP or Computername to the MAC address.

For IP address, I tried to spawn an EXE (NBTSTAT -a IP_Address) so I could parse the output.
For computername, I was going to ping to get the IP address and then do the above step.

However, calling an EXE from a webservice doesn't seem to be as easy as doing it from a console app.
i.e. The code executes but there is nothing being sent to standard output and no error.

What would be the best way to accomplish this?
    Call an EXE from a webservice?
    Or, is there something in the C# library that will allow me to turn a computername or an IP address into a MAC address?

Thanks!
0
Comment
Question by:winterminute
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 6
  • 3
  • +1
17 Comments
 
LVL 4

Expert Comment

by:john-at-7fff
ID: 10829055
Just out of curiosity, are you doing this in order to get a unique identifier?

Also: One IP address can have multiple Mac addresses if the machines are clustered or there's load distribution hardware in front of the machine.

0
 
LVL 2

Author Comment

by:winterminute
ID: 10829061
Nope, I am using an application which requires me to work with MAC addresses, but rather than force users to know their MAC address, I'll take a computer name or IP address and work from there.
0
 
LVL 4

Expert Comment

by:john-at-7fff
ID: 10829115
Well, on Windows you can get the MAC address for your OWN machine through a NETBIOS call.

For the flavor of this, here's a VB example that you might be able to adapt to C#:

http://custom.programming-in.net/articles/art9-1.asp?f=HeapAlloc

Here's another thing you might look at:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/iphlp/iphlp/getadaptersinfo.asp
http://msdn.microsoft.com/library/en-us/iphlp/iphlp/ip_adapter_info.asp?frame=true
0
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
LVL 4

Expert Comment

by:john-at-7fff
ID: 10829136
I see where you're going with this. You want to do an arp -a programmatically. This is what you want:

http://www.codeguru.com/Cpp/I-N/internet/internetprotocolip/article.php/c6153/

0
 
LVL 2

Author Comment

by:winterminute
ID: 10829143
Yeah, so can you translate that into C#?
0
 
LVL 4

Expert Comment

by:john-at-7fff
ID: 10829147
More:

http://www.codeguru.com/Cpp/I-N/network/basicnetworkoperations/article.php/c6861/

Unfortunately you'll have to adapt it to C#, or link in some C++.
0
 
LVL 4

Expert Comment

by:john-at-7fff
ID: 10829152
No, but now that you have the C++ code, it may be that someone else can contribute the translation, or strategy to use the C++ module from you C# program . . .
0
 
LVL 3

Accepted Solution

by:
int_20h earned 125 total points
ID: 10829164
You can use SendARP which is a Win32 API but can call it using PInvoke:

using System.Net;
using System.Runtime.InteropServices;
....
[DllImport("iphlpapi.dll", ExactSpelling=true)]
public static extern int SendARP( int DestIP, int SrcIP, [Out] byte[] pMacAddr, ref int PhyAddrLen );
....
IPAddress addr = IPAddress.Parse("192.168.1.121"); // TODO: real ip address

byte[] ab = new byte[6];
int len = ab.Length;
int r = SendARP( (int) addr.Address, 0, ab, ref len );
string mac = BitConverter.ToString( ab, 0, 6 );

Note: Above code ripped from:
http://www.dotnet247.com/247reference/msgs/28/144732.aspx
0
 
LVL 2

Author Comment

by:winterminute
ID: 10829166
I think that System.Net.Dns has something for me.
GetHostByAddress and GetHostByName both return a "System.Net.IPHostEntry" instance.
I looked through that object and didn't see the MAC address listed, but I am still looking....
0
 
LVL 4

Expert Comment

by:john-at-7fff
ID: 10829172
There you go -- see what int_20h sez.
0
 
LVL 2

Author Comment

by:winterminute
ID: 10829224
int_20h >> There doesn't seem to be a parse method for IPAddress?
0
 
LVL 3

Expert Comment

by:int_20h
ID: 10833807
From the doc, there is a parse method in IPAddress:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemNetIPAddressClassParseTopic.asp

Are you using .NET Framework 1.1?
0
 
LVL 2

Author Comment

by:winterminute
ID: 10834504
Yes, I am using 1.1.  Let me look into that article...
0
 
LVL 2

Author Comment

by:winterminute
ID: 10837765
int_20h >> That worked perfectly!  Thanks!  I already awarded the points, but if you can point me two a web resources which talks more about the System.Net namespace and the various objects, I woudl appercaite it.

If not, thanks again for your help!
0
 
LVL 4

Expert Comment

by:john-at-7fff
ID: 10837787
Glad you found that C# code for SendARP!
0
 
LVL 3

Expert Comment

by:int_20h
ID: 10837828
Thanks winterminute!  Here is a URL that has lots of Networking articles you might want to read!
http://www.c-sharpcorner.com/Networking.asp

Enjoy!!!
0
 

Expert Comment

by:cezart
ID: 10984148
An other way is WMI. This is a snippet (I hope understandable):

ManagementPath path = new ManagementPath("\\\\" + "xxx.xxx.xxx.xxx" + "\\root\\cimv2");
ConnectionOptions opt = new ConnectionOptions();
opt.Username = txtUser.Text;
opt.Password = txtPassword.Text;
System.Management.ManagementScope mstest = new ManagementScope(path, opt);
System.Management.ManagementScope ms = new ManagementScope(path, opt);
ms.Options.EnablePrivileges = true;
for(int i = 0; i <= 16; i++)
{
      string queryMAC = "select MACAddress, description from Win32_NetworkAdapter where                         Manufacturer <> 'Microsoft' and Manufacturer <> NULL and DeviceID=" + "'" + i + "'";
      System.Management.ObjectQuery oqMAC = new ObjectQuery(queryMAC);
      ManagementObjectSearcher query1MAC = new ManagementObjectSearcher(ms, oqMAC) ;
      ManagementObjectCollection queryCollection1MAC = query1MAC.Get();
      foreach(ManagementObject mo in queryCollection1MAC)
      {
                      if(mo["MACAddress"] == null)
            {}
            else
            {
                  mac = mo["MACAddress"].ToString();
                  nic = mo["description"].ToString();
                  item.SubItems.Add(mac);
                  item.SubItems.Add(nic);
            }

            Cursor.Current = Cursors.WaitCursor;
                  
            }
      }
}
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question