现在的位置: 首页 > 综合 > 正文

获取局域网内部机器的MAC地址

2012年08月09日 ⁄ 综合 ⁄ 共 894字 ⁄ 字号 评论关闭

[DllImport("Iphlpapi.dll")]
    private static extern int SendARP(Int32 dest, Int32 host, ref Int64 mac, ref Int32 length);
    [DllImport("Ws2_32.dll")]
    private static extern Int32 inet_addr(string ip);

    static private Int64 getremotemac(string localip, string remoteip)
    {

        Int32 ldest = inet_addr(remoteip);   //目的地的ip 
        Int32 lhost = inet_addr(localip);   //本地服务器的ip 

        try
        {
            Int64 macinfo = new Int64();
            Int32 len = 6;
            int res = SendARP(ldest, 0, ref   macinfo, ref   len);
            return macinfo;
        }
        catch (Exception err)
        {
            Console.WriteLine("error:{0}", err.Message);
        }
        return 0;
    }

    private void Page_Load(object sender, System.EventArgs e)
    {
        string userip = Request.UserHostAddress;
        //Int32 ldest = inet_addr(userip); //目的地的ip
        //Int32 lhost = inet_addr(" "); //本地服务器的ip
        this.userinfo.Text = Convert.ToString(getremotemac(" ", userip)) ;
    }

抱歉!评论已关闭.