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 idftp

Delphi colorbox不需要系统那么多颜色,只想自定义显示其中几个,怎么做?

Delphi防止同时出现多个应用程序实例

Delphi接收文件拖放

Delphi ttask无法传递参数的一个解决方案

Delphi 与 xml 示例(直接利用ixmldocument)

Delphi区分汉字和字母的函数

Delphi结合winrar生成自解压文件

Delphi tadoquery 中文使用说明

Delphi 如何在在windows平台下实现进程隐藏

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



打赏

取消

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

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

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

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

评论

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