我按照这个问题的公认答案来查询机器网络适配器。它终于奏效了,但我在读取这些属性的值时仍然遇到问题:
- Win32_NetworkAdapterConfiguration.Caption
- Win32_NetworkAdapterConfiguration.Description
每次代码到达这一行时,networkAdapter.Caption
都会产生一个运行时错误,说:
运行时错误(在 60:8952):未知方法。
这是我的代码,取自上述 Stack Overflow 问题:
Log('Querying WMI for network adapter data...');
query := 'SELECT IPEnabled, IPAddress, MACAddress, InterfaceIndex 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 and (not VarIsNull(networkAdapter.IPAddress)) then
begin
SetArrayLength(sysInfo.networkAdapters, GetArrayLength(sysInfo.networkAdapters) + 1);
nicRec := sysInfo.networkAdapters[adapterIndex];
{ Adapter name }
nicRec.name := defaultIfNull(networkAdapter.Caption, Format('Adapter %d', [i]));
Log(Format(' NIC[%d] name........: %s', [adapterIndex, nicRec.name]));
{ Adapter index }
nicRec.index := defaultIfNull(networkAdapter.InterfaceIndex, adapterIndex);
Log(Format(' NIC[%d] index.......: %d', [adapterIndex, nicRec.index]));
{ Adapter MAC address }
nicRec.macAddress := defaultIfNull(networkAdapter.MACAddress, '');
Log(Format(' NIC[%d] MAC address.: %s', [adapterIndex, nicRec.macAddress]));
{ Adapter ip address(es) }
nicRec.ipAddresses := TStringList.Create;
if not VarIsNull(networkAdapter.IPAddress) then
begin
ips := networkAdapter.IPAddress;
for j := 0 to GetArrayLength(ips) - 1 do
begin
nicRec.ipAddresses.Add(ips[j]);
Log(Format(' NIC[%d] IPv4 address: %s', [adapterIndex, nicRec.ipAddresses.Strings[j]]));
end;
end;
adapterIndex := adapterIndex + 1;
end;
end;
end;
在阅读了一些 Microsoft 文档后,我发现了这些属性的描述。它指出,Win32_NetworkAdapterConfiguration
该类扩展了CIM_Setting
该类。属性Caption
和Description
在那里定义。这是 Inno Setup 编译器的问题(我使用的是最新的 6.0.2)还是我必须对可能的变体变量应用某种类型的转换?