Using System;
Using System. Collections. Generic;
Using System. ComponentModel;
Using System. Data;
Using System. Drawing;
Using System. Text;
Using System. Windows. Forms;
Using System. Runtime. InteropServices;
Using System. Net;
Namespace MacApp
{
Public partial class Form1: Form
{
[DllImport ("ws2_32.dll")]
Private static extern int inet_addr (string cp );
[DllImport ("IPHLPAPI. dll")]
Private static extern int SendARP (Int32 DestIP, Int32 SrcIP, ref Int64 pMacAddr, ref Int32 PhyAddrLen );
Public Form1 ()
{
InitializeComponent ();
}
Private void button#click (object sender, EventArgs e)
{
RichTextBox1.Text = GetMacAddress (textBox1.Text); // obtain the MAC address of the remote IP address (cross-network segment not supported)
}
Private string GetMacAddress (string hostip) // obtain the MAC address of the remote IP address (cross-network segment not supported)
{
String Mac = "";
Try
{
Int32 ldest = inet_addr (hostip); // convert an IP address from the point format to an unsigned long integer
Int64 macinfo = new Int64 ();
Int32 len = 6;
SendARP (ldest, 0, ref macinfo, ref len );
String TmpMac = Convert. ToString (macinfo, 16). PadLeft (); // Convert it to hexadecimal notation. Note that some do not have 12 digits.
Mac = TmpMac. Substring (0, 2). ToUpper ();//
For (int I = 2; I <TmpMac. Length; I = I + 2)
{
Mac = TmpMac. Substring (I, 2). ToUpper () + "-" + Mac;
}
}
Catch (Exception Mye)
{
Mac = "MAC error obtaining remote host:" + Mye. Message;
}
Return Mac;
}
Private void Form1_Load (object sender, EventArgs e)
{
If (Dns. GetHostEntry (Dns. GetHostName (). AddressList. Length> 0)
{
TextBox1.Text = Dns. GetHostEntry (Dns. GetHostName (). AddressList [0]. ToString (); // obtain the local IP Address
}
}
}
}