473,554 Members | 2,282 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to retrieve clients MAC address?

Hello,
i can't figure out how to retrieve clients MAC address using C#.NET.
I can get IP but not MAC.

Thanks in advance.

Gidraz
Jul 21 '05 #1
5 16520
maybe you can use the IP with this:
http://www.ripe.net/perl/whois?form_..._search=Search

"Gidraz" <gi************ **@saikas.lt> wrote in message
news:OW******** ******@TK2MSFTN GP14.phx.gbl...
Hello,
i can't figure out how to retrieve clients MAC address using C#.NET.
I can get IP but not MAC.

Thanks in advance.

Gidraz

Jul 21 '05 #2
First 2 results on a Google search for: retrieve MAC address c#

http://www.codeproject.com/useritems...in_Network.asp

http://www.developersdex.com/gurus/code/223.asp

Gidraz wrote:
Hello,
i can't figure out how to retrieve clients MAC address using C#.NET.
I can get IP but not MAC.

Thanks in advance.

Gidraz

Jul 21 '05 #3
Hi,
I have WEB app. I want to retrieve clients MAC adress. ActiveX not working
properly.
I can't use some Windows app.

"Joshua Flanagan" <jo**@msnews.co m> wrote in message
news:eb******** ******@TK2MSFTN GP09.phx.gbl...
First 2 results on a Google search for: retrieve MAC address c#

http://www.codeproject.com/useritems...in_Network.asp

http://www.developersdex.com/gurus/code/223.asp

Gidraz wrote:
Hello,
i can't figure out how to retrieve clients MAC address using C#.NET.
I can get IP but not MAC.

Thanks in advance.

Gidraz

Jul 21 '05 #4
If you ping the IP addresss, then the MAC address for that IP will be
stored in an ARP table on your machine. You can see this by typing
"ARP -a" at a command prompt. You can access this table through an API
call to GetIPNetTable() in the IpHlpApi.dll. I'm not sure if the
IP-MAC address will be on your server if someone hits your web
application, but if it's not you can just ping the IP address since you
do have that information and then the MAC should be in the ARP table on
your machine. Here is the C# code needed to pull the MAC out of the
table by IP address.

//Setup
#region GetIpNetTable interop
// The max number of physical addresses.
const int MAXLEN_PHYSADDR = 8;

// Define the MIB_IPNETROW structure.
[StructLayout(La youtKind.Sequen tial)]
struct MIB_IPNETROW
{
[MarshalAs(Unman agedType.U4)]
public int dwIndex;
[MarshalAs(Unman agedType.U4)]
public int dwPhysAddrLen;
[MarshalAs(Unman agedType.ByValA rray, SizeConst = MAXLEN_PHYSADDR )]
public byte[] bPhysAddr;
[MarshalAs(Unman agedType.U4)]
public int dwAddr;
[MarshalAs(Unman agedType.U4)]
public int dwType;
}

// Declare the GetIpNetTable function.
[DllImport("IpHl pApi.dll")]
[return: MarshalAs(Unman agedType.U4)]
static extern int GetIpNetTable(
IntPtr pIpNetTable,
[MarshalAs(Unman agedType.U4)]
ref int pdwSize,
bool bOrder);

// The insufficient buffer error.
const int ERROR_INSUFFICI ENT_BUFFER = 122;

#endregion

/// <summary>
/// Pulls the MAC address for a given IP address out of the ARP table
on the machine executing this code.
/// </summary>
/// <param name="ip">IP address to lookup</param>
/// <returns>The MAC address for the given IP address or null if the
MAC is not found.</returns>
public string ResolveMAC(stri ng ip)
{
// The number of bytes needed.
int bytesNeeded = 0;

// The result from the API call.
int result = GetIpNetTable(I ntPtr.Zero, ref bytesNeeded, false);

// Call the function, expecting an insufficient buffer.
if (result != ERROR_INSUFFICI ENT_BUFFER)
{
// Throw an exception.
throw new Win32Exception( result);
}

// Allocate the memory, do it in a try/finally block, to ensure
// that it is released.
IntPtr buffer = IntPtr.Zero;

try
{
// Allocate the memory.
buffer = Marshal.AllocCo TaskMem(bytesNe eded);

// Make the call again. If it did not succeed, then
// raise an error.
result = GetIpNetTable(b uffer, ref bytesNeeded, false);

// If the result is not 0 (no error), then throw an exception.
if (result != 0)
{
// Throw an exception.
throw new Win32Exception( result);
}

// Now we have the buffer, we have to marshal it. We can read
// the first 4 bytes to get the length of the buffer.
int entries = Marshal.ReadInt 32(buffer);

// Increment the memory pointer by the size of the int.
IntPtr currentBuffer = new IntPtr(buffer.T oInt64() +
Marshal.SizeOf( new int()));

// Allocate an array of entries.
MIB_IPNETROW[] table = new MIB_IPNETROW[entries];

// Cycle through the entries.
for (int index = 0; index < entries; index++)
{
// Call PtrToStructure, getting the structure information.
table[index] = (MIB_IPNETROW) Marshal.PtrToSt ructure(new
IntPtr(currentB uffer.ToInt64() + (index *
Marshal.SizeOf( typeof(MIB_IPNE TROW)))), typeof(MIB_IPNE TROW));
}

byte[] macBytes = null;
for (int i = 0; i < entries; i++)
{
string tableIP = String.Format(" {0}.{1}.{2}.{3} ",
table[i].dwAddr & 0x000000ff,
(table[i].dwAddr & 0x0000ff00) >> 8,
(table[i].dwAddr & 0x00ff0000) >> 16,
(table[i].dwAddr & 0xff000000) >> 24);

if(tableIP == ip)
{
macBytes = table[i].bPhysAddr;
break;
}
}

string mac = "";

for(int i = 0; i < 6; i++)
{
string hexTet = macBytes[i].ToString("X", new NumberFormatInf o());

if(hexTet.Lengt h == 1)
{
hexTet = "0" + hexTet;
}

mac += hexTet;

if(i < 5)
{
mac += ":";
}

}

return mac;
}
catch
{
return null;
}
finally
{
// Release the memory.
Marshal.FreeCoT askMem(buffer);
}
}

