Delphi获取网卡MAC地址的两种方法


本文整理自网络,侵删。

 
1.使用Dll中的API函数:

function MacAddress: string;
var 
  Lib: Cardinal; 
  Func: function(GUID: PGUID): Longint; stdcall; 
  GUID1, GUID2: TGUID; 
begin 
  Result := ''; 
  Lib := LoadLibrary('rpcrt4.dll');
  if Lib <> 0 then 
  begin 
    if Win32Platform <>VER_PLATFORM_WIN32_NT then 
      @Func := GetProcAddress(Lib, 'UuidCreate') 
      else @Func := GetProcAddress(Lib, 'UuidCreateSequential'); 
    if Assigned(Func) then 
    begin 
      if (Func(@GUID1) = 0) and 
        (Func(@GUID2) = 0) and 
        (GUID1.D4[2] = GUID2.D4[2]) and 
        (GUID1.D4[3] = GUID2.D4[3]) and 
        (GUID1.D4[4] = GUID2.D4[4]) and 
        (GUID1.D4[5] = GUID2.D4[5]) and 
        (GUID1.D4[6] = GUID2.D4[6]) and 
        (GUID1.D4[7] = GUID2.D4[7]) then 
      begin 
        Result := 
         IntToHex(GUID1.D4[2], 2) + '-' + 
         IntToHex(GUID1.D4[3], 2) + '-' + 
         IntToHex(GUID1.D4[4], 2) + '-' + 
         IntToHex(GUID1.D4[5], 2) + '-' + 
         IntToHex(GUID1.D4[6], 2) + '-' + 
         IntToHex(GUID1.D4[7], 2); 
      end; 
    end; 
    FreeLibrary(Lib); 
  end; 
end;


2.使用NB30单元:

uses 
  NB30; 

function GetAdapterInfo(Lana: Char): String; 
var 
  Adapter: TAdapterStatus; 
  NCB: TNCB; 
begin 
  FillChar(NCB, SizeOf(NCB), 0); 
  NCB.ncb_command := Char(NCBRESET); 
  NCB.ncb_lana_num := Lana; 
  if Netbios(@NCB) <> Char(NRC_GOODRET) then 
  begin 
    Result := 'mac not found'; 
    Exit; 
  end; 

  FillChar(NCB, SizeOf(NCB), 0); 
  NCB.ncb_command := Char(NCBASTAT); 
  NCB.ncb_lana_num := Lana; 
  NCB.ncb_callname := '*'; 

  FillChar(Adapter, SizeOf(Adapter), 0); 
  NCB.ncb_buffer := @Adapter; 
  NCB.ncb_length := SizeOf(Adapter); 
  if Netbios(@NCB) <> Char(NRC_GOODRET) then 
  begin 
    Result := 'mac not found'; 
    Exit; 
  end; 
  Result := 
    IntToHex(Byte(Adapter.adapter_address[0]), 2) + '-' + 
    IntToHex(Byte(Adapter.adapter_address[1]), 2) + '-' + 
    IntToHex(Byte(Adapter.adapter_address[2]), 2) + '-' + 
    IntToHex(Byte(Adapter.adapter_address[3]), 2) + '-' + 
    IntToHex(Byte(Adapter.adapter_address[4]), 2) + '-' + 
    IntToHex(Byte(Adapter.adapter_address[5]), 2); 
end; 

function GetMACAddress: string; 
var 
  AdapterList: TLanaEnum; 
  NCB: TNCB; 
begin 
  FillChar(NCB, SizeOf(NCB), 0); 
  NCB.ncb_command := Char(NCBENUM); 
  NCB.ncb_buffer := @AdapterList; 
  NCB.ncb_length := SizeOf(AdapterList); 
  Netbios(@NCB); 
  if Byte(AdapterList.length) > 0 then 
    Result := GetAdapterInfo(AdapterList.lana[0]) 
  else 
    Result := 'mac not found'; 
end; 

相关阅读 >>

Delphi shellexecute多种用法

Delphi 获取listview1鼠标所在位置的行名称

如何用Delphi实现子目录级的文件查询

Delphi 取正在运行的dll或exe的路径

Delphi 调用外部 dll 中的函数(2. 晚绑定)

Delphi 取得当前进程占用内存及线程数

Delphi 获取临时文件夹路径

Delphi x 的 y 次方

Delphi winsock 获取计算机名和ip

Delphi文件操作的一些函数

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



打赏

取消

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

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

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

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

评论

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