use of com.sun.jna.Memory in project jna by java-native-access.
the class Advapi32Test method testGetAce.
public void testGetAce() 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 += 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));
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));
}
use of com.sun.jna.Memory in project jna by java-native-access.
the class Advapi32Test method testReadEventLogEntries.
public void testReadEventLogEntries() {
HANDLE h = Advapi32.INSTANCE.OpenEventLog(null, "Application");
IntByReference pnBytesRead = new IntByReference();
IntByReference pnMinNumberOfBytesNeeded = new IntByReference();
Memory buffer = new Memory(1024 * 64);
// shorten test, avoid iterating through all events
int maxReads = 3;
int rc = 0;
while (true) {
if (maxReads-- <= 0)
break;
if (!Advapi32.INSTANCE.ReadEventLog(h, WinNT.EVENTLOG_SEQUENTIAL_READ | WinNT.EVENTLOG_FORWARDS_READ, 0, buffer, (int) buffer.size(), pnBytesRead, pnMinNumberOfBytesNeeded)) {
rc = Kernel32.INSTANCE.GetLastError();
if (rc == W32Errors.ERROR_INSUFFICIENT_BUFFER) {
buffer = new Memory(pnMinNumberOfBytesNeeded.getValue());
rc = 0;
continue;
}
break;
}
int dwRead = pnBytesRead.getValue();
Pointer pevlr = buffer;
int maxRecords = 3;
while (dwRead > 0 && maxRecords-- > 0) {
EVENTLOGRECORD record = new EVENTLOGRECORD(pevlr);
/*
System.out.println(record.RecordNumber.intValue()
+ " Event ID: " + record.EventID.intValue()
+ " Event Type: " + record.EventType.intValue()
+ " Event Source: " + pevlr.getString(record.size(), true));
*/
dwRead -= record.Length.intValue();
pevlr = pevlr.share(record.Length.intValue());
}
}
assertTrue("Unexpected error after reading event log: " + new Win32Exception(rc), rc == W32Errors.ERROR_HANDLE_EOF || rc == 0);
assertTrue("Error closing event log", Advapi32.INSTANCE.CloseEventLog(h));
}
use of com.sun.jna.Memory in project jna by java-native-access.
the class Advapi32Test method testAddAccessAllowedAce.
public void testAddAccessAllowedAce() 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);
assertTrue(Advapi32.INSTANCE.InitializeAcl(pAcl, cbAcl, WinNT.ACL_REVISION));
assertTrue(Advapi32.INSTANCE.AddAccessAllowedAce(pAcl, WinNT.ACL_REVISION, WinNT.STANDARD_RIGHTS_ALL, pSid));
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));
}
use of com.sun.jna.Memory in project jna by java-native-access.
the class Advapi32Test method testLookupAccountName.
public void testLookupAccountName() {
IntByReference pSid = new IntByReference(0);
IntByReference pDomain = new IntByReference(0);
PointerByReference peUse = new PointerByReference();
String accountName = "Administrator";
assertFalse(Advapi32.INSTANCE.LookupAccountName(null, accountName, null, pSid, null, pDomain, peUse));
assertEquals(W32Errors.ERROR_INSUFFICIENT_BUFFER, Kernel32.INSTANCE.GetLastError());
assertTrue(pSid.getValue() > 0);
Memory sidMemory = new Memory(pSid.getValue());
PSID pSidMemory = new PSID(sidMemory);
char[] referencedDomainName = new char[pDomain.getValue() + 1];
assertTrue(Advapi32.INSTANCE.LookupAccountName(null, accountName, pSidMemory, pSid, referencedDomainName, pDomain, peUse));
assertEquals(SID_NAME_USE.SidTypeUser, peUse.getPointer().getInt(0));
assertTrue(Native.toString(referencedDomainName).length() > 0);
}
use of com.sun.jna.Memory in project jna by java-native-access.
the class Advapi32Test method testReportEvent.
public void testReportEvent() {
String applicationEventLog = "SYSTEM\\CurrentControlSet\\Services\\EventLog\\Application";
String jnaEventSource = "JNADevEventSource";
String jnaEventSourceRegistryPath = applicationEventLog + "\\" + jnaEventSource;
// ignore test if not able to create key (need to be administrator to do this).
try {
final boolean keyCreated = Advapi32Util.registryCreateKey(WinReg.HKEY_LOCAL_MACHINE, jnaEventSourceRegistryPath);
if (!keyCreated) {
return;
}
} catch (Win32Exception e) {
return;
}
HANDLE h = Advapi32.INSTANCE.RegisterEventSource(null, jnaEventSource);
IntByReference before = new IntByReference();
assertTrue(Advapi32.INSTANCE.GetNumberOfEventLogRecords(h, before));
assertNotNull(h);
String[] s = { "JNA", "Event" };
Memory m = new Memory(4);
m.setByte(0, (byte) 1);
m.setByte(1, (byte) 2);
m.setByte(2, (byte) 3);
m.setByte(3, (byte) 4);
assertTrue(Advapi32.INSTANCE.ReportEvent(h, WinNT.EVENTLOG_ERROR_TYPE, 0, 0, null, 2, 4, s, m));
IntByReference after = new IntByReference();
assertTrue(Advapi32.INSTANCE.GetNumberOfEventLogRecords(h, after));
assertTrue(before.getValue() < after.getValue());
assertFalse(h.equals(WinBase.INVALID_HANDLE_VALUE));
assertTrue(Advapi32.INSTANCE.DeregisterEventSource(h));
Advapi32Util.registryDeleteKey(WinReg.HKEY_LOCAL_MACHINE, jnaEventSourceRegistryPath);
}
Aggregations