Stack Exchange
Collect mac adresss in an array an found a specific value in this array
I need your help as I'm a beginner.
I'm trying to use the following function to collect in an array the mac addresses of the Client. A specific mac address defined previously should check in the array and then saved in a variable if found. 
function WbemQuery(WbemServices: Variant; Query: string): Variant;
var
  WbemObjectSet: Variant;
begin
  Result := Null;
  WbemObjectSet := WbemServices.ExecQuery(Query);
  if not VarIsNull(WbemObjectSet) and (WbemObjectSet.Count > 0) then
  begin
    Result := WbemObjectSet.ItemIndex(0);
  end;
end;

procedure CollectInformation;
var
  Query: string;
  WbemLocator, WbemServices: Variant;
  ComputerSystem, OperatingSystem, Processor, NetworkAdapters, NetworkAdapter: Variant;
  IPAddresses: array of string;
  I, I2: Integer;
begin
  WbemLocator := CreateOleObject('WbemScripting.SWbemLocator');
  WbemServices := WbemLocator.ConnectServer('.', 'root\CIMV2');

  Query :='SELECT IPEnabled, IPAddress, MACAddress FROM Win32_NetworkAdapterConfiguration';
  NetworkAdapters := WbemServices.ExecQuery(Query);
  if not VarIsNull(NetworkAdapters) then
  begin
    for I := 0 to NetworkAdapters.Count - 1 do
    begin
      NetworkAdapter := NetworkAdapters.ItemIndex(I);
      if (not VarIsNull(NetworkAdapter.MACAddress)) and NetworkAdapter.IPEnabled then
      begin
        Log(Format('Adapter %d MAC=%s', [I, NetworkAdapter.MACAddress]));
        if not VarIsNull(NetworkAdapter.IPAddress) then
        begin
          IPAddresses := NetworkAdapter.IPAddress;
          for I2 := 0 to GetArrayLength(IPAddresses) - 1 do
          begin
            Log(Format('Adapter %d IP %d=%s', [I, I2, IPAddresses[I2]]));
          end;
        end;
      end;
    end;
  end;
end;

Thanks for your help
Regards

I got the list below when executing this on Windows command-line: powershell -command Get-WmiObject -Query 'select * from Win32_NetworkAdapterConfiguration'
DHCPEnabled      : False
IPAddress        :
DefaultIPGateway :
DNSDomain        :
ServiceName      : RasSstp
Description      : WAN Miniport (SSTP)
Index            : 0

DHCPEnabled      : False
IPAddress        :
DefaultIPGateway :
DNSDomain        :
ServiceName      : RasAgileVpn
Description      : WAN Miniport (IKEv2)
Index            : 1

DHCPEnabled      : False
IPAddress        :
DefaultIPGateway :
DNSDomain        :
ServiceName      : Rasl2tp
Description      : WAN Miniport (L2TP)
Index            : 2

DHCPEnabled      : False
IPAddress        :
DefaultIPGateway :
DNSDomain        :
ServiceName      : PptpMiniport
Description      : WAN Miniport (PPTP)
Index            : 3

DHCPEnabled      : False
IPAddress        :
DefaultIPGateway :
DNSDomain        :
ServiceName      : RasPppoe
Description      : WAN Miniport (PPPOE)
Index            : 4

DHCPEnabled      : False
IPAddress        :
DefaultIPGateway :
DNSDomain        :
ServiceName      : NdisWan
Description      : WAN Miniport (IPv6)
Index            : 5

DHCPEnabled      : False
IPAddress        :
DefaultIPGateway :
DNSDomain        :
ServiceName      : NdisWan
Description      : WAN Miniport (Network Monitor)
Index            : 6

DHCPEnabled      : True
IPAddress        :
DefaultIPGateway :
DNSDomain        :
ServiceName      : e1express
Description      : Intel(R) 82566DM-2-Gigabit-Netzwerkverbindung
Index            : 7

DHCPEnabled      : False
IPAddress        :
DefaultIPGateway :
DNSDomain        :
ServiceName      : NdisWan
Description      : WAN Miniport (IP)
Index            : 8

DHCPEnabled      : False
IPAddress        :
DefaultIPGateway :
DNSDomain        :
ServiceName      : tunnel
Description      : Microsoft-ISATAP-Adapter
Index            : 9

DHCPEnabled      : False
IPAddress        :
DefaultIPGateway :
DNSDomain        :
ServiceName      : AsyncMac
Description      : RAS Async Adapter
Index            : 10

DHCPEnabled      : True
IPAddress        : {192.168.178.65, fe80::39c2:13d2:50ce:82c1, 2a02:8070:d296:f900:39c2:13d2:50ce:82c1, 2a02:8070:c1a8:aa00:ad48:fe3a:543c:1bc...}
DefaultIPGateway : {192.168.178.1, fe80::2665:11ff:fe2b:3ddc}
DNSDomain        : fritz.box
ServiceName      : netr7364
Description      : AirPlus G DWL-G122-Drahtlos-USB-Adapter(rev.C)
Index            : 11

