为什么用SendARP获取局域网的网卡的MAC地址只返回前4位???而不是6位呢?
可以得到全段的值吗??

解决方案 »

  1.   

    试试用WMI
    public static string GetMac()
    {
    System.Management.ManagementClass mc = new System.Management.ManagementClass("Win32_NetworkAdapterConfiguration");
    System.Management.ManagementObjectCollection moc = mc.GetInstances();
    System.Text.StringBuilder sb = new System.Text.StringBuilder();
    foreach(ManagementObject mo in moc)
    {
    if((bool)mo["IPEnabled"] == true)
    sb.Append(mo["MacAddress"].ToString());
    mo.Dispose();
    }
    return(sb.ToString());
    }
      

  2.   

    http://www.codeproject.com/csharp/Host_Info_within_Network.asphttp://blog.joycode.com/liuhuimiao/archive/2003/12/23/9754.aspx
    你没贴出你的代码, 不知道是否是你的代码有问题, 看看上面两个联结
      

  3.   

    代码如下:
    private string getRemoteMAC(string localIP, string remoteIP)
    {
    Int32 ldest= inet_addr(remoteIP); //目的地的ip
    Int32 lhost= inet_addr(localIP); //本地服务器的ip try
    {
    Byte[] macinfo=new Byte[6];
    Int32 length=6;

    IntPtr mac=new IntPtr(macinfo[0]);
    IntPtr len=new IntPtr(6);
    int ii=SendARP(ldest,lhost, ref mac, ref len);

    return mac.ToString();
    }
    catch(Exception err)
    {
    return string.Empty;
    }

    return string.Empty;
    }可以得到数转换为16进制时,只有前4位
    例如:
    01-22-23-24-25-26
    我只能得到01222324
    不能得到全部.
      

  4.   

    try[DllImport("iphlpapi.dll", ExactSpelling=true)]
    public static extern int SendARP( Int32 DestIP, Int32 SrcIP,[Out] byte[] pMacAddr, ref IntPtr PhyAddrLen );int ii=SendARP(ldest, lhost, macinfo, ref len);
      

  5.   

    [DllImport("Iphlpapi.dll")]
    public static extern int SendARP (Int32 dest,Int32 host,ref Int64 mac,ref Int32 length);//dest为目标机器的IP;Host为本机器的IP [DllImport("Ws2_32.dll")]
    public static extern Int32 inet_addr (string ip);IPHostEntry myiphost=Dns.GetHostByName(System.Environment.MachineName);
    IPAddress []myip=myiphost.AddressList;
    this.textBox3.Text=myip[0].ToString();
    Int32 mysource=inet_addr(this.textBox3.Text.Trim());//本地IP
    Int32 iptest=inet_addr(myip);//目标IP
    Int64 macinfo=new Int64();
    Int32 len=6; string z;
    SendARP(iptest,mysource,ref macinfo,ref len);
    z=System.Convert.ToString(macinfo,16);