|
25 | 25 |
|
26 | 26 | import com.sun.jna.Pointer;
|
27 | 27 | import com.sun.jna.WString;
|
| 28 | +import com.sun.jna.platform.win32.*; |
| 29 | +import com.sun.jna.platform.win32.COM.COMUtils; |
| 30 | +import com.sun.jna.platform.win32.COM.Unknown; |
28 | 31 | import com.sun.jna.platform.win32.Guid.CLSID;
|
29 | 32 | import com.sun.jna.platform.win32.Guid.GUID;
|
30 | 33 | import com.sun.jna.platform.win32.OaIdl.SAFEARRAY;
|
|
45 | 48 | */
|
46 | 49 | public interface Wbemcli {
|
47 | 50 |
|
| 51 | + public static final int WBEM_FLAG_RETURN_WBEM_COMPLETE = 0x00000000; |
48 | 52 | public static final int WBEM_FLAG_RETURN_IMMEDIATELY = 0x00000010;
|
49 | 53 | public static final int WBEM_FLAG_FORWARD_ONLY = 0x00000020;
|
| 54 | + public static final int WBEM_FLAG_NO_ERROR_OBJECT = 0x00000040; |
| 55 | + public static final int WBEM_FLAG_SEND_STATUS = 0x00000080; |
| 56 | + public static final int WBEM_FLAG_ENSURE_LOCATABLE = 0x00000100; |
| 57 | + public static final int WBEM_FLAG_DIRECT_READ = 0x00000200; |
| 58 | + public static final int WBEM_MASK_RESERVED_FLAGS = 0x0001F000; |
| 59 | + public static final int WBEM_FLAG_USE_AMENDED_QUALIFIERS = 0x00020000; |
| 60 | + public static final int WBEM_FLAG_STRONG_VALIDATION = 0x00100000; |
50 | 61 | public static final int WBEM_INFINITE = 0xFFFFFFFF;
|
51 | 62 |
|
52 | 63 | // Non-error constants
|
@@ -146,6 +157,88 @@ public String[] GetNames(String wszQualifierName, int lFlags, VARIANT.ByReferenc
|
146 | 157 | }
|
147 | 158 | return names;
|
148 | 159 | }
|
| 160 | + |
| 161 | + public HRESULT GetQualifierSet(PointerByReference ppQualSet) { |
| 162 | + // Get is the fourth method of IWbemClassObjectVtbl in WbemCli.h : |
| 163 | + return (HRESULT) _invokeNativeObject(3, |
| 164 | + new Object[] { getPointer(), ppQualSet }, HRESULT.class); |
| 165 | + } |
| 166 | + |
| 167 | + public IWbemQualifierSet GetQualifierSet() { |
| 168 | + PointerByReference ppQualSet = new PointerByReference(); |
| 169 | + HRESULT hr = GetQualifierSet(ppQualSet); |
| 170 | + COMUtils.checkRC(hr); |
| 171 | + IWbemQualifierSet qualifier = new IWbemQualifierSet(ppQualSet.getValue()); |
| 172 | + |
| 173 | + return qualifier; |
| 174 | + } |
| 175 | + |
| 176 | + /* |
| 177 | + // https://docs.microsoft.com/en-us/windows/win32/api/wbemcli/nf-wbemcli-iwbemclassobject-getpropertyqualifierset |
| 178 | + HRESULT GetPropertyQualifierSet( |
| 179 | + [in] LPCWSTR wszProperty, |
| 180 | + [out] IWbemQualifierSet **ppQualSet |
| 181 | + ); |
| 182 | + */ |
| 183 | + public HRESULT GetPropertyQualifierSet(WString wszProperty, PointerByReference ppQualSet) { |
| 184 | + // Get is 12th method of IWbemClassObjectVtbl in WbemCli.h : |
| 185 | + return (HRESULT) _invokeNativeObject(11, |
| 186 | + new Object[] { getPointer(), wszProperty, ppQualSet }, HRESULT.class); |
| 187 | + } |
| 188 | + |
| 189 | + public IWbemQualifierSet GetPropertyQualifierSet(String strProperty) { |
| 190 | + WString wszProperty = new WString(strProperty); |
| 191 | + PointerByReference ppQualSet = new PointerByReference(); |
| 192 | + |
| 193 | + COMUtils.checkRC(GetPropertyQualifierSet(wszProperty, ppQualSet)); |
| 194 | + IWbemQualifierSet qualifier = new IWbemQualifierSet(ppQualSet.getValue()); |
| 195 | + return qualifier; |
| 196 | + } |
| 197 | + } |
| 198 | + |
| 199 | + class IWbemQualifierSet extends Unknown { |
| 200 | + public IWbemQualifierSet(Pointer pvInstance) { |
| 201 | + super(pvInstance); |
| 202 | + } |
| 203 | + |
| 204 | + public HRESULT Get(WString wszName, int lFlags, VARIANT.ByReference pVal, IntByReference plFlavor) { |
| 205 | + return (HRESULT) _invokeNativeObject(3, |
| 206 | + new Object[] { getPointer(), wszName, lFlags, pVal, plFlavor }, HRESULT.class); |
| 207 | + } |
| 208 | + |
| 209 | + public String Get(String wszName) { |
| 210 | + WString wszNameStr = new WString(wszName); |
| 211 | + Variant.VARIANT.ByReference pQualifierVal = new Variant.VARIANT.ByReference(); |
| 212 | + HRESULT hres = Get(wszNameStr, 0, pQualifierVal, null); |
| 213 | + if(hres.intValue() == 0x80041002) { |
| 214 | + // This error for some classes only. |
| 215 | + return null; |
| 216 | + } |
| 217 | + int qualifierInt = pQualifierVal.getVarType().intValue(); |
| 218 | + switch(qualifierInt) { |
| 219 | + case Wbemcli.CIM_BOOLEAN: |
| 220 | + return String.valueOf(pQualifierVal.booleanValue()); |
| 221 | + case Wbemcli.CIM_STRING: |
| 222 | + return pQualifierVal.stringValue(); |
| 223 | + } |
| 224 | + return null; |
| 225 | + } |
| 226 | + |
| 227 | + public HRESULT GetNames(int lFlags, PointerByReference pNames) { |
| 228 | + return (HRESULT) _invokeNativeObject(6, |
| 229 | + new Object[] { getPointer(), lFlags, pNames }, HRESULT.class); |
| 230 | + } |
| 231 | + |
| 232 | + public String[] GetNames() { |
| 233 | + PointerByReference pbr = new PointerByReference(); |
| 234 | + COMUtils.checkRC(GetNames(0, pbr)); |
| 235 | + Object[] nameObjects = (Object[]) OaIdlUtil.toPrimitiveArray(new SAFEARRAY(pbr.getValue()), true); |
| 236 | + String[] qualifierNames = new String[nameObjects.length]; |
| 237 | + for(int i = 0; i < nameObjects.length; i++) { |
| 238 | + qualifierNames[i] = (String) nameObjects[i]; |
| 239 | + } |
| 240 | + return qualifierNames; |
| 241 | + } |
149 | 242 | }
|
150 | 243 |
|
151 | 244 | /**
|
@@ -278,19 +371,83 @@ public IEnumWbemClassObject ExecQuery(String strQueryLanguage, String strQuery,
|
278 | 371 | OleAuto.INSTANCE.SysFreeString(strQueryBSTR);
|
279 | 372 | }
|
280 | 373 | }
|
| 374 | + |
| 375 | + public HRESULT GetObject(BSTR strObjectPath, int lFlags, IWbemContext pCtx, |
| 376 | + PointerByReference ppObject, PointerByReference ppCallResult) { |
| 377 | + // GetObject is the 7th method of IWbemServicesVtbl in WbemCli.h |
| 378 | + return (HRESULT) _invokeNativeObject(6, |
| 379 | + new Object[] { getPointer(), strObjectPath, lFlags, pCtx, ppObject, ppCallResult}, HRESULT.class); |
| 380 | + } |
| 381 | + |
| 382 | + public IWbemClassObject GetObject(String strObjectPath, int lFlags, IWbemContext pCtx) { |
| 383 | + BSTR strObjectPathBSTR = OleAuto.INSTANCE.SysAllocString(strObjectPath); |
| 384 | + try { |
| 385 | + PointerByReference ppObject = new PointerByReference(); |
| 386 | + HRESULT res = GetObject(strObjectPathBSTR, lFlags, pCtx, ppObject, null); |
| 387 | + COMUtils.checkRC(res); |
| 388 | + return new IWbemClassObject(ppObject.getValue()); |
| 389 | + } finally { |
| 390 | + OleAuto.INSTANCE.SysFreeString(strObjectPathBSTR); |
| 391 | + } |
| 392 | + } |
281 | 393 | }
|
282 | 394 |
|
283 | 395 | /**
|
284 | 396 | * Optionally used to communicate additional context information to
|
285 | 397 | * providers when submitting IWbemServices calls to WMI
|
286 | 398 | */
|
287 | 399 | class IWbemContext extends Unknown {
|
| 400 | + public static final CLSID CLSID_WbemContext = new CLSID("674B6698-EE92-11D0-AD71-00C04FD8FDFF"); |
| 401 | + public static final GUID IID_IWbemContext = new GUID("44aca674-e8fc-11d0-a07c-00c04fb68820"); |
288 | 402 |
|
289 | 403 | public IWbemContext() {
|
290 | 404 | }
|
291 | 405 |
|
| 406 | + public static IWbemContext create() { |
| 407 | + PointerByReference pbr = new PointerByReference(); |
| 408 | + |
| 409 | + HRESULT hres = Ole32.INSTANCE.CoCreateInstance(CLSID_WbemContext, null, WTypes.CLSCTX_INPROC_SERVER, |
| 410 | + IID_IWbemContext, pbr); |
| 411 | + if (COMUtils.FAILED(hres)) { |
| 412 | + return null; |
| 413 | + } |
| 414 | + |
| 415 | + return new IWbemContext(pbr.getValue()); |
| 416 | + } |
| 417 | + |
292 | 418 | public IWbemContext(Pointer pvInstance) {
|
293 | 419 | super(pvInstance);
|
294 | 420 | }
|
| 421 | + |
| 422 | + public void SetValue(String wszName, int lFlag, Variant.VARIANT pValue) { |
| 423 | + BSTR wszNameBSTR = OleAuto.INSTANCE.SysAllocString(wszName); |
| 424 | + try { |
| 425 | + // SetValue is the 9th method of IWbemContextVtbl in WbemCli.h |
| 426 | + HRESULT res = (HRESULT) _invokeNativeObject(8, |
| 427 | + new Object[] { getPointer(), wszNameBSTR, lFlag, pValue}, HRESULT.class); |
| 428 | + COMUtils.checkRC(res); |
| 429 | + } finally { |
| 430 | + OleAuto.INSTANCE.SysFreeString(wszNameBSTR); |
| 431 | + } |
| 432 | + } |
| 433 | + |
| 434 | + public void SetValue(String wszName, int lFlag, boolean pValue) { |
| 435 | + Variant.VARIANT aVariant = new Variant.VARIANT(); |
| 436 | + aVariant.setValue(Variant.VT_BOOL, pValue ? Variant.VARIANT_TRUE : Variant.VARIANT_FALSE); |
| 437 | + SetValue(wszName, lFlag, aVariant); |
| 438 | + OleAuto.INSTANCE.VariantClear(aVariant); |
| 439 | + } |
| 440 | + |
| 441 | + public void SetValue(String wszName, int lFlag, String pValue) { |
| 442 | + Variant.VARIANT aVariant = new Variant.VARIANT(); |
| 443 | + BSTR strValue = OleAuto.INSTANCE.SysAllocString(pValue); |
| 444 | + try { |
| 445 | + aVariant.setValue(Variant.VT_LPSTR, strValue); |
| 446 | + SetValue(wszName, lFlag, aVariant); |
| 447 | + } |
| 448 | + finally { |
| 449 | + OleAuto.INSTANCE.SysFreeString(strValue); |
| 450 | + } |
| 451 | + } |
295 | 452 | }
|
296 | 453 | }
|
0 commit comments