/[AnywhereTS-MSSQL]/trunk/AnywhereTS.Extensions/Extensions.cs
ViewVC logotype

Annotation of /trunk/AnywhereTS.Extensions/Extensions.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 312 - (hide annotations) (download)
Sun Aug 26 02:35:34 2012 UTC (7 years, 10 months ago) by william
File size: 1314 byte(s)

1 william 310 using System;
2     using System.Collections.Generic;
3     using System.Linq;
4     using System.Text;
5 william 312 using System.Runtime.InteropServices;
6 william 310
7     namespace AnywhereTS.Extensions
8     {
9 william 312 #region IPAddress Extensions
10     public static class IPAddressExtensions
11 william 310 {
12 william 312 // For Lookup of MAC address
13     [DllImport("iphlpapi.dll", ExactSpelling = true)]
14     public static extern int SendARP(int DestIP, int SrcIP, [Out] byte[]
15     pMacAddr, ref int PhyAddrLen);
16     public static string ToMacAddress(this System.Net.IPAddress ip)
17     {
18     int rv;
19     string macStr;
20     byte[] mac = new byte[6];
21     int maclen = mac.Length;
22    
23     rv = SendARP(BitConverter.ToInt32(ip.GetAddressBytes(), 0), 0, mac, ref maclen);
24     if (rv == 0) // If not 0, error
25     {
26     // macStr = BitConverter.ToString(mac, 0, 6);
27     macStr = BitConverter.ToString(mac, 0, 1) + BitConverter.ToString(mac, 1, 1) + BitConverter.ToString(mac, 2, 1)
28     + BitConverter.ToString(mac, 3, 1) + BitConverter.ToString(mac, 4, 1) + BitConverter.ToString(mac, 5, 1);
29     }
30     else
31     {
32     macStr = "";
33     }
34     return macStr;
35     }
36 william 310 }
37 william 312 #endregion
38 william 310 }

  ViewVC Help
Powered by ViewVC 1.1.22