delphi获取MAC地址


本文整理自网络,侵删。

 
获取MAC地址有很多种方法,可以读取注册表,可以读取底层信息等等。本例采用调用系统dll方法获取网卡MAC地址。在该例子的基础上稍加修改就可以用网卡MAC地址进行软件加密。
  注意:必须在uses部分加入NB30 
  演示:


//代码如下: 
unit Unit1;
interface
uses 
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, 
  Dialogs, StdCtrls, Nb30; 
type 
  TForm1 = class(TForm) 
    Edit_1: TEdit; 
    Button_1: TButton; 
    Button_2: TButton; 
    Label_1: TLabel; 
    procedure Button_2Click(Sender: TObject); 
    procedure Button_1Click(Sender: TObject); 
  private 
    { Private declarations } 
  public 
    { Public declarations } 
  end;
var 
  Form1: TForm1; 
implementation 
{$R *.dfm} 
//另一种获取方式function GetMACAdress: string; 
var 
  NCB: TNCB; 
  Adapter: TAdapterStatus; 
  LanEnum: TlanaEnum; 
  procedure ResetAdapter(num:AnsiChar); 
    begin 
      Fillchar(NCB, SizeOf(NCB), 0); 
      NCB.ncb_command := Char(NCBRESET); 
      NCB.ncb_lana_num := num; 
      Netbios(@NCB); 
    end; 
  var 
    i:Integer; 
    LanNum:AnsiChar; 
    Address:record 
            Part1:LongInt; 
            Part2:Word; 
  end absolute Adapter; 
  begin 
    Result    := ''; 
    Fillchar(NCB, SizeOf(NCB), 0); 
      Ncb.ncb_command  := Char(NCBRESET); 
      Ncb.ncb_buffer :=@LanEnum; 
      NCB.ncb_length:=SizeOf(LanEnum); 
    Netbios(@NCB); 
    if LanEnum.length=#0 then Exit; 
    LanNum:=LanEnum.lana[0]; 
    ResetAdapter(LanNum); 
    FillChar(NCB, SizeOf(NCB), 0); 
      Ncb.ncb_command  := Char(NCBRESET); 
      Ncb.ncb_lana_num :=LanNum; 
      NCB.ncb_callname[0]:='*'; 
      Ncb.ncb_buffer :=@Adapter; 
      NCB.ncb_length:=SizeOf(Adapter); 
    Netbios(@NCB); 
    ResetAdapter(LanNum); 
    for i := 0 to 5 do 
      begin 
        Result:=Result+IntToHex(Integer(Adapter.adapter_address), 2); 
        if i<5 then Result:=Result+'-';//返回MAC地址 
      end; 
end; 
//调用函数
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; //点击获取MAC地址
procedure TForm1.Button_1Click(Sender: TObject); 
begin 
  Edit_1.Text:=MacAddress; 
end; 
//关闭程序
procedure TForm1.Button_2Click(Sender: TObject); 
begin 
  Close; 
end;
end.

相关阅读 >>

Delphi idhttpserver实现webservice

Delphi dbnavigator1 删除时弹出确认对话框

Delphi 删除cookies文件

Delphi 整理内存

Delphi xe6 android 界面皮肤美化 用stylebook

解决Delphi程序在非中文系统下乱码

Delphi程序中使用自定义的鼠标

Delphi xe 的 tdictionary

Delphi一句话获取本机ip

Delphi中ocx的动态注册方法

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



打赏

取消

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

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

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

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

评论

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