Search in sources :

Example 1 with ULONGByReference

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

the class EnumMoniker_Test method Skip.

@Test
public void Skip() {
    // 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);
    // Skip
    hr = iterator.Skip(new ULONG(1));
    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 2 with ULONGByReference

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

the class Advapi32Util method backupEncryptedFile.

/**
     * Backup an encrypted file or folder without decrypting it. A file named
     * "bar/sample.text" will be backed-up to "destDir/sample.text". A directory
     * named "bar" will be backed-up to "destDir/bar". This method is NOT
     * recursive. If you have an encrypted directory with encrypted files, this
     * method must be called once for the directory, and once for each encrypted
     * file to be backed-up.
     *
     * @param src
     *         The encrypted file or directory to backup.
     * @param destDir
     *         The directory where the backup will be saved.
     */
public static void backupEncryptedFile(File src, File destDir) {
    if (!destDir.isDirectory()) {
        throw new IllegalArgumentException("destDir must be a directory.");
    }
    // Open the file for export (backup)
    ULONG readFlag = new ULONG(0);
    // Import (restore) file
    ULONG writeFlag = new ULONG(CREATE_FOR_IMPORT);
    if (src.isDirectory()) {
        writeFlag.setValue(CREATE_FOR_IMPORT | CREATE_FOR_DIR);
    }
    // open encrypted file for export
    String srcFileName = src.getAbsolutePath();
    PointerByReference pvContext = new PointerByReference();
    if (Advapi32.INSTANCE.OpenEncryptedFileRaw(srcFileName, readFlag, pvContext) != W32Errors.ERROR_SUCCESS) {
        throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
    }
    // 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) {
            byte[] arr = pbData.getByteArray(0, ulLength.intValue());
            try {
                outputStream.write(arr);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            return new DWORD(W32Errors.ERROR_SUCCESS);
        }
    };
    if (Advapi32.INSTANCE.ReadEncryptedFileRaw(pfExportCallback, null, pvContext.getValue()) != W32Errors.ERROR_SUCCESS) {
        throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
    }
    // close
    try {
        outputStream.close();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    Advapi32.INSTANCE.CloseEncryptedFileRaw(pvContext.getValue());
    // open file for import
    String destFileName = destDir.getAbsolutePath() + File.separator + src.getName();
    pvContext = new PointerByReference();
    if (Advapi32.INSTANCE.OpenEncryptedFileRaw(destFileName, writeFlag, pvContext) != W32Errors.ERROR_SUCCESS) {
        throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
    }
    // 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);
        }
    };
    if (Advapi32.INSTANCE.WriteEncryptedFileRaw(pfImportCallback, null, pvContext.getValue()) != W32Errors.ERROR_SUCCESS) {
        throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
    }
    // close
    Advapi32.INSTANCE.CloseEncryptedFileRaw(pvContext.getValue());
}
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) FE_IMPORT_FUNC(com.sun.jna.platform.win32.WinBase.FE_IMPORT_FUNC)

Example 3 with ULONGByReference

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

the class Kernel32NamedPipeTest method testNamedPipeServerAPI.

