获取本机MAC地址

1、单网卡可以按以下方法获取,调用rpcrt4.dll

function MacAddress: string;

var

 Lib: Cardinal;

 MACGUID: TGUID;

 Func: function(GUID: PGUID): Longint; stdcall;

begin

 Result := '';

 Lib := LoadLibrary('rpcrt4.dll');

 try

    if Lib = 0 then Exit;

    if Win32Platform <> VER_PLATFORM_WIN32_NT then

      @Func := GetProcAddress(Lib, 'UuidCreate')

    else

      @Func := GetProcAddress(Lib, 'UuidCreateSequential');

    if not Assigned(Func) then Exit;

    if Func(@MACGUID) <> 0 then Exit;

    Result:= Format('%s-%s-%s-%s-%s-%s',[

             IntToHex(MACGUID.D4[2], 2),

             IntToHex(MACGUID.D4[3], 2),

             IntToHex(MACGUID.D4[4], 2),

             IntToHex(MACGUID.D4[5], 2),

             IntToHex(MACGUID.D4[6], 2),

             IntToHex(MACGUID.D4[7], 2)]);

 finally

    FreeLibrary(Lib);

 end;

end;

 

2、调用Iphlpapi.dll,根据IP获取MAC地址

function GetMacByIP(sIP: string): string;

var

 Lib: Cardinal;

 ip, macleng: ULONG;

 mac: array [0..5] of Byte;

 Func: function(ipaddr: ULONG; temp: DWORD; macaddr, macaddrleng: Pointer):DWORD; stdcall;

begin

 Result:= '';

 ip:= inet_addr(PChar(sIP));

 Lib:= LoadLibrary('Iphlpapi.dll');

 macleng:= Length(mac);

 try

    if Lib = 0 then Exit;

    @Func:= GetProcAddress(Lib, 'SendARP');

    if not Assigned(Func) then Exit;

    if Func(ip, 0, @mac, @ macleng) <> 0 then Exit;

    Result:= Format('%2.2x-%2.2x-%2.2x-%2.2x-%2.2x-%2.2x',[mac[0],mac[1],mac[2],mac[3],mac[4],mac[5]]);

 finally

    FreeLibrary(Lib);

 end;  

end;

 

3、多网卡可输入序号获取对应的MAC地址。

function GetMacByNo(MacIndex: Integer): string;

var

 NCB: TNCB; // Netbios control block //NetBios控制块

 ADAPTER: TADAPTERSTATUS; // Netbios adapter status//取网卡状态

 LANAENUM: TLANAENUM; // Netbios lana

 intIdx: Integer; // Temporary work value//临时变量

 cRC: Char; // Netbios return code//NetBios返回值

 strTemp: string; // Temporary string//临时变量

begin

 Result := '';

 

 try

    ZeroMemory(@NCB, SizeOf(NCB)); // Zero control blocl

 

    NCB.ncb_command := Chr(NCBENUM); // Issue enum command

    NetBios(@NCB);

 

    NCB.ncb_buffer := @LANAENUM; // Reissue enum command

    NCB.ncb_length := SizeOf(LANAENUM);

    cRC := NetBios(@NCB);

    if Ord(cRC) <> 0 then exit;

 

    ZeroMemory(@NCB, SizeOf(NCB)); // Reset adapter

    NCB.ncb_command := Chr(NCBRESET);

    NCB.ncb_lana_num := LANAENUM.lana[MacIndex];

    cRC := NetBios(@NCB);

    if Ord(cRC) <> 0 then exit;

 

 

    ZeroMemory(@NCB, SizeOf(NCB)); // Get adapter address

    NCB.ncb_command := Chr(NCBASTAT);

    NCB.ncb_lana_num := LANAENUM.lana[MacIndex];

    StrPCopy(NCB.ncb_callname, '*');

    NCB.ncb_buffer := @ADAPTER;

    NCB.ncb_length := SizeOf(ADAPTER);

    NetBios(@NCB);

 

    strTemp := ''; // Convert it to string

    for intIdx := 0 to 5 do

    strTemp := strTemp + InttoHex(Integer(ADAPTER.adapter_address[intIdx]), 2) + '-';

    Delete(strTemp, Length(strTemp), 1);

    Result := strTemp;

 except

    Result:= '';

    Exit;

 end;

end;

你可能感兴趣的:(mac)