6

How do I get the MacAddress of an Network Card using Delphi ?

halfer
  • 19,824
  • 17
  • 99
  • 186
Charles Faiga
  • 11,665
  • 25
  • 102
  • 139

4 Answers4

18

Get the JEDI conversion of the Microsoft IP Helper Library from the Project JEDI API Header Library - the file is IPHlpAPI.zip. Unpack the files, you need IpTypes.pas and IpHlpApi.pas. Then you can use something like this:

procedure TForm1.Button1Click(Sender: TObject);
var
  NumInterfaces: Cardinal;
  AdapterInfo: array of TIpAdapterInfo;
  OutBufLen: ULONG;
  i: integer;
begin
  GetNumberOfInterfaces(NumInterfaces);
  SetLength(AdapterInfo, NumInterfaces);
  OutBufLen := NumInterfaces * SizeOf(TIpAdapterInfo);
  GetAdaptersInfo(@AdapterInfo[0], OutBufLen);

  Memo1.Lines.Clear;
  for i := 0 to NumInterfaces - 1 do begin
    Memo1.Lines.Add(Format('%.2x:%.2x:%.2x:%.2x:%.2x:%.2x',
      [AdapterInfo[i].Address[0], AdapterInfo[i].Address[1],
       AdapterInfo[i].Address[2], AdapterInfo[i].Address[3],
       AdapterInfo[i].Address[4], AdapterInfo[i].Address[5]]));
  end;
end;

(All error handling omitted, you should add it of course.)

mghie
  • 32,028
  • 6
  • 87
  • 129
  • I used it and for me it gives all MAC address as 00:00:00:00:00:00 . What might I be doing wrong in this? I used OutBufLen as Cardinal instead of ULONG as mentioned here. – skjoshi Nov 12 '13 at 10:54
  • @Sanju: There is no error checking of any kind, so this may be just zero-filled (uninitialized) memory after a failed API call. Try adding a check for the result of `GetAdaptersInfo()`. Also, try running this as administrator. – mghie Nov 12 '13 at 11:35
  • 1
    IPHlpAPI.zip is not available anymore. Where could I find it? – Hwau May 11 '16 at 20:21
  • @Hwau use this url: https://github.com/project-jedi/website/tree/master/delphi-jedi.org/www/files/api – Pejman Nikram Mar 29 '17 at 07:52
3

The GetAdaptersAddresses function is the preferred way to obtain adapters information since 2001 with Windows XP.

The adapter's information is returned in the IP_ADAPTER_ADDRESSES structure by the AdapterAddresses parameter.

The GetAdaptersAddresses function can retrieve information for IPv4 and IPv6 addresses.

The recommended method of calling the GetAdaptersAddresses function is to pre-allocate a 15KB working buffer pointed to by the AdapterAddresses parameter. On typical computers, this dramatically reduces the chances that the GetAdaptersAddresses function returns ERROR_BUFFER_OVERFLOW, which would require calling GetAdaptersAddresses function multiple times.


procedure TForm1.Button1Click(Sender: TObject);
const
  AF_UNSPEC = 0;
  GAA_FLAG_INCLUDE_ALL_INTERFACES = $100;
  WORKING_BUFFER_SIZE = 15000;
  MAX_TRIES = 3;
var
  pAddresses,
  pCurrAddresses: PIpAdapterAddresses;
  dwRetVal,
  outBufLen: Cardinal;
  i: Integer;
  macAddress: string;
begin
  Memo1.Lines.Clear;

  outBufLen := WORKING_BUFFER_SIZE;
  pAddresses := nil;
  i := 0;
  repeat
    if Assigned(pAddresses) then
      FreeMem(pAddresses);

    GetMem(pAddresses, outBufLen);
    if not Assigned(pAddresses) then
      raise Exception.Create('Memory allocation failed for IP_ADAPTER_ADDRESSES struct');

    dwRetVal := GetAdaptersAddresses(AF_UNSPEC, GAA_FLAG_INCLUDE_ALL_INTERFACES, nil, pAddresses, @outBufLen);
    Inc(i);
  until (dwRetVal <> ERROR_BUFFER_OVERFLOW) or (i = MAX_TRIES);

  try
    if NO_ERROR <> dwRetVal then begin
      if ERROR_NO_DATA = dwRetVal then begin
        MessageDlg('No addresses were found for the requested parameters', mtInformation, [mbOK], 0);
        Exit;
      end
      else
        raise Exception.Create(SysErrorMessage(dwRetVal));
    end;

    pCurrAddresses := pAddresses;
    while Assigned(pCurrAddresses) do begin
      if pCurrAddresses^.PhysicalAddressLength > 0 then begin
        Memo1.Lines.Add(pCurrAddresses^.FriendlyName);
        macAddress := '';
        for i := 0 to pCurrAddresses^.PhysicalAddressLength - 1 do begin
          if i > 0 then
            macAddress := macAddress + ':';
          macAddress := macAddress + Format('%.2X', [pCurrAddresses^.PhysicalAddress[i]]);
        end;
        Memo1.Lines.Add(macAddress);
        Memo1.Lines.Add('');
      end;
      pCurrAddresses := pCurrAddresses^.Next;
    end;

  finally
    if Assigned(pAddresses) then
      FreeMem(pAddresses);
  end;
