Recent

Author Topic: How to get the MAC address in WINDOWS ?  (Read 1465 times)

the_magik_mushroom

  • New Member
  • *
  • Posts: 11
How to get the MAC address in WINDOWS ?
« on: August 09, 2024, 11:04:41 pm »
Hi everyone,
simple question but how to simply and quickly get the mac address in win7/10/11?

the only way I managed with chatgpt is kind of dirty (an invisible cmd line with getmac and scrapping the result):
Code: Pascal  [Select][+][-]
  1. uses: Process;
  2.  
  3. function MAC: string; //GetMACAddress
  4. var
  5.   AProcess: TProcess;
  6.   AStringList: TStringList;
  7.   I: Integer;
  8.   OutputLine: string;
  9. begin
  10.   Result := '';
  11.   AProcess := TProcess.Create(nil);
  12.   AStringList := TStringList.Create;
  13.   try
  14.     AProcess.Executable := 'getmac';
  15.     AProcess.Options := AProcess.Options + [poWaitOnExit, poUsePipes, poNoConsole];
  16.     AProcess.Execute;
  17.     AStringList.LoadFromStream(AProcess.Output);
  18.  
  19.     for I := 0 to AStringList.Count - 1 do
  20.     begin
  21.       OutputLine := AStringList[I];
  22.       // Assuming the MAC address is in the second column, adjust the parsing as needed
  23.       if Pos('-', OutputLine) > 0 then
  24.       begin
  25.         Result := Trim(Copy(OutputLine, 1, Pos(' ', OutputLine) - 1));
  26.         Break;
  27.       end;
  28.     end;
  29.   finally
  30.     AProcess.Free;
  31.     AStringList.Free;
  32.   end;
  33. end;
  34.  

It's slow (~0.4 sec on my system)
anyway to get it faster?
thanks  :-*
« Last Edit: August 09, 2024, 11:06:33 pm by the_magik_mushroom »

dsiders

  • Hero Member
  • *****
  • Posts: 1238
Re: How to get the MAC address in WINDOWS ?
« Reply #1 on: August 10, 2024, 12:27:27 am »
Hi everyone,
simple question but how to simply and quickly get the mac address in win7/10/11?

the only way I managed with chatgpt is kind of dirty (an invisible cmd line with getmac and scrapping the result):
Code: Pascal  [Select][+][-]
  1. uses: Process;
  2.  
  3. function MAC: string; //GetMACAddress
  4. var
  5.   AProcess: TProcess;
  6.   AStringList: TStringList;
  7.   I: Integer;
  8.   OutputLine: string;
  9. begin
  10.   Result := '';
  11.   AProcess := TProcess.Create(nil);
  12.   AStringList := TStringList.Create;
  13.   try
  14.     AProcess.Executable := 'getmac';
  15.     AProcess.Options := AProcess.Options + [poWaitOnExit, poUsePipes, poNoConsole];
  16.     AProcess.Execute;
  17.     AStringList.LoadFromStream(AProcess.Output);
  18.  
  19.     for I := 0 to AStringList.Count - 1 do
  20.     begin
  21.       OutputLine := AStringList[I];
  22.       // Assuming the MAC address is in the second column, adjust the parsing as needed
  23.       if Pos('-', OutputLine) > 0 then
  24.       begin
  25.         Result := Trim(Copy(OutputLine, 1, Pos(' ', OutputLine) - 1));
  26.         Break;
  27.       end;
  28.     end;
  29.   finally
  30.     AProcess.Free;
  31.     AStringList.Free;
  32.   end;
  33. end;
  34.  

It's slow (~0.4 sec on my system)
anyway to get it faster?
thanks  :-*

Did you try using GetAdaptersAddresses in the Windows IpHlp API?
https://learn.microsoft.com/en-us/windows/win32/api/iphlpapi/nf-iphlpapi-getadaptersaddresses

Wrappers in packages/winunit-jedi/src/jwaiphlpapi.pas. Should be faster than spawn a process and parsing the results.

Preview Lazarus 3.99 documentation at: https://dsiders.gitlab.io/lazdocsnext


silvercoder70

  • New Member
  • *
  • Posts: 48
    • Tim Coates
