Search in sources :

Example 16 with COMException

use of com.sun.jna.platform.win32.COM.COMException in project jna by java-native-access.

the class MSWord method SaveAs.

public void SaveAs(String FileName, LONG FileFormat) throws COMException {
    VARIANT vtFileName = new VARIANT(FileName);
    VARIANT vtFileFormat = new VARIANT(FileFormat);
    this.invokeNoReply("SaveAs", this.getActiveDocument(), vtFileName, vtFileFormat);
}
Also used : VARIANT(com.sun.jna.platform.win32.Variant.VARIANT)

Example 17 with COMException

use of com.sun.jna.platform.win32.COM.COMException in project jna by java-native-access.

the class COMBindingBaseObject method oleMethod.

protected HRESULT oleMethod(int nType, VARIANT.ByReference pvResult, IDispatch pDisp, String name, VARIANT[] pArgs) throws COMException {
    if (pDisp == null)
        throw new COMException("pDisp (IDispatch) parameter is null!");
    // variable declaration
    WString[] ptName = new WString[] { new WString(name) };
    DISPIDByReference pdispID = new DISPIDByReference();
    // Get DISPID for name passed...
    HRESULT hr = pDisp.GetIDsOfNames(new REFIID(Guid.IID_NULL), ptName, 1, LOCALE_USER_DEFAULT, pdispID);
    COMUtils.checkRC(hr);
    return this.oleMethod(nType, pvResult, pDisp, pdispID.getValue(), pArgs);
}
Also used : WString(com.sun.jna.WString) HRESULT(com.sun.jna.platform.win32.WinNT.HRESULT) DISPIDByReference(com.sun.jna.platform.win32.OaIdl.DISPIDByReference) REFIID(com.sun.jna.platform.win32.Guid.REFIID)

Example 18 with COMException

use of com.sun.jna.platform.win32.COM.COMException in project jna by java-native-access.

the class COMUtils method comIsInitialized.

/**
     * Check if COM was initialized correctly. The initialization status is not changed!
     *
     * <p>This is a debug function, not for normal usage!</p>
     * 
     * @return whether COM has been initialized
     */
public static boolean comIsInitialized() {
    WinNT.HRESULT hr = Ole32.INSTANCE.CoInitializeEx(Pointer.NULL, Ole32.COINIT_MULTITHREADED);
    if (hr.equals(W32Errors.S_OK)) {
        // User failed - uninitialize again and return false
        Ole32.INSTANCE.CoUninitialize();
        return false;
    } else if (hr.equals(W32Errors.S_FALSE)) {
        // OK Variant 1 - User initialized COM with same threading module as
        // in this check. According to MSDN CoUninitialize needs to be called
        // in this case.
        Ole32.INSTANCE.CoUninitialize();
        return true;
    } else if (hr.intValue() == W32Errors.RPC_E_CHANGED_MODE) {
        return true;
    }
    // If another result than the checked ones above happens handling is
    // delegated to the "normal" COM exception handling and a COMException
    // will be raised.
    COMUtils.checkRC(hr);
    // The return will not be met, as COMUtils#checkRC will raise an exception
    return false;
}
Also used : WinNT(com.sun.jna.platform.win32.WinNT) HRESULT(com.sun.jna.platform.win32.WinNT.HRESULT)

Example 19 with COMException

use of com.sun.jna.platform.win32.COM.COMException in project jna by java-native-access.

the class ObjectFactory method createObject.

/**
	 * Creates a new COM object (CoCreateInstance) for the given progId and
	 * returns a ProxyObject for the given interface.
	 */
