Search in sources :

Example 96 with PointerByReference

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

the class Advapi32Test method testConvertSid.

public void testConvertSid() {
    String sidString = EVERYONE;
    PSIDByReference sid = new PSIDByReference();
    assertTrue("Failed to convert SID string", Advapi32.INSTANCE.ConvertStringSidToSid(sidString, sid));
    PSID value = sid.getValue();
    try {
        PointerByReference convertedSidStringPtr = new PointerByReference();
        assertTrue("Failed to convert SID string", Advapi32.INSTANCE.ConvertSidToStringSid(value, convertedSidStringPtr));
        Pointer conv = convertedSidStringPtr.getValue();
        try {
            String convertedSidString = conv.getWideString(0);
            assertEquals("Mismatched SID string", convertedSidString, sidString);
        } finally {
            Kernel32Util.freeLocalMemory(conv);
        }
    } finally {
        Kernel32Util.freeLocalMemory(value.getPointer());
    }
}
Also used : PSIDByReference(com.sun.jna.platform.win32.WinNT.PSIDByReference) PointerByReference(com.sun.jna.ptr.PointerByReference) Pointer(com.sun.jna.Pointer) PSID(com.sun.jna.platform.win32.WinNT.PSID)

Example 97 with PointerByReference

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

the class Advapi32Test method testAddAce.

public void testAddAce() throws IOException {
    ACL pAcl;
    int cbAcl = 0;
    PSID pSid = new PSID(WinNT.SECURITY_MAX_SID_SIZE);
    IntByReference cbSid = new IntByReference(WinNT.SECURITY_MAX_SID_SIZE);
    assertTrue("Failed to create well-known SID", Advapi32.INSTANCE.CreateWellKnownSid(WELL_KNOWN_SID_TYPE.WinBuiltinAdministratorsSid, null, pSid, cbSid));
    int sidLength = Advapi32.INSTANCE.GetLengthSid(pSid);
    cbAcl = Native.getNativeSize(ACL.class, null);
    cbAcl += Advapi32Util.getAceSize(sidLength);
    cbAcl = Advapi32Util.alignOnDWORD(cbAcl);
    pAcl = new ACL(cbAcl);
    ACCESS_ALLOWED_ACE pace = new ACCESS_ALLOWED_ACE(WinNT.STANDARD_RIGHTS_ALL, WinNT.INHERITED_ACE, pSid);
    assertTrue(Advapi32.INSTANCE.InitializeAcl(pAcl, cbAcl, WinNT.ACL_REVISION));
    assertTrue(Advapi32.INSTANCE.AddAce(pAcl, WinNT.ACL_REVISION, WinNT.MAXDWORD, pace.getPointer(), pace.size()));
    PointerByReference pAce = new PointerByReference(new Memory(16));
    assertTrue(Advapi32.INSTANCE.GetAce(pAcl, 0, pAce));
    ACCESS_ALLOWED_ACE pAceGet = new ACCESS_ALLOWED_ACE(pAce.getValue());
    assertTrue(pAceGet.Mask == WinNT.STANDARD_RIGHTS_ALL);
    assertTrue(Advapi32.INSTANCE.EqualSid(pAceGet.psid, pSid));
}
Also used : IntByReference(com.sun.jna.ptr.IntByReference) ACCESS_ALLOWED_ACE(com.sun.jna.platform.win32.WinNT.ACCESS_ALLOWED_ACE) Memory(com.sun.jna.Memory) PointerByReference(com.sun.jna.ptr.PointerByReference) ACL(com.sun.jna.platform.win32.WinNT.ACL) PSID(com.sun.jna.platform.win32.WinNT.PSID)

Example 98 with PointerByReference

use of com.sun.jna.ptr.PointerByReference 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 99 with PointerByReference

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

the class Advapi32Test method testGetSetSecurityInfoNoSACL.

public void testGetSetSecurityInfoNoSACL() throws Exception {
    int infoType = OWNER_SECURITY_INFORMATION | GROUP_SECURITY_INFORMATION | DACL_SECURITY_INFORMATION;
    PointerByReference ppsidOwner = new PointerByReference();
    PointerByReference ppsidGroup = new PointerByReference();
    PointerByReference ppDacl = new PointerByReference();
    PointerByReference ppSecurityDescriptor = new PointerByReference();
    // create a temp file
    File file = createTempFile();
    String filePath = file.getAbsolutePath();
    HANDLE hFile = Kernel32.INSTANCE.CreateFile(filePath, WinNT.GENERIC_ALL | WinNT.WRITE_OWNER | WinNT.WRITE_DAC, WinNT.FILE_SHARE_READ, new WinBase.SECURITY_ATTRIBUTES(), WinNT.OPEN_EXISTING, WinNT.FILE_ATTRIBUTE_NORMAL, null);
    assertFalse("Failed to create file handle: " + filePath, WinBase.INVALID_HANDLE_VALUE.equals(hFile));
    try {
        try {
            assertEquals("GetSecurityInfo(" + filePath + ")", 0, Advapi32.INSTANCE.GetSecurityInfo(hFile, AccCtrl.SE_OBJECT_TYPE.SE_FILE_OBJECT, infoType, ppsidOwner, ppsidGroup, ppDacl, null, ppSecurityDescriptor));
            assertEquals("SetSecurityInfo(" + filePath + ")", 0, Advapi32.INSTANCE.SetSecurityInfo(hFile, AccCtrl.SE_OBJECT_TYPE.SE_FILE_OBJECT, infoType, ppsidOwner.getValue(), ppsidGroup.getValue(), ppDacl.getValue(), null));
        } finally {
            Kernel32.INSTANCE.CloseHandle(hFile);
            file.delete();
        }
    } finally {
        Kernel32Util.freeLocalMemory(ppSecurityDescriptor.getValue());
    }
}
Also used : PointerByReference(com.sun.jna.ptr.PointerByReference) File(java.io.File) SC_HANDLE(com.sun.jna.platform.win32.Winsvc.SC_HANDLE) HANDLE(com.sun.jna.platform.win32.WinNT.HANDLE)

Example 100 with PointerByReference

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

the class Advapi32Test method testOpenEncryptedFileRaw.

public void testOpenEncryptedFileRaw() 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));
    Advapi32.INSTANCE.CloseEncryptedFileRaw(pvContext.getValue());
    file.delete();
}
Also used : ULONG(com.sun.jna.platform.win32.WinDef.ULONG) PointerByReference(com.sun.jna.ptr.PointerByReference) File(java.io.File)

Aggregations

PointerByReference (com.sun.jna.ptr.PointerByReference)127 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