Re: How to get the MAC address in WINDOWS ?
« Reply #3 on: August 10, 2024, 04:00:06 am »
This code should (?) work and uses IPHlpAPI DLL... (I know it works in Delphi
)
Code: Pascal  [Select][+][-]
  1. //------------------------------------------------------------------------------
  2. function SendARP(DestIP: DWORD; SrcIP: DWORD; pMacAddr: PDWORD; var
  3. PhyAddrLen: DWORD): DWORD; stdcall; external 'IPHlpAPI.DLL';
  4.  
  5. //------------------------------------------------------------------------------
  6. function GetRemoteMACAddress2(SourceIp, DestIP: string; var AMacAddr: string): DWORD;
  7. type
  8.   TInfo = array[0..7] of BYTE;
  9. var
  10.   dwSourceIP: DWORD;
  11.   dwTargetIP: DWORD;
  12.   dwMacAddress: array[0..1] of DWORD;
  13.   dwMacLen: DWORD;
  14.   dwResult: DWORD;
  15.   X: TInfo;
  16. begin
  17.   dwSourceIP := 0;
  18.   if SourceIp <> '' then
  19.   begin
  20.     dwSourceIP := Inet_Addr(PChar(SourceIp));
  21.   end;
  22.   dwTargetIP := Inet_Addr(PChar(DestIP));
  23.   dwMacLen := 6;
  24.   dwResult := SendARP(dwTargetIP, dwSourceIP, @dwMacAddress[0], dwMacLen);
  25.   result := dwResult;
  26.  
  27.   if dwResult = NO_ERROR then
  28.   begin
  29.     X := TInfo(dwMacAddress);
  30.     AMacAddr := Format('%.2x-%.2x-%.2x-%.2x-%.2x-%.2x',
  31.       [X[0], X[1], X[2], X[3], X[4], X[5]]);
  32.   end
  33.   else
  34.   begin
  35.     AMacAddr := '';
  36.   end;
  37. end;
  38.  
P Plate on FPC | YouTube - https://www.youtube.com/@silvercoder70

bpranoto

  • Full Member
  • ***
  • Posts: 183
Re: How to get the MAC address in WINDOWS ?
« Reply #4 on: August 10, 2024, 05:04:19 am »
This is my network interfaces query for MAC address and IPv4 Address, it works on linux and windows.

