Delphi 获取windows系统网卡MAC地址


本文整理自网络,侵删。

 
uses NB30;

function GetMACAdress: TStringList;
var
  NCB: PNCB;
  Adapter: PAdapterStatus;

  URetCode: PChar;
  RetCode: char;
  I: integer;
  Lenum: PlanaEnum;
  _SystemID: string;
  TMPSTR: string;

begin
  Result    := TStringList.create();
  _SystemID := '';
  Getmem(NCB, SizeOf(TNCB));
  Fillchar(NCB^, SizeOf(TNCB), 0);

  Getmem(Lenum, SizeOf(TLanaEnum));
  Fillchar(Lenum^, SizeOf(TLanaEnum), 0);

  Getmem(Adapter, SizeOf(TAdapterStatus));
  Fillchar(Adapter^, SizeOf(TAdapterStatus), 0);

  Lenum.Length    := chr(0);
  NCB.ncb_command := chr(NCBENUM);
  NCB.ncb_buffer  := Pointer(Lenum);
  NCB.ncb_length  := SizeOf(Lenum);
  RetCode         := Char(Netbios(NCB));

  try
    i := 0;
    repeat
      Fillchar(NCB^, SizeOf(TNCB), 0);
      Ncb.ncb_command  := chr(NCBRESET);
      Ncb.ncb_lana_num := lenum.lana[I];
      RetCode          := Char(Netbios(Ncb));

      Fillchar(NCB^, SizeOf(TNCB), 0);
      Ncb.ncb_command  := chr(NCBASTAT);
      Ncb.ncb_lana_num := lenum.lana[I];
      // Must be 16
      Ncb.ncb_callname := '*               ';

      Ncb.ncb_buffer := Pointer(Adapter);

      Ncb.ncb_length := SizeOf(TAdapterStatus);
      RetCode        := Char(Netbios(Ncb));
      //---- calc _systemId from mac-address[2-5] XOR mac-address[1]...
      if (RetCode = chr(0)) or (RetCode = chr(6)) then
      begin
        _SystemId := IntToHex(Ord(Adapter.adapter_address[0]), 2) + '-' +
          IntToHex(Ord(Adapter.adapter_address[1]), 2) + '-' +
          IntToHex(Ord(Adapter.adapter_address[2]), 2) + '-' +
          IntToHex(Ord(Adapter.adapter_address[3]), 2) + '-' +
          IntToHex(Ord(Adapter.adapter_address[4]), 2) + '-' +
          IntToHex(Ord(Adapter.adapter_address[5]), 2);

        if (_SystemID <> '00-00-00-00-00-00') and (Result.IndexOf(_SystemID)=-1) then
         Result.add(_SystemId);
      end;
      Inc(i);
    until (I >= Ord(Lenum.Length));
  finally
   FreeMem(NCB);
   FreeMem(Adapter);
   FreeMem(Lenum);
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);


begin
Caption:=GetMACAdress.Strings[0];
end;

相关阅读 >>

Delphi 安卓获得设备 id

Delphi 判断按键状态

Delphi tnethttpclient https忽略证书验证

Delphi与sql server存储过程编程详解

Delphi 中string字符串转换byte[]字节数组

Delphi编写系统服务

Delphi 跳出循环的几种方法

Delphi map file

Delphi memo1 行随机打乱

Delphi 汉字与区位码

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...