Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

delphi - Iterate SWbemObjectSet in Windows XP and Inno Setup

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', 'rootcimv2');

    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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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', 'rootcimv2');

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?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...