delphi 读取网卡mac的3种方式

来源:互联网 发布:c 数据段 编辑:程序博客网 时间:2024/06/06 01:25
7月11日

用硬件id加密程序,其中一项加密是用mac地址。
mac如果是真实网卡信息是唯一的,不好的消息是网卡很容易就能虚拟,所以需要配合其它手段。
现在有三种方式,各有利弊
第一种 最简单也最实用,通过调用ipconfig -all 来取得网卡。
第二种 通过netbios,因为是网络协议,如果网卡没有连接上,取不到mac地址
第三种 通过rpc调用,只能得到一块网卡.在有多块网卡时会出现有时取到的MAC不一致.
第一种
function   MacAddress():   string;
implementation
function GetIpMacAddress(): TStringList;
var
   files,re:TStringList;
   i:integer;
   row:String;
begin
re:=TStringList.Create;
files:= TStringList.Create;
   winexec('cmd   /c ipconfig -all >c:/fastspeeed.txt',SW_HIDE);
   sleep(500);
    files.LoadFromFile('c:/fastspeeed.txt');
    for I := 0 to files.Count -1 do
    begin
     row:= files.Strings[i];
     if ContainsText(row,'Physical Address') then
     begin
     try
       re.Add(RightStr(row,17));
     Except
     end;
     end;
    end;
     winexec('cmd   /c del c:/fastspeeed.txt',SW_HIDE);
    Result:= re;
end;
第二种
function GetMacAddress(): TStringList ;
var
   re:TStringList;
   i:integer;
begin
    re:=TStringList.Create;
    for I := 0 to 10 do
    begin
    try
      re.add(NBGetAdapterAddress(i));
    Except
       continue;
    end;
    end;
    Result:= re;
end;
function NBGetAdapterAddress(adapterNum: integer): String;
//adapter00为指定的网卡适配器
Var
   NCB:TNCB; // NetBios控制块
   ADAPTER : TADAPTERSTATUS; // 获取网卡状态
   LANAENUM : TLANAENUM; // Netbios lana
   intIdx : Integer; // 临时变量
   cRC : AnsiChar; // NetBios返回值
   strTemp : String; // 临时变量
Begin
   Result := '';
   Try
     // 清空控制块
     ZeroMemory(@NCB, SizeOf(NCB));
     NCB.ncb_command:=Chr(NCBENUM);
     cRC := NetBios(@NCB);
     // 重新设置enum命令
     NCB.ncb_buffer := @LANAENUM;
     NCB.ncb_length := SizeOf(LANAENUM);
     cRC := NetBios(@NCB);
     If Ord(cRC)<>0 Then
       exit;
     // 重新设定网卡
     ZeroMemory(@NCB, SizeOf(NCB));
     NCB.ncb_command := Chr(NCBRESET);
     NCB.ncb_lana_num := LANAENUM.lana[adapterNum];
     cRC := NetBios(@NCB);
     If Ord(cRC)<>0 Then
       exit;
     // 获取网卡地址
     ZeroMemory(@NCB, SizeOf(NCB));
     NCB.ncb_command := Chr(NCBASTAT);
     NCB.ncb_lana_num := LANAENUM.lana[adapterNum];
     StrPCopy(NCB.ncb_callname, '*');
     NCB.ncb_buffer := @ADAPTER;
     NCB.ncb_length := SizeOf(ADAPTER);
     cRC := NetBios(@NCB);
     // 将获得信息转换为字符串
     strTemp := '';
     For intIdx := 0 To 5 Do
       strTemp := strTemp + InttoHex(Integer(ADAPTER.adapter_address[intIdx]),2);
     Result := strTemp;
   Finally
   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
        @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;
end;
end;