获取MAC地址有很多种方法,可以读取注册表,可以读取底层信息等等。本例采用调用系统dll方法获取网卡MAC地址。在该例子的基础上稍加修改就可以用网卡MAC地址进行软件加密。
//代码如下:
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.