Inno Setup:无法访问继承的 OLE 对象属性?

Inno Setup: Inherited OLE-Object properties not accessible?

我按照这个接受的答案来查询机器的网络适配器。它终于奏效了,但我在读取这些属性的值时仍然遇到问题:

每次代码到达调用 networkAdapter.Caption 的这一行时,都会产生运行时错误:

Runtime error (at 60:8952): Unknown method.

这是我的代码,取自上面提到的 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 class 扩展了 CIM_Setting class。属性 CaptionDescription 在那里定义。这是 Inno Setup 编译器的问题(我使用的是最新的 6.0.2)还是我必须对可能的变体变量应用某种强制转换?

当然可以访问继承的属性。实际上 Inno Setup 甚至不关心 class 是什么。它使用 late binding。 属性 名称解析委托给 class 本身。

但是您没有使用 Win32_NetworkAdapterConfigurationIWbemServices.ExecQuery returns IEnumWbemClassObject, which in turn enumerates IWbemClassObject。其中包含 您的查询 的结果。您的查询不要求 CaptionDescription 属性,因此结果集自然不包含它们。

将属性添加到查询:

Query := 'SELECT IPEnabled, IPAddress, MACAddress, InterfaceIndex, Caption, Description FROM Win32_NetworkAdapterConfiguration';