Skip to content

Commit c32053e

Browse files
Merge pull request #1534 from faddom/master
Added GetMethod, Put, SpawnInstance, and ExecMethod functions
2 parents e96f301 + 0e2386e commit c32053e

File tree

3 files changed

+117
-0
lines changed

3 files changed

+117
-0
lines changed

CHANGES.md

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ Next Release (5.14.0)
77

88
Features
99
--------
10+
* [#1534](https://github.com/java-native-access/jna/pull/1534): Add `GetMethod`, `Put`, `SpawnInstance` to `c.s.j.p.win32.COM.WbemCli#IWbemClassObject` and `ExecMethod` to `c.s.j.p.win32.COM.WbemCli#IWbemServices` - [@faddom](https://github.com/faddom).
1011

1112
Bug Fixes
1213
---------

contrib/platform/src/com/sun/jna/platform/win32/COM/Wbemcli.java

+78
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,23 @@ public HRESULT Get(String wszName, int lFlags, VARIANT.ByReference pVal, IntByRe
137137
return Get(wszName == null ? null : new WString(wszName), lFlags, pVal, pType, plFlavor);
138138
}
139139

140+
public HRESULT GetMethod(String wszName, int lFlags, PointerByReference ppInSignature, PointerByReference ppOutSignature) {
141+
return GetMethod(wszName == null ? null : new WString(wszName), lFlags, ppInSignature, ppOutSignature);
142+
}
143+
144+
public HRESULT GetMethod(WString wszName, int lFlags, PointerByReference ppInSignature, PointerByReference ppOutSignature) {
145+
// 20th method in IWbemClassObjectVtbl
146+
return (HRESULT) _invokeNativeObject(19,
147+
new Object[]{ getPointer(), wszName, lFlags, ppInSignature, ppOutSignature}, HRESULT.class);
148+
}
149+
150+
public IWbemClassObject GetMethod(String wszName) {
151+
PointerByReference ppInSignature = new PointerByReference();
152+
HRESULT res = GetMethod(wszName, 0, ppInSignature, null);
153+
COMUtils.checkRC(res);
154+
return new IWbemClassObject(ppInSignature.getValue());
155+
}
156+
140157
public HRESULT GetNames(String wszQualifierName, int lFlags, VARIANT.ByReference pQualifierVal, PointerByReference pNames) {
141158
return GetNames(wszQualifierName == null ? null : new WString(wszQualifierName), lFlags, pQualifierVal, pNames);
142159
}
@@ -194,6 +211,42 @@ public IWbemQualifierSet GetPropertyQualifierSet(String strProperty) {
194211
IWbemQualifierSet qualifier = new IWbemQualifierSet(ppQualSet.getValue());
195212
return qualifier;
196213
}
214+
215+
public HRESULT Put(String wszName, int lFlags, Variant.VARIANT pVal, int Type) {
216+
return Put(wszName == null ? null : new WString(wszName), lFlags, pVal, Type);
217+
}
218+
219+
public HRESULT Put(WString wszName, int lFlags, Variant.VARIANT pVal, int Type) {
220+
// 6th method in IWbemClassObjectVtbl
221+
return (HRESULT) _invokeNativeObject(5,
222+
new Object[]{ getPointer(), wszName, lFlags, pVal, Type}, HRESULT.class);
223+
}
224+
225+
public void Put(String wszName, String pValue) {
226+
Variant.VARIANT aVariant = new Variant.VARIANT();
227+
BSTR strValue = OleAuto.INSTANCE.SysAllocString(pValue);
228+
try {
229+
aVariant.setValue(Variant.VT_BSTR, strValue);
230+
HRESULT res = Put(wszName, 0, aVariant, 0);
231+
COMUtils.checkRC(res);
232+
}
233+
finally {
234+
OleAuto.INSTANCE.VariantClear(aVariant);
235+
}
236+
}
237+
238+
public HRESULT SpawnInstance(int lFlags, PointerByReference ppNewInstance) {
239+
// 16th method in IWbemClassObjectVtbl
240+
return (HRESULT) _invokeNativeObject(15,
241+
new Object[]{ getPointer(), lFlags, ppNewInstance}, HRESULT.class);
242+
}
243+
244+
public IWbemClassObject SpawnInstance() {
245+
PointerByReference ppNewInstance = new PointerByReference();
246+
HRESULT res = SpawnInstance(0, ppNewInstance);
247+
COMUtils.checkRC(res);
248+
return new IWbemClassObject(ppNewInstance.getValue());
249+
}
197250
}
198251

