Iterate SWbemObjectSet in Windows XP and Inno Setu

2020-03-27 17:19发布

问题:

I have a problem with taking MAC-addresses list in Windows XP from Inno Setup installer.

I'm trying to write some code (took it from Get MAC address in Inno Setup):

function GetMacAddressesList(out List: Array of String): Integer;
var
    I: Integer;
    WQLQuery: string;
    WbemLocator: Variant;
    WbemServices: Variant;
    WbemObject: Variant;
    WbemObjectSet: Variant;
begin
    Result := 0;

    WbemLocator := CreateOleObject('WbemScripting.SWbemLocator');
    WbemServices := WbemLocator.ConnectServer('localhost', 'root\cimv2');

    WQLQuery := 'Select * from Win32_NetworkAdapterConfiguration where IPEnabled=true';

    WbemObjectSet := WbemServices.ExecQuery(WQLQuery);
    if not VarIsNull(WbemObjectSet) and (WbemObjectSet.Count > 0) then
    begin
        Result := WbemObjectSet.Count;
        SetArrayLength(List, WbemObjectSet.Count);
        for I := 0 to WbemObjectSet.Count - 1 do
        begin
            WbemObject := WbemObjectSet.ItemIndex(I);
            if not VarIsNull(WbemObject) then
            begin
                List[I] := WbemObject.MACAddress;
                StringChange(List[i], ':', '');
                StringChange(List[I], '-', '');
            end;
        end;
    end;
end;

And I have a problem with ItemIndex method. It appears only in Windows Vista. How can I do this on XP? I really don't know, because every solution, that I have found in Internet doesn't work. May be because in Inno Setup libraries there is no such type as IEnumVariant and I can't iterate by SWbemObjectSet with for each obj in objset syntax...

I was also trying to get SWbemObject with Item method:

WbemObject := WbemObjectSet.Item('Win32_NetworkAdapterConfiguration.Index=' + IntToStr(I));

but it returns error

SWbemObjectSet: not found

Can anybody help me? Has this problem some solution?

回答1:

Yes, you would have to implement the IEnumVariant. Not sure if that's possible with Pascal Script.


Use of the SWbemObjectSet.Item method is like this:

WbemLocator := CreateOleObject('WbemScripting.SWbemLocator');
WbemServices := WbemLocator.ConnectServer('localhost', 'root\cimv2');

WQLQuery := 'Select * from Win32_NetworkAdapterConfiguration';
WbemObjectSet := WbemServices.ExecQuery(WQLQuery);
if not VarIsNull(WbemObjectSet) then
begin
  for I := 0 to WbemObjectSet.Count - 1 do
  begin
    WbemObject := WbemObjectSet.Item(Format('Win32_NetworkAdapterConfiguration=%d', [I]));
    if WbemObject.IPEnabled then
    begin
      Log(WbemObject.MACAddress);
    end;
  end;
end;

But it seems that neither this approach works on Windows XP.


A possible workaround is to execute

wmic nicconfig get MACAddress 

redirect to a file and read it.

See How to get an output of an Exec'ed program in Inno Setup?