c 中使用ARP獲取mac位址

2021-04-08 16:40:31 字數 1223 閱讀 6205

using system.runtime.interopservices;

using system.text.regularexpressions;

using system.text;

[dllimport("iphlpapi.dll")]

private static unsafe extern int sendarp(int32 dest,int32 host,ref int32 mac,ref int32 length);

[dllimport("ws2_32.dll")]

private static extern int32 inet_addr(string ip); 

public string getmacfromip(string a_strip)

string strret = "unknown";

string strippattern = @"^/d+/./d+/./d+/./d+$";

regex objrex =new regex(strippattern);

if(objrex.i**atch(a_strip) == true)

int32 intdest = inet_addr(a_strip);

int32 arrmac = new int32[2];

int32 intlen = 6;

int intresult = sendarp(intdest,0,ref arrmac[0],ref intlen);

if(intresult == 0)

byte arrbyte = new byte[8];

arrbyte[5] = (byte)(arrmac[1] >> 8);

arrbyte[4] = (byte)arrmac[1];

arrbyte[3] = (byte)(arrmac[0] >> 24);

arrbyte[2] = (byte)(arrmac[0] >> 16);

arrbyte[1] = (byte)(arrmac[0] >> 8);

arrbyte[0] = (byte)arrmac[0];

stringbuilder strbmac = new stringbuilder();

for(int intindex = 0 ;intindex < 6;intindex ++)

strret = strbmac.tostring();

return strret;

ARP協議改mac位址

改變同一網段別人的mac位址 include include socket include htons include eth p all include close include include include sockaddr ll include struct ifreq include s...

c 獲取網絡卡MAC位址

一台機器上可能有多個網絡卡,每乙個網絡卡只有乙個mac位址,但是每乙個網絡卡可能配置有多個ip位址 如平常的膝上型電腦中,就會有無線網絡卡和有線網絡卡 網線介面 兩種 因此,如果要獲得本機所有網絡卡的ip和mac位址資訊,則必須順序獲得每個網絡卡,再依次獲取其資訊等 在windows sdk中,用i...

C 獲取本機MAC位址

static void main string args private static string getmac moc null mc null return mac catch 另外,需要說明的就是乙個錯誤 問題 在c 程式設計中,已經在程式中已經新增了 using system.manage...