DHCPEnabled      : False
IPAddress        :
DefaultIPGateway :
DNSDomain        :
ServiceName      : tunnel
Description      : Microsoft-Teredo-Tunneling-Adapter
Index            : 12

DHCPEnabled      : False
IPAddress        :
DefaultIPGateway :
DNSDomain        :
ServiceName      : tunnel
Description      : Microsoft-ISATAP-Adapter
Index            : 13


"saved in a variable if found" - What do you want to save in the variable? True/False = Found/not found? Or what? Why do you want to save the MAC addresses in an array? Do you want to do anything with them, except for searching the list for the "adresse defined previously"?

You have tried to edit my answer, instead of commenting. Your post was: Hello, sorry for my englich I still don´t understood how I can use the function above to determine my Macadress. e.g my Pc has three nic with the following addresses ( nic01=XXX, nic02=YYY, nic03=ZZZ). How can I save the specified Mac address e.g nic02 in a variable (myVar) so that at the end myVar = YYY.

That's not, what have your asked for! Your question was about "collecting an array of MAC addresses". I was pretty sure that you do not need any array, because it did not make any sense. Hence my question in the first comment. You never responded to that! You obviously have XY problem. So edit your question to ask for what you need, not for what you think you need to achieve what you really need.

What do you get if you execute this on Windows command-line: powershell -command "Get-WmiObject -Query 'select * from Win32_NetworkAdapterConfiguration'"

sorry if I´m not precise.

Regarding  your not answered question about the array. I realised that this was not the best way to do what I´m trying to do. what I exactly need is to first check if a specific mac adress is granted on a pc an then save the value in a variable.

the command has returned a list with following  informations
DHCPEnabled : False
IPAddress  :
DefaultIPGateway :
DNSDomain  :
ServiceName  : RasSstp
Description : WAN Miniport (SSTP)
Index : 0

So you have only one network adapter? I'm lost.

No, I have 13 adapters
:

DHCPEnabled : False
IPAddress:
DefaultIPGatewa :
DNSDomain:
ServiceName: RasSstp
Description : WAN Miniport (SSTP)
Index: 0
DHCPEnabled      : False
IPAddress        :
DefaultIPGateway :
DNSDomain        :
ServiceName      : RasAgileVpn
Description      : WAN Miniport (IKEv2)
Index            : 1
.
.
.
DHCPEnabled: False
IPAddress :
DefaultIPGateway :
DNSDomain :
ServiceName : tunnel
Description : Microsoft-ISATAP-Adapter
Index: 13

Append the full listing to your question. It’s unreadable in comments

I´ve link the full listing in the question.                                                  Thanks

OK, and how do you identify the entry you are interested in?

I´m wondering, because the needed informations are not shown. But when I run the following command "wmic nicconfig get MACAddress" I got two macAdresses. how can i save one of these two macadress i a variable depending of a specified value? Thank

What "specified value"? Is there other column in the nicconfig results set that you can use to identify the entry? Try wmic nicconfig to see a complete result set.

This function checks if the system has the specified MAC:
function CheckMAC(MAC: string): Boolean;
var
  Query: string;
  WbemLocator, WbemServices: Variant;
  NetworkAdapters, NetworkAdapter: Variant;
  I: Integer;
begin
  WbemLocator := CreateOleObject('WbemScripting.SWbemLocator');
  WbemServices := WbemLocator.ConnectServer('.', 'root\CIMV2');

  Result := False;
  Query := 'SELECT MACAddress FROM Win32_NetworkAdapterConfiguration';
  NetworkAdapters := WbemServices.ExecQuery(Query);
  if not VarIsNull(NetworkAdapters) then
  begin
    for I := 0 to NetworkAdapters.Count - 1 do
    begin
      NetworkAdapter := NetworkAdapters.ItemIndex(I);
      if not VarIsNull(NetworkAdapter.MACAddress) then
      begin
        Log(Format('Adapter %d MAC=%s', [I, NetworkAdapter.MACAddress]));
        if NetworkAdapter.MACAddress = MAC then
        begin
          Result := True;
          Break;
        end;
      end;
    end;
  end;
end;


You can use it like:
MAC := GetMACAddress;
if CheckMAC(MAC) then
begin
  Log(Format('Found MAC %s', [MAC]));
end
  else
begin
  Log(Format('MAC %s not found', [MAC]));
end;

(where GetMACAddress is your external function)

I´m trying to set the variable EntValue01 depending on the returned value of the function CheckMac (MAC) as shown below. it´s not doing. it is possible to set this variable by this way?
MAC := '00:19:5b:7b:3b:7d';
if CheckMAC(MAC) then
begin
EntValue01 :='00195b7b3b7d';
'end
else
begin
EntValue01 :='xxx';
end;                                                                                                       Thanks for your help.

That works for me. "it´s not doing" is pretty vague.
