Skip to content

Commit

Permalink
Merge pull request #1534 from faddom/master
Browse files Browse the repository at this point in the history
Added GetMethod, Put, SpawnInstance, and ExecMethod functions
  • Loading branch information
matthiasblaesing committed Aug 6, 2023
2 parents e96f301 + 0e2386e commit c32053e
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Expand Up @@ -7,6 +7,7 @@ Next Release (5.14.0)

Features
--------
* [#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).

Bug Fixes
---------
Expand Down
78 changes: 78 additions & 0 deletions contrib/platform/src/com/sun/jna/platform/win32/COM/Wbemcli.java
Expand Up @@ -137,6 +137,23 @@ public HRESULT Get(String wszName, int lFlags, VARIANT.ByReference pVal, IntByRe
return Get(wszName == null ? null : new WString(wszName), lFlags, pVal, pType, plFlavor);
}

public HRESULT GetMethod(String wszName, int lFlags, PointerByReference ppInSignature, PointerByReference ppOutSignature) {
return GetMethod(wszName == null ? null : new WString(wszName), lFlags, ppInSignature, ppOutSignature);
}

public HRESULT GetMethod(WString wszName, int lFlags, PointerByReference ppInSignature, PointerByReference ppOutSignature) {
// 20th method in IWbemClassObjectVtbl
return (HRESULT) _invokeNativeObject(19,
new Object[]{ getPointer(), wszName, lFlags, ppInSignature, ppOutSignature}, HRESULT.class);
}

public IWbemClassObject GetMethod(String wszName) {
PointerByReference ppInSignature = new PointerByReference();
HRESULT res = GetMethod(wszName, 0, ppInSignature, null);
COMUtils.checkRC(res);
return new IWbemClassObject(ppInSignature.getValue());
}

public HRESULT GetNames(String wszQualifierName, int lFlags, VARIANT.ByReference pQualifierVal, PointerByReference pNames) {
return GetNames(wszQualifierName == null ? null : new WString(wszQualifierName), lFlags, pQualifierVal, pNames);
}
Expand Down Expand Up @@ -194,6 +211,42 @@ public IWbemQualifierSet GetPropertyQualifierSet(String strProperty) {
IWbemQualifierSet qualifier = new IWbemQualifierSet(ppQualSet.getValue());
return qualifier;
}

public HRESULT Put(String wszName, int lFlags, Variant.VARIANT pVal, int Type) {
return Put(wszName == null ? null : new WString(wszName), lFlags, pVal, Type);
}

public HRESULT Put(WString wszName, int lFlags, Variant.VARIANT pVal, int Type) {
// 6th method in IWbemClassObjectVtbl
return (HRESULT) _invokeNativeObject(5,
new Object[]{ getPointer(), wszName, lFlags, pVal, Type}, HRESULT.class);
}

public void Put(String wszName, String pValue) {
Variant.VARIANT aVariant = new Variant.VARIANT();
BSTR strValue = OleAuto.INSTANCE.SysAllocString(pValue);
try {
aVariant.setValue(Variant.VT_BSTR, strValue);
HRESULT res = Put(wszName, 0, aVariant, 0);
COMUtils.checkRC(res);
}
finally {
OleAuto.INSTANCE.VariantClear(aVariant);
}
}

public HRESULT SpawnInstance(int lFlags, PointerByReference ppNewInstance) {
// 16th method in IWbemClassObjectVtbl
return (HRESULT) _invokeNativeObject(15,
new Object[]{ getPointer(), lFlags, ppNewInstance}, HRESULT.class);
}

public IWbemClassObject SpawnInstance() {
PointerByReference ppNewInstance = new PointerByReference();
HRESULT res = SpawnInstance(0, ppNewInstance);
COMUtils.checkRC(res);
return new IWbemClassObject(ppNewInstance.getValue());
}
}

class IWbemQualifierSet extends Unknown {
Expand Down Expand Up @@ -348,6 +401,31 @@ public IWbemServices(Pointer pvInstance) {
super(pvInstance);
}

public HRESULT ExecMethod(BSTR strObjectPath, BSTR strMethodName, int lFlags, IWbemContext pCtx,
Pointer pInParams, PointerByReference ppOutParams, PointerByReference ppCallResult) {
// ExecMethod is 25st method of IWbemServicesVtbl in WbemCli.h
return (HRESULT) _invokeNativeObject(24,
new Object[] { getPointer(), strObjectPath, strMethodName, lFlags, pCtx, pInParams, ppOutParams, ppCallResult }, HRESULT.class);
}

public IWbemClassObject ExecMethod(String strObjectPath, String strMethodName, int lFlags, IWbemContext pCtx,
IWbemClassObject inParams) {
BSTR strObjectPathBSTR = OleAuto.INSTANCE.SysAllocString(strObjectPath);
BSTR strMethodNameBSTR = OleAuto.INSTANCE.SysAllocString(strMethodName);
try {
PointerByReference ppOutParams = new PointerByReference();

HRESULT res = ExecMethod(strObjectPathBSTR, strMethodNameBSTR, lFlags, pCtx, inParams.getPointer(), ppOutParams, null);

COMUtils.checkRC(res);

return new IWbemClassObject(ppOutParams.getValue());
} finally {
OleAuto.INSTANCE.SysFreeString(strObjectPathBSTR);
OleAuto.INSTANCE.SysFreeString(strMethodNameBSTR);
}
}

public HRESULT ExecQuery(BSTR strQueryLanguage, BSTR strQuery, int lFlags, IWbemContext pCtx,
PointerByReference ppEnum) {
// ExecQuery is 21st method of IWbemServicesVtbl in WbemCli.h
Expand Down
Expand Up @@ -429,6 +429,44 @@ public void testIWbemContextSetValue() {
assertEquals(currentPid, pVal.longValue());
}

@Test
public void testWmiObject() {
Wbemcli.IWbemServices svc = null;
Variant.VARIANT.ByReference pVal = new Variant.VARIANT.ByReference();

String className = "StdRegProv";
String methodName = "GetStringValue";

IWbemClassObject regClass = null;
IWbemClassObject method = null;
IWbemClassObject instance = null;
IWbemClassObject result = null;

try {
svc = WbemcliUtil.connectServer("ROOT\\Default");

regClass = svc.GetObject(className, 0, null);
method = regClass.GetMethod(methodName);
instance = method.SpawnInstance();

instance.Put("sSubKeyName", "SOFTWARE\\Microsoft\\Windows\\CurrentVersion");
instance.Put("sValueName", "CommonFilesDir");

result = svc.ExecMethod(className, methodName, 0, null, instance);

result.Get("sValue", 0, pVal, null, null);
assertEquals(Variant.VT_BSTR, pVal.getVarType().intValue());
assertTrue(!pVal.stringValue().isEmpty());
OleAuto.INSTANCE.VariantClear(pVal);
} finally {
if (svc != null) svc.Release();
if (result != null) result.Release();
if (method != null) method.Release();
if (instance != null) instance.Release();
if (regClass != null) regClass.Release();
}
}

/**
* Copy from WbemcliUtil#connectServer with American English selected as
* locale.
Expand Down

0 comments on commit c32053e

Please sign in to comment.