获取客户端Mac地址

来源:互联网 发布:新闻软件哪个好 编辑:程序博客网 时间:2024/05/16 06:32
/// <summary>        /// 返回IP地址的MAC地址        /// </summary>        /// <param name="dest"></param>        /// <param name="host"></param>        /// <param name="mac"></param>        /// <param name="length"></param>        /// <returns></returns>        [DllImport("Iphlpapi.dll")]        private static extern int SendARP(Int32 dest, Int32 host, ref Int64 mac, ref Int32 length);        /// <summary>        /// 通过IP返回IP的int数字表示法        /// </summary>        /// <param name="ip"></param>        /// <returns></returns>        [DllImport("Ws2_32.dll")]        private static extern Int32 inet_addr(string ip);        protected void btnHostAddress_Click(object sender, EventArgs e)        {            string returnstr = "";            if (HttpContext.Current.Request.UserHostAddress != null)            {                string strClientIp = HttpContext.Current.Request.UserHostAddress.Trim();                Int32 ldest = inet_addr(strClientIp); //目的地的ip                 Int64 macinfo = new Int64();                Int32 len = 6;                int res = SendARP(ldest, 0, ref macinfo, ref len);                string mac_src = macinfo.ToString("X");                if (mac_src == "0")                {                    returnstr = String.Empty;                }                while (mac_src.Length < 12)                {                    mac_src = mac_src.Insert(0, "0");                }                string mac_dest = "";                for (int i = 0; i < 11; i++)                {                    if (0 == (i % 2))                    {                        if (i == 10)                        {                            mac_dest = mac_dest.Insert(0, mac_src.Substring(i, 2));                        }                        else                        {                            mac_dest = "-" + mac_dest.Insert(0, mac_src.Substring(i, 2));                        }                    }                }                returnstr = mac_dest;            }        }

0 0