在云那方

首页 新随笔 联系 订阅 管理
using System;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.RegularExpressions;
namespace TelEvent
{
 
/// 
 
/// HardwareInfo 的摘要说明。
 
/// 

 public class HardwareInfo
 
{   
  [DllImport(
"Iphlpapi.dll")]
  
private static  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 static string GetMACFromIP(string A_strIP)
  
{
   
string strRet = "00-00-00-00-00-00";
   
string strIPPattern = @"^\d+\.\d+\.\d+\.\d+$";
   Regex objRex 
=new Regex(strIPPattern);
   
if(objRex.IsMatch(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 ++)
     
{
      
if(intIndex > 0) strbMAC.Append("-");
      strbMAC.Append(arrbyte[intIndex].ToString(
"X2"));
     }

     strRet 
= strbMAC.ToString();
    }

   }

   
return strRet;
  }

 }

}

posted on 2008-07-04 12:45  Rich.T  阅读(501)  评论(0编辑  收藏  举报