Search in sources :

Example 1 with EventLogRecord

use of com.sun.jna.platform.win32.Advapi32Util.EventLogRecord 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 2 with EventLogRecord

use of com.sun.jna.platform.win32.Advapi32Util.EventLogRecord in project jna by java-native-access.

the class Advapi32UtilTest method testEventLogIteratorBackwards.

public void testEventLogIteratorBackwards() {
    EventLogIterator iter = new EventLogIterator(null, "Application", WinNT.EVENTLOG_BACKWARDS_READ);
    try {
        int max = 100;
        int lastId = -1;
        while (iter.hasNext()) {
            EventLogRecord record = iter.next();
            /*
                  System.out.println(record.getRecordNumber()
                  + ": Event ID: " + record.getEventId()
                  + ", Event Type: " + record.getType()
                  + ", Event Source: " + record.getSource());
                */
            assertTrue(record.getRecordNumber() < lastId || lastId == -1);
            lastId = record.getRecordNumber();
            if (max-- <= 0) {
                // shorten test
                break;
            }
        }
    } finally {
        iter.close();
    }
}
Also used : EventLogIterator(com.sun.jna.platform.win32.Advapi32Util.EventLogIterator) EventLogRecord(com.sun.jna.platform.win32.Advapi32Util.EventLogRecord)

Example 3 with EventLogRecord

use of com.sun.jna.platform.win32.Advapi32Util.EventLogRecord in project jna by java-native-access.

the class Advapi32UtilTest method testEventLogIteratorForwards.

public void testEventLogIteratorForwards() {
    EventLogIterator iter = new EventLogIterator("Application");
    try {
        int max = 100;
        int lastId = 0;
        while (iter.hasNext()) {
            EventLogRecord record = iter.next();
            assertTrue(record.getRecordNumber() > lastId);
            lastId = record.getRecordNumber();
            assertNotNull(record.getType().name());
            assertNotNull(record.getSource());
            if (record.getRecord().DataLength.intValue() > 0) {
                assertEquals(record.getData().length, record.getRecord().DataLength.intValue());
            } else {
                assertNull(record.getData());
            }
            if (record.getRecord().NumStrings.intValue() > 0) {
                assertEquals(record.getStrings().length, record.getRecord().NumStrings.intValue());
            } else {
                assertNull(record.getStrings());
            }
            if (max-- <= 0) {
                // shorten test
                break;
            }
        /*
                  System.out.println(record.getRecordNumber()
                  + ": Event ID: " + record.getEventId()
                  + ", Event Type: " + record.getType()
                  + ", Event Source: " + record.getSource());
                */
        }
    } finally {
        iter.close();
    }
}
Also used : EventLogIterator(com.sun.jna.platform.win32.Advapi32Util.EventLogIterator) EventLogRecord(com.sun.jna.platform.win32.Advapi32Util.EventLogRecord)

Aggregations

EventLogIterator (com.sun.jna.platform.win32.Advapi32Util.EventLogIterator)2 EventLogRecord (com.sun.jna.platform.win32.Advapi32Util.EventLogRecord)2 Memory (com.sun.jna.Memory)1 Pointer (com.sun.jna.Pointer)1 EVENTLOGRECORD (com.sun.jna.platform.win32.WinNT.EVENTLOGRECORD)1 HANDLE (com.sun.jna.platform.win32.WinNT.HANDLE)1 SC_HANDLE (com.sun.jna.platform.win32.Winsvc.SC_HANDLE)1 IntByReference (com.sun.jna.ptr.IntByReference)1