@Test
public void testNamedPipeServerAPI() {
    String pipeName = "\\\\.\\pipe\\" + getCurrentTestName();
    HANDLE hNamedPipe = Kernel32.INSTANCE.CreateNamedPipe(pipeName, // dwOpenMode
    WinBase.PIPE_ACCESS_DUPLEX, // dwPipeMode
    WinBase.PIPE_TYPE_BYTE | WinBase.PIPE_READMODE_BYTE | WinBase.PIPE_WAIT, // nMaxInstances,
    1, // nOutBufferSize,
    Byte.MAX_VALUE, // nInBufferSize,
    Byte.MAX_VALUE, // nDefaultTimeOut,
    1000, // lpSecurityAttributes
    null);
    assertCallSucceeded("CreateNamedPipe", !WinBase.INVALID_HANDLE_VALUE.equals(hNamedPipe));
    // NOTE: we don't really care what the returned values are only that the call succeeds
    try {
        IntByReference lpFlags = new IntByReference(0);
        IntByReference lpOutBuffferSize = new IntByReference(0);
        IntByReference lpInBufferSize = new IntByReference(0);
        IntByReference lpMaxInstances = new IntByReference(0);
        assertCallSucceeded("GetNamedPipeInfo", Kernel32.INSTANCE.GetNamedPipeInfo(hNamedPipe, lpFlags, lpOutBuffferSize, lpInBufferSize, lpMaxInstances));
        ULONGByReference ServerProcessId = new ULONGByReference();
        assertCallSucceeded("GetNamedPipeServerProcessId", Kernel32.INSTANCE.GetNamedPipeServerProcessId(hNamedPipe, ServerProcessId));
        ULONGByReference ServerSessionId = new ULONGByReference();
        assertCallSucceeded("GetNamedPipeServerSessionId", Kernel32.INSTANCE.GetNamedPipeServerSessionId(hNamedPipe, ServerSessionId));
        assertCallSucceeded("DisconnectNamedPipe", Kernel32.INSTANCE.DisconnectNamedPipe(hNamedPipe));
    } finally {
        // clean up
        assertCallSucceeded("Named pipe handle close", Kernel32.INSTANCE.CloseHandle(hNamedPipe));
    }
}
Also used : IntByReference(com.sun.jna.ptr.IntByReference) HANDLE(com.sun.jna.platform.win32.WinNT.HANDLE) ULONGByReference(com.sun.jna.platform.win32.WinDef.ULONGByReference) Test(org.junit.Test)

Example 4 with ULONGByReference

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

the class EnumMoniker method Next.

// The magic number values for (vTableId) below, are worked out by
// counting the number of methods in the full interface (0 indexed), as this
// inherits IUnknown, which has 3 methods, we start here at 3.
@Override
public HRESULT Next(ULONG celt, PointerByReference rgelt, ULONGByReference pceltFetched) {
    final int vTableId = 3;
    WinNT.HRESULT hr = (WinNT.HRESULT) this._invokeNativeObject(vTableId, new Object[] { this.getPointer(), celt, rgelt, pceltFetched }, WinNT.HRESULT.class);
    return hr;
}
Also used : HRESULT(com.sun.jna.platform.win32.WinNT.HRESULT) WinNT(com.sun.jna.platform.win32.WinNT) HRESULT(com.sun.jna.platform.win32.WinNT.HRESULT)

Example 5 with ULONGByReference

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

the class EnumMoniker_Test method Clone.

@Test
public void Clone() {
    // 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 iterator1 = new EnumMoniker(ppenumMoniker.getValue());
    // iterator1.Reset
    hr = iterator1.Reset();
    COMUtils.checkRC(hr);
    // iterator1.Next
    PointerByReference rgelt1 = new PointerByReference();
    ULONGByReference pceltFetched1 = new ULONGByReference();
    hr = iterator1.Next(new ULONG(1), rgelt1, pceltFetched1);
    COMUtils.checkRC(hr);
    // iterator1.Clone
    PointerByReference ppenum = new PointerByReference();
    hr = iterator1.Clone(ppenum);
    COMUtils.checkRC(hr);
    IEnumMoniker iterator2 = new EnumMoniker(ppenum.getValue());
    // iterator2.Next
    PointerByReference rgelt2 = new PointerByReference();
    ULONGByReference pceltFetched2 = new ULONGByReference();
    hr = iterator2.Next(new ULONG(1), rgelt2, pceltFetched2);
    COMUtils.checkRC(hr);
    assertNotEquals(rgelt1.getValue(), rgelt2.getValue());
    // iterator1.Next
    rgelt1 = new PointerByReference();
    pceltFetched1 = new ULONGByReference();
    hr = iterator1.Next(new ULONG(1), rgelt1, pceltFetched1);
    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)

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