在JScript.NET中,下面的代码片段:
wmi.js
------
var wmi = GetObject("winmgmts:{impersonationLevel=impersonate}!\\\\.\\root\\cimv2"),
col =null, prc=null;
col=wmi.ExecQuery("SELECT * From Win32_Process", "WQL", 32);
//col=wmi.InstancesOf("Win32_Process");
var e = new Enumerator(col);
for (; !e.atEnd(); e.moveNext()){
prc = e.item();
print(prc.CommandLine);
}汇编有:
%windir%\Microsoft.NET\Framework64\v4.0.30319\jsc.exe /platform:x64 wmi.js并执行,但使用以下方法更改WMI调用:
col=wmi.ExecQuery("SELECT * From Win32_Process", "WQL", 32);编译仍然有效,而执行将给出:
Unhandled Exception: System.InvalidCastException:
Unable to cast COM object of type 'System.__ComObject' to interface type 'System.Collections.IEnumerable'.
This operation failed because the QueryInterface call on the COM component for the interface with IID '{496B0ABE-CDEE-11D3-88E8-00902754C43A}' failed due to the following error:
'No such interface supported (Exception from HRESULT: 0x80004002 我不明白为什么,因为InstancesOf和ExecQuery文档都说:
如果成功,该方法将返回一个SWbemObjectSet
此外,WSH JScript还可以枚举InstancesOf集合和ExecQuery。
发布于 2017-03-28 04:29:07
首先,删除wbemFlagForwardOnly的标志,ExecQuery返回一个按预期工作的对象。
var wmi = GetObject("winmgmts:{impersonationLevel=impersonate}!\\\\.\\root\\cimv2")
, col =null, prc=null;
col=wmi.ExecQuery("SELECT * From Win32_Process");
//col=wmi.InstancesOf("Win32_Process");
var e = new Enumerator(col);
for (; !e.atEnd(); e.moveNext()){
prc = e.item();
print(prc.CommandLine);
}作为解释,这里有一个暗处(我不是每天和Jscript.NET一起工作,我也不是专家)。
来自https://msdn.microsoft.com/en-us/library/ms974547.aspx 的
“仅使用前向枚举数的执行速度要比默认枚举数快得多,因为WMI不维护对SWbemObjectSet中对象的引用”
来自错误的:
“无法将'System.__ComObject‘类型的COM对象转换为接口类型’System.Collections.IEnumerable‘。”
将集合转换为枚举数似乎需要对正在转换的对象的引用。使用wbemFlagForwardOnly标志时,不存在传递的引用,因此强制转换失败。
我就是这么看这个的。把它当作它的价值。
我在研究时发现了一件有趣的事情:使用wscript/cscript的枚举器与从jsc/csc执行的exe相比没有错误。
此外,VBScript似乎对枚举这些标志没有问题;请查看示例并比较- https://msdn.microsoft.com/en-us/library/ms525775(v=vs.90).aspx。
https://stackoverflow.com/questions/41863159
复制相似问题