delphi 获取MAC地址单元文件  
官方Delphi 学习QQ群: 682628230(三千人)
频道

delphi 获取MAC地址单元文件


unit MacUnt;


interface


uses windows,sysutils,winsock;


function GetLocatMACAddress(Const sIPAddress : String):String;


implementation


function GetMacRPCMode : String;

//Only Get Location MAC Address

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;


function GetMacARPMode(sIPAddress : String): String;

Type

  rmiInfo = Array[0..7] of Byte;

Var

  dwTargetIP : DWord;

  dwMacAddress : Array[0..1] of DWord;

  dwMacLen : DWord;

  dwResult : DWord;

  rmiGetInfo : rmiInfo;

  sMacTemp : String;

  iloop : Integer;

  Lib : Cardinal;

  Func: function(Destip,scrip:DWORD;pmacaddr:PDWORD;VAR phyAddrlen:DWORD):DWORD; stdcall;

begin

  Result := '';

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


  if Lib <> 0 then 

  begin

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


    if Assigned(Func) then

    begin

      dwTargetIP := Inet_Addr(PChar(sIPAddress));

      dwMacLen := 6;

      dwResult := Func(dwTargetIP,0,@dwMacAddress[0], dwMacLen);


      if dwResult=NO_ERROR then

      begin

        rmiGetInfo := rmiInfo(dwMacAddress);

        for iloop:= 0 to 5 do

        begin

          if iloop = 0 then

            sMacTemp := InttoHex(rmiGetInfo[iloop], 2)

          else

            sMacTemp := sMacTemp + '-' + InttoHex(rmiGetInfo[iloop], 2);

        end;

        Result := sMacTemp;

      end;

    end;

  end;

end;


function GetLocatMACAddress(Const sIPAddress : String):String;

begin

  Result := GetMacARPMode(sIPAddress);

  if Length(Result)=0 then

    Result := GetMacRPCMode;

  if Length(Result)=0 then

    Result:='00-00-00-00-00-00';

end;



end.



推荐分享
图文皆来源于网络,内容仅做公益性分享,版权归原作者所有,如有侵权请告知删除!
 

Copyright © 2014 DelphiW.com 开发 源码 文档 技巧 All Rights Reserved
晋ICP备14006235号-8 晋公网安备 14108102000087号

执行时间: 0.036612987518311 seconds