Search in sources :

Example 6 with Pointer

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

the class Advapi32Test method testReadEncryptedFileRaw.

public void testReadEncryptedFileRaw() throws Exception {
    // create an encrypted file
    File file = createTempFile();
    String lpFileName = file.getAbsolutePath();
    assertTrue("EncryptFile(" + lpFileName + ")", Advapi32.INSTANCE.EncryptFile(lpFileName));
    // open file for export
    ULONG ulFlags = new ULONG(0);
    PointerByReference pvContext = new PointerByReference();
    assertEquals(W32Errors.ERROR_SUCCESS, Advapi32.INSTANCE.OpenEncryptedFileRaw(lpFileName, ulFlags, pvContext));
    // read encrypted file
    final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    FE_EXPORT_FUNC pfExportCallback = new FE_EXPORT_FUNC() {

        @Override
        public DWORD callback(Pointer pbData, Pointer pvCallbackContext, ULONG ulLength) {
            if (pbData == null) {
                throw new NullPointerException("Callback data unexpectedly missing");
            }
            byte[] arr = pbData.getByteArray(0, ulLength.intValue());
            try {
                outputStream.write(arr);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            return new DWORD(W32Errors.ERROR_SUCCESS);
        }
    };
    assertEquals(W32Errors.ERROR_SUCCESS, Advapi32.INSTANCE.ReadEncryptedFileRaw(pfExportCallback, null, pvContext.getValue()));
    outputStream.close();
    Advapi32.INSTANCE.CloseEncryptedFileRaw(pvContext.getValue());
    file.delete();
}
Also used : ULONG(com.sun.jna.platform.win32.WinDef.ULONG) PointerByReference(com.sun.jna.ptr.PointerByReference) DWORD(com.sun.jna.platform.win32.WinDef.DWORD) FE_EXPORT_FUNC(com.sun.jna.platform.win32.WinBase.FE_EXPORT_FUNC) Pointer(com.sun.jna.Pointer) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) File(java.io.File)

Example 7 with Pointer

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

the class ITypeLibTest method testFindName.

public void testFindName() {
    ITypeLib shellTypeLib = loadShellTypeLib();
    // The found member is Count, search done with lowercase value to test
    // correct behaviour (search is case insensitive)
    String memberValue = "count";
    String memberValueOk = "Count";
    Pointer p = Ole32.INSTANCE.CoTaskMemAlloc((memberValue.length() + 1L) * Native.WCHAR_SIZE);
    WTypes.LPOLESTR olestr = new WTypes.LPOLESTR(p);
    olestr.setValue(memberValue);
    short maxResults = 100;
    ULONG lHashVal = new ULONG(0);
    USHORTByReference pcFound = new USHORTByReference(maxResults);
    Pointer[] pointers = new Pointer[maxResults];
    MEMBERID[] rgMemId = new MEMBERID[maxResults];
    HRESULT hr = shellTypeLib.FindName(olestr, lHashVal, pointers, rgMemId, pcFound);
    assertTrue(COMUtils.SUCCEEDED(hr));
    // If a reader can come up with more tests it would be appretiated,
    // the documentation is unclear what more can be expected
    // 2 matches come from manual tests
    assertTrue(pcFound.getValue().intValue() == 2);
    // Check that function return corrected member name (Count) - see uppercase C
    assertEquals(memberValueOk, olestr.getValue());
    // There have to be as many pointers as reported by pcFound
    assertNotNull(pointers[0]);
    assertNotNull(pointers[1]);
    // Might be flaky, contract only defined positions 0 -> (pcFound - 1)
    assertNull(pointers[2]);
    // Test access to second value
    TypeInfo secondTypeInfo = new TypeInfo(pointers[1]);
    PointerByReference pbr = new PointerByReference();
    hr = secondTypeInfo.GetTypeAttr(pbr);
    assertTrue(COMUtils.SUCCEEDED(hr));
    OaIdl.TYPEATTR pTypeAttr = new OaIdl.TYPEATTR(pbr.getValue());
    // Either interface FolderItemVerbs ({1F8352C0-50B0-11CF-960C-0080C7F4EE85})
    // or FolderItems ({744129E0-CBE5-11CE-8350-444553540000})
    String typeGUID = pTypeAttr.guid.toGuidString();
    assertTrue(typeGUID.equals("{1F8352C0-50B0-11CF-960C-0080C7F4EE85}") || typeGUID.equals("{744129E0-CBE5-11CE-8350-444553540000}"));
    secondTypeInfo.ReleaseTypeAttr(pTypeAttr);
    Ole32.INSTANCE.CoTaskMemFree(olestr.getPointer());
}
Also used : ULONG(com.sun.jna.platform.win32.WinDef.ULONG) HRESULT(com.sun.jna.platform.win32.WinNT.HRESULT) MEMBERID(com.sun.jna.platform.win32.OaIdl.MEMBERID) Pointer(com.sun.jna.Pointer) WString(com.sun.jna.WString) WTypes(com.sun.jna.platform.win32.WTypes) PointerByReference(com.sun.jna.ptr.PointerByReference) USHORTByReference(com.sun.jna.platform.win32.WinDef.USHORTByReference) OaIdl(com.sun.jna.platform.win32.OaIdl)

