use of com.sun.jna.platform.win32.COM.util.IDispatch in project jna by java-native-access.
the class COMBindingBaseObject method oleMethod.
protected HRESULT oleMethod(int nType, VARIANT.ByReference pvResult, IDispatch pDisp, DISPID dispId, VARIANT[] pArgs) throws COMException {
if (pDisp == null)
throw new COMException("pDisp (IDispatch) parameter is null!");
// variable declaration
int _argsLen = 0;
VARIANT[] _args = null;
DISPPARAMS.ByReference dp = new DISPPARAMS.ByReference();
EXCEPINFO.ByReference pExcepInfo = new EXCEPINFO.ByReference();
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 });
}
// Build DISPPARAMS
if (_argsLen > 0) {
dp.setArgs(_args);
// write 'DISPPARAMS' structure to memory
dp.write();
}
// 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;
}
// Make the call!
HRESULT hr = pDisp.Invoke(dispId, new REFIID(Guid.IID_NULL), LOCALE_SYSTEM_DEFAULT, new WinDef.WORD(finalNType), dp, pvResult, pExcepInfo, puArgErr);
COMUtils.checkRC(hr, pExcepInfo, puArgErr);
return hr;
}
use of com.sun.jna.platform.win32.COM.util.IDispatch in project jna by java-native-access.
the class ProxyObject method resolveDispId.
protected DISPID resolveDispId(final IDispatch pDisp, String name) {
assert COMUtils.comIsInitialized() : "COM not initialized";
if (pDisp == null)
throw new COMException("pDisp (IDispatch) parameter is null!");
// variable declaration
final WString[] ptName = new WString[] { new WString(name) };
final DISPIDByReference pdispID = new DISPIDByReference();
// Get DISPID for name passed...
HRESULT hr = pDisp.GetIDsOfNames(new REFIID(Guid.IID_NULL), ptName, 1, factory.getLCID(), pdispID);
COMUtils.checkRC(hr);
return pdispID.getValue();
}
use of com.sun.jna.platform.win32.COM.util.IDispatch in project jna by java-native-access.
the class RunningObjectTable method enumRunning.
@Override
public Iterable<IDispatch> enumRunning() {
assert COMUtils.comIsInitialized() : "COM not initialized";
final PointerByReference ppenumMoniker = new PointerByReference();
WinNT.HRESULT hr = this.raw.EnumRunning(ppenumMoniker);
COMUtils.checkRC(hr);
com.sun.jna.platform.win32.COM.EnumMoniker raw = new com.sun.jna.platform.win32.COM.EnumMoniker(ppenumMoniker.getValue());
return new EnumMoniker(raw, this.raw, this.factory);
}
use of com.sun.jna.platform.win32.COM.util.IDispatch in project jna by java-native-access.
the class RunningObjectTable method getActiveObjectsByInterface.
@Override
public <T> List<T> getActiveObjectsByInterface(Class<T> comInterface) {
assert COMUtils.comIsInitialized() : "COM not initialized";
List<T> result = new ArrayList<T>();
for (IDispatch obj : this.enumRunning()) {
try {
T dobj = obj.queryInterface(comInterface);
result.add(dobj);
} catch (COMException ex) {
}
}
return result;
}
use of com.sun.jna.platform.win32.COM.util.IDispatch in project jna by java-native-access.
the class CallbackProxy method invokeOnThread.
void invokeOnThread(final DISPID dispIdMember, final REFIID riid, LCID lcid, WORD wFlags, final DISPPARAMS.ByReference pDispParams) {
VARIANT[] arguments = pDispParams.getArgs();
final Method eventMethod = CallbackProxy.this.dsipIdMap.get(dispIdMember);
if (eventMethod == null) {
CallbackProxy.this.comEventCallbackListener.errorReceivingCallbackEvent("No method found with dispId = " + dispIdMember, null);
return;
}
/**
* DISPPARAMs provides two different ways to pass arguments.
*
* Arguments can be passed as a linear list with all arguments
* specified to a certain position (positional) or the position of
* an argument can be passed via the rgdispidNamedArgs array
* (named).
*
* pDispParams.rgvarg (length in pDispParams.cArgs) contains all
* arguments (named + position based)
*
* pDispParams.rgdispidNamedArgs (length in pDispParams.cNamedArgs)
* contains the named parameters as DISPIDs - the DISPIDs are the
* target index in the method signature (zero based).
*
* Each entry in pDispParams.rgvarg is either position based or name
* based and the position bases arguments are passed in reverse
* order, so getting this:
*
* rgvarg = ["arg1", "arg2", "arg3", "arg4", "arg5"]
* rgdispidNamedArgs = [3, 4]
*
* Would lead to this paramater array in the handler:
*
* ["arg5", "arg4", "arg3", "arg1", "arg2"]
*
* See also:
* https://msdn.microsoft.com/de-de/library/windows/desktop/ms221653%28v=vs.85%29.aspx
*/
// Arguments are converted to the JAVA side and IDispatch Interfaces
// are wrapped into an ProxyObject if so requested.
//
// Out-Parameter need to be specified as VARIANT, VARIANT args are
// not converted, so COM memory allocation rules apply.
DISPID[] positionMap = pDispParams.getRgdispidNamedArgs();
final Class<?>[] paramTypes = eventMethod.getParameterTypes();
final Object[] params = new Object[paramTypes.length];
// Handle position based parameters first
for (int i = 0; i < params.length && (arguments.length - positionMap.length - i) > 0; i++) {
Class targetClass = paramTypes[i];
Variant.VARIANT varg = arguments[arguments.length - i - 1];
params[i] = Convert.toJavaObject(varg, targetClass, factory, true, false);
}
for (int i = 0; i < positionMap.length; i++) {
int targetPosition = positionMap[i].intValue();
if (targetPosition >= params.length) {
// If less parameters are mapped then supplied, ignore
continue;
}
Class targetClass = paramTypes[targetPosition];
Variant.VARIANT varg = arguments[i];
params[targetPosition] = Convert.toJavaObject(varg, targetClass, factory, true, false);
}
// exception occurs while doing the call into the target method
for (int i = 0; i < params.length; i++) {
if (params[i] == null && paramTypes[i].isPrimitive()) {
if (paramTypes[i].equals(boolean.class)) {
params[i] = DEFAULT_BOOLEAN;
} else if (paramTypes[i].equals(byte.class)) {
params[i] = DEFAULT_BYTE;
} else if (paramTypes[i].equals(short.class)) {
params[i] = DEFAULT_SHORT;
} else if (paramTypes[i].equals(int.class)) {
params[i] = DEFAULT_INT;
} else if (paramTypes[i].equals(long.class)) {
params[i] = DEFAULT_LONG;
} else if (paramTypes[i].equals(float.class)) {
params[i] = DEFAULT_FLOAT;
} else if (paramTypes[i].equals(double.class)) {
params[i] = DEFAULT_DOUBLE;
} else {
throw new IllegalArgumentException("Class type " + paramTypes[i].getName() + " not mapped to primitive default value.");
}
}
}
try {
eventMethod.invoke(comEventCallbackListener, params);
} catch (Exception e) {
List<String> decodedClassNames = new ArrayList<String>(params.length);
for (Object o : params) {
if (o == null) {
decodedClassNames.add("NULL");
} else {
decodedClassNames.add(o.getClass().getName());
}
}
CallbackProxy.this.comEventCallbackListener.errorReceivingCallbackEvent("Exception invoking method " + eventMethod + " supplied: " + decodedClassNames.toString(), e);
}
}
Aggregations