199252
class IWbemQualifierSet extends Unknown {
@@ -348,6 +401,31 @@ public IWbemServices(Pointer pvInstance) {
348401
super(pvInstance);
349402
}
350403

404+
public HRESULT ExecMethod(BSTR strObjectPath, BSTR strMethodName, int lFlags, IWbemContext pCtx,
405+
Pointer pInParams, PointerByReference ppOutParams, PointerByReference ppCallResult) {
406+
// ExecMethod is 25st method of IWbemServicesVtbl in WbemCli.h
407+
return (HRESULT) _invokeNativeObject(24,
408+
new Object[] { getPointer(), strObjectPath, strMethodName, lFlags, pCtx, pInParams, ppOutParams, ppCallResult }, HRESULT.class);
409+
}
410+
411+
public IWbemClassObject ExecMethod(String strObjectPath, String strMethodName, int lFlags, IWbemContext pCtx,
412+
IWbemClassObject inParams) {
413+
BSTR strObjectPathBSTR = OleAuto.INSTANCE.SysAllocString(strObjectPath);
414+
BSTR strMethodNameBSTR = OleAuto.INSTANCE.SysAllocString(strMethodName);
415+
try {
416+
PointerByReference ppOutParams = new PointerByReference();
417+
418+
HRESULT res = ExecMethod(strObjectPathBSTR, strMethodNameBSTR, lFlags, pCtx, inParams.getPointer(), ppOutParams, null);
419+
420+
COMUtils.checkRC(res);
421+
422+
return new IWbemClassObject(ppOutParams.getValue());
423+
} finally {
424+
OleAuto.INSTANCE.SysFreeString(strObjectPathBSTR);
425+
OleAuto.INSTANCE.SysFreeString(strMethodNameBSTR);
426+
}
427+
}
428+
351429
public HRESULT ExecQuery(BSTR strQueryLanguage, BSTR strQuery, int lFlags, IWbemContext pCtx,
352430
PointerByReference ppEnum) {
353431
// ExecQuery is 21st method of IWbemServicesVtbl in WbemCli.h

contrib/platform/test/com/sun/jna/platform/win32/COM/WbemcliTest.java

+38
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,44 @@ public void testIWbemContextSetValue() {
429429
assertEquals(currentPid, pVal.longValue());
430430
}
431431

432+
@Test
433+
public void testWmiObject() {
434+
Wbemcli.IWbemServices svc = null;
435+
Variant.VARIANT.ByReference pVal = new Variant.VARIANT.ByReference();
436+
437+
String className = "StdRegProv";
438+
String methodName = "GetStringValue";
439+
440+
IWbemClassObject regClass = null;
441+
IWbemClassObject method = null;
442+
IWbemClassObject instance = null;
443+
IWbemClassObject result = null;
444+
445+
try {
446+
svc = WbemcliUtil.connectServer("ROOT\\Default");
447+
448+
regClass = svc.GetObject(className, 0, null);
449+
method = regClass.GetMethod(methodName);
450+
instance = method.SpawnInstance();
451+
452+
instance.Put("sSubKeyName", "SOFTWARE\\Microsoft\\Windows\\CurrentVersion");
453+
instance.Put("sValueName", "CommonFilesDir");
454+
455+
result = svc.ExecMethod(className, methodName, 0, null, instance);
456+
457+
result.Get("sValue", 0, pVal, null, null);
458+
assertEquals(Variant.VT_BSTR, pVal.getVarType().intValue());
459+
assertTrue(!pVal.stringValue().isEmpty());
460+
OleAuto.INSTANCE.VariantClear(pVal);
461+
} finally {
462+
if (svc != null) svc.Release();
463+
if (result != null) result.Release();
464+
if (method != null) method.Release();
465+
if (instance != null) instance.Release();
466+
if (regClass != null) regClass.Release();
467+
}
468+
}
469+
432470
/**
433471
* Copy from WbemcliUtil#connectServer with American English selected as
434472
* locale.

0 commit comments

Comments
 (0)