Recent

Author Topic: How to read MAC address programmatically?  (Read 5457 times)

dieselnutjob

  • Full Member
  • ***
  • Posts: 217
How to read MAC address programmatically?
« on: April 15, 2014, 05:18:02 pm »
I have figured out how to read the MAC address(es) of a PC in Linux (from /proc/net/dev or /sys/class/net), and from Windows (Win 32 GetIfTable API call) but I really cannot figure out how to do this in Mac OS X.

Does anyone know?

dieselnutjob

  • Full Member
  • ***
  • Posts: 217
Re: How to read MAC address programmatically?
« Reply #1 on: April 15, 2014, 05:41:11 pm »
ok I found this
https://developer.apple.com/library/mac/samplecode/GetPrimaryMACAddress/Introduction/Intro.html

I haven't figured out yet if I can do this from fpc

jwdietrich

  • Hero Member
  • *****
  • Posts: 1232
    • formatio reticularis
Re: How to read MAC address programmatically?
« Reply #2 on: April 18, 2014, 12:10:17 am »
An (albeit not too elegant) solution could be:

Code: [Select]
uses
  Process, StrUtils;

function ExtractMAC(theList: TStringList; theKey: string): string;
var
  i, j, theLine, thePos: integer;
begin
  theLine := -1;
  for i := 0 to theList.Count - 1 do
    begin
    j := pos(theKey, theList.Strings[i]);
    if j > 0 then
      begin
        theLine := i;
        thePos := j + length(theKey) + 1;
      end;
    end;
  if theLine > -1 then
    result := ExtractSubStr(theList.Strings[theLine], thePos, [' ']);
end;

procedure TForm1.GetaddressButtonClick(Sender: TObject);
var
  theProcess: TProcess;
  theOutput: TStringList;
begin
  DomainEdit.Text := GetDomainName;
  HostEdit.Text := GetHostName;
  theProcess := TProcess.Create(nil);
  theProcess.Executable := 'ifconfig';
  theProcess.Parameters.Add('en0');
  theProcess.Options := theProcess.Options + [poWaitOnExit, poUsePipes];
  theProcess.Execute;
  theOutput := TStringList.Create;
  theOutput.LoadFromStream(theProcess.Output);
  MAC1Edit.Text := ExtractMAC(theOutput, 'ether');
  theOutput.Free;
  theProcess.Free;
  theProcess := TProcess.Create(nil);
  theProcess.Executable := 'ifconfig';
  theProcess.Parameters.Add('en1');
  theProcess.Options := theProcess.Options + [poWaitOnExit, poUsePipes];
  theProcess.Execute;
  theOutput := TStringList.Create;
  theOutput.LoadFromStream(theProcess.Output);
  MAC2Edit.Text := ExtractMAC(theOutput, 'ether');
  theOutput.Free;
  theProcess.Free;
  theProcess := TProcess.Create(nil);
  theProcess.Executable := 'ifconfig';
  theProcess.Parameters.Add('fw0');
  theProcess.Options := theProcess.Options + [poWaitOnExit, poUsePipes];
  theProcess.Execute;
  theOutput := TStringList.Create;
  theOutput.LoadFromStream(theProcess.Output);
  MAC3Edit.Text := ExtractMAC(theOutput, 'lladdr');
  theOutput.Free;
  theProcess.Free;
end;                     

This procedure fills the contents of three edits fields with the MAC addresses of two ethernet interfaces (en0 and en1) and one firewire interface (fw0). You may want to change the code as required.

See also the attached demo project for reference.
function GetRandomNumber: integer; // xkcd.com
begin
  GetRandomNumber := 4; // chosen by fair dice roll. Guaranteed to be random.
end;

http://www.formatio-reticularis.de

Lazarus 2.2.6 | FPC 3.2.2 | PPC, Intel, ARM | macOS, Windows, Linux

 

TinyPortal © 2005-2018