程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> Delphi >> 獲得遠程或本地機器的網卡的MAC

獲得遠程或本地機器的網卡的MAC

編輯:Delphi
 下面的函數以'XX-XX-XX-XX-XX-XX' 的格式返回遠程或本地機器的Mac地址。

  Function to return the MAC address of a remote or local Machine in the format 'XX-XX-XX-XX-XX-XX'

  返回的Mac地址是一個能用在多個方面的唯一標識。使用方法:
  ShowMessage(GetMacAddress('MHEYDON');
  輸出'00-02-08-E7-99-6B'

  // ======================================================================
  //返回值是主機AServerName的Mac地址
  //AServerName參數的格式為'ServerName' 或者 'ServerName'
  //參數ServerName為空時返回本機的Mac地址
  //Mac地址以'XX-XX-XX-XX-XX-XX'的格式返回
  // ======================================================================

  function GetMacAddress(const AServerName : string) : string;
  type
       TNetTransportEnum = function(pszServer : PWideChar;
                                    Level : DWord;
                                    var pbBuffer : pointer;
                                    PrefMaxLen : LongInt;
                                    var EntrIEsRead : DWord;
                                    var TotalEntrIEs : DWord;
                                    var ResumeHandle : DWORD) : DWord; stdcall;

       TNetApiBufferFree = function(Buffer : pointer) : DWord; stdcall;

       PTransportInfo = ^TTransportInfo;
       TTransportInfo = record
                         quality_of_service : DWord;
                         number_of_vcs : DWord;
                         transport_name : PWChar;
                         transport_address : PWChar;
                         wan_ish : boolean;
                       end;

  var E,ResumeHandle,
      EntrIEsRead,
      TotalEntrIEs : DWord;
      FLibHandle : THandle;
      sMachineName,
      sMacAddr,
      Retvar : string;
      pBuffer : pointer;
      pInfo : PTransportInfo;
      FNetTransportEnum : TNetTransportEnum;
      FNetApiBufferFree : TNetApiBufferFree;
      pszServer : array[0..128] of WideChar;
      i,ii,iIdx : integer;
  begin
    sMachineName := trim(AServerName);
    Retvar := '00-00-00-00-00-00';

    // Add leading if missing
    if (sMachineName <> '') and (length(sMachineName) >= 2) then begin
      if copy(sMachineName,1,2) <> '' then
        sMachineName := '' + sMachineName
    end;

    // Setup and load from DLL
    pBuffer := nil;
    ResumeHandle := 0;
    FLibHandle := LoadLibrary('NETAPI32.DLL');

    // Execute the external function
    if FLibHandle <> 0 then begin
      @FNetTransportEnum := GetProcAddress(FLibHandle,'NetWkstaTransportEnum');
      @FNetApiBufferFree := GetProcAddress(FLibHandle,'NetApiBufferFree');
      E := FNetTransportEnum(StringToWideChar(sMachineName,pszServer,129),0,
                             pBuffer,-1,EntriesRead,TotalEntrIEs,Resumehandle);

      if E = 0 then begin
        pInfo := pBuffer;

        // Enumerate all protocols - look for TCPIP
        for i := 1 to EntrIEsRead do begin
          if pos('TCPIP',UpperCase(pInfo^.transport_name)) <> 0 then begin
            // Got It - now format result 'xx-xx-xx-xx-xx-xx'
            iIdx := 1;
            sMacAddr := pInfo^.transport_address;

            for ii := 1 to 12 do begin
              Retvar[iIdx] := sMacAddr[ii];
              inc(iIdx);
              if iIdx in [3,6,9,12,15] then inc(iIdx);
            end;
          end;

          inc(pInfo);
        end;
        if pBuffer <> nil then FNetApiBufferFree(pBuffer);
      end;

      try
        FreeLibrary(FLibHandle);
      except 
        // 錯誤處理
      end;
    end;
    result:=Retvar;
  end;

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved