随笔-4  评论-3  文章-5  trackbacks-0

[DllImport("Ws2_32.dll")]
        public static extern Int32 inet_addr(string ip);
        [DllImport("Iphlpapi.dll")]
        public static extern uint SendARP([In]Int32 dest, [In]Int32 soc, ref Int64 mac, [In, Out]ref Int32 len);

        private static string mac;
        private static Int64 macinfo;

        public static string GetMac(string deIP)
        {
            Int32 des = inet_addr(deIP);
            macinfo = new Int64();
            Int32 len = 6;
            SendARP(des, 0, ref macinfo, ref len);
            mac = macinfo.ToString("x4");
            StringBuilder sb = new StringBuilder();
            string s;
            int i = 0;
            if (mac != "0000" && mac.Length == 12)
            {
                while (i < 12)
                {
                    s = mac.Substring(10 - i, 2);
                    sb.Append(s.ToUpper());
                    if (i != 10)
                        sb.Append("-");
                    i += 2;
                }
                mac = sb.ToString();
            }
            else
            {
                mac = "Error!";
            }
            return mac;
        }

posted on 2007-09-08 08:59 Realvan 阅读(844) 评论(0)  编辑 收藏 引用 所属分类: 网络日志
只有注册用户登录后才能发表评论。