我是初学者,需要你的帮助。 我试图使用以下函数在数组中收集客户端的mac地址。之前定义的特定mac adresse应检入数组,然后保存在变量中(如果找到)。
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;
谢谢你的帮助 此致
在Windows命令行执行此命令时,我得到了以下列表:powershell -command“Get-WmiObject -Query'select * from Win32_NetworkAdapterConfiguration'”
DHCPEnabled:错误 IP地址 : DefaultIPGateway: DNSDomain: 服务名称:RasSstp 描述:WAN Miniport(SSTP) 指数:0
DHCPEnabled:错误 IP地址 : DefaultIPGateway: DNSDomain: ServiceName:RasAgileVpn 描述:WAN Miniport(IKEv2) 指数:1
DHCPEnabled:错误 IP地址 : DefaultIPGateway: DNSDomain: 服务名称:Rasl2tp 描述:WAN微型端口(L2TP) 指数:2
DHCPEnabled:错误 IP地址 : DefaultIPGateway: DNSDomain: ServiceName:PptpMiniport 描述:WAN微型端口(PPTP) 指数:3
DHCPEnabled:错误 IP地址 : DefaultIPGateway: DNSDomain: 服务名称:RasPppoe 描述:WAN Miniport(PPPOE) 指数:4
DHCPEnabled:错误 IP地址 : DefaultIPGateway: DNSDomain: 服务名称:NdisWan 描述:WAN微型端口(IPv6) 指数:5
DHCPEnabled:错误 IP地址 : DefaultIPGateway: DNSDomain: 服务名称:NdisWan 描述:WAN Miniport(网络监视器) 指数:6
DHCPEnabled:是的 IP地址 : DefaultIPGateway: DNSDomain: 服务名称:e1express 描述:英特尔(R)82566DM-2-Gigabit-Netzwerkverbindung 指数:7
DHCPEnabled:错误 IP地址 : DefaultIPGateway: DNSDomain: 服务名称:NdisWan 描述:WAN Miniport(IP) 指数:8
DHCPEnabled:错误 IP地址 : DefaultIPGateway: DNSDomain: ServiceName:隧道 说明:Microsoft-ISATAP适配器 指数:9
DHCPEnabled:错误 IP地址 : DefaultIPGateway: DNSDomain: ServiceName:AsyncMac 描述:RAS异步适配器 指数:10
DHCPEnabled:是的 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 服务名称:netr7364 描述:AirPlus G DWL-G122-Drahtlos-USB适配器(rev.C) 指数:11
DHCPEnabled:错误 IP地址 : DefaultIPGateway: DNSDomain: ServiceName:隧道 说明:Microsoft-Teredo-Tunneling-Adapter 指数:12
DHCPEnabled:错误 IP地址 : DefaultIPGateway: DNSDomain: ServiceName:隧道 说明:Microsoft-ISATAP适配器 指数:13
答案 0 :(得分:0)
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)