我在从Inno Setup安装程序中获取Windows XP中的MAC地址列表时遇到问题。
我正在尝试编写一些代码(从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;
我的ItemIndex
方法存在问题。它仅出现在Windows Vista中。我怎么能在XP上这样做?我真的不知道,因为我在互联网上发现的每一个解决方案都不起作用。可能是因为在Inno Setup库中没有IEnumVariant
这样的类型,我不能用SWbemObjectSet
语法迭代for each obj in objset
......
我还尝试使用SWbemObject
方法获取Item
:
WbemObject := WbemObjectSet.Item('Win32_NetworkAdapterConfiguration.Index=' + IntToStr(I));
但它返回错误
SWbemObjectSet:找不到
任何人都可以帮助我吗?这个问题有一些解决方案吗?
答案 0 :(得分:1)
是的,您必须实施IEnumVariant
。不确定Pascal脚本是否可行。
使用SWbemObjectSet.Item
method是这样的:
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;
但似乎这种方法都不适用于Windows XP。
可能的解决方法是执行
wmic nicconfig get MACAddress
重定向到文件并阅读。
请参阅How to get an output of an Exec'ed program in Inno Setup?