Delphi 获取网卡MAC代码可用2020.01.22


本文整理自网络,侵删。

 
unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;



type
  TForm1 = class(TForm)
    Memo1: TMemo;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }

    // MAC Address



  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

uses    Types, WinSock, Nb30;

// MAC Address

type
  TMacAddress = array [0..5] of Byte;
  TMacAddresses = array of TMacAddress;


function ToHex(Value: TByteDynArray; const Separator: string): string;
var I: Integer;
begin
  Result := '';
  for I := 0 to Length(Value) - 1 do
  begin
    if I > 0 then
      Result := Result + Separator;
    Result := Result + IntToHex(Value[I], 2);
  end;
end;


function MacAddresses: TMacAddresses;
var
  Ncb: TNCB;
  LanaEnum: TLanaEnum;
  I, Index: Integer;
  AdapterStatus: TAdapterStatus;
begin
  Result := nil;
  Index := 0;
  FillChar(Ncb, SizeOf(Ncb),0);
  Byte(Ncb.ncb_command) := NCBENUM;
  Ncb.ncb_buffer := @LanaEnum;
  Ncb.ncb_length := SizeOf(LanaEnum);
  if Ord(NetBios(@Ncb)) = NRC_GOODRET then
  begin
    SetLength(Result, Ord(LanaEnum.length));
    for I := 0 to Length(Result) - 1 do
    begin
      FillChar(Ncb, SizeOf(Ncb),0);
      Byte(Ncb.ncb_command) := NCBRESET;
      Ncb.ncb_lana_num := LanaEnum.lana[I];
      if Ord(NetBios(@Ncb)) = NRC_GOODRET then
      begin
        FillChar(Ncb, SizeOf(Ncb),0);
        Byte(Ncb.ncb_command) := NCBASTAT;
        Ncb.ncb_lana_num := LanaEnum.lana[I];
        FillChar(Ncb.ncb_callname, SizeOf(Ncb.ncb_callname), ' ');
        AnsiChar(Ncb.ncb_callname[0]) := '*';
        FillChar(AdapterStatus, SizeOf(AdapterStatus), 0);
        Ncb.ncb_buffer := @AdapterStatus;
        Ncb.ncb_length := SizeOf(AdapterStatus);
        if Ord(NetBios(@Ncb)) = NRC_GOODRET then
        begin
          Move(AdapterStatus.adapter_address[0], Result[Index], SizeOf(TMacAddress));
          Inc(Index);
        end;
      end;
    end;
    SetLength(Result, Index);
  end;
end;


function Catenate(const S1, S2: string; const Separator: string = ', '): string; overload;
begin
  if S1 = '' then
    Result := S2
  else if S2 = '' then
    Result := S1
  else
    Result := S1 + Separator + S2;
end;


procedure TForm1.Button1Click(Sender: TObject);
var
  Text: string;
  Addresses: TMacAddresses;
  Address: TByteDynArray;
  I: Integer;
begin
  Text := '';
  SetLength(Address, Sizeof(TMacAddress));
  Addresses := MacAddresses;
  for I := 0 to Length(Addresses) - 1 do
  begin
    Move(Addresses[I], Address[0], SizeOf(TMacAddress));
    Text := Catenate(Text, 'Adapter ' + IntToStr(I + 1) + ': ' + ToHex(Address, '-'));
  end;
  Memo1.Lines.Add(Text);
end;

end.

相关阅读 >>

Delphi 修改内存

Delphi 防止刷新时闪烁的终极解决办法

Delphi 键盘记录源代码

Delphi goto 语句的使用

Delphi 内存分配 stralloc

idtcpserver 用户掉线检测方法

Delphi real控件 rm格式的播放器

Delphi执行js

Delphi以二进制方式读取图片并显示出来

Delphi10.3.2安卓sdk安装

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



打赏

取消

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

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

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

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

评论

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