Use IP, use C # to get the MAC address of the remote host

xiaoxiao2021-03-06  62

I used to see a such article on a website, I feel good. These days, because a program written by me and friends may need this function, I use the method of the article, but I have found a lot of unsatisfactory places, which means that his idea is right, but the program is written. something wrong. I wrote a DLL file to a friend. It is very simple to implement, which is the way to use P / Invoke, and the function used is mainly a sendarp (), mainly the result of the result is taken into account. Of course, you may know this DLL file limit - it can only get the MAC address of the host in the local area network. I still can't think of a good way, I don't know which master can give pointers, thank you. My email is janent521@163.com. The code is posted below, please refer to it! Using system; using system.runtime .interopservices;

Namespace Sniffermac {///

/// Mac summary description. /// public class libwrap {[DLLIMPORT ("WS2_32.dll")] public static extern INT32 INET_ADDR (String IP); [DLLIMPORT ("iPhlpapi.dll")] Public Static Extern uint sendarp ([in] INT32 DEST, [IN] INT32 SOC, REF INT64 MAC, [IN, OUT] Ref Int32 LEN); PUBLIC CLASS MAC {Private Static String Mac; Private Static Int64 MacInfo;

Public Mac () {// // Todo: Add Constructor Logic //} Public Static String GetMac (String DEIP) {INT32 DES = Libwrap.Inet_Addr (DEIP); macinfo = new int64 (); int32 len = 6; libwrap.sendarp (des, 0, ref macinfo, ref let; mac = macinfo.toString ("x4"); stringbuilder sb = new stringbuilder (); string s; int i = 0; if (Mac! = " 0000 "&& mac.length == 12) {while (i <12) {s = mac.Substring (10-i, 2); sb.append (S.toupper ()); if (i! = 10) SB. Append ("-"); i = 2;} mac = sb.tostring ();} else {mac = "WRONG MAC!";} Return Mac;}}}

转载请注明原文地址:https://www.9cbs.com/read-84667.html

New Post(0)