From:
http://blog.joycode.com/liuhuiMiao/
Friends Mingal urge to ask me about get an ASP.NET implementation of the remote network card MAC address. I thought it was to get the MAC address of this unit and said a few ways to give him. Since he also needs to get the server (native) related information, such as hard disk serial numbers, CPU information, etc. So I introduced a WMI method to him:
Using system.management;
string strMac = string.Empty; ManagementClass mc = new ManagementClass ( "Win32_NetworkAdapterConfiguration"); ManagementObjectCollection moc = mc.GetInstances (); ??? foreach (ManagementObject mo in moc) {???? if ((bool) mo [ " Ipenabled "] == true) ??? {????? Strmac = mo [" macaddress "]. TOSTRING () "; ????}}
Later I learned that he wants the MAC address of the LAN to browse the user's network card, that can be broken. Later, I found relevant implementation code, which is roughly the use of address translation protocols to broadcast queries, mainly Sendarp this API:
DWORD SENDARP (? Ipaddr destip, ???? // destination IP address? Ipaddr srcip, ????? // source IP address, optional parameters, put it into 0 does not have problems? Pulong Pmacaddr, ?? // Return the physical address? Pulong phyaddrlen? // The length of the physical address);
Implemented in C #:
[DllImport ( "Iphlpapi.dll")] private static extern int SendARP (Int32 dest, Int32 host, ref IntPtr mac, ref IntPtr length); [DllImport ( "Ws2_32.dll")] private static extern Int32 inet_addr (string ip) ;
Private INTPTR GETREMOTEMAC (String Localip, String Remoteip) {?? ?? INT32 LDEST = INET_ADDR (Remoteip) ;? // Destination IP ???? INT32 LHOST = INET_ADDR (localip); ????? // Local Server IP
??? Try ??? {????? Byte [] macinfo = new byte [6]; ??? ?? INTPTR MAC = new INTPTR (MacInfo [0]); ????? INTPTR LEN = New INTPTR; ?????? INT II = Sendarp (LDEST, LHOST, REF MAC, REF LEN); ????? Return Mac; ???} ??? Catch (Exception Err) ??? {?? ?} ????? Return INTPTR.ZERO;}
??? However, when the MAC address of the obtained INTPTR type is converted to a hexadecimal, an unexpected scene has occurred. For example, my NIC MAC address is 00-50-BA-29-22-1A, but the conversion hexadecimal is 29ba5000. Obviously, every two are sorted, but why is it lack of 22-1A? The result of the reason should be 1A2229BA5000. In addition, ARP can only get the same network segment, cannot cross the network segment! Depressed ing ... What is better way? ? ? It should not be used with INTPTR. It can only be accessed to the content of the INT32 (on the 32-bit platform).
The C # code modified below has been verified (not converting the network byte order to host byte order):
[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);
Static Private INT64 GetRemoteMac (String Localip, String Remoteip)
{
INT32 LDEST = INET_ADDR (Remoteip); // Destination IP
INT32 LHOST = INET_ADDR (localip); // Local server IP
Try
{
INT64 MacInfo = New INT64 ();
INT32 LEN = 6;
Int Res = Sendarp (LDest, 0, Ref MacInfo, Ref LEN);
Return macinfo;
}
Catch (Exception Err)
{
Console.writeline ("Error: {0}", Err.Message);
}
Return 0;
}
As for the second question, under the Standard Network Protocol, the ARP package is impossible to transmit by network segments, so I want to pass.
The ARP protocol is unable to query the cross-segment device MAC address.
How to get System.Management?
References under System.Management this .NET component.
Tip: Type or Name Space Name "Management" does not exist in class or namespace "System" (Is it lacking assembly references?) - What happened?
"Add Reference" -> ". Net Components" -> Find system.management.dll components. Press the reference to pull.