Search in sources :

Example 1 with Win32Exception

use of com.sun.jna.platform.win32.Win32Exception 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 Win32Exception

use of com.sun.jna.platform.win32.Win32Exception 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)

Example 3 with Win32Exception

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

the class WevtapiTest method testEvtOpenPublisherEnum.

public void testEvtOpenPublisherEnum() throws Exception {
    Winevt.EVT_RPC_LOGIN login = new Winevt.EVT_RPC_LOGIN("localhost", null, null, null, Winevt.EVT_RPC_LOGIN_FLAGS.EvtRpcLoginAuthDefault);
    EVT_HANDLE session = null;
    EVT_HANDLE publisherEnumHandle = null;
    List<String> publisherList = new ArrayList<String>();
    try {
        session = Wevtapi.INSTANCE.EvtOpenSession(Winevt.EVT_LOGIN_CLASS.EvtRpcLogin, login, 0, 0);
        if (session == null) {
            throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
        }
        publisherEnumHandle = Wevtapi.INSTANCE.EvtOpenPublisherEnum(session, 0);
        if (publisherEnumHandle == null) {
            throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
        }
        String providerName;
        while (true) {
            try {
                providerName = WevtapiUtil.EvtNextPublisherId(publisherEnumHandle);
            } catch (Win32Exception e) {
                if (e.getErrorCode() == WinError.ERROR_NO_MORE_ITEMS) {
                    break;
                } else {
                    throw e;
                }
            }
            publisherList.add(providerName);
        }
        assertThat(publisherList.size() > 0, is(true));
    } finally {
        if (publisherEnumHandle != null) {
            Wevtapi.INSTANCE.EvtClose(publisherEnumHandle);
        }
        if (session != null) {
            Wevtapi.INSTANCE.EvtClose(session);
        }
    }
}
Also used : EVT_HANDLE(com.sun.jna.platform.win32.Winevt.EVT_HANDLE) ArrayList(java.util.ArrayList)

Example 4 with Win32Exception

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

the class WevtapiTest method testEvtOpenChannelEnum.

public void testEvtOpenChannelEnum() throws Exception {
    EVT_HANDLE channelHandle = null;
    List<String> channelList = new ArrayList<String>();
    try {
        channelHandle = Wevtapi.INSTANCE.EvtOpenChannelEnum(null, 0);
        if (channelHandle == null) {
            throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
        }
        char[] buff = new char[1024];
        IntByReference buffUsed = new IntByReference();
        while (true) {
            if (!Wevtapi.INSTANCE.EvtNextChannelPath(channelHandle, buff.length, buff, buffUsed)) {
                if (Kernel32.INSTANCE.GetLastError() == WinError.ERROR_NO_MORE_ITEMS) {
                    break;
                } else if (Kernel32.INSTANCE.GetLastError() == WinError.ERROR_INSUFFICIENT_BUFFER) {
                    buff = new char[buffUsed.getValue()];
                    if (!Wevtapi.INSTANCE.EvtNextChannelPath(channelHandle, buff.length, buff, buffUsed)) {
                        throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
                    }
                }
            }
            channelList.add(Native.toString(buff));
        }
        assertThat(channelList.size() > 0, is(true));
    } finally {
        if (channelHandle != null) {
            Wevtapi.INSTANCE.EvtClose(channelHandle);
        }
    }
}
Also used : IntByReference(com.sun.jna.ptr.IntByReference) EVT_HANDLE(com.sun.jna.platform.win32.Winevt.EVT_HANDLE) ArrayList(java.util.ArrayList)

Example 5 with Win32Exception

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

the class WevtapiTest method testEvtOpenLog.

public void testEvtOpenLog() throws Exception {
    File testEvtx = new File(getClass().getResource("/res/WevtapiTest.sample1.evtx").toURI());
    EVT_HANDLE logHandle = Wevtapi.INSTANCE.EvtOpenLog(null, testEvtx.getAbsolutePath(), Winevt.EVT_OPEN_LOG_FLAGS.EvtOpenFilePath);
    if (logHandle == null) {
        throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
    }
    Memory buff = new Memory(1024);
    IntByReference buffUsed = new IntByReference();
    if (!Wevtapi.INSTANCE.EvtGetLogInfo(logHandle, Winevt.EVT_LOG_PROPERTY_ID.EvtLogFileSize, (int) buff.size(), buff, buffUsed)) {
        if (Kernel32.INSTANCE.GetLastError() == WinError.ERROR_INSUFFICIENT_BUFFER) {
            buff = new Memory(buffUsed.getValue());
            if (!Wevtapi.INSTANCE.EvtGetLogInfo(logHandle, Winevt.EVT_LOG_PROPERTY_ID.EvtLogFileSize, (int) buff.size(), buff, buffUsed)) {
                throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
            }
        } else {
            throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
        }
    }
    assertThat(buff.getLong(0), is(69632L));
}
Also used : IntByReference(com.sun.jna.ptr.IntByReference) EVT_HANDLE(com.sun.jna.platform.win32.Winevt.EVT_HANDLE) Memory(com.sun.jna.Memory) File(java.io.File)

Aggregations

IntByReference (com.sun.jna.ptr.IntByReference)35 HKEYByReference (com.sun.jna.platform.win32.WinReg.HKEYByReference)18 HANDLE (com.sun.jna.platform.win32.WinNT.HANDLE)17 Memory (com.sun.jna.Memory)15 PointerByReference (com.sun.jna.ptr.PointerByReference)11 ArrayList (java.util.ArrayList)11 EVT_HANDLE (com.sun.jna.platform.win32.Winevt.EVT_HANDLE)7 Pointer (com.sun.jna.Pointer)6 File (java.io.File)6 HANDLEByReference (com.sun.jna.platform.win32.WinNT.HANDLEByReference)5 Test (org.junit.Test)5 DWORD (com.sun.jna.platform.win32.WinDef.DWORD)4 HRESULT (com.sun.jna.platform.win32.WinNT.HRESULT)4 PSID (com.sun.jna.platform.win32.WinNT.PSID)4 Win32Exception (com.sun.jna.platform.win32.Win32Exception)3 HMODULE (com.sun.jna.platform.win32.WinDef.HMODULE)3 LOCALGROUP_INFO_1 (com.sun.jna.platform.win32.LMAccess.LOCALGROUP_INFO_1)2 LOCALGROUP_USERS_INFO_0 (com.sun.jna.platform.win32.LMAccess.LOCALGROUP_USERS_INFO_0)2 DATA_BLOB (com.sun.jna.platform.win32.WinCrypt.DATA_BLOB)2 DWORDByReference (com.sun.jna.platform.win32.WinDef.DWORDByReference)2