Search in sources :

Example 91 with PointerByReference

use of com.sun.jna.ptr.PointerByReference in project jna by java-native-access.

the class ProxyObject method queryInterface.

@Override
public <T> T queryInterface(Class<T> comInterface) throws COMException {
    assert COMUtils.comIsInitialized() : "COM not initialized";
    try {
        ComInterface comInterfaceAnnotation = comInterface.getAnnotation(ComInterface.class);
        if (null == comInterfaceAnnotation) {
            throw new COMException("queryInterface: Interface must define a value for iid via the ComInterface annotation");
        }
        final IID iid = this.getIID(comInterfaceAnnotation);
        final PointerByReference ppvObject = new PointerByReference();
        HRESULT hr = ProxyObject.this.getRawDispatch().QueryInterface(new REFIID(iid), ppvObject);
        if (WinNT.S_OK.equals(hr)) {
            Dispatch dispatch = new Dispatch(ppvObject.getValue());
            T t = this.factory.createProxy(comInterface, dispatch);
            // QueryInterface returns a COM object pointer with a +1
            // reference, we must drop one,
            // Note: createProxy adds one;
            int n = dispatch.Release();
            return t;
        } else {
            String formatMessageFromHR = Kernel32Util.formatMessage(hr);
            throw new COMException("queryInterface: " + formatMessageFromHR);
        }
    } catch (Exception e) {
        throw new COMException("Error occured when trying to query for interface " + comInterface.getName(), e);
    }
}
Also used : COMException(com.sun.jna.platform.win32.COM.COMException) HRESULT(com.sun.jna.platform.win32.WinNT.HRESULT) HRESULT(com.sun.jna.platform.win32.WinNT.HRESULT) WinNT(com.sun.jna.platform.win32.WinNT) VARIANT(com.sun.jna.platform.win32.Variant.VARIANT) IID(com.sun.jna.platform.win32.Guid.IID) REFIID(com.sun.jna.platform.win32.Guid.REFIID) PointerByReference(com.sun.jna.ptr.PointerByReference) Dispatch(com.sun.jna.platform.win32.COM.Dispatch) IDispatch(com.sun.jna.platform.win32.COM.IDispatch) REFIID(com.sun.jna.platform.win32.Guid.REFIID) WString(com.sun.jna.WString) 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)

Example 92 with PointerByReference

use of com.sun.jna.ptr.PointerByReference in project jna by java-native-access.

the class Crypt32Util method cryptUnprotectData.

/**
     * Unprotect a blob of data.
     * @param data
     *  Data to unprotect.
     * @param entropy
     *  Optional entropy.
     * @param flags
     *  Optional flags.
     * @param prompt
     *  Optional prompt structure.
     * @return
     *  Unprotected blob of data.
     */
public static byte[] cryptUnprotectData(byte[] data, byte[] entropy, int flags, CRYPTPROTECT_PROMPTSTRUCT prompt) {
    DATA_BLOB pDataIn = new DATA_BLOB(data);
    DATA_BLOB pDataUnprotected = new DATA_BLOB();
    DATA_BLOB pEntropy = (entropy == null) ? null : new DATA_BLOB(entropy);
    PointerByReference pDescription = new PointerByReference();
    Win32Exception err = null;
    byte[] unProtectedData = null;
    try {
        if (!Crypt32.INSTANCE.CryptUnprotectData(pDataIn, pDescription, pEntropy, null, prompt, flags, pDataUnprotected)) {
            err = new Win32Exception(Kernel32.INSTANCE.GetLastError());
        } else {
            unProtectedData = pDataUnprotected.getData();
        }
    } finally {
        if (pDataUnprotected.pbData != null) {
            try {
                Kernel32Util.freeLocalMemory(pDataUnprotected.pbData);
            } catch (Win32Exception e) {
                if (err == null) {
                    err = e;
                } else {
                    err.addSuppressed(e);
                }
            }
        }
        if (pDescription.getValue() != null) {
            try {
                Kernel32Util.freeLocalMemory(pDescription.getValue());
            } catch (Win32Exception e) {
                if (err == null) {
                    err = e;
                } else {
                    err.addSuppressed(e);
                }
            }
        }
    }
    if (err != null) {
        throw err;
    }
    return unProtectedData;
}
Also used : PointerByReference(com.sun.jna.ptr.PointerByReference) DATA_BLOB(com.sun.jna.platform.win32.WinCrypt.DATA_BLOB)

