Skip to content

Commit

Permalink
Merge pull request java-native-access#1084 from matthiasblaesing/wmi
Browse files Browse the repository at this point in the history
WMI Query enhancement
  • Loading branch information
matthiasblaesing authored Apr 15, 2019
2 parents aa9341d + 126af4e commit b0a6e17
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Features
* [#1068](https://github.com/java-native-access/jna/pull/1068): `c.s.j.p.win32.Advapi32Util.getAccountBySid(String systemName, PSID sid)` ignored parameter instead of passing it to the native function - [@nirud](https://github.com/nirud).
* [#813](https://github.com/java-native-access/jna/issues/813): Support for default methods on interfaces (experimental) - [@matthiasblaesing](https://github.com/matthiasblaesing).
* [#1073](https://github.com/java-native-access/jna/issues/1073): Support COM setters with multiple parameters using `c.s.j.p.win32.COM.util.ProxyObject` - [@matthiasblaesing](https://github.com/matthiasblaesing).
* [#1083](https://github.com/java-native-access/jna/issues/1083): Prevent access to unsupported values in `c.s.j.p.win32.COM.WbemcliUtil#enumerateProperties` and bind `c.s.j.p.win32.COM.Wbemcli.IWbemClassObject.GetNames` - [@matthiasblaesing](https://github.com/matthiasblaesing).

Bug Fixes
---------
Expand Down
41 changes: 41 additions & 0 deletions contrib/platform/src/com/sun/jna/platform/win32/COM/Wbemcli.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
import com.sun.jna.WString;
import com.sun.jna.platform.win32.Guid.CLSID;
import com.sun.jna.platform.win32.Guid.GUID;
import com.sun.jna.platform.win32.OaIdl.SAFEARRAY;
import com.sun.jna.platform.win32.OaIdlUtil;
import com.sun.jna.platform.win32.Ole32;
import com.sun.jna.platform.win32.OleAuto;
import com.sun.jna.platform.win32.Variant.VARIANT;
Expand Down Expand Up @@ -81,6 +83,24 @@ public interface Wbemcli {
public static final int CIM_OBJECT = 13;
public static final int CIM_FLAG_ARRAY = 0x2000;

public interface WBEM_CONDITION_FLAG_TYPE {
public static final int WBEM_FLAG_ALWAYS = 0;
public static final int WBEM_FLAG_ONLY_IF_TRUE = 0x1;
public static final int WBEM_FLAG_ONLY_IF_FALSE = 0x2;
public static final int WBEM_FLAG_ONLY_IF_IDENTICAL = 0x3;
public static final int WBEM_MASK_PRIMARY_CONDITION = 0x3;
public static final int WBEM_FLAG_KEYS_ONLY = 0x4;
public static final int WBEM_FLAG_REFS_ONLY = 0x8;
public static final int WBEM_FLAG_LOCAL_ONLY = 0x10;
public static final int WBEM_FLAG_PROPAGATED_ONLY = 0x20;
public static final int WBEM_FLAG_SYSTEM_ONLY = 0x30;
public static final int WBEM_FLAG_NONSYSTEM_ONLY = 0x40;
public static final int WBEM_MASK_CONDITION_ORIGIN = 0x70;
public static final int WBEM_FLAG_CLASS_OVERRIDES_ONLY = 0x100;
public static final int WBEM_FLAG_CLASS_LOCAL_AND_OVERRIDES = 0x200;
public static final int WBEM_MASK_CLASS_CONDITION = 0x300;
}

/**
* Contains and manipulates both WMI class definitions and class object
* instances.
Expand All @@ -105,6 +125,27 @@ public HRESULT Get(String wszName, int lFlags, VARIANT.ByReference pVal, IntByRe
IntByReference plFlavor) {
return Get(wszName == null ? null : new WString(wszName), lFlags, pVal, pType, plFlavor);
}

public HRESULT GetNames(String wszQualifierName, int lFlags, VARIANT.ByReference pQualifierVal, PointerByReference pNames) {
return GetNames(wszQualifierName == null ? null : new WString(wszQualifierName), lFlags, pQualifierVal, pNames);
}

public HRESULT GetNames(WString wszQualifierName, int lFlags, VARIANT.ByReference pQualifierVal, PointerByReference pNames) {
// 8th method in IWbemClassObjectVtbl
return (HRESULT) _invokeNativeObject(7,
new Object[]{ getPointer(), wszQualifierName, lFlags, pQualifierVal, pNames}, HRESULT.class);
}

public String[] GetNames(String wszQualifierName, int lFlags, VARIANT.ByReference pQualifierVal) {
PointerByReference pbr = new PointerByReference();
COMUtils.checkRC(GetNames(wszQualifierName, lFlags, pQualifierVal, pbr));
Object[] nameObjects = (Object[]) OaIdlUtil.toPrimitiveArray(new SAFEARRAY(pbr.getValue()), true);
String[] names = new String[nameObjects.length];
for(int i = 0; i < nameObjects.length; i++) {
names[i] = (String) nameObjects[i];
}
return names;
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@
import com.sun.jna.platform.win32.COM.Wbemcli.IWbemClassObject;
import com.sun.jna.platform.win32.COM.Wbemcli.IWbemLocator;
import com.sun.jna.platform.win32.COM.Wbemcli.IWbemServices;
import static com.sun.jna.platform.win32.Variant.VT_ARRAY;
import static com.sun.jna.platform.win32.Variant.VT_DISPATCH;
import static com.sun.jna.platform.win32.Variant.VT_UNKNOWN;
import static com.sun.jna.platform.win32.Variant.VT_VECTOR;
import com.sun.jna.ptr.IntByReference;

/**
Expand Down Expand Up @@ -271,7 +275,9 @@ private static <T extends Enum<T>> IEnumWbemClassObject selectProperties(IWbemSe
* will always wait for results.
*
* @return A WmiResult object encapsulating an EnumMap which will hold
* the results.
* the results. Values, that are not supported by this helper
* ({@code Dispatch}, {@code Unknown}, {@code SAFEARRAY}) are
* not returned and reported as {@code null}.
*
* @throws TimeoutException
* if the query times out before completion
Expand Down Expand Up @@ -336,12 +342,20 @@ private static <T extends Enum<T>> WmiResult<T> enumerateProperties(IEnumWbemCla
case Variant.VT_R8:
values.add(vtType, cimType, property, pVal.doubleValue());
break;
case Variant.VT_EMPTY:
case Variant.VT_NULL:
values.add(vtType, cimType, property, null);
break;
// Unimplemented type. User must cast
default:
values.add(vtType, cimType, property, pVal.getValue());
if(((vtType & VT_ARRAY) == VT_ARRAY) ||
((vtType & VT_UNKNOWN) == VT_UNKNOWN)||
((vtType & VT_DISPATCH) == VT_DISPATCH)||
((vtType & VT_VECTOR) == VT_VECTOR)) {
values.add(vtType, cimType, property, null);
} else {
values.add(vtType, cimType, property, pVal.getValue());
}
}
OleAuto.INSTANCE.VariantClear(pVal);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@
import com.sun.jna.platform.win32.COM.WbemcliUtil.WmiResult;
import com.sun.jna.platform.win32.OleAuto;
import com.sun.jna.ptr.IntByReference;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

/**
* Test class for Wbemcli and WbemcliUti methods and classes used to query WMI.
Expand Down Expand Up @@ -189,6 +192,12 @@ public void testShowProperties() {

for(IWbemClassObject iwco: results) {
resultCount++;
Set<String> names = new HashSet<String>(Arrays.asList(iwco.GetNames(null, 0, null)));
assertTrue(names.contains("CommandLine"));
assertTrue(names.contains("ProcessId"));
assertTrue(names.contains("WorkingSetSize"));
assertTrue(names.contains("ExecutionState"));
assertTrue(names.contains("CreationDate"));
try {
// COMMANDLINE is STRING = VT_BSTR
iwco.Get("COMMANDLINE", 0, pVal, pType, plFlavor);
Expand Down Expand Up @@ -282,4 +291,22 @@ public void testWmiOperatingSystem() {
assertEquals(Variant.VT_BOOL, os.getVtType(OperatingSystemProperty.PRIMARY));
assertNotNull(os.getValue(OperatingSystemProperty.PRIMARY, 0));
}

enum Win32_DiskDrive_Values {
CAPTION,
CAPABILITIES
}

@Test
public void testUnsupportedValues() {
WmiQuery<Win32_DiskDrive_Values> serialNumberQuery = new WmiQuery<Win32_DiskDrive_Values>("Win32_DiskDrive", Win32_DiskDrive_Values.class);
WmiResult<Win32_DiskDrive_Values> result = serialNumberQuery.execute();
assertTrue(result.getResultCount() > 0);
for (int i = 0; i < result.getResultCount(); i++) {
assertNotNull(result.getValue(Win32_DiskDrive_Values.CAPTION, i));
// Capabilities are represented by a SAFEARRAY, this not supported
// in the simplified API
assertNull(result.getValue(Win32_DiskDrive_Values.CAPABILITIES, i));
}
}
}

0 comments on commit b0a6e17

Please sign in to comment.