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 setcurrentdir 设置当前文件夹路径

Delphi dbnavigator1 模拟点击

Delphi mac地址转换字符串

Delphi 调用viewer-windows10 图像浏览器

Delphi基于高斯-拉普拉斯算子的图像边缘检测

Delphi 调用浏览文件夹 selectdirectory

Delphi 实现透明窗体

Delphi paramstr 获取外部参数

Delphi读取一个access数据库中的表名

Delphi 10.3.1新的变量的声明方法

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



打赏

取消

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

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

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

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

评论

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