public <T> T createObject(Class<T> comInterface) {
    assert COMUtils.comIsInitialized() : "COM not initialized";
    ComObject comObectAnnotation = comInterface.getAnnotation(ComObject.class);
    if (null == comObectAnnotation) {
        throw new COMException("createObject: Interface must define a value for either clsId or progId via the ComInterface annotation");
    }
    final GUID guid = this.discoverClsId(comObectAnnotation);
    final PointerByReference ptrDisp = new PointerByReference();
    WinNT.HRESULT hr = Ole32.INSTANCE.CoCreateInstance(guid, null, WTypes.CLSCTX_SERVER, IDispatch.IID_IDISPATCH, ptrDisp);
    COMUtils.checkRC(hr);
    Dispatch d = new Dispatch(ptrDisp.getValue());
    T t = this.createProxy(comInterface, d);
    //CoCreateInstance returns a pointer to COM object with a +1 reference count, so we must drop one
    //Note: the createProxy adds one
    int n = d.Release();
    return t;
}
Also used : COMException(com.sun.jna.platform.win32.COM.COMException) HRESULT(com.sun.jna.platform.win32.WinNT.HRESULT) WinNT(com.sun.jna.platform.win32.WinNT) PointerByReference(com.sun.jna.ptr.PointerByReference) ComObject(com.sun.jna.platform.win32.COM.util.annotation.ComObject) GUID(com.sun.jna.platform.win32.Guid.GUID) WinNT(com.sun.jna.platform.win32.WinNT) Dispatch(com.sun.jna.platform.win32.COM.Dispatch) IDispatch(com.sun.jna.platform.win32.COM.IDispatch) HRESULT(com.sun.jna.platform.win32.WinNT.HRESULT)

Example 20 with COMException

use of com.sun.jna.platform.win32.COM.COMException 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;
}
Also used : IntByReference(com.sun.jna.ptr.IntByReference) COMException(com.sun.jna.platform.win32.COM.COMException) HRESULT(com.sun.jna.platform.win32.WinNT.HRESULT) EXCEPINFO(com.sun.jna.platform.win32.OaIdl.EXCEPINFO) REFIID(com.sun.jna.platform.win32.Guid.REFIID) VARIANT(com.sun.jna.platform.win32.Variant.VARIANT) DWORDByReference(com.sun.jna.platform.win32.WinDef.DWORDByReference) DISPIDByReference(com.sun.jna.platform.win32.OaIdl.DISPIDByReference) PointerByReference(com.sun.jna.ptr.PointerByReference) IntByReference(com.sun.jna.ptr.IntByReference) ConnectionPoint(com.sun.jna.platform.win32.COM.ConnectionPoint) WinDef(com.sun.jna.platform.win32.WinDef) DISPPARAMS(com.sun.jna.platform.win32.OleAuto.DISPPARAMS)

Aggregations

COMException (com.sun.jna.platform.win32.COM.COMException)13 HRESULT (com.sun.jna.platform.win32.WinNT.HRESULT)13 REFIID (com.sun.jna.platform.win32.Guid.REFIID)10 VARIANT (com.sun.jna.platform.win32.Variant.VARIANT)8 PointerByReference (com.sun.jna.ptr.PointerByReference)7 WString (com.sun.jna.WString)5 ConnectionPoint (com.sun.jna.platform.win32.COM.ConnectionPoint)5 IID (com.sun.jna.platform.win32.Guid.IID)5 WinNT (com.sun.jna.platform.win32.WinNT)5 Dispatch (com.sun.jna.platform.win32.COM.Dispatch)4 IDispatch (com.sun.jna.platform.win32.COM.IDispatch)4 ComInterface (com.sun.jna.platform.win32.COM.util.annotation.ComInterface)4 DISPIDByReference (com.sun.jna.platform.win32.OaIdl.DISPIDByReference)4 InvocationTargetException (java.lang.reflect.InvocationTargetException)4 ExecutionException (java.util.concurrent.ExecutionException)4 TimeoutException (java.util.concurrent.TimeoutException)4 ComObject (com.sun.jna.platform.win32.COM.util.annotation.ComObject)3 GUID (com.sun.jna.platform.win32.Guid.GUID)2 EXCEPINFO (com.sun.jna.platform.win32.OaIdl.EXCEPINFO)2 DISPPARAMS (com.sun.jna.platform.win32.OleAuto.DISPPARAMS)2