首页 > 代码库 > Delphi 获取MAC 地址

Delphi 获取MAC 地址

function GetMacAddress(index:integer):string;
var
   ncb : TNCB;                {NetBios控制块}
   AdapterS : TAdapterStatus; {网卡状态结构}
   LanaNum : TLanaeNum;       {Netbios Lana}
   i : integer;
   rc : Char;                 {NetBios的返回代码}
   str : String;
begin
   Result := ‘‘;
   try
      ZeroMemory(@ncb, SizeOf(ncb));   {NetBios控制块清零}
      ncb.ncb_command := chr(NCBENUM); {ENUM}
      rc := NetBios(@ncb);             {取返回代码}

      ncb.ncb_buffer := @LanaNum;      {再一次处理ENUM命令}
      ncb.ncb_length := Sizeof(LanaNum);
      rc := NetBios(@ncb);             {取返回代码}

      if ord(rc)<>0 then exit;     

      ZeroMemory(@ncb, Sizeof(ncb));   {NetBios控制块清零}
      ncb.ncb_command := chr(NCBRESET);
      ncb.ncb_lana_num := LanaNum.lana[index];
      rc := NetBios(@ncb);
      if ord(rc)<>0 then exit;
     
      ZeroMemory(@ncb, Sizeof(ncb));   {取网卡的状态}
      ncb.ncb_command := chr(NCBASTAT);
      ncb.ncb_lana_num := LanaNum.lana[index];
      StrPCopy(ncb.ncb_callname,‘*‘);
      ncb.ncb_buffer := @AdapterS;
      ncb.ncb_length := SizeOf(AdapterS);
      rc := NetBios(@ncb);
     
      str := ‘‘;                       {将MAC地址转换成字符串}
      for i:=0 to 5 do
         str := str + IntToHex(Integer(AdapterS.adapter_address[i]),2); 
      Result := str;
   finally
   end;
end;