Search in sources :

Example 41 with HRESULT

use of com.sun.jna.platform.win32.WinNT.HRESULT in project jna by java-native-access.

the class HybdridCOMInvocationTest method testOfficeInvocationDemonstration.

public void testOfficeInvocationDemonstration() {
    // THIS IS NOT A TEST
    //
    // This reproduces the problem by using the dispatch directly.
    PointerByReference pDispatch = new PointerByReference();
    HRESULT hr = Ole32.INSTANCE.CoCreateInstance(CLSID_WORD, null, WTypes.CLSCTX_SERVER, IID_APPLICATION, pDispatch);
    if (!COMUtils.SUCCEEDED(hr)) {
        LOG.log(Level.INFO, "HybdridCOMInvocationTest test was not run, MS Word object could not be instantiated.");
        return;
    }
    Dispatch dp = new Dispatch(pDispatch.getValue());
    // DispID of InchesToPoints
    DISPID dispId = new OaIdl.DISPID(0x00000172);
    // Interface _Application of MS Word type library
    WinDef.LCID LOCALE_SYSTEM_DEFAULT = Kernel32.INSTANCE.GetSystemDefaultLCID();
    Variant.VARIANT.ByReference result = new Variant.VARIANT.ByReference();
    OaIdl.EXCEPINFO.ByReference pExcepInfo = new OaIdl.EXCEPINFO.ByReference();
    IntByReference puArgErr = new IntByReference();
    WORD wFlagsMethod = new WinDef.WORD(OleAuto.DISPATCH_METHOD);
    WORD wFlagsGet = new WinDef.WORD(OleAuto.DISPATCH_PROPERTYGET);
    WORD wFlagsCombined = new WinDef.WORD(OleAuto.DISPATCH_METHOD | OleAuto.DISPATCH_PROPERTYGET);
    OleAuto.DISPPARAMS.ByReference pDispParams = new OleAuto.DISPPARAMS.ByReference();
    VARIANT[] params = new VARIANT[] { new VARIANT(1f) };
    pDispParams.setArgs(params);
    // Call InchesToPoints as a method
    hr = dp.Invoke(dispId, new REFIID(Guid.IID_NULL), LOCALE_SYSTEM_DEFAULT, wFlagsMethod, pDispParams, result, pExcepInfo, puArgErr);
    assertTrue(COMUtils.FAILED(hr));
    // Call InchesToPoints as a property getter
    hr = dp.Invoke(dispId, new REFIID(Guid.IID_NULL), LOCALE_SYSTEM_DEFAULT, wFlagsGet, pDispParams, result, pExcepInfo, puArgErr);
    assertTrue(COMUtils.FAILED(hr));
    // Call InchesToPoints as a hybrid
    hr = dp.Invoke(dispId, new REFIID(Guid.IID_NULL), LOCALE_SYSTEM_DEFAULT, wFlagsCombined, pDispParams, result, pExcepInfo, puArgErr);
    assertTrue(COMUtils.SUCCEEDED(hr));
    assertEquals(72.0f, result.floatValue());
}
Also used : IntByReference(com.sun.jna.ptr.IntByReference) WORD(com.sun.jna.platform.win32.WinDef.WORD) HRESULT(com.sun.jna.platform.win32.WinNT.HRESULT) DISPID(com.sun.jna.platform.win32.OaIdl.DISPID) Dispatch(com.sun.jna.platform.win32.COM.Dispatch) REFIID(com.sun.jna.platform.win32.Guid.REFIID) PointerByReference(com.sun.jna.ptr.PointerByReference) IntByReference(com.sun.jna.ptr.IntByReference) VARIANT(com.sun.jna.platform.win32.Variant.VARIANT) Variant(com.sun.jna.platform.win32.Variant) OleAuto(com.sun.jna.platform.win32.OleAuto) PointerByReference(com.sun.jna.ptr.PointerByReference) WinDef(com.sun.jna.platform.win32.WinDef) OaIdl(com.sun.jna.platform.win32.OaIdl)

Example 42 with HRESULT

use of com.sun.jna.platform.win32.WinNT.HRESULT in project jna by java-native-access.

the class Kernel32UtilTest method testFreeGlobalMemory.

public void testFreeGlobalMemory() {
    try {
        Pointer ptr = new Pointer(0xFFFFFFFFFFFFFFFFL);
        Kernel32Util.freeGlobalMemory(ptr);
        fail("Unexpected success to free bad global memory");
    } catch (Win32Exception e) {
        HRESULT hr = e.getHR();
        int code = W32Errors.HRESULT_CODE(hr.intValue());
        assertEquals("Mismatched failure reason code", WinError.ERROR_INVALID_HANDLE, code);
    }
}
Also used : HRESULT(com.sun.jna.platform.win32.WinNT.HRESULT) Pointer(com.sun.jna.Pointer)

Example 43 with HRESULT

use of com.sun.jna.platform.win32.WinNT.HRESULT in project jna by java-native-access.

the class Kernel32UtilTest method testFreeLocalMemory.

public void testFreeLocalMemory() {
    try {
        Pointer ptr = new Pointer(0xFFFFFFFFFFFFFFFFL);
        Kernel32Util.freeLocalMemory(ptr);
        fail("Unexpected success to free bad local memory");
    } catch (Win32Exception e) {
        HRESULT hr = e.getHR();
        int code = W32Errors.HRESULT_CODE(hr.intValue());
        assertEquals("Mismatched failure reason code", WinError.ERROR_INVALID_HANDLE, code);
    }
}
Also used : HRESULT(com.sun.jna.platform.win32.WinNT.HRESULT) Pointer(com.sun.jna.Pointer)

Example 44 with HRESULT

use of com.sun.jna.platform.win32.WinNT.HRESULT in project jna by java-native-access.