Jul 21 '05 #5
I wasn't suggesting you use a windows app. I was suggesting you read
those articles and their source code to see how they retrieve the MAC
address. The second article shows hot to do it using WMI.
I'm sure both articles include code that could be used from a web
application.

Gidraz wrote:
Hi,
I have WEB app. I want to retrieve clients MAC adress. ActiveX not working
properly.
I can't use some Windows app.

"Joshua Flanagan" <jo**@msnews.co m> wrote in message
news:eb******** ******@TK2MSFTN GP09.phx.gbl...
First 2 results on a Google search for: retrieve MAC address c#

http://www.codeproject.com/useritems...in_Network.asp

http://www.developersdex.com/gurus/code/223.asp

Gidraz wrote:
Hello,
i can't figure out how to retrieve clients MAC address using C#.NET.
I can get IP but not MAC.

Thanks in advance.

Gidraz


Jul 21 '05 #6

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

Similar topics

6
4416
by: Miguel Orrego | last post by:
Hi, I have found some code that authenticates users agains a domain using ADSI. I then redirect to another page and pass the username they have entered as a string. However, it would be nice to also get their full name from Active Directory once authenticated. Can anyone suggest how I may modify this code to achieve this? I would also like...
3
2281
by: serge calderara | last post by:
Dear all, I have a tool which is able to receive any kind of data from a remote system. For that I need to get the ipadress of the remote system. If the system is not connected to a DHCP server no problem , I set in my configuration the ip adress. Then if my remote system is connected to a DHCP server,
0
1212
by: GoCMS | last post by:
Sorry for the duplicate posting, but I tried to cross_post from adsi.general board, and it didn't work. :( Here's my question: I'm using windows 2000, and integrated windows authentication for company intranet. I'm constructing a web page with ASP. I got user's login by Server("Auth_User") variable. How can I use that go get user's email...
0
1266
by: rey17 | last post by:
hi i'm working on a project and my application will be running on pocket pc(i'm using .NET CF). basically my application should be able to retrieve my pocket's ipv6 address (it's the link-local address not the global address) and then multicast the address to other pocket pc around it; so, could anyone could told me show me/what should i do...
1
5491
by: Vinny Vinn | last post by:
Using C#,What is the simplest way of retrieving the ip address of the local machine? Thanks
5
469
by: Gidraz | last post by:
Hello, i can't figure out how to retrieve clients MAC address using C#.NET. I can get IP but not MAC. Thanks in advance. Gidraz
8
8450
by: frizzle | last post by:
Hi there, I'm saving ip addresses of blocked visitors into a mySQL DB. The function with wich i retrieve the address is below this message. What i wonder is, if it's ok to remove the dots from the ip address, and only save the numbers into the database. I assume it will be quicker to compare. But i don't know if it could mess up ip...
2
3331
by: Noro | last post by:
Hi All, When I try to get the email address of the current user, I retrieve a string like this that represents the email address: /o=ORGANIZATION/ou=Sede/cn=Recipients/cn=USERNAME What can I do to retrieve the smtp address from this exchange address? Thank you!
4
2064
by: kang jia | last post by:
hi i am doing mailinglist currently. the code in my first page is like this : : <html> <head> <link rel="stylesheet" type="text/css" href="gallery.css" /> <script language="JavaScript"> <!--
0
7603
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...
0
8047
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...
1
7568
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...
0
7891
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...
0
6151
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...
1
5439
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...
0
5159
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...
1
2021
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
845
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...

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.