我按照这个问题的公认答案来查询计算机网络适配器。它终于奏效了,但我仍然在读取这些属性的值时遇到问题:
- Win32_NetworkAdapterConfiguration.标题
- Win32_NetworkAdapterConfiguration.说明
每次代码到达称为networkAdapter.Caption
的这一行时,都会产生运行时错误,指出:
运行时错误(60:8952 时(:未知方法。
这是我的代码,采用自上面提到的堆栈溢出问题:
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(还是必须对 may 变量应用某种强制转换?
当然,继承的属性是可访问的。实际上,Inno Setup甚至不在乎那是什么类。它使用后期绑定。属性名称解析委托给类本身。
但是您没有与Win32_NetworkAdapterConfiguration
合作. IWbemServices.ExecQuery
返回 IEnumWbemClassObject
,而 又枚举IWbemClassObject
。其中包含查询结果。查询不要求Caption
和Description
属性,因此结果集自然不包含它们。
将属性添加到查询:
Query := 'SELECT IPEnabled, IPAddress, MACAddress, InterfaceIndex, Caption, Description FROM Win32_NetworkAdapterConfiguration';