Search in sources :

Example 51 with DWORD

use of com.sun.jna.platform.win32.WinDef.DWORD 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 52 with DWORD

use of com.sun.jna.platform.win32.WinDef.DWORD 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 53 with DWORD

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

the class Advapi32Test method testAccessCheck.

public void testAccessCheck() {
    final GENERIC_MAPPING mapping = new GENERIC_MAPPING();
    mapping.genericRead = new DWORD(FILE_GENERIC_READ);
    mapping.genericWrite = new DWORD(FILE_GENERIC_WRITE);
    mapping.genericExecute = new DWORD(FILE_GENERIC_EXECUTE);
    mapping.genericAll = new DWORD(FILE_ALL_ACCESS);
    final Memory securityDescriptorMemoryPointer = new Memory(1);
    final PRIVILEGE_SET privileges = new PRIVILEGE_SET(1);
    privileges.PrivilegeCount = new DWORD(0);
    final DWORDByReference privilegeLength = new DWORDByReference(new DWORD(privileges.size()));
    final DWORDByReference grantedAccess = new DWORDByReference();
    final BOOLByReference result = new BOOLByReference();
    final boolean status = Advapi32.INSTANCE.AccessCheck(securityDescriptorMemoryPointer, null, new DWORD(FILE_GENERIC_READ), mapping, privileges, privilegeLength, grantedAccess, result);
    assertFalse(status);
    assertFalse(result.getValue().booleanValue());
    assertEquals(WinError.ERROR_INVALID_HANDLE, Kernel32.INSTANCE.GetLastError());
}
Also used : BOOLByReference(com.sun.jna.platform.win32.WinDef.BOOLByReference) PRIVILEGE_SET(com.sun.jna.platform.win32.WinNT.PRIVILEGE_SET) GENERIC_MAPPING(com.sun.jna.platform.win32.WinNT.GENERIC_MAPPING) DWORDByReference(com.sun.jna.platform.win32.WinDef.DWORDByReference) Memory(com.sun.jna.Memory) DWORD(com.sun.jna.platform.win32.WinDef.DWORD)

Example 54 with DWORD

use of com.sun.jna.platform.win32.WinDef.DWORD 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 55 with DWORD

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

the class Advapi32Test method testSetNamedSecurityInfoForFileWithSACL.

