Search in sources :

Example 11 with IntByReference

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

the class Advapi32Test method testLookupPrivilegeValueAndLookupPrivilegeName.

/**
     * Tests both {@link Advapi32#LookupPrivilegeValue} and {@link Advapi32#LookupPrivilegeName}
     */
public void testLookupPrivilegeValueAndLookupPrivilegeName() {
    WinNT.LUID luid = new WinNT.LUID();
    assertFalse(Advapi32.INSTANCE.LookupPrivilegeValue(null, "InvalidName", luid));
    assertEquals(Kernel32.INSTANCE.GetLastError(), W32Errors.ERROR_NO_SUCH_PRIVILEGE);
    assertTrue(Advapi32.INSTANCE.LookupPrivilegeValue(null, WinNT.SE_BACKUP_NAME, luid));
    assertTrue(luid.LowPart > 0 || luid.HighPart > 0);
    char[] lpName = new char[256];
    IntByReference cchName = new IntByReference(lpName.length);
    assertTrue(Advapi32.INSTANCE.LookupPrivilegeName(null, luid, lpName, cchName));
    assertEquals(WinNT.SE_BACKUP_NAME.length(), cchName.getValue());
    assertEquals(WinNT.SE_BACKUP_NAME, Native.toString(lpName));
}
Also used : IntByReference(com.sun.jna.ptr.IntByReference)

Example 12 with IntByReference

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

the class Advapi32Test method testRegQueryValueEx.

public void testRegQueryValueEx() {
    HKEYByReference phKey = new HKEYByReference();
    assertEquals(W32Errors.ERROR_SUCCESS, Advapi32.INSTANCE.RegOpenKeyEx(WinReg.HKEY_CURRENT_USER, "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", 0, WinNT.KEY_READ, phKey));
    IntByReference lpcbData = new IntByReference();
    IntByReference lpType = new IntByReference();
    assertEquals(W32Errors.ERROR_SUCCESS, Advapi32.INSTANCE.RegQueryValueEx(phKey.getValue(), "User Agent", 0, lpType, (char[]) null, lpcbData));
    assertEquals(WinNT.REG_SZ, lpType.getValue());
    assertTrue(lpcbData.getValue() > 0);
    char[] buffer = new char[lpcbData.getValue()];
    assertEquals(W32Errors.ERROR_SUCCESS, Advapi32.INSTANCE.RegQueryValueEx(phKey.getValue(), "User Agent", 0, lpType, buffer, lpcbData));
    assertEquals(W32Errors.ERROR_SUCCESS, Advapi32.INSTANCE.RegCloseKey(phKey.getValue()));
}
Also used : HKEYByReference(com.sun.jna.platform.win32.WinReg.HKEYByReference) IntByReference(com.sun.jna.ptr.IntByReference)

Example 13 with IntByReference

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

the class Advapi32Test method testMakeAbsoluteSD.

public void testMakeAbsoluteSD() throws Exception {
    SECURITY_DESCRIPTOR absolute = new SECURITY_DESCRIPTOR(64 * 1024);
    // Get a SD in self relative form
    int infoType = OWNER_SECURITY_INFORMATION | GROUP_SECURITY_INFORMATION | DACL_SECURITY_INFORMATION;
    PointerByReference relativeByReference = new PointerByReference();
    File file = createTempFile();
    try {
        try {
            assertEquals("GetNamedSecurityInfo(" + file + ")", Advapi32.INSTANCE.GetNamedSecurityInfo(file.getAbsolutePath(), AccCtrl.SE_OBJECT_TYPE.SE_FILE_OBJECT, infoType, null, null, null, null, relativeByReference), 0);
            SECURITY_DESCRIPTOR_RELATIVE relative = new SECURITY_DESCRIPTOR_RELATIVE(relativeByReference.getValue());
            PSID pOwner = new PSID(WinNT.SECURITY_MAX_SID_SIZE);
            PSID pGroup = new PSID(WinNT.SECURITY_MAX_SID_SIZE);
            ACL pDacl = new ACL(ACL.MAX_ACL_SIZE);
            ACL pSacl = new ACL(ACL.MAX_ACL_SIZE);
            IntByReference lpdwBufferLength = new IntByReference(absolute.size());
            IntByReference lpdwDaclSize = new IntByReference(ACL.MAX_ACL_SIZE);
            IntByReference lpdwSaclSize = new IntByReference(ACL.MAX_ACL_SIZE);
            IntByReference lpdwOwnerSize = new IntByReference(WinNT.SECURITY_MAX_SID_SIZE);
            IntByReference lpdwPrimaryGroupSize = new IntByReference(WinNT.SECURITY_MAX_SID_SIZE);
            assertTrue(Advapi32.INSTANCE.MakeAbsoluteSD(relative, absolute, lpdwBufferLength, pDacl, lpdwDaclSize, pSacl, lpdwSaclSize, pOwner, lpdwOwnerSize, pGroup, lpdwPrimaryGroupSize));
        } finally {
            file.delete();
        }
    } finally {
        Kernel32Util.freeLocalMemory(relativeByReference.getValue());
    }
}
Also used : IntByReference(com.sun.jna.ptr.IntByReference) PointerByReference(com.sun.jna.ptr.PointerByReference) SECURITY_DESCRIPTOR_RELATIVE(com.sun.jna.platform.win32.WinNT.SECURITY_DESCRIPTOR_RELATIVE) SECURITY_DESCRIPTOR(com.sun.jna.platform.win32.WinNT.SECURITY_DESCRIPTOR) ACL(com.sun.jna.platform.win32.WinNT.ACL) PSID(com.sun.jna.platform.win32.WinNT.PSID) File(java.io.File)

