Skip to content

Commit

Permalink
Added testIWbemClassObjectGetQualifierSet and testIWbemContextSetValue
Browse files Browse the repository at this point in the history
  • Loading branch information
rchateauneu committed Oct 27, 2022
1 parent 74f146f commit 57cf499
Showing 1 changed file with 119 additions and 0 deletions.
119 changes: 119 additions & 0 deletions contrib/platform/test/com/sun/jna/platform/win32/COM/WbemcliTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,19 @@
import org.junit.Before;
import org.junit.Test;

import static com.sun.jna.platform.win32.Variant.VT_ARRAY;
import static com.sun.jna.platform.win32.Variant.VT_BSTR;

import com.sun.jna.platform.win32.Ole32;
import com.sun.jna.platform.win32.Variant;
import com.sun.jna.platform.win32.COM.Wbemcli.IEnumWbemClassObject;
import com.sun.jna.platform.win32.COM.Wbemcli.IWbemClassObject;
import com.sun.jna.platform.win32.COM.WbemcliUtil.WmiQuery;
import com.sun.jna.platform.win32.COM.WbemcliUtil.WmiResult;
import com.sun.jna.platform.win32.OleAuto;
import com.sun.jna.platform.win32.OaIdl.SAFEARRAY;
import com.sun.jna.platform.win32.WTypes;
import com.sun.jna.platform.win32.Kernel32;
import com.sun.jna.ptr.IntByReference;
import java.util.Arrays;
import java.util.HashSet;
Expand Down Expand Up @@ -309,4 +315,117 @@ public void testUnsupportedValues() {
assertNull(result.getValue(Win32_DiskDrive_Values.CAPABILITIES, i));
}
}

@Test
public void testIWbemClassObjectGetQualifierSet() {

Wbemcli.IWbemServices svc = null;
Wbemcli.IEnumWbemClassObject enumRes = null;
Variant.VARIANT.ByReference pVal = new Variant.VARIANT.ByReference();
IntByReference pType = new IntByReference();
IntByReference plFlavor = new IntByReference();

boolean foundWin32_Process = false;
try {
svc = WbemcliUtil.connectServer(WbemcliUtil.DEFAULT_NAMESPACE);
enumRes = svc.ExecQuery(
"WQL",
"SELECT * FROM meta_class",
Wbemcli.WBEM_FLAG_FORWARD_ONLY | Wbemcli.WBEM_FLAG_USE_AMENDED_QUALIFIERS, null);

while (true) {
Wbemcli.IWbemClassObject[] results = enumRes.Next(Wbemcli.WBEM_INFINITE, 1);
if (results.length == 0) {
break;
}

Wbemcli.IWbemClassObject classObject = results[0];
Variant.VARIANT.ByReference pQualifierVal = new Variant.VARIANT.ByReference();

COMUtils.checkRC(classObject.Get("__CLASS", 0, pVal, pType, plFlavor));
String className = pVal.stringValue();
if(! className.equals("Win32_Process")) {
continue;
}
foundWin32_Process = true;
OleAuto.INSTANCE.VariantClear(pVal);

COMUtils.checkRC(classObject.Get("__SUPERCLASS", 0, pVal, pType, plFlavor));
Object baseClass = pVal.getValue();
OleAuto.INSTANCE.VariantClear(pVal);
assertEquals("CIM_Process", baseClass.toString());

String[] propertyNames = classObject.GetNames(null, 0, pQualifierVal);
assertTrue(Arrays.asList(propertyNames).contains("ProcessId"));

Wbemcli.IWbemQualifierSet classQualifiersSet = classObject.GetQualifierSet();
String[] classQualifiersNames = classQualifiersSet.GetNames();
assertTrue(Arrays.asList(classQualifiersNames).contains("DisplayName"));
String classDisplayName = classQualifiersSet.Get("DisplayName");
assertEquals("Processes", classDisplayName);

Wbemcli.IWbemQualifierSet propertyQualifiersSet = classObject.GetPropertyQualifierSet("ProcessId");
String[] propertyQualifierNames = propertyQualifiersSet.GetNames();

assertTrue(Arrays.asList(propertyQualifierNames).contains("DisplayName"));
String propertyDisplayName = propertyQualifiersSet.Get("DisplayName");
assertEquals("Process Id", propertyDisplayName);

assertTrue(Arrays.asList(propertyQualifierNames).contains("CIMTYPE"));
String propertyCIMTYPE = propertyQualifiersSet.Get("CIMTYPE");
assertEquals("uint32", propertyCIMTYPE);

classObject.Release();
}
} finally {
if (svc != null) svc.Release();
if (enumRes != null) enumRes.Release();
}
assertTrue(foundWin32_Process);
}

@Test
public void testIWbemContextSetValue() {
long currentPid = Kernel32.INSTANCE.GetCurrentProcessId();
String objectPath = String.format("\\\\.\\%s:Win32_Process.Handle=\"%d\"", WbemcliUtil.DEFAULT_NAMESPACE, currentPid);

// This context object retrieves only parts of a WMI instance.
Wbemcli.IWbemContext pctxDrive = new Wbemcli.IWbemContext().create();
pctxDrive.SetValue("__GET_EXTENSIONS", 0, true);
pctxDrive.SetValue("__GET_EXT_CLIENT_REQUEST", 0, true);

// Create a safe array of just one property to retrieve.
SAFEARRAY psaProperties = SAFEARRAY.createSafeArray(new WTypes.VARTYPE(VT_BSTR), 1);
OleAuto.INSTANCE.SafeArrayLock(psaProperties);
try {
WTypes.BSTR strPropertyBSTR = OleAuto.INSTANCE.SysAllocString("ProcessId");
try {
psaProperties.putElement(strPropertyBSTR, 0);
} finally {
OleAuto.INSTANCE.SysFreeString(strPropertyBSTR);
}
} finally {
OleAuto.INSTANCE.SafeArrayUnlock(psaProperties);
}

Variant.VARIANT.ByReference vPropertyList = new Variant.VARIANT.ByReference();
vPropertyList.setVarType((short) (VT_ARRAY | VT_BSTR));
vPropertyList.setValue(psaProperties);
pctxDrive.SetValue("__GET_EXT_PROPERTIES", 0, vPropertyList);
psaProperties.destroy();

Variant.VARIANT.ByReference pVal = new Variant.VARIANT.ByReference();
Wbemcli.IWbemServices svc = null;
try {
svc = WbemcliUtil.connectServer(WbemcliUtil.DEFAULT_NAMESPACE);
Wbemcli.IWbemClassObject classObject = svc.GetObject(objectPath, Wbemcli.WBEM_FLAG_RETURN_WBEM_COMPLETE, pctxDrive);
// The properties "Handle" and "PropertyId" must have the same values with different types.
COMUtils.checkRC(classObject.Get("ProcessId", 0, pVal, null, null));
}
finally {
if (svc != null) svc.Release();
}
assertEquals(currentPid, pVal.longValue());
}

}

0 comments on commit 57cf499

Please sign in to comment.