【发布时间】:2019-04-12 12:25:11
【问题描述】:
我使用 jna 来运行 WMI 查询。
以下代码查询 WMI SELECT Caption,Capabilities from Win32_DiskDrive。 Win32_DiskDrive.Capabilities 的类型是 uint16[] 并且 result.getValue 返回一个 SAFEARRAY 实例。
System.out.println("Var Type(3 expected): " + value.getVarType().intValue());
如果我多次启动该过程,则随机返回 0 或 3。
System.out.println("Size (>0 expected): " + (value.getUBound(0) - value.getLBound(0)));
是正确的,但是
Object el = value.getElement(0);
失败。
value.accessData();
返回 null 这也是意外的,所以我不能使用 OaIdlUtil#toPrimitiveArray (Nullpointer)
不幸的是,代码不起作用,我不知道可能出了什么问题。有什么想法吗?
enum Win32_DiskDrive_Values {
Caption,
Capabilities
}
public static void main(String[] args) throws IOException, InterruptedException {
try {
WmiQuery<Win32_DiskDrive_Values> serialNumberQuery = new WmiQuery<Win32_DiskDrive_Values>("Win32_DiskDrive", Win32_DiskDrive_Values.class);
Ole32.INSTANCE.CoInitializeEx(null, Ole32.COINIT_MULTITHREADED);
WmiResult<Win32_DiskDrive_Values> result = serialNumberQuery.execute();
for (int i = 0; i < result.getResultCount(); i++) {
System.out.println(result.getValue(Win32_DiskDrive_Values.Caption, i));
SAFEARRAY value = (SAFEARRAY) result.getValue(Win32_DiskDrive_Values.Capabilities, i);
// According to https://docs.microsoft.com/en-us/windows/desktop/cimwin32prov/win32-diskdrive, the type of Capabilities
// should be uint16[] which should be Variant.VT_I2 (2-byte integer)
// however, it is not constant. sometimes it is 0, sometimes Variant.VT_I2 (3);
System.out.println("Var Type(3 expected): " + value.getVarType().intValue());
System.out.println("Size (>0 expected): " + (value.getUBound(0) - value.getLBound(0)));
Object el = value.getElement(0);
System.out.println("Element 0 (!=null expected): " + el);
Pointer pointer = value.accessData();
System.out.println("pointer (!=null expected): " + pointer);
}
} catch (Throwable e) {
e.printStackTrace();
} finally {
Ole32.INSTANCE.CoUninitialize();
}
}
【问题讨论】: