Search in sources :

Example 1 with Memory

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));
}
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 2 with Memory

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));
}
Also used : IntByReference(com.sun.jna.ptr.IntByReference) Memory(com.sun.jna.Memory) Pointer(com.sun.jna.Pointer) SC_HANDLE(com.sun.jna.platform.win32.Winsvc.SC_HANDLE) HANDLE(com.sun.jna.platform.win32.WinNT.HANDLE) EVENTLOGRECORD(com.sun.jna.platform.win32.WinNT.EVENTLOGRECORD)

Example 3 with Memory

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));
}
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 4 with Memory

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);
}
Also used : IntByReference(com.sun.jna.ptr.IntByReference) Memory(com.sun.jna.Memory) PointerByReference(com.sun.jna.ptr.PointerByReference) PSID(com.sun.jna.platform.win32.WinNT.PSID)

Example 5 with Memory

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);
}
Also used : IntByReference(com.sun.jna.ptr.IntByReference) Memory(com.sun.jna.Memory) SC_HANDLE(com.sun.jna.platform.win32.Winsvc.SC_HANDLE) HANDLE(com.sun.jna.platform.win32.WinNT.HANDLE)

Aggregations

Memory (com.sun.jna.Memory)64 IntByReference (com.sun.jna.ptr.IntByReference)33 HANDLE (com.sun.jna.platform.win32.WinNT.HANDLE)17 Pointer (com.sun.jna.Pointer)15 File (java.io.File)15 DWORD (com.sun.jna.platform.win32.WinDef.DWORD)9 PointerByReference (com.sun.jna.ptr.PointerByReference)8 DWORDByReference (com.sun.jna.platform.win32.WinDef.DWORDByReference)6 PSID (com.sun.jna.platform.win32.WinNT.PSID)6 Test (org.junit.Test)6 HDDEDATA (com.sun.jna.platform.win32.Ddeml.HDDEDATA)5 HSZ (com.sun.jna.platform.win32.Ddeml.HSZ)5 ACL (com.sun.jna.platform.win32.WinNT.ACL)5 EVT_HANDLE (com.sun.jna.platform.win32.Winevt.EVT_HANDLE)5 ACCESS_ALLOWED_ACE (com.sun.jna.platform.win32.WinNT.ACCESS_ALLOWED_ACE)4 HCONV (com.sun.jna.platform.win32.Ddeml.HCONV)3 ConnectHandler (com.sun.jna.platform.win32.DdemlUtil.ConnectHandler)3 IDdeConnection (com.sun.jna.platform.win32.DdemlUtil.IDdeConnection)3 StandaloneDdeClient (com.sun.jna.platform.win32.DdemlUtil.StandaloneDdeClient)3 WIN32_FIND_DATA (com.sun.jna.platform.win32.WinBase.WIN32_FIND_DATA)3