Search code examples
wmiinno-setupolepascalscriptwbem

Inno Setup: Inherited OLE-Object properties not accessible?


I followed this question's accepted answer to query the machines network adapters. It finally worked but I still face a problem reading the values of these properties:

  • Win32_NetworkAdapterConfiguration.Caption
  • Win32_NetworkAdapterConfiguration.Description

Every time the code reaches this line where networkAdapter.Caption is called a runtime error is produced saying:

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

This is my code, adopted from the above mentioned Stack Overflow question:

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;

After some reading in the Microsoft docs, I came across the description of these properties. It states, that the Win32_NetworkAdapterConfiguration class extends the CIM_Setting class. The properties Caption and Description are defined there. Is this an issue with the Inno Setup compiler (I am using the latest 6.0.2) or do I have to apply some sort of cast to may variant variable?


Solution

  • Of course that inherited properties are accessible. Actually Inno Setup does not even care what class is that. It uses late binding. Property name resolution is delegated to the class itself.

    But you are not working with Win32_NetworkAdapterConfiguration. IWbemServices.ExecQuery returns IEnumWbemClassObject, which in turn enumerates IWbemClassObject. And that contains results of your query. Your query does not ask for Caption and Description properties, so the result set naturally does not contain them.

    Add the properties to the query:

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