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

Diff of /trunk/AnywhereTS.Extensions/Extensions.cs

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

--- trunk/AnywhereTS.Extensions/Extensions.cs	2012/08/26 02:29:18	311
+++ trunk/AnywhereTS.Extensions/Extensions.cs	2012/08/26 02:35:34	312
@@ -2,10 +2,37 @@
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
+using System.Runtime.InteropServices;
 
 namespace AnywhereTS.Extensions
 {
-    public static class Extensions
+    #region IPAddress Extensions
+    public static class IPAddressExtensions
     {
+        // For Lookup of MAC address
+        [DllImport("iphlpapi.dll", ExactSpelling = true)]
+        public static extern int SendARP(int DestIP, int SrcIP, [Out] byte[]
+        pMacAddr, ref int PhyAddrLen);
+        public static string ToMacAddress(this System.Net.IPAddress ip)
+        {
+            int rv;
+            string macStr;
+            byte[] mac = new byte[6];
+            int maclen = mac.Length;
+
+            rv = SendARP(BitConverter.ToInt32(ip.GetAddressBytes(), 0), 0, mac, ref maclen);
+            if (rv == 0) // If not 0, error
+            {
+                // macStr = BitConverter.ToString(mac, 0, 6);
+                macStr = BitConverter.ToString(mac, 0, 1) + BitConverter.ToString(mac, 1, 1) + BitConverter.ToString(mac, 2, 1)
+                    + BitConverter.ToString(mac, 3, 1) + BitConverter.ToString(mac, 4, 1) + BitConverter.ToString(mac, 5, 1);
+            }
+            else
+            {
+                macStr = "";
+            }
+            return macStr;
+        }
     }
+    #endregion
 }

 

  ViewVC Help
Powered by ViewVC 1.1.22