Search in sources :

Example 6 with ULONGByReference

use of com.sun.jna.platform.win32.WinDef.ULONGByReference in project jna by java-native-access.

the class EnumMoniker_Test method Next.

@Test
public void Next() {
    // GetRunningObjectTable
    PointerByReference pprot = new PointerByReference();
    HRESULT hr = Ole32.INSTANCE.GetRunningObjectTable(new DWORD(0), pprot);
    COMUtils.checkRC(hr);
    IRunningObjectTable rot = new RunningObjectTable(pprot.getValue());
    // EnumRunning
    PointerByReference ppenumMoniker = new PointerByReference();
    hr = rot.EnumRunning(ppenumMoniker);
    COMUtils.checkRC(hr);
    IEnumMoniker iterator = new EnumMoniker(ppenumMoniker.getValue());
    // Reset
    hr = iterator.Reset();
    COMUtils.checkRC(hr);
    // Next
    PointerByReference rgelt1 = new PointerByReference();
    ULONGByReference pceltFetched1 = new ULONGByReference();
    hr = iterator.Next(new ULONG(1), rgelt1, pceltFetched1);
    COMUtils.checkRC(hr);
    // Next
    PointerByReference rgelt2 = new PointerByReference();
    ULONGByReference pceltFetched2 = new ULONGByReference();
    hr = iterator.Next(new ULONG(1), rgelt2, pceltFetched2);
    COMUtils.checkRC(hr);
    assertNotEquals(rgelt1.getValue(), rgelt2.getValue());
}
Also used : ULONG(com.sun.jna.platform.win32.WinDef.ULONG) HRESULT(com.sun.jna.platform.win32.WinNT.HRESULT) PointerByReference(com.sun.jna.ptr.PointerByReference) DWORD(com.sun.jna.platform.win32.WinDef.DWORD) ULONGByReference(com.sun.jna.platform.win32.WinDef.ULONGByReference) Test(org.junit.Test)

Example 7 with ULONGByReference

use of com.sun.jna.platform.win32.WinDef.ULONGByReference in project jna by java-native-access.

the class EnumMoniker_Test method Reset.

@Test
public void Reset() {
    // GetRunningObjectTable
    PointerByReference pprot = new PointerByReference();
    HRESULT hr = Ole32.INSTANCE.GetRunningObjectTable(new DWORD(0), pprot);
    COMUtils.checkRC(hr);
    IRunningObjectTable rot = new RunningObjectTable(pprot.getValue());
    // EnumRunning
    PointerByReference ppenumMoniker = new PointerByReference();
    hr = rot.EnumRunning(ppenumMoniker);
    COMUtils.checkRC(hr);
    IEnumMoniker iterator = new EnumMoniker(ppenumMoniker.getValue());
    // Reset
    hr = iterator.Reset();
    COMUtils.checkRC(hr);
    // Next
    PointerByReference rgelt1 = new PointerByReference();
    ULONGByReference pceltFetched1 = new ULONGByReference();
    hr = iterator.Next(new ULONG(1), rgelt1, pceltFetched1);
    COMUtils.checkRC(hr);
    // Reset
    hr = iterator.Reset();
    COMUtils.checkRC(hr);
    // Next
    PointerByReference rgelt2 = new PointerByReference();
    ULONGByReference pceltFetched2 = new ULONGByReference();
    hr = iterator.Next(new ULONG(1), rgelt2, pceltFetched2);
    COMUtils.checkRC(hr);
    assertEquals(rgelt1.getValue(), rgelt2.getValue());
}
Also used : ULONG(com.sun.jna.platform.win32.WinDef.ULONG) HRESULT(com.sun.jna.platform.win32.WinNT.HRESULT) PointerByReference(com.sun.jna.ptr.PointerByReference) DWORD(com.sun.jna.platform.win32.WinDef.DWORD) ULONGByReference(com.sun.jna.platform.win32.WinDef.ULONGByReference) Test(org.junit.Test)

Example 8 with ULONGByReference

use of com.sun.jna.platform.win32.WinDef.ULONGByReference in project jna by java-native-access.

the class Advapi32Test method testWriteEncryptedFileRaw.

