【问题标题】:Inno Setup: Inherited OLE-Object properties not accessible?Inno Setup:继承的 OLE 对象属性不可访问?
【发布时间】:2019-05-29 06:55:44
【问题描述】:

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

  • 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 类。属性CaptionDescription 在那里定义。这是 Inno Setup 编译器的问题(我使用的是最新的 6.0.2)还是我必须对可能变体变量应用某种类型的转换?

【问题讨论】:

    标签: wmi inno-setup ole pascalscript wbem


    【解决方案1】:

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

    但是您没有使用Win32_NetworkAdapterConfigurationIWbemServices.ExecQuery 返回IEnumWbemClassObject,它又枚举IWbemClassObject。其中包含您的查询的结果。您的查询不要求 CaptionDescription 属性,因此结果集自然不包含它们。

    将属性添加到查询中:

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

    【讨论】:

    • 天哪,多么愚蠢。谢谢你。我什至没有浪费一秒钟来思考这个问题。
    猜你喜欢
    • 2011-10-28
    • 1970-01-01
    • 2021-11-12
    • 2018-07-11
    • 2014-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多