Example 8 with Pointer

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

the class ITypeLibTest method testIsName.

public void testIsName() {
    ITypeLib shellTypeLib = loadShellTypeLib();
    String memberValue = "Folder";
    Pointer p = Ole32.INSTANCE.CoTaskMemAlloc((memberValue.length() + 1L) * Native.WCHAR_SIZE);
    WTypes.LPOLESTR olestr = new WTypes.LPOLESTR(p);
    olestr.setValue(memberValue);
    WinDef.BOOLByReference boolByRef = new WinDef.BOOLByReference();
    HRESULT hr = shellTypeLib.IsName(olestr, new ULONG(0), boolByRef);
    assertTrue(COMUtils.SUCCEEDED(hr));
    // Folder is a member
    assertTrue(boolByRef.getValue().booleanValue());
    Ole32.INSTANCE.CoTaskMemFree(p);
}
Also used : ULONG(com.sun.jna.platform.win32.WinDef.ULONG) HRESULT(com.sun.jna.platform.win32.WinNT.HRESULT) WinDef(com.sun.jna.platform.win32.WinDef) Pointer(com.sun.jna.Pointer) WString(com.sun.jna.WString) WTypes(com.sun.jna.platform.win32.WTypes)

Example 9 with Pointer

use of com.sun.jna.Pointer in project jmonkeyengine by jMonkeyEngine.

the class OpenVRUtil method getTrackedDeviceStringProperty.

/**
	 * Get the value of the given string {@link JOpenVRLibrary.ETrackedDeviceProperty property} attached to the given device.
	 * @param system the underlying OpenVR system.
	 * @param deviceIndex the index of the device to query.
	 * @param property the property to query.
	 * @param bufferSize the size of the buffer to use for storing native string.
	 * @return the value of the given string property attached to the given device.
	 * @see OpenVRInput#getTrackedControllerCount()
	 * @see JOpenVRLibrary.ETrackedDeviceProperty
	 * @see #getTrackedDeviceStringProperty(VR_IVRSystem_FnTable, int, int)
	 */
public static String getTrackedDeviceStringProperty(VR_IVRSystem_FnTable system, int deviceIndex, int property, int bufferSize) {
    String str = "";
    int unBufferSize = 256;
    Pointer pchValue = new Memory(unBufferSize);
    IntByReference pError = new IntByReference();
    system.GetStringTrackedDeviceProperty.apply(deviceIndex, property, pchValue, unBufferSize, pError);
    if (pError.getValue() == ETrackedPropertyError.ETrackedPropertyError_TrackedProp_Success) {
        str = pchValue.getString(0);
    } else if (pError.getValue() == ETrackedPropertyError.ETrackedPropertyError_TrackedProp_BufferTooSmall) {
        throw new IllegalArgumentException("Cannot access property \"" + getETrackedDevicePropertyString(property) + "\" (" + property + ") for device " + deviceIndex + ": " + getETrackedPropertyErrorString(pError.getValue()) + " (" + pError.getValue() + ")");
    } else if (pError.getValue() == ETrackedPropertyError.ETrackedPropertyError_TrackedProp_CouldNotContactServer) {
        throw new IllegalArgumentException("Cannot access property \"" + getETrackedDevicePropertyString(property) + "\" (" + property + ") for device " + deviceIndex + ": " + getETrackedPropertyErrorString(pError.getValue()) + " (" + pError.getValue() + ")");
    } else if (pError.getValue() == ETrackedPropertyError.ETrackedPropertyError_TrackedProp_InvalidDevice) {
        throw new IllegalArgumentException("Cannot access property \"" + getETrackedDevicePropertyString(property) + "\" (" + property + ") for device " + deviceIndex + ": " + getETrackedPropertyErrorString(pError.getValue()) + " (" + pError.getValue() + ")");
    } else if (pError.getValue() == ETrackedPropertyError.ETrackedPropertyError_TrackedProp_InvalidOperation) {
        throw new IllegalArgumentException("Cannot access property \"" + getETrackedDevicePropertyString(property) + "\" (" + property + ") for device " + deviceIndex + ": " + getETrackedPropertyErrorString(pError.getValue()) + " (" + pError.getValue() + ")");
    } else if (pError.getValue() == ETrackedPropertyError.ETrackedPropertyError_TrackedProp_NotYetAvailable) {
        throw new IllegalArgumentException("Cannot access property \"" + getETrackedDevicePropertyString(property) + "\" (" + property + ") for device " + deviceIndex + ": " + getETrackedPropertyErrorString(pError.getValue()) + " (" + pError.getValue() + ")");
    } else if (pError.getValue() == ETrackedPropertyError.ETrackedPropertyError_TrackedProp_PermissionDenied) {
        throw new IllegalArgumentException("Cannot access property \"" + getETrackedDevicePropertyString(property) + "\" (" + property + ") for device " + deviceIndex + ": " + getETrackedPropertyErrorString(pError.getValue()) + " (" + pError.getValue() + ")");
    } else if (pError.getValue() == ETrackedPropertyError.ETrackedPropertyError_TrackedProp_StringExceedsMaximumLength) {
        throw new IllegalArgumentException("Cannot access property \"" + getETrackedDevicePropertyString(property) + "\" (" + property + ") for device " + deviceIndex + ": " + getETrackedPropertyErrorString(pError.getValue()) + " (" + pError.getValue() + ")");
    } else if (pError.getValue() == ETrackedPropertyError.ETrackedPropertyError_TrackedProp_UnknownProperty) {
        throw new IllegalArgumentException("Cannot access property \"" + getETrackedDevicePropertyString(property) + "\" (" + property + ") for device " + deviceIndex + ": " + getETrackedPropertyErrorString(pError.getValue()) + " (" + pError.getValue() + ")");
    } else if (pError.getValue() == ETrackedPropertyError.ETrackedPropertyError_TrackedProp_ValueNotProvidedByDevice) {
        throw new IllegalArgumentException("Cannot access property \"" + getETrackedDevicePropertyString(property) + "\" (" + property + ") for device " + deviceIndex + ": " + getETrackedPropertyErrorString(pError.getValue()) + " (" + pError.getValue() + ")");
    } else if (pError.getValue() == ETrackedPropertyError.ETrackedPropertyError_TrackedProp_WrongDataType) {
        throw new IllegalArgumentException("Cannot access property \"" + getETrackedDevicePropertyString(property) + "\" (" + property + ") for device " + deviceIndex + ": " + getETrackedPropertyErrorString(pError.getValue()) + " (" + pError.getValue() + ")");
    } else if (pError.getValue() == ETrackedPropertyError.ETrackedPropertyError_TrackedProp_WrongDeviceClass) {
        throw new IllegalArgumentException("Cannot access property \"" + getETrackedDevicePropertyString(property) + "\" (" + property + ") for device " + deviceIndex + ": " + getETrackedPropertyErrorString(pError.getValue()) + " (" + pError.getValue() + ")");
    } else {
        throw new IllegalArgumentException("Cannot access property \"" + getETrackedDevicePropertyString(property) + "\" (" + property + ") for device " + deviceIndex + ": " + getETrackedPropertyErrorString(pError.getValue()) + " (" + pError.getValue() + ")");
    }
    return str;
}
Also used : IntByReference(com.sun.jna.ptr.IntByReference) Memory(com.sun.jna.Memory) Pointer(com.sun.jna.Pointer)

