use of com.sun.jna.platform.win32.Advapi32 in project jna by java-native-access.
the class Advapi32Test method testFileEncryptionStatus.
public void testFileEncryptionStatus() throws Exception {
DWORDByReference lpStatus = new DWORDByReference();
// create a temp file
File file = createTempFile();
String lpFileName = file.getAbsolutePath();
// unencrypted file
assertTrue(Advapi32.INSTANCE.FileEncryptionStatus(lpFileName, lpStatus));
assertEquals(FILE_ENCRYPTABLE, lpStatus.getValue().intValue());
// read only file
file.setWritable(false);
assertTrue(Advapi32.INSTANCE.FileEncryptionStatus(lpFileName, lpStatus));
assertEquals(FILE_READ_ONLY, lpStatus.getValue().intValue());
// encrypted file
file.setWritable(true);
assertTrue(Advapi32.INSTANCE.EncryptFile(lpFileName));
assertTrue(Advapi32.INSTANCE.FileEncryptionStatus(lpFileName, lpStatus));
assertEquals(FILE_IS_ENCRYPTED, lpStatus.getValue().intValue());
file.delete();
}
use of com.sun.jna.platform.win32.Advapi32 in project jna by java-native-access.
the class Advapi32Test method testOpenProcessToken.
public void testOpenProcessToken() {
HANDLEByReference phToken = new HANDLEByReference();
try {
HANDLE processHandle = Kernel32.INSTANCE.GetCurrentProcess();
assertTrue(Advapi32.INSTANCE.OpenProcessToken(processHandle, WinNT.TOKEN_DUPLICATE | WinNT.TOKEN_QUERY, phToken));
} finally {
Kernel32Util.closeHandleRef(phToken);
}
}
use of com.sun.jna.platform.win32.Advapi32 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));
}
use of com.sun.jna.platform.win32.Advapi32 in project jna by java-native-access.
the class Advapi32Test method testOpenSCManager.
public void testOpenSCManager() {
SC_HANDLE handle = Advapi32.INSTANCE.OpenSCManager(null, null, Winsvc.SC_MANAGER_CONNECT);
assertNotNull(handle);
assertTrue(Advapi32.INSTANCE.CloseServiceHandle(handle));
assertNull(Advapi32.INSTANCE.OpenSCManager("invalidMachineName", null, Winsvc.SC_MANAGER_CONNECT));
int err = Kernel32.INSTANCE.GetLastError();
assertTrue("Unexpected error in OpenSCManager: " + err, err == W32Errors.RPC_S_SERVER_UNAVAILABLE || err == W32Errors.RPC_S_INVALID_NET_ADDR);
assertNull(Advapi32.INSTANCE.OpenSCManager(null, "invalidDatabase", Winsvc.SC_MANAGER_CONNECT));
assertEquals(W32Errors.ERROR_INVALID_NAME, Kernel32.INSTANCE.GetLastError());
}
use of com.sun.jna.platform.win32.Advapi32 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);
}
Aggregations