the class ProxyObject method advise.

public IComEventCallbackCookie advise(Class<?> comEventCallbackInterface, final IComEventCallbackListener comEventCallbackListener) {
    assert COMUtils.comIsInitialized() : "COM not initialized";
    try {
        ComInterface comInterfaceAnnotation = comEventCallbackInterface.getAnnotation(ComInterface.class);
        if (null == comInterfaceAnnotation) {
            throw new COMException("advise: Interface must define a value for either iid via the ComInterface annotation");
        }
        final IID iid = this.getIID(comInterfaceAnnotation);
        final ConnectionPoint rawCp = this.fetchRawConnectionPoint(iid);
        // create the dispatch listener
        final IDispatchCallback rawListener = factory.createDispatchCallback(comEventCallbackInterface, comEventCallbackListener);
        // store it the comEventCallback argument, so it is not garbage
        // collected.
        comEventCallbackListener.setDispatchCallbackListener(rawListener);
        // set the dispatch listener to listen to events from the connection
        // point
        final DWORDByReference pdwCookie = new DWORDByReference();
        HRESULT hr = rawCp.Advise(rawListener, pdwCookie);
        // release before check in case check
        int n = rawCp.Release();
        // throws exception
        COMUtils.checkRC(hr);
        // return the cookie so that a call to stop listening can be made
        return new ComEventCallbackCookie(pdwCookie.getValue());
    } catch (Exception e) {
        throw new COMException("Error occured in advise when trying to connect the listener " + comEventCallbackListener, e);
    }
}
Also used : COMException(com.sun.jna.platform.win32.COM.COMException) HRESULT(com.sun.jna.platform.win32.WinNT.HRESULT) IID(com.sun.jna.platform.win32.Guid.IID) REFIID(com.sun.jna.platform.win32.Guid.REFIID) IDispatchCallback(com.sun.jna.platform.win32.COM.IDispatchCallback) DWORDByReference(com.sun.jna.platform.win32.WinDef.DWORDByReference) ComInterface(com.sun.jna.platform.win32.COM.util.annotation.ComInterface) ConnectionPoint(com.sun.jna.platform.win32.COM.ConnectionPoint) ConnectionPoint(com.sun.jna.platform.win32.COM.ConnectionPoint) TimeoutException(java.util.concurrent.TimeoutException) COMException(com.sun.jna.platform.win32.COM.COMException) InvocationTargetException(java.lang.reflect.InvocationTargetException) ExecutionException(java.util.concurrent.ExecutionException)

Example 45 with HRESULT

use of com.sun.jna.platform.win32.WinNT.HRESULT in project jna by java-native-access.

the class ProxyObject method unadvise.

public void unadvise(Class<?> comEventCallbackInterface, final IComEventCallbackCookie cookie) {
    assert COMUtils.comIsInitialized() : "COM not initialized";
    try {
        ComInterface comInterfaceAnnotation = comEventCallbackInterface.getAnnotation(ComInterface.class);
        if (null == comInterfaceAnnotation) {
            throw new COMException("unadvise: Interface must define a value for iid via the ComInterface annotation");
        }
        IID iid = this.getIID(comInterfaceAnnotation);
        final ConnectionPoint rawCp = this.fetchRawConnectionPoint(iid);
        HRESULT hr = rawCp.Unadvise(((ComEventCallbackCookie) cookie).getValue());
        rawCp.Release();
        COMUtils.checkRC(hr);
    } catch (Exception e) {
        throw new COMException("Error occured in unadvise when trying to disconnect the listener from " + this, e);
    }
}
Also used : COMException(com.sun.jna.platform.win32.COM.COMException) HRESULT(com.sun.jna.platform.win32.WinNT.HRESULT) IID(com.sun.jna.platform.win32.Guid.IID) REFIID(com.sun.jna.platform.win32.Guid.REFIID) ComInterface(com.sun.jna.platform.win32.COM.util.annotation.ComInterface) ConnectionPoint(com.sun.jna.platform.win32.COM.ConnectionPoint) TimeoutException(java.util.concurrent.TimeoutException) COMException(com.sun.jna.platform.win32.COM.COMException) InvocationTargetException(java.lang.reflect.InvocationTargetException) ExecutionException(java.util.concurrent.ExecutionException)

Aggregations

HRESULT (com.sun.jna.platform.win32.WinNT.HRESULT)102 PointerByReference (com.sun.jna.ptr.PointerByReference)57 Test (org.junit.Test)26 REFIID (com.sun.jna.platform.win32.Guid.REFIID)20 UINT (com.sun.jna.platform.win32.WinDef.UINT)15 WString (com.sun.jna.WString)12 DWORD (com.sun.jna.platform.win32.WinDef.DWORD)12 WinNT (com.sun.jna.platform.win32.WinNT)12 DWORDByReference (com.sun.jna.platform.win32.WinDef.DWORDByReference)11 VARIANT (com.sun.jna.platform.win32.Variant.VARIANT)10 MEMBERID (com.sun.jna.platform.win32.OaIdl.MEMBERID)9 BSTRByReference (com.sun.jna.platform.win32.WTypes.BSTRByReference)9 ULONG (com.sun.jna.platform.win32.WinDef.ULONG)9 IID (com.sun.jna.platform.win32.Guid.IID)8 DISPIDByReference (com.sun.jna.platform.win32.OaIdl.DISPIDByReference)8 IntByReference (com.sun.jna.ptr.IntByReference)8 UINTByReference (com.sun.jna.platform.win32.WinDef.UINTByReference)7 COMException (com.sun.jna.platform.win32.COM.COMException)6 ConnectionPoint (com.sun.jna.platform.win32.COM.ConnectionPoint)6 Pointer (com.sun.jna.Pointer)5