Code: Pascal  [Select][+][-]
  1. unit network_interfaces;
  2.  
  3. {$mode ObjFPC}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils,Generics.collections,Generics.defaults,variants
  9.   {$IFDEF UNIX}
  10.   ,BaseUnix,Sockets,streamex
  11.   {$ENDIF}
  12.  
  13.   {$IFDEF WINDOWS}
  14.   ,Windows, Winsock,Registry,ComObj,ActiveX
  15.   {$ENDIF}
  16.  
  17.   ;
  18.  
  19. type
  20.   TNetworkInterfaceRecord=record
  21.     Name:String;
  22.     Index:Integer;
  23.     IPv4:String;
  24.     MacAddress:String;
  25.   end;
  26.   TANetworkInterfaceRecord=array of TNetworkInterfaceRecord;
  27.  
  28.   { TNetworkInterfaces }
  29.  
  30.   TNetworkInterfaces=class(TObject)
  31.   private
  32.     FInterfaces:TANetworkInterfaceRecord;
  33.     function GetInterfaces:TANetworkInterfaceRecord;
  34.     {$IFDEF UNIX}
  35.     function GetIPV4Address( IfName:String):String;
  36.     {$ENDIF}
  37.  
  38.     {$IFDEF WINDOWS}
  39.     procedure SetWMIQuery(out SWbemLocator: olevariant;out WMIService: olevariant);
  40.     function GetIPV4Address(Index:UInt32):TStringArray;
  41.     {$ENDIF}
  42.   public
  43.     constructor Create;
  44.     property Interfaces:TANetworkInterfaceRecord read FInterfaces;
  45.   end;
  46.  
  47.   { TNetworkInterfaceComparer }
  48.  
  49.   TNetworkInterfaceComparer=class(TInterfacedObject,specialize IComparer<TNetworkInterfaceRecord>)
  50.   public
  51.     function Compare(constref NIC1,NIC2: TNetworkInterfaceRecord): Integer;
  52.   end;
  53.  
  54.  
  55. implementation
  56.  
  57.  
  58. {$IFDEF UNIX}
  59. const
  60.  NETDEV_FILE='/proc/net/dev';
  61.  NETDEV_INFO_DIR='/sys/class/net/';
  62.  SIOCGIFADDR = $8915;
  63.  IFNAMSIZ = 16; // Adjust if necessary
  64.  
  65. type
  66.   TIfReq = packed record
  67.     ifr_name: array[0..IFNAMSIZ-1] of char;
  68.     ifr_addr: sockaddr_in;
  69.   end;
  70.  
  71.   TIPAddress = array[0..3] of Byte;
  72.  
  73. function inet_ntoa( IPv4:cuint32):String;
  74. var
  75.   AParam:cuint32;
  76.   IPAddress:TIPAddress absolute AParam;
  77. begin
  78.   AParam:=IPV4;
  79.   Result:=IntToStr(IPAddress[0]) + '.' + IntToStr(IPAddress[1]) + '.' +
  80.       IntToStr(IPAddress[2]) + '.' + IntToStr(IPAddress[3]);
  81. end;
  82. {$ENDIF}
  83.  
  84. {$IFDEF WINDOWS}
  85. const
  86.   wbemFlagForwardOnly = $00000020;
  87. {$ENDIF}
  88.  
  89.  
  90.  
  91. { TNetworkInterfaceComparer }
  92.  
  93. function TNetworkInterfaceComparer.Compare(constref NIC1,
  94.   NIC2: TNetworkInterfaceRecord): Integer;
  95. begin
  96.   if NIC1.Index<NIC2.Index then begin
  97.     Result:=-1;
  98.   end
  99.   else if NIC1.Index=NIC2.Index then begin
  100.     Result:=0;
  101.   end
  102.   else begin
  103.     Result:=1;
  104.   end;
  105. end;
  106.  
  107. { TNetworkInterfaces }
  108.  
  109. function TNetworkInterfaces.GetInterfaces: TANetworkInterfaceRecord;
  110. var
  111.   Comparer:specialize IComparer<TNetworkInterfaceRecord>;
  112.   i,j:Integer;
  113.  
  114.  
  115. {$IFDEF UNIX}
  116.   NetDev:TFileStream;
  117.   Reader:TStreamReader;
  118.   Line:String;
  119.   DevDir:String;
  120.   IfIndex:String;
  121.   IfName:String;
  122. {$ENDIF}
  123.  
  124. {$IFDEF WINDOWS}
  125.   SWbemLocator: olevariant;
  126.   WMIService: olevariant;
  127.   QueryStr:String;
  128.   WbemObjectSet: olevariant;
  129.   WbemObject: olevariant;
  130.   oEnum: IEnumvariant;
  131.   iValue: longword;
  132.   IPV4Array:TStringArray;
  133.  
  134. {$ENDIF}
  135.  
  136.  
  137. begin
  138.   // Ensure empty result
  139.   Result:=TANetworkInterfaceRecord.Create;
  140.  
  141.   {$IFDEF UNIX}
  142.     // Get Active interfaces from /proc/net/dev
  143.     Reader:=NIL;
  144.     NetDev:=TFileStream.Create(NETDEV_FILE,fmOpenRead);
  145.     try
  146.       Reader:=TStreamReader.Create(NetDev);
  147.       while not Reader.Eof do begin
  148.         Line:=Reader.ReadLine;
  149.         // Interface lines have :
  150.         i:=Pos(':',Line);
  151.         if i>0 then begin
  152.  
  153.           // Get interface name
  154.           ifName:=Trim(Copy(Line,1,i-1));
  155.  
  156.           // Save it to the result
  157.           i:=Length(Result);SetLength(Result,i+1);
  158.           Result[i].Name:=IfName;
  159.         end;
  160.       end;
  161.  
  162.       // Read the info from /sys/class/net
  163.       for i:=0 to Length(Result)-1 do begin
  164.         DevDir:=IncludeTrailingPathDelimiter(NETDEV_INFO_DIR+Result[i].Name);
  165.  
  166.         // Read Index
  167.         NetDev.Free;NetDev:=NIL;
  168.         NetDev:=TFileStream.Create(DevDir+'ifindex',fmOpenRead);
  169.         Reader.Free;Reader:=NIL;
  170.         Reader:=TStreamReader.Create(NetDev);
  171.         //
  172.         IfIndex:=Reader.ReadLine;
  173.         Result[i].Index:=StrToInt(IfIndex);
  174.  
  175.         // Read MacAddress
  176.         NetDev.Free;NetDev:=NIL;
  177.         NetDev:=TFileStream.Create(DevDir+'address',fmOpenRead);
  178.         Reader.Free;Reader:=NIL;
  179.         Reader:=TStreamReader.Create(NetDev);
  180.         Result[i].MacAddress:=Reader.ReadLine;
  181.  
  182.  
  183.         // Read IP address
  184.         Result[i].IPv4:=Self.GetIPV4Address(Result[i].Name);
  185.       end;
  186.  
  187.     finally
  188.       Reader.Free;
  189.       NetDev.Free;
  190.     end;
  191.   {$ENDIF}
  192.  
  193.   {$IFDEF WINDOWS}
  194.   Self.SetWMIQuery(SWbemLocator,WMIService);
  195.  
  196.   // QueryStr:='SELECT * FROM Win32_NetworkAdapter WHERE  Manufacturer != ''Microsoft'' AND NOT PNPDeviceID LIKE ''ROOT\\%''';
  197.   QueryStr:='SELECT * FROM Win32_NetworkAdapter WHERE  Manufacturer != ''Microsoft''';
  198.  
  199.   WbemObjectSet := WMIService.ExecQuery( QueryStr,'WQL', wbemFlagForwardOnly);
  200.   oEnum := IUnknown(WbemObjectSet._NewEnum) as IEnumVariant;
  201.  
  202.   while oEnum.Next(1, WbemObject, iValue) = 0 do begin
  203.     i:=Length(Result);SetLength(Result,i+1);
  204.  
  205.     // WINDOWS XP doesn't have InterfaceObject
  206.     if Win32MajorVersion>5 then begin
  207.       if not VarIsNull(WbemObject.InterfaceIndex) then begin
  208.         Result[i].Index:=WbemObject.InterfaceIndex;
  209.       end;
  210.     end
  211.     else begin
  212.       // WINDOWS XP use DeviceID
  213.       if not VarIsNull(WbemObject.Index) then begin
  214.         Result[i].Index:=WbemObject.DeviceID;
  215.       end;
  216.     end;
  217.  
  218.     if not VarIsNull(WbemObject.Name) then begin
  219.       Result[i].Name:=WbemObject.Name;
  220.     end;
  221.  
  222.     if not VarIsNull(WbemObject.MACAddress) then begin
  223.       Result[i].MacAddress := WbemObject.MACAddress;
  224.     end;
  225.  
  226.     if not VarIsNull(WbemObject.Description) then begin
  227.  
  228.       // Get IPv4, one adapter can have more than 1 address
  229.       IPV4Array :=Self.GetIPV4Address(WbemObject.DeviceID);
  230.       if Length(IPV4Array)>0 then begin
  231.         Result[i].IPv4:=IPV4Array[0];
  232.  
  233.         // if there are more than 1 ip, make additional entry
  234.         if Length(IPV4Array) > 1 then begin
  235.           for j:=1 to Length(IPV4Array) - 1 do begin
  236.             i:=Length(Result);SetLength(Result,i+1);
  237.             // Copy other properties
  238.             Result[i].Index:=Result[i-1].Index;
  239.             Result[i].Name:=Result[i-1].Name;
  240.             Result[i].MacAddress:=Result[i-1].MacAddress;
  241.             Result[i].IPv4:=IPV4Array[j];
  242.           end;
  243.         end;
  244.       end;
  245.     end;
  246.   end;
  247.  
  248.   {$ENDIF}
  249.  
  250.  
  251.   // Sort base on index
  252.   Comparer:=TNetworkInterfaceComparer.Create;
  253.   specialize TArrayHelper<TNetworkInterfaceRecord>.Sort(Result,Comparer);
  254. end;
  255.  
  256. {$IFDEF WINDOWS}
  257.  
  258. procedure TNetworkInterfaces.SetWMIQuery(out SWbemLocator: olevariant; out
  259.   WMIService: olevariant);
  260. const
  261.   WbemUser = '';
  262.   WbemPassword = '';
  263.   WbemComputer = 'localhost';
  264. begin
  265.   SWbemLocator := CreateOleObject('WbemScripting.SWbemLocator');
  266.   WMIService := SWbemLocator.ConnectServer(WbemComputer, 'root\CIMV2',
  267.     WbemUser, WbemPassword);
  268. end;
  269.  
  270. function OccurrencesOfChar(const S: string; const C: char): integer;
  271. var
  272.   i: integer;
  273. begin
  274.   Result := 0;
  275.   for i := 1 to Length(S) do begin
  276.     if S[i] = C then begin
  277.       Inc(Result);
  278.     end;
  279.   end;
  280. end;
  281.  
  282. function TNetworkInterfaces.GetIPV4Address(Index: UInt32): TStringArray;
  283. var
  284.   SWbemLocator: olevariant;
  285.   WMIService: olevariant;
  286.   WbemObjectSet: olevariant;
  287.   WbemObject: olevariant;
  288.   oEnum: IEnumvariant;
  289.   iValue: longword;
  290.   i,j: integer;
  291. begin
  292.   Result:=TStringArray.Create;
  293.   Self.SetWMIQuery(SWbemLocator,WMIService);
  294.  
  295.   WbemObjectSet := WMIService.ExecQuery(
  296.     'SELECT * FROM Win32_NetworkAdapterConfiguration WHERE Index= ''' +
  297.     IntToStr(Index) + '''', 'WQL', wbemFlagForwardOnly);
  298.   oEnum := IUnknown(WbemObjectSet._NewEnum) as IEnumVariant;
  299.   //
  300.   while oEnum.Next(1, WbemObject, iValue) = 0 do  begin
  301.     if not VarIsClear(WbemObject.IPAddress) and not VarIsNull(WbemObject.IPAddress) then begin
  302.       for i := VarArrayLowBound(WbemObject.IPAddress, 1)
  303.         to VarArrayHighBound(WbemObject.IPAddress, 1)
  304.       do begin
  305.  
  306.         // IPV4
  307.         if OccurrencesOfChar(WbemObject.IPAddress[i], '.') = 3 then begin
  308.           j:=Length(Result);SetLength(Result,j+1);
  309.           Result[j]:=WbemObject.IPAddress[i];
  310.         end;
  311.       end;
  312.     end;
  313.   end;
  314. end;
  315.  
  316. {$ENDIF}
  317.  
  318. {$IFDEF UNIX}
  319. function TNetworkInterfaces.GetIPV4Address(IfName: String): String;
  320. var
  321.   fd: TSocket;
  322.   ifr: TIfReq;
  323. begin
  324.   fd := fpsocket(AF_INET, SOCK_DGRAM, 0);
  325.   if fd < 0 then
  326.     ExitCode := 1;
  327.  
  328.   ifr.ifr_addr.sin_family := AF_INET;
  329.   StrPCopy(ifr.ifr_name, IfName);
  330.  
  331.   if Fpioctl(fd, SIOCGIFADDR, @ifr) < 0 then
  332.     ExitCode := 1;
  333.  
  334.   Result:=inet_ntoa(ifr.ifr_addr.sin_addr.s_addr);
  335.   CloseSocket(fd);
  336. end;
  337. {$ENDIF}
  338.  
  339. constructor TNetworkInterfaces.Create;
  340. begin
  341.   Self.FInterfaces:=Self.GetInterfaces;
  342. end;
  343.  
  344. end.
  345.  
  346.  

Jurassic Pork

  • Hero Member
  • *****
  • Posts: 1238
Re: How to get the MAC address in WINDOWS ?
« Reply #5 on: August 10, 2024, 07:55:06 am »
Hello,
you can also use the ipconfig command to have the Mac Address of all the network adapters.
Code Example to extract all adapter Names and their Mac Addresses using ipconfig and regular expressions :
Code: Pascal  [Select][+][-]
  1. implementation
  2. uses regexpr, process;
  3. {$R *.lfm}
  4. procedure TForm1.Button1Click(Sender: TObject);
  5. var s : string;
  6.      re: TRegExpr;
  7. begin
  8.   runcommand('ipconfig',['/all'],s,[],swoHIDE);
  9.   try
  10.     re := TRegExpr.Create;
  11.     re.Expression := 'Description[^:]+:(.*)$\s+' +
  12.                      'Adresse physique[^:]+:(.*)$';
  13.     re.ModifierM := True;
  14.     re.ModifierG := False;
  15.     if  re.Exec(s) then
  16.     repeat
  17.       Memo1.Append(re.Match[1] + ' -> ' + re.Match[2]);
  18.     until not re.ExecNext;
  19.   finally
  20.     re.Free;
  21.   end;
  22. end;
 

Replace Description and Adresse physique with your translation.
For linux use ip -a  and for macos ifconfig -a. Don't forget to change the pattern of the regex.

Friendly, J.P
« Last Edit: August 10, 2024, 08:01:05 am by Jurassic Pork »
Jurassic computer : Sinclair ZX81 - Zilog Z80A à 3,25 MHz - RAM 1 Ko - ROM 8 Ko

the_magik_mushroom

  • New Member
  • *
  • Posts: 11
Re: How to get the MAC address in WINDOWS ?
« Reply #6 on: August 11, 2024, 12:01:00 am »
@silvercoder70 I can't get the IPHlpAPI.DLL method to work with lazarus..

the unit network_interfaces works, but I'm not to sure how to get the 'main' mac address, I don't want to list them all. I'll check this.

thanks everyone   O:-)
« Last Edit: August 11, 2024, 12:03:11 am by the_magik_mushroom »

ASerge

  • Hero Member
  • *****
  • Posts: 2320
Re: How to get the MAC address in WINDOWS ?
« Reply #7 on: August 11, 2024, 01:56:48 am »
Did you try using GetAdaptersAddresses in the Windows IpHlp API?
https://learn.microsoft.com/en-us/windows/win32/api/iphlpapi/nf-iphlpapi-getadaptersaddresses

Wrappers in packages/winunit-jedi/src/jwaiphlpapi.pas. Should be faster than spawn a process and parsing the results.
+1
And example:
Code: Pascal  [Select][+][-]
  1. uses JwaWinError, JwaIpTypes, JwaIpHlpApi;
  2.  
  3. procedure TForm1.Button1Click(Sender: TObject);
  4. var
  5.   PA, Buffer: PIpAdapterAddresses;
  6.   BufLen: LongWord;
  7.   Result: LongWord;
  8.   B: Byte;
  9.   MAC: string;
  10. begin
  11.   BufLen := 0;
  12.   Result := GetAdaptersAddresses(0, 0, nil, nil, @BufLen);
  13.   if Result <> ERROR_BUFFER_OVERFLOW then
  14.     RaiseLastOSError(Result);
  15.   GetMem(Buffer, BufLen);
  16.   try
  17.     Result := GetAdaptersAddresses(0, 0, nil, Buffer, @BufLen);
  18.     if Result <> ERROR_SUCCESS then
  19.       RaiseLastOSError(Result);
  20.     PA := Buffer;
  21.     repeat
  22.       if PA^.PhysicalAddressLength = 0 then
  23.         MAC := 'do not have a data-link layer'
  24.       else
  25.       begin
  26.         MAC := '';
  27.         for B in PA^.PhysicalAddress do
  28.         begin
  29.           if MAC <> '' then
  30.             MAC := MAC + ':';
  31.           MAC := MAC + IntToHex(B);
  32.         end;
  33.       end;
  34.       Memo1.Append(Format('%s'#9'%s, %s', [MAC, PA^.Description, PA^.FriendlyName]));
  35.       PA := PA^.Next;
  36.     until PA = nil;
  37.   finally
  38.     FreeMem(Buffer);
  39.   end;
  40. end;

bpranoto

  • Full Member
  • ***
  • Posts: 183
Re: How to get the MAC address in WINDOWS ?
« Reply #8 on: August 11, 2024, 02:14:33 am »
...
the unit network_interfaces works, but I'm not to sure how to get the 'main' mac address, I don't want to list them all. I'll check this.

thanks everyone   O:-)

I think there's no main mac address. In my code I sort the list by  the interface index. As long as I test, the order is the same with the windows ipconfig or linux ifconfig output command. Please check

Thaddy

  • Hero Member
  • *****
  • Posts: 15641
  • Censorship about opinions does not belong here.
Re: How to get the MAC address in WINDOWS ?
« Reply #9 on: August 11, 2024, 10:04:06 am »
Indeed, there is no such thing as a "main" MAC address.
If I smell bad code it usually is bad code and that includes my own code.

Thaddy

  • Hero Member
  • *****
  • Posts: 15641
  • Censorship about opinions does not belong here.
Re: How to get the MAC address in WINDOWS ?
« Reply #10 on: August 11, 2024, 02:12:26 pm »
This code should (?) work and uses IPHlpAPI DLL... (I know it works in Delphi
)
Yes, that code works in FPC too, but you need to use jwaWindows instead of windows, iphlpapi and its type unit.
IOW uses jwaWindows, sysutils;
Or simply jwaIpHlpApi.
( but I am lazy, so if I am missing a windows unit I tend to simply use jwaWindows, which includes almost all Windows api's in one go. There is regularly missing a few bits and bolts, though)

« Last Edit: August 11, 2024, 02:19:19 pm by Thaddy »
If I smell bad code it usually is bad code and that includes my own code.

the_magik_mushroom

  • New Member
  • *
  • Posts: 11
Re: How to get the MAC address in WINDOWS ?
« Reply #11 on: August 13, 2024, 02:52:52 am »
Yes thank you the uses JwaWinError, JwaIpTypes, JwaIpHlpApi; was missing.

when I wrote the 'main' mac address, I mean the first one you get when you type the cmd getmac.

here is the list I get with your code ASerge:

do not have a data-link layer
00:FF:AE:0C:E2:2F:00:00
do not have a data-link layer
8C:55:4A:AF:11:33:00:00
8E:55:4A:AF:11:32:00:00   << that's the one I want from my getmac code
8C:55:4A:AF:11:32:00:00
8C:55:4A:AF:11:36:00:00
do not have a data-link layer

ASerge

  • Hero Member
  • *****
  • Posts: 2320
Re: How to get the MAC address in WINDOWS ?
« Reply #12 on: August 14, 2024, 05:27:59 pm »
do not have a data-link layer
00:FF:AE:0C:E2:2F:00:00
do not have a data-link layer
8C:55:4A:AF:11:33:00:00
8E:55:4A:AF:11:32:00:00   << that's the one I want from my getmac code
8C:55:4A:AF:11:32:00:00
8C:55:4A:AF:11:36:00:00
do not have a data-link layer
You can perform filtering over an Ethernet network.
Add jwaIpIfCons in uses, and
Code: Pascal  [Select][+][-]
  1. ...
  2. if PA^.IfType = IF_TYPE_ETHERNET_CSMACD then
  3.   Memo1.Append(...

Jurassic Pork

  • Hero Member
  • *****
  • Posts: 1238
Re: How to get the MAC address in WINDOWS ?
« Reply #13 on: August 14, 2024, 07:35:21 pm »
Hello,
simple question but how to simply and quickly get the mac address in win7/10/11?
the only way I managed with chatgpt is kind of dirty (an invisible cmd line with getmac and scrapping the result):
It's slow (~0.4 sec on my system)
anyway to get it faster?  :-*
IpConfig in a runcommand with swoHide mode is faster :  60 ms with  regex extract and display :
Code: Pascal  [Select][+][-]
  1. implementation
  2.  uses regexpr, process;
  3. {$R *.lfm}
  4. procedure TForm1.Button1Click(Sender: TObject);
  5. var s : string;
  6.      re: TRegExpr;
  7.     ET : TEpikTimer;
  8. begin
  9.   ET := TEpikTimer.Create(Nil);
  10.   ET.Clear;
  11.   ET.Start;
  12.   runcommand('ipconfig',['/all'],s,[],swoHIDE);
  13.   try
  14.     re := TRegExpr.Create;
  15.     re.Expression := 'Description[^:]+:(.*)$\s+' +
  16.                      'Adresse physique[^:]+:(.*)$';
  17.     re.ModifierM := True;
  18.     re.ModifierG := False;
  19.     if  re.Exec(s) then
  20.     repeat
  21.       Memo1.Append(re.Match[1] + ' -> ' + re.Match[2]);
  22.     until not re.ExecNext;
  23.   finally
  24.     re.Free;
  25.     Memo1.Append('Elapsed Time : ' + FloatToStr(ET.ELapsed) );
  26.   end;
  27. end;
 

Friendly, J.P
« Last Edit: August 14, 2024, 07:42:20 pm by Jurassic Pork »
Jurassic computer : Sinclair ZX81 - Zilog Z80A à 3,25 MHz - RAM 1 Ko - ROM 8 Ko

 

TinyPortal © 2005-2018