日期:2014-05-18 浏览次数:20851 次
[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); private void button1_Click(object sender, EventArgs e) { Thread t=new Thread(new ThreadStart(EnumComputers)); t.Start(); } int i = 0; private void EnumComputers() { try { for (int i = 1; i <= 255; i++) { Ping myPing; myPing = new Ping(); myPing.PingCompleted += new PingCompletedEventHandler(_myPing_PingCompleted); string pingIP = "169.254.5." + i.ToString(); myPing.SendAsync(pingIP, 1000, null); } } catch { } } private void _myPing_PingCompleted(object sender, PingCompletedEventArgs e) { if (e.Reply.Status == IPStatus.Success) { ListItemAdds(e.Reply.Address.ToString() + "| " + getRemoteMac(e.Reply.Address.ToString()) +"\r\n"); i++; } } public string getRemoteMac( string remoteIP) { Int32 ldest = inet_addr(remoteIP); //目的ip try { Int64 macinfo = new Int64(); Int32 len = 6; int res = SendARP(ldest, 0, ref macinfo, ref len); return Convert.ToString(macinfo, 16); } catch (Exception err) { Console.WriteLine("Error:{0}", err.Message); } return 0.ToString(); } delegate void ListItemAdd(string s_Value); public void ListItemAdds(string s_Value) { if (listBox1.InvokeRequired) { this.Invoke(new ListItemAdd(ListItemAdds), new object[] { s_Value }); } else { listBox1.Items.Add(s_Value); } } }