use of com.sun.jna.platform.win32.COM.IDispatch in project jna by java-native-access.
the class ProxyObject method oleMethod.
/*
* @see com.sun.jna.platform.win32.COM.COMBindingBaseObject#oleMethod
*/
protected HRESULT oleMethod(final int nType, final VARIANT.ByReference pvResult, final IDispatch pDisp, final DISPID dispId, VARIANT[] pArgs) throws COMException {
assert COMUtils.comIsInitialized() : "COM not initialized";
if (pDisp == null)
throw new COMException("pDisp (IDispatch) parameter is null!");
// variable declaration
int _argsLen = 0;
VARIANT[] _args = null;
final DISPPARAMS.ByReference dp = new DISPPARAMS.ByReference();
final EXCEPINFO.ByReference pExcepInfo = new EXCEPINFO.ByReference();
final IntByReference puArgErr = new IntByReference();
// make parameter reverse ordering as expected by COM runtime
if ((pArgs != null) && (pArgs.length > 0)) {
_argsLen = pArgs.length;
_args = new VARIANT[_argsLen];
int revCount = _argsLen;
for (int i = 0; i < _argsLen; i++) {
_args[i] = pArgs[--revCount];
}
}
// Handle special-case for property-puts!
if (nType == OleAuto.DISPATCH_PROPERTYPUT) {
dp.setRgdispidNamedArgs(new DISPID[] { OaIdl.DISPID_PROPERTYPUT });
}
// Apply "fix" according to
// https://www.delphitools.info/2013/04/30/gaining-visual-basic-ole-super-powers/
// https://msdn.microsoft.com/en-us/library/windows/desktop/ms221486(v=vs.85).aspx
//
// Summary: there are methods in the word typelibrary that require both
// PROPERTYGET _and_ METHOD to be set. With only one of these set the call
// fails.
//
// The article from delphitools argues, that automation compatible libraries
// need to be compatible with VisualBasic which does not distingish methods
// and property getters and will set both flags always.
//
// The MSDN article advises this behaviour: "[...] Some languages cannot
// distinguish between retrieving a property and calling a method. In this
//case, you should set the flags DISPATCH_PROPERTYGET and DISPATCH_METHOD.
// [...]"))
//
// This was found when trying to bind InchesToPoints from the _Application
// dispatch interface of the MS Word 15 type library
//
// The signature according the ITypeLib Viewer (OLE/COM Object Viewer):
// [id(0x00000172), helpcontext(0x09700172)]
// single InchesToPoints([in] single Inches);
final int finalNType;
if (nType == OleAuto.DISPATCH_METHOD || nType == OleAuto.DISPATCH_PROPERTYGET) {
finalNType = OleAuto.DISPATCH_METHOD | OleAuto.DISPATCH_PROPERTYGET;
} else {
finalNType = nType;
}
// Build DISPPARAMS
if (_argsLen > 0) {
dp.setArgs(_args);
// write 'DISPPARAMS' structure to memory
dp.write();
}
HRESULT hr = pDisp.Invoke(dispId, new REFIID(Guid.IID_NULL), factory.getLCID(), new WinDef.WORD(finalNType), dp, pvResult, pExcepInfo, puArgErr);
COMUtils.checkRC(hr, pExcepInfo, puArgErr);
return hr;
}
use of com.sun.jna.platform.win32.COM.IDispatch in project jna by java-native-access.
the class ProxyObject method setProperty.
// --------------------- IDispatch ------------------------------
@Override
public <T> void setProperty(String name, T value) {
DISPID dispID = resolveDispId(this.getRawDispatch(), name);
setProperty(dispID, value);
}
use of com.sun.jna.platform.win32.COM.IDispatch in project jna by java-native-access.
the class VariantTest method testVariantConstructors.
public void testVariantConstructors() {
VARIANT variant;
// skipped: BSTRByReference constructor
// skipped: empty constructor
// skipped: pointer constructor
// skipped: IDispatch constructor
String testString = "TeST$รถ";
BSTR bstr = OleAuto.INSTANCE.SysAllocString(testString);
variant = new VARIANT(bstr);
assertThat(variant.getValue(), instanceOf(BSTR.class));
assertThat(((BSTR) variant.getValue()).getValue(), equalTo(testString));
assertThat(variant.stringValue(), equalTo(testString));
variant = new VARIANT(testString);
assertThat(variant.getValue(), instanceOf(BSTR.class));
assertThat(((BSTR) variant.getValue()).getValue(), equalTo(testString));
assertThat(variant.stringValue(), equalTo(testString));
OleAuto.INSTANCE.SysFreeString(bstr);
OleAuto.INSTANCE.SysFreeString((BSTR) variant.getValue());
BOOL boolTrue = new WinDef.BOOL(true);
variant = new VARIANT(Variant.VARIANT_TRUE);
assertThat(variant.getValue(), instanceOf(VARIANT_BOOL.class));
assertThat(((VARIANT_BOOL) variant.getValue()).shortValue(), equalTo((short) 0xFFFF));
assertThat(variant.booleanValue(), equalTo(true));
variant = new VARIANT(boolTrue);
assertThat(variant.getValue(), instanceOf(VARIANT_BOOL.class));
assertThat(((VARIANT_BOOL) variant.getValue()).shortValue(), equalTo((short) 0xFFFF));
assertThat(variant.booleanValue(), equalTo(true));
int testInt = 4223;
LONG testIntWin = new LONG(testInt);
variant = new VARIANT(testIntWin);
assertThat(variant.getValue(), instanceOf(LONG.class));
assertThat(((LONG) variant.getValue()).intValue(), equalTo(testInt));
assertThat(variant.intValue(), equalTo(testInt));
variant = new VARIANT(testInt);
assertThat(variant.getValue(), instanceOf(LONG.class));
assertThat(((LONG) variant.getValue()).intValue(), equalTo(testInt));
assertThat(variant.intValue(), equalTo(testInt));
short testShort = 23;
SHORT testShortWin = new SHORT(testShort);
variant = new VARIANT(testShortWin);
assertThat(variant.getValue(), instanceOf(SHORT.class));
assertThat(((SHORT) variant.getValue()).shortValue(), equalTo(testShort));
assertThat(variant.shortValue(), equalTo(testShort));
variant = new VARIANT(testShort);
assertThat(variant.getValue(), instanceOf(SHORT.class));
assertThat(((SHORT) variant.getValue()).shortValue(), equalTo(testShort));
assertThat(variant.shortValue(), equalTo(testShort));
long testLong = 4223L + Integer.MAX_VALUE;
variant = new VARIANT(testLong);
assertThat(variant.getValue(), instanceOf(LONGLONG.class));
assertThat(((LONGLONG) variant.getValue()).longValue(), equalTo(testLong));
assertThat(variant.longValue(), equalTo(testLong));
Date testDate = new Date(2042 - 1900, 2, 3, 23, 0, 0);
variant = new VARIANT(testDate);
assertThat(variant.getValue(), instanceOf(DATE.class));
assertThat(variant.dateValue(), equalTo(testDate));
byte testByte = 42;
BYTE testByteWin = new BYTE(testByte);
CHAR testByteWin2 = new CHAR(testByte);
variant = new VARIANT(testByte);
assertThat(variant.getValue(), instanceOf(BYTE.class));
assertThat(((BYTE) variant.getValue()).byteValue(), equalTo(testByte));
assertThat(variant.byteValue(), equalTo(testByte));
variant = new VARIANT(testByteWin);
assertThat(variant.getValue(), instanceOf(BYTE.class));
assertThat(((BYTE) variant.getValue()).byteValue(), equalTo(testByte));
assertThat(variant.byteValue(), equalTo(testByte));
variant = new VARIANT(testByteWin2);
assertThat(variant.getValue(), instanceOf(CHAR.class));
assertThat(((CHAR) variant.getValue()).byteValue(), equalTo(testByte));
assertThat(variant.byteValue(), equalTo(testByte));
variant = new VARIANT(testByteWin2);
assertThat(variant.getValue(), instanceOf(CHAR.class));
assertThat(((CHAR) variant.getValue()).byteValue(), equalTo(testByte));
assertThat(variant.byteValue(), equalTo(testByte));
double testDouble = 42.23;
variant = new VARIANT(testDouble);
assertThat(variant.getValue(), instanceOf(Double.class));
// If this fails introduce comparison with range
assertThat(variant.doubleValue(), equalTo(testDouble));
float testFloat = 42.23f;
variant = new VARIANT(testFloat);
assertThat(variant.getValue(), instanceOf(Float.class));
// If this fails introduce comparison with range
assertThat(variant.floatValue(), equalTo(testFloat));
char testChar = 42 + Short.MAX_VALUE;
variant = new VARIANT(testChar);
assertThat(variant.getValue(), instanceOf(USHORT.class));
assertThat(((USHORT) variant.getValue()).intValue(), equalTo((int) testChar));
assertThat(variant.intValue(), equalTo((int) testChar));
}
Aggregations