public void testSetNamedSecurityInfoForFileWithSACL() throws Exception {
    boolean impersontating = false;
    HANDLEByReference phToken = new HANDLEByReference();
    HANDLEByReference phTokenDuplicate = new HANDLEByReference();
    try {
        // open thread or process token, elevate
        if (!Advapi32.INSTANCE.OpenThreadToken(Kernel32.INSTANCE.GetCurrentThread(), TOKEN_ADJUST_PRIVILEGES, false, phToken)) {
            assertEquals(W32Errors.ERROR_NO_TOKEN, Kernel32.INSTANCE.GetLastError());
            // OpenThreadToken may fail with W32Errors.ERROR_NO_TOKEN if current thread is anonymous.  When this happens,
            // we need to open the process token to duplicate it, then set our thread token.
            assertTrue(Advapi32.INSTANCE.OpenProcessToken(Kernel32.INSTANCE.GetCurrentProcess(), TOKEN_DUPLICATE, phToken));
            // Process token opened, now duplicate
            assertTrue(Advapi32.INSTANCE.DuplicateTokenEx(phToken.getValue(), TOKEN_ADJUST_PRIVILEGES | TOKEN_IMPERSONATE, null, SECURITY_IMPERSONATION_LEVEL.SecurityImpersonation, TOKEN_TYPE.TokenImpersonation, phTokenDuplicate));
            // And set thread token.
            assertTrue(Advapi32.INSTANCE.SetThreadToken(null, phTokenDuplicate.getValue()));
            impersontating = true;
        }
        // Which token to adjust depends on whether we had to impersonate or not.
        HANDLE tokenAdjust = impersontating ? phTokenDuplicate.getValue() : phToken.getValue();
        WinNT.TOKEN_PRIVILEGES tp = new WinNT.TOKEN_PRIVILEGES(1);
        WinNT.LUID pLuid = new WinNT.LUID();
        assertTrue(Advapi32.INSTANCE.LookupPrivilegeValue(null, SE_SECURITY_NAME, pLuid));
        tp.Privileges[0] = new WinNT.LUID_AND_ATTRIBUTES(pLuid, new DWORD(WinNT.SE_PRIVILEGE_ENABLED));
        assertTrue(Advapi32.INSTANCE.AdjustTokenPrivileges(tokenAdjust, false, tp, 0, null, null));
        assertTrue(Advapi32.INSTANCE.LookupPrivilegeValue(null, SE_RESTORE_NAME, pLuid));
        tp.Privileges[0] = new WinNT.LUID_AND_ATTRIBUTES(pLuid, new DWORD(WinNT.SE_PRIVILEGE_ENABLED));
        assertTrue(Advapi32.INSTANCE.AdjustTokenPrivileges(tokenAdjust, false, tp, 0, null, null));
        // create a temp file
        File file = createTempFile();
        int infoType = OWNER_SECURITY_INFORMATION | GROUP_SECURITY_INFORMATION | DACL_SECURITY_INFORMATION | SACL_SECURITY_INFORMATION;
        PointerByReference ppsidOwner = new PointerByReference();
        PointerByReference ppsidGroup = new PointerByReference();
        PointerByReference ppDacl = new PointerByReference();
        PointerByReference ppSacl = new PointerByReference();
        PointerByReference ppSecurityDescriptor = new PointerByReference();
        String filePath = file.getAbsolutePath();
        try {
            try {
                assertEquals("GetNamedSecurityInfo(" + filePath + ")", 0, Advapi32.INSTANCE.GetNamedSecurityInfo(filePath, AccCtrl.SE_OBJECT_TYPE.SE_FILE_OBJECT, infoType, ppsidOwner, ppsidGroup, ppDacl, ppSacl, ppSecurityDescriptor));
                // Send the DACL as a SACL
                assertEquals("SetNamedSecurityInfo(" + filePath + ")", 0, Advapi32.INSTANCE.SetNamedSecurityInfo(filePath, AccCtrl.SE_OBJECT_TYPE.SE_FILE_OBJECT, infoType, ppsidOwner.getValue(), ppsidGroup.getValue(), ppDacl.getValue(), ppDacl.getValue()));
            } finally {
                file.delete();
            }
        } finally {
            Kernel32Util.freeLocalMemory(ppSecurityDescriptor.getValue());
        }
        if (impersontating) {
            assertTrue("SetThreadToken", Advapi32.INSTANCE.SetThreadToken(null, null));
        } else {
            tp.Privileges[0] = new WinNT.LUID_AND_ATTRIBUTES(pLuid, new DWORD(0));
            assertTrue("AdjustTokenPrivileges", Advapi32.INSTANCE.AdjustTokenPrivileges(tokenAdjust, false, tp, 0, null, null));
        }
    } finally {
        Kernel32Util.closeHandleRefs(phToken, phTokenDuplicate);
    }
}
Also used : TOKEN_PRIVILEGES(com.sun.jna.platform.win32.WinNT.TOKEN_PRIVILEGES) TOKEN_PRIVILEGES(com.sun.jna.platform.win32.WinNT.TOKEN_PRIVILEGES) DWORD(com.sun.jna.platform.win32.WinDef.DWORD) PointerByReference(com.sun.jna.ptr.PointerByReference) HANDLEByReference(com.sun.jna.platform.win32.WinNT.HANDLEByReference) SC_HANDLE(com.sun.jna.platform.win32.Winsvc.SC_HANDLE) HANDLE(com.sun.jna.platform.win32.WinNT.HANDLE) File(java.io.File)

Aggregations

DWORD (com.sun.jna.platform.win32.WinDef.DWORD)56 PointerByReference (com.sun.jna.ptr.PointerByReference)23 HANDLE (com.sun.jna.platform.win32.WinNT.HANDLE)17 File (java.io.File)17 HRESULT (com.sun.jna.platform.win32.WinNT.HRESULT)16 Test (org.junit.Test)15 DWORDByReference (com.sun.jna.platform.win32.WinDef.DWORDByReference)11 Memory (com.sun.jna.Memory)9 ULONG (com.sun.jna.platform.win32.WinDef.ULONG)9 ULONGByReference (com.sun.jna.platform.win32.WinDef.ULONGByReference)7 HANDLEByReference (com.sun.jna.platform.win32.WinNT.HANDLEByReference)7 GENERIC_MAPPING (com.sun.jna.platform.win32.WinNT.GENERIC_MAPPING)6 UINT_PTR (com.sun.jna.platform.win32.WinDef.UINT_PTR)5 TOKEN_PRIVILEGES (com.sun.jna.platform.win32.WinNT.TOKEN_PRIVILEGES)5 IntByReference (com.sun.jna.ptr.IntByReference)5 APPBARDATA (com.sun.jna.platform.win32.ShellAPI.APPBARDATA)4 WinNT (com.sun.jna.platform.win32.WinNT)4 Pointer (com.sun.jna.Pointer)3 SC_HANDLE (com.sun.jna.platform.win32.Winsvc.SC_HANDLE)3 BufferedWriter (java.io.BufferedWriter)3