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));
}
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);
}
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);
}
}
}
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);
}
}
}
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));
}
Aggregations