在Windows XP和Inno安装程序中迭代SWbemObjectSet [英] Iterate SWbemObjectSet in Windows XP and Inno Setup
问题描述
我正在尝试写一些代码(从 href =http://stackoverflow.com/a/41959996/6891843>在Inno设置中获取MAC地址):
function GetMacAddressesList(out List:Array of String):Integer;
var
I:整数;
WQLQuery:string;
WbemLocator:Variant;
WbemServices:Variant;
WbemObject:Variant;
WbemObjectSet:Variant;
begin
结果:= 0;
WbemLocator:= CreateOleObject('WbemScripting.SWbemLocator');
WbemServices:= WbemLocator.ConnectServer('localhost','root\cimv2');
WQLQuery:='选择*从Win32_NetworkAdapterConfiguration其中IPEnabled = true';
WbemObjectSet:= WbemServices.ExecQuery(WQLQuery);
如果不是VarIsNull(WbemObjectSet)和(WbemObjectSet.Count> 0)然后
begin
结果:= WbemObjectSet.Count;
SetArrayLength(List,WbemObjectSet.Count);
for I:= 0 to WbemObjectSet.Count - 1 do
begin
WbemObject:= WbemObjectSet.ItemIndex(I);
如果不是VarIsNull(WbemObject)然后
begin
列表[I]:= WbemObject.MACAddress;
StringChange(List [i],':','');
StringChange(List [I],' - ','');
结束
结束
结束
结束
我有一个问题, ItemIndex
方法。它只出现在Windows Vista中。如何在XP上执行此操作?我真的不知道,因为我在互联网上找到的每一个解决方案都不行。可能是因为在Inno Setup库中没有类似于 IEnumVariant
的类型,我不能通过 SWbemObjectSet
code>对于objset中的每个obj 语法...
我也试图获得 SWbemObject
与项目
方法:
WbemObject:= WbemObjectSet.Item('Win32_NetworkAdapterConfiguration.Index ='+ IntToStr(I));
但它返回错误
SWbemObjectSet:未找到
有人可以帮助我吗?有这个问题有一些解决方案吗?
是的,你必须实现 IEnumVariant
。不确定是否可以使用Pascal脚本。
使用 SWbemObjectSet.Item
方法是这样的:
WbemLocator:= CreateOleObject('WbemScripting.SWbemLocator');
WbemServices:= WbemLocator.ConnectServer('localhost','root\cimv2');
WQLQuery:='选择*从Win32_NetworkAdapterConfiguration';
WbemObjectSet:= WbemServices.ExecQuery(WQLQuery);
如果不是VarIsNull(WbemObjectSet)然后
开始
为I:= 0到WbemObjectSet.Count - 1 do
begin
WbemObject:= WbemObjectSet.Item(Format 'Win32_NetworkAdapterConfiguration =%d',[I]));
如果WbemObject.IPEnabled然后
开始
日志(WbemObject.MACAddress);
结束
结束
结束
但似乎这种方法在Windows XP上都不起作用。
可能的解决方法是执行
wmic nicconfig获取MACAddress
重定向到一个文件并阅读。
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?
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?
这篇关于在Windows XP和Inno安装程序中迭代SWbemObjectSet的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!