public void testWriteEncryptedFileRaw() throws Exception {
    // create an encrypted file
    File file = createTempFile();
    String lpFileName = file.getAbsolutePath();
    assertTrue(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 null");
            }
            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());
    // open file for import
    String lbFileName2 = System.getProperty("java.io.tmpdir") + File.separator + "backup-" + file.getName();
    ULONG ulFlags2 = new ULONG(CREATE_FOR_IMPORT);
    PointerByReference pvContext2 = new PointerByReference();
    assertEquals(W32Errors.ERROR_SUCCESS, Advapi32.INSTANCE.OpenEncryptedFileRaw(lbFileName2, ulFlags2, pvContext2));
    // write encrypted file
    final IntByReference elementsReadWrapper = new IntByReference(0);
    FE_IMPORT_FUNC pfImportCallback = new FE_IMPORT_FUNC() {

        @Override
        public DWORD callback(Pointer pbData, Pointer pvCallbackContext, ULONGByReference ulLength) {
            int elementsRead = elementsReadWrapper.getValue();
            int remainingElements = outputStream.size() - elementsRead;
            int length = Math.min(remainingElements, ulLength.getValue().intValue());
            pbData.write(0, outputStream.toByteArray(), elementsRead, length);
            elementsReadWrapper.setValue(elementsRead + length);
            ulLength.setValue(new ULONG(length));
            return new DWORD(W32Errors.ERROR_SUCCESS);
        }
    };
    assertEquals(W32Errors.ERROR_SUCCESS, Advapi32.INSTANCE.WriteEncryptedFileRaw(pfImportCallback, null, pvContext2.getValue()));
    Advapi32.INSTANCE.CloseEncryptedFileRaw(pvContext2.getValue());
    file.delete();
    new File(lbFileName2.toString()).delete();
}
Also used : ULONG(com.sun.jna.platform.win32.WinDef.ULONG) IntByReference(com.sun.jna.ptr.IntByReference) DWORD(com.sun.jna.platform.win32.WinDef.DWORD) Pointer(com.sun.jna.Pointer) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) ULONGByReference(com.sun.jna.platform.win32.WinDef.ULONGByReference) PointerByReference(com.sun.jna.ptr.PointerByReference) FE_EXPORT_FUNC(com.sun.jna.platform.win32.WinBase.FE_EXPORT_FUNC) File(java.io.File) FE_IMPORT_FUNC(com.sun.jna.platform.win32.WinBase.FE_IMPORT_FUNC)

Example 9 with ULONGByReference

use of com.sun.jna.platform.win32.WinDef.ULONGByReference in project jna by java-native-access.

the class RunningObjectTable_Test method GetObject.

@Test
public void GetObject() {
    PointerByReference pprot = new PointerByReference();
    HRESULT hr = Ole32.INSTANCE.GetRunningObjectTable(new DWORD(0), pprot);
    COMUtils.checkRC(hr);
    IRunningObjectTable rot = new RunningObjectTable(pprot.getValue());
    PointerByReference ppenumMoniker = new PointerByReference();
    hr = rot.EnumRunning(ppenumMoniker);
    COMUtils.checkRC(hr);
    IEnumMoniker iterator = new EnumMoniker(ppenumMoniker.getValue());
    iterator.Reset();
    PointerByReference rgelt = new PointerByReference();
    ULONGByReference pceltFetched = new ULONGByReference();
    hr = iterator.Next(new ULONG(1), rgelt, pceltFetched);
    while (WinNT.S_OK.equals(hr) && pceltFetched.getValue().intValue() > 0) {
        Moniker moniker = new Moniker(rgelt.getValue());
        PointerByReference ppbc = new PointerByReference();
        Ole32.INSTANCE.CreateBindCtx(new DWORD(), ppbc);
        String name = moniker.GetDisplayName(ppbc.getValue(), moniker.getPointer());
        PointerByReference ppunkObject = new PointerByReference();
        hr = rot.GetObject(moniker.getPointer(), ppunkObject);
        COMUtils.checkRC(hr);
        IUnknown unk = new Unknown(ppunkObject.getValue());
        PointerByReference ppvObject = new PointerByReference();
        hr = unk.QueryInterface(new REFIID(IUnknown.IID_IUNKNOWN), ppvObject);
        assertEquals(0, hr.intValue());
        assertNotNull(ppvObject.getValue());
        moniker.Release();
        hr = iterator.Next(new ULONG(1), rgelt, pceltFetched);
    }
}
Also used : ULONG(com.sun.jna.platform.win32.WinDef.ULONG) HRESULT(com.sun.jna.platform.win32.WinNT.HRESULT) DWORD(com.sun.jna.platform.win32.WinDef.DWORD) REFIID(com.sun.jna.platform.win32.Guid.REFIID) ULONGByReference(com.sun.jna.platform.win32.WinDef.ULONGByReference) PointerByReference(com.sun.jna.ptr.PointerByReference) Test(org.junit.Test)

Aggregations

ULONGByReference (com.sun.jna.platform.win32.WinDef.ULONGByReference)8 DWORD (com.sun.jna.platform.win32.WinDef.DWORD)7 ULONG (com.sun.jna.platform.win32.WinDef.ULONG)7 PointerByReference (com.sun.jna.ptr.PointerByReference)7 HRESULT (com.sun.jna.platform.win32.WinNT.HRESULT)6 Test (org.junit.Test)6 IntByReference (com.sun.jna.ptr.IntByReference)3 Pointer (com.sun.jna.Pointer)2 FE_EXPORT_FUNC (com.sun.jna.platform.win32.WinBase.FE_EXPORT_FUNC)2 FE_IMPORT_FUNC (com.sun.jna.platform.win32.WinBase.FE_IMPORT_FUNC)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 IOException (java.io.IOException)2 REFIID (com.sun.jna.platform.win32.Guid.REFIID)1 WinNT (com.sun.jna.platform.win32.WinNT)1 HANDLE (com.sun.jna.platform.win32.WinNT.HANDLE)1 File (java.io.File)1