Example 10 with Pointer

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

the class SAFEARRAYTest method toArrayDirect.

private Object[] toArrayDirect(SAFEARRAY wrap) {
    Pointer dataPointer = wrap.accessData();
    long rowMax = wrap.getUBound(2);
    long columnMax = wrap.getUBound(1);
    VARIANT[] variantData = (VARIANT[]) new VARIANT(dataPointer).toArray((int) ((rowMax + 1) * (columnMax + 1)));
    Object[][] result = new Object[(int) (rowMax + 1)][(int) (columnMax + 1)];
    for (long i = 0; i <= rowMax; i++) {
        long rowOffset = i * columnMax;
        for (long j = 0; j <= columnMax; j++) {
            VARIANT cell = variantData[(int) (rowOffset + j)];
            result[(int) i][(int) j] = cell.getValue();
        }
    }
    wrap.unaccessData();
    return result;
}
Also used : Pointer(com.sun.jna.Pointer) ComObject(com.sun.jna.platform.win32.COM.util.annotation.ComObject) VARIANT(com.sun.jna.platform.win32.Variant.VARIANT)

Aggregations

Pointer (com.sun.jna.Pointer)152 PointerByReference (com.sun.jna.ptr.PointerByReference)31 Test (org.junit.Test)31 Memory (com.sun.jna.Memory)23 File (java.io.File)21 ByteBuffer (java.nio.ByteBuffer)18 NativeLong (com.sun.jna.NativeLong)17 IntByReference (com.sun.jna.ptr.IntByReference)17 BufferedImage (java.awt.image.BufferedImage)15 FileInputStream (java.io.FileInputStream)11 HDDEDATA (com.sun.jna.platform.win32.Ddeml.HDDEDATA)8 HSZ (com.sun.jna.platform.win32.Ddeml.HSZ)8 DirectBuffer (sun.nio.ch.DirectBuffer)8 HCONV (com.sun.jna.platform.win32.Ddeml.HCONV)7 ConnectHandler (com.sun.jna.platform.win32.DdemlUtil.ConnectHandler)7 IDdeConnection (com.sun.jna.platform.win32.DdemlUtil.IDdeConnection)7 StandaloneDdeClient (com.sun.jna.platform.win32.DdemlUtil.StandaloneDdeClient)7 HANDLE (com.sun.jna.platform.win32.WinNT.HANDLE)7 IOException (java.io.IOException)7 CountDownLatch (java.util.concurrent.CountDownLatch)7