Example 14 with IntByReference

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

the class Advapi32Test method testGetOldestEventLogRecord.

public void testGetOldestEventLogRecord() {
    HANDLE h = Advapi32.INSTANCE.OpenEventLog(null, "Application");
    IntByReference oldestRecord = new IntByReference();
    assertTrue(Advapi32.INSTANCE.GetOldestEventLogRecord(h, oldestRecord));
    assertTrue(oldestRecord.getValue() >= 0);
    assertTrue(Advapi32.INSTANCE.CloseEventLog(h));
}
Also used : IntByReference(com.sun.jna.ptr.IntByReference) SC_HANDLE(com.sun.jna.platform.win32.Winsvc.SC_HANDLE) HANDLE(com.sun.jna.platform.win32.WinNT.HANDLE)

Example 15 with IntByReference

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

the class Advapi32Test method testSetGetSecurityDescriptorDacl.

public void testSetGetSecurityDescriptorDacl() throws IOException {
    SECURITY_DESCRIPTOR sd = new SECURITY_DESCRIPTOR(64 * 1024);
    assertTrue(Advapi32.INSTANCE.InitializeSecurityDescriptor(sd, WinNT.SECURITY_DESCRIPTOR_REVISION));
    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 += Native.getNativeSize(ACCESS_ALLOWED_ACE.class, null);
    cbAcl += (sidLength - DWORD.SIZE);
    cbAcl = Advapi32Util.alignOnDWORD(cbAcl);
    pAcl = new ACL(cbAcl);
    assertTrue(Advapi32.INSTANCE.InitializeAcl(pAcl, cbAcl, WinNT.ACL_REVISION));
    assertTrue(Advapi32.INSTANCE.AddAccessAllowedAce(pAcl, WinNT.ACL_REVISION, WinNT.STANDARD_RIGHTS_ALL, pSid));
    assertTrue(Advapi32.INSTANCE.SetSecurityDescriptorDacl(sd, true, pAcl, false));
    BOOLByReference lpbDaclPresent = new BOOLByReference();
    BOOLByReference lpbDaclDefaulted = new BOOLByReference();
    PACLByReference pDacl = new PACLByReference();
    assertTrue(Advapi32.INSTANCE.GetSecurityDescriptorDacl(sd, lpbDaclPresent, pDacl, lpbDaclDefaulted));
    ACL pAclGet = pDacl.getValue();
    assertEquals(new BOOL(true), lpbDaclPresent.getValue());
    assertEquals(new BOOL(false), lpbDaclDefaulted.getValue());
    assertEquals(1, pAclGet.AceCount);
    assertEquals(WinNT.ACL_REVISION, pAclGet.AclRevision);
}
Also used : IntByReference(com.sun.jna.ptr.IntByReference) ACCESS_ALLOWED_ACE(com.sun.jna.platform.win32.WinNT.ACCESS_ALLOWED_ACE) BOOLByReference(com.sun.jna.platform.win32.WinDef.BOOLByReference) BOOL(com.sun.jna.platform.win32.WinDef.BOOL) SECURITY_DESCRIPTOR(com.sun.jna.platform.win32.WinNT.SECURITY_DESCRIPTOR) PACLByReference(com.sun.jna.platform.win32.WinNT.PACLByReference) ACL(com.sun.jna.platform.win32.WinNT.ACL) PSID(com.sun.jna.platform.win32.WinNT.PSID)

Aggregations

IntByReference (com.sun.jna.ptr.IntByReference)199 PointerByReference (com.sun.jna.ptr.PointerByReference)38 Memory (com.sun.jna.Memory)33 HANDLE (com.sun.jna.platform.win32.WinNT.HANDLE)26 File (java.io.File)19 Pointer (com.sun.jna.Pointer)15 Test (org.junit.Test)15 ArrayList (java.util.ArrayList)14 PSID (com.sun.jna.platform.win32.WinNT.PSID)13 HANDLEByReference (com.sun.jna.platform.win32.WinNT.HANDLEByReference)11 SC_HANDLE (com.sun.jna.platform.win32.Winsvc.SC_HANDLE)11 HKEYByReference (com.sun.jna.platform.win32.WinReg.HKEYByReference)9 ACL (com.sun.jna.platform.win32.WinNT.ACL)8 Advapi32 (com.sun.jna.platform.win32.Advapi32)7 HRESULT (com.sun.jna.platform.win32.WinNT.HRESULT)7 ACCESS_ALLOWED_ACE (com.sun.jna.platform.win32.WinNT.ACCESS_ALLOWED_ACE)6 SECURITY_DESCRIPTOR (com.sun.jna.platform.win32.WinNT.SECURITY_DESCRIPTOR)6 HKEY (com.sun.jna.platform.win32.WinReg.HKEY)6 EVT_HANDLE (com.sun.jna.platform.win32.Winevt.EVT_HANDLE)6 CredHandle (com.sun.jna.platform.win32.Sspi.CredHandle)5