Example 93 with PointerByReference

use of com.sun.jna.ptr.PointerByReference in project jna by java-native-access.

the class TypeInfoUtil method CreateInstance.

/**
     * Creates the instance.
     * 
     * @param pUnkOuter
     *            the unk outer
     * @param riid
     *            the riid
     * @return the pointer by reference
     */
public PointerByReference CreateInstance(IUnknown pUnkOuter, REFIID riid) {
    PointerByReference ppvObj = new PointerByReference();
    HRESULT hr = this.typeInfo.CreateInstance(pUnkOuter, riid, ppvObj);
    COMUtils.checkRC(hr);
    return ppvObj;
}
Also used : HRESULT(com.sun.jna.platform.win32.WinNT.HRESULT) PointerByReference(com.sun.jna.ptr.PointerByReference)

Example 94 with PointerByReference

use of com.sun.jna.ptr.PointerByReference in project jna by java-native-access.

the class TypeInfoUtil method getTypeAttr.

/**
     * Gets the type attr.
     * 
     * @return the type attr
     */
public TYPEATTR getTypeAttr() {
    PointerByReference ppTypeAttr = new PointerByReference();
    HRESULT hr = this.typeInfo.GetTypeAttr(ppTypeAttr);
    COMUtils.checkRC(hr);
    return new TYPEATTR(ppTypeAttr.getValue());
}
Also used : HRESULT(com.sun.jna.platform.win32.WinNT.HRESULT) PointerByReference(com.sun.jna.ptr.PointerByReference) TYPEATTR(com.sun.jna.platform.win32.OaIdl.TYPEATTR)

Example 95 with PointerByReference

use of com.sun.jna.ptr.PointerByReference in project jna by java-native-access.

the class TypeInfoUtil method getVarDesc.

/**
     * Gets the var desc.
     * 
     * @param index
     *            the index
     * @return the var desc
     */
public VARDESC getVarDesc(int index) {
    PointerByReference ppVarDesc = new PointerByReference();
    HRESULT hr = this.typeInfo.GetVarDesc(new UINT(index), ppVarDesc);
    COMUtils.checkRC(hr);
    return new VARDESC(ppVarDesc.getValue());
}
Also used : HRESULT(com.sun.jna.platform.win32.WinNT.HRESULT) PointerByReference(com.sun.jna.ptr.PointerByReference) UINT(com.sun.jna.platform.win32.WinDef.UINT) VARDESC(com.sun.jna.platform.win32.OaIdl.VARDESC)

Aggregations

PointerByReference (com.sun.jna.ptr.PointerByReference)128 HRESULT (com.sun.jna.platform.win32.WinNT.HRESULT)54 IntByReference (com.sun.jna.ptr.IntByReference)35 Pointer (com.sun.jna.Pointer)20 Test (org.junit.Test)19 DWORD (com.sun.jna.platform.win32.WinDef.DWORD)18 REFIID (com.sun.jna.platform.win32.Guid.REFIID)13 File (java.io.File)13 ULONG (com.sun.jna.platform.win32.WinDef.ULONG)10 PSID (com.sun.jna.platform.win32.WinNT.PSID)10 Memory (com.sun.jna.Memory)8 WString (com.sun.jna.WString)8 Dispatch (com.sun.jna.platform.win32.COM.Dispatch)7 ULONGByReference (com.sun.jna.platform.win32.WinDef.ULONGByReference)7 WinNT (com.sun.jna.platform.win32.WinNT)7 HANDLE (com.sun.jna.platform.win32.WinNT.HANDLE)7 IID (com.sun.jna.platform.win32.Guid.IID)6 ArrayList (java.util.ArrayList)6 IDispatch (com.sun.jna.platform.win32.COM.IDispatch)5 UINT (com.sun.jna.platform.win32.WinDef.UINT)5