end;
Community
  • 1
  • 1
fantaghirocco
  • 4,761
  • 6
  • 38
  • 48
1

if you have Indy then you can use MB30 (code taken from https://www.swissdelphicenter.ch/en/showcode.php?id=651 )

uses classes, sysUtils, NB30;

function GetMACAdress: TStringList;
var
  NCB: PNCB;
  Adapter: PAdapterStatus;

  URetCode: PChar;
  RetCode: char;
  I: integer;
  Lenum: PlanaEnum;
  _SystemID: string;
  TMPSTR: string;

begin
  Result    := TStringList.create();
  _SystemID := '';
  Getmem(NCB, SizeOf(TNCB));
  Fillchar(NCB^, SizeOf(TNCB), 0);

  Getmem(Lenum, SizeOf(TLanaEnum));
  Fillchar(Lenum^, SizeOf(TLanaEnum), 0);

  Getmem(Adapter, SizeOf(TAdapterStatus));
  Fillchar(Adapter^, SizeOf(TAdapterStatus), 0);

  Lenum.Length    := chr(0);
  NCB.ncb_command := chr(NCBENUM);
  NCB.ncb_buffer  := Pointer(Lenum);
  NCB.ncb_length  := SizeOf(Lenum);
  RetCode         := Netbios(NCB);

  try
    i := 0;
    repeat
      Fillchar(NCB^, SizeOf(TNCB), 0);
      Ncb.ncb_command  := chr(NCBRESET);
      Ncb.ncb_lana_num := lenum.lana[I];
      RetCode          := Netbios(Ncb);

      Fillchar(NCB^, SizeOf(TNCB), 0);
      Ncb.ncb_command  := chr(NCBASTAT);
      Ncb.ncb_lana_num := lenum.lana[I];
      // Must be 16
      Ncb.ncb_callname := '*               ';

      Ncb.ncb_buffer := Pointer(Adapter);

      Ncb.ncb_length := SizeOf(TAdapterStatus);
      RetCode        := Netbios(Ncb);
      //---- calc _systemId from mac-address[2-5] XOR mac-address[1]...
      if (RetCode = chr(0)) or (RetCode = chr(6)) then
      begin
        _SystemId := IntToHex(Ord(Adapter.adapter_address[0]), 2) + '-' +
          IntToHex(Ord(Adapter.adapter_address[1]), 2) + '-' +
          IntToHex(Ord(Adapter.adapter_address[2]), 2) + '-' +
          IntToHex(Ord(Adapter.adapter_address[3]), 2) + '-' +
          IntToHex(Ord(Adapter.adapter_address[4]), 2) + '-' +
          IntToHex(Ord(Adapter.adapter_address[5]), 2);

        if (_SystemID <> '00-00-00-00-00-00') and (Result.IndexOf(_SystemID)=-1) then
         Result.add(_SystemId);
      end;
      Inc(i);
    until (I >= Ord(Lenum.Length));
  finally
   FreeMem(NCB);
   FreeMem(Adapter);
   FreeMem(Lenum);
  end;
end;
-5

without knowing almost anything at all about delphi, how about running %system32%\ipconfig.exe /all and parsing the output?

  • 1
    The output might be localized so parsing would require testing on for different versions of Windows - from Afghanistan to Zimbabwe – mjn Nov 16 '15 at 14:36
  • 2
    Worst possible approach, besides localization the output might change between Windows versions, addition of IPv6, extra network cards and there is no error handling. – Remko Oct 29 '18 at 08:55