Click here to Skip to main content
Click here to Skip to main content

HostName, IP, and MAC Address

By , 23 Jan 2005
 

Sample Image - Host_Info_within_Network.jpg

Introduction

This article is used to get the intranet information in a network. It shows information like the host name, corresponding IP address and also physical address.

Using the code

The code retrieves all the System names using the Directory entries information.

//       // Use Your work Group WinNT://Baba(Work Group Name)
//     DirectoryEntry DomainEntry = 
//         new DirectoryEntry("WinNT://" + this.TxtWorkGroup.Text.Trim());
//     DomainEntry.Children.SchemaFilter.Add("computer");

Then we collect the IP address using the DNS.

//    System.Net.IPHostEntry Tempaddr = 
 //     (System.Net.IPHostEntry)Dns.GetHostByName(machine.Name);
//    System.Net.IPAddress[] TempAd = Tempaddr.AddressList;

The physical MAC address is collected using the DLL "iphlpapi.dll" with the API SendARP().

For each System, we use the Hostname to retrieve the MAC address.

//    [DllImport("iphlpapi.dll", ExactSpelling=true)]
//    public static extern int SendARP( int DestIP, int SrcIP, 
//         [Out] byte[] pMacAddr, ref int PhyAddrLen );

Points of Interest

This is my first article in CodeProject.

History

  • 01/24/2005

    Article released.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

MahendranG
Technical Lead
India India
Member
I loves computers. I am interested in programming such like Networking using Sockets, Imaging, Telecom and robotics.
 
I am very much interested in programming in windows and Linux.
 
I am working with Verizon Data Services India Ltd.,

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralThis code does not work on Windows 7.Please help.memberrohini.vangury1 Nov '10 - 2:17 
Hello,
 
I have implemented this code to trace the MAC address in a windows application, while it worked perfectly well in Windows XP and Vista, it gives no result in Windows 7. Really unable to figure out y.
GeneralTo get the User Name of the client accessing a websitememberMoiz Ahmed8 Jan '10 - 23:28 
Write the line given below in your aspx page this shall give you the account user name accessing the page
Page.User.Identity.Name.ToString();

GeneralGreat articlememberEaverae2 Oct '09 - 0:58 
Nice article! It helped me out alot! I used your code for writing an ASP.Net version.
 
thanks again!
GeneralError when running your util HostNIPAddr.exemembersmith6667 Oct '08 - 8:06 
Hi,
this utility is exactly what iam looking for. Unfortunately when I try to run HostNIPAddr.exe I get the error message. The error is in Slovak language, so that I dont put it here.
I run WinXP SP2.
 
What can I do to make it run?

QuestionHow to Get WorkGroupNames in LANmemberGowri12345613 Jun '08 - 1:25 
hi,
i want to get all WorkGroup names Available in my Lan.
 
how i do this.
 
I used some Coding.but not well.
 

So i need Solution For this.
 

thanks & Regards,
Gowri.g
Generalworkgroup n ames in lanmemberthiruvadi_anand31 Mar '08 - 1:58 
Hi!
 

In my application i used this code to find all systems connected in LAN.But it return some systems only. this is the code
 
Dim childEntry As DirectoryEntry
Dim ParentEntry As New DirectoryEntry()
Try
ParentEntry.Path = "WinNT:"
For Each childEntry In ParentEntry.Children
Dim newNode As New TreeNode(childEntry.Name)
Select Case childEntry.SchemaClassName
Case "Domain"
Dim ParentDomain As New TreeNode(childEntry.Name)
TreeView1.Nodes.AddRange(New TreeNode() {ParentDomain})
Dim SubChildEntry As DirectoryEntry
Dim SubParentEntry As New DirectoryEntry()
SubParentEntry.Path = "WinNT://" & childEntry.Name
For Each SubChildEntry In SubParentEntry.Children
Dim newNode1 As New TreeNode(SubChildEntry.Name)
Select Case SubChildEntry.SchemaClassName
Case "Computer"
ParentDomain.Nodes.Add(newNode1)
End Select
Next
End Select
Next
Catch Excep As Exception
MsgBox("Error While Reading Directories")
Finally
ParentEntry = Nothing
End Try
 
In that the
SubChildEntry.SchemaClassName its return value like "Schema" instead of "Computers".For this i'm not able to list out all systems in my LAN.
 
Can you give solution for this .plz
 
Regards,
Anand.M
Questionno working in other range IPmemberpablohack4 Sep '07 - 23:23 
sorry my english.
 
this exmple it's amazing but no working in other range IP.
 
for example:
10.2.1.1 , 10.2.1.56 , etc
mac ok
 
but 64.233.169.99 no
mac: 00:00:00:00 why?
 
please helpme. this is very important.
 
im create a function just mac for an IP, the code is succesfull.
 
using System;
using System.Collections;
using System.Runtime.InteropServices;
 

public class MAC
{
[DllImport("iphlpapi.dll", ExactSpelling = true)]
private static extern int SendARP(int DestIP, int SrcIP, [Out] byte[] pMacAddr, ref int PhyAddrLen);
 
public string getmac(string ip)
{
System.Net.IPAddress TempAD = System.Net.IPAddress.Parse(ip);
 
byte[] ab = new byte[6];
int len = ab.Length;
 
// This Function Used to Get The Physical Address
int r = SendARP((int)TempAD.Address, 0, ab, ref len);
string mac = BitConverter.ToString(ab, 0, 6);
//msgbox(mac);
return mac;
}
}

 

GeneralUsage of variable names.memberDesotron17 May '07 - 6:34 
Thank you for the code example.
 
However, you should try harder to use more english-like variable names instead of acronyms. (This helps people understand it easier).
 

for example, you use:
byte[] ab = new byte[6];
 
you should use:
byte[] macAddressBytes = new byte[6];
 
Don't be so lazy when you code, it makes it really hard to understand (as well as maintain) your code later on. Poke tongue | ;-P
GeneralHelp me!membernguyenquanglamcnttk277 May '07 - 23:57 
I have error in programming!
so I need your asistance.
desciption:
<img scr="http://aycu20.webshots.com/image/16019/2005784138176631572_rs.jpg">

 
QuangLam

QuestionIs there a way to detect Network Printers' IP addresses?memberJan Palmer25 Dec '06 - 23:55 
I have been trying to solve on how to detect IP addresses based on my installed printer drivers around my network but I am getting frustrated each day..heheh.. Win32_Printer, Win32_NetworkAdapter and Win32_TCPIPPrinter failed to get the information that I want..
 
if you're kind enough share your knowledge to email it to me at janverge@gmail.com
 
I am using C# as well.
 
nice one..

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130523.1 | Last Updated 24 Jan 2005
Article Copyright 2005 by MahendranG
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid