⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 eventlog.java

📁 java 调用windows的api
💻 JAVA
字号:
//******************************************************************
// Released under the DevelopMentor OpenSource Software License.
// Please consult the LICENSE file in the project root directory,
// or at http://www.develop.com for details before using this
// software.
//******************************************************************

package org.jawin.donated.win32;

import java.io.IOException;

import org.jawin.COMError;
import org.jawin.COMException;
import org.jawin.FuncPtr;
import org.jawin.ReturnFlags;
import org.jawin.io.LittleEndianOutputStream;
import org.jawin.io.NakedByteStream;
import org.jawin.marshal.StructConverter;

public class EventLog {
	static private final FuncPtr fpRES;
	static private final FuncPtr fpDES;
	static private final FuncPtr fpRE;
	static private final FuncPtr fpCEL;
	static private final FuncPtr fpOEL;
	static private final FuncPtr fpGOELR;
	static private final FuncPtr fpGNOELR;
	static private final FuncPtr fpREL;

	static {
		try {
			fpRES = new FuncPtr("ADVAPI32.DLL", "RegisterEventSourceW");
			fpDES = new FuncPtr("ADVAPI32.DLL", "DeregisterEventSource");
			fpRE = new FuncPtr("ADVAPI32.DLL", "ReportEventW");
			fpOEL = new FuncPtr("ADVAPI32.DLL", "OpenEventLogW");
			fpCEL = new FuncPtr("ADVAPI32.DLL", "CloseEventLog");
			fpGOELR = new FuncPtr("ADVAPI32.DLL", "GetOldestEventLogRecord");
			fpGNOELR = new FuncPtr("ADVAPI32.DLL", "GetNumberOfEventLogRecords");
			fpREL = new FuncPtr("ADVAPI32.DLL", "ReadEventLogW");
		} catch (COMException ce) {
			throw new COMError("Unable to load event log entry points");
		}
	}

	public static int RegisterEventSource(String serverName, String sourceName) throws COMException, IOException {
		return fpRES.invoke_I(serverName, sourceName, ReturnFlags.CHECK_FALSE);
	}

	public static int OpenEventLog(String serverName, String sourceName) throws COMException, IOException {
		return fpOEL.invoke_I(serverName, sourceName, ReturnFlags.CHECK_FALSE);
	}

	public static int DeregisterEventSource(int handle) throws COMException, IOException {
		return fpDES.invoke_I(handle, ReturnFlags.CHECK_FALSE);
	}

	public static int CloseEventLog(int handle) throws COMException, IOException {
		return fpCEL.invoke_I(handle, ReturnFlags.CHECK_FALSE);
	}
	
	public static int GetOldestEventLogRecord(int handle) throws COMException {
		return fpGOELR.invoke_OI(handle, ReturnFlags.CHECK_FALSE);
	}

	public static int GetNumberOfEventLogRecords(int handle) throws COMException {
		return fpGNOELR.invoke_OI(handle, ReturnFlags.CHECK_FALSE);
	}
	
	public static int ReportEvent(int log, int type, int category, int id, String event) throws COMException, IOException {
		NakedByteStream nbs = new NakedByteStream();
		LittleEndianOutputStream leos = new LittleEndianOutputStream(nbs);
		//top level args
		leos.writeInt(log);
		leos.writeInt(type);
		leos.writeInt(category);
		leos.writeInt(id);
		leos.writeInt(0);
		leos.writeInt(1); //number of Strings in array
		leos.writeInt(0);
		leos.writeInt(0); //offset of String array	
		leos.writeInt(0);
		Object[] strings = new Object[] { new String[] { event } };
		byte[] res = fpRE.invoke("B28T4I:T1:", 36,
				nbs, strings, ReturnFlags.CHECK_FALSE);
		return StructConverter.bytesIntoInt(res, 0);
	}

	/*
	 * HANDLE hEventLog,                // handle to event log
	 * DWORD dwReadFlags,               // how to read log
	 * DWORD dwRecordOffset,            // offset of first record
	 * LPVOID lpBuffer,                 // buffer for read data
	 * DWORD nNumberOfBytesToRead,      // bytes to read
	 * DWORD *pnBytesRead,              // number of bytes read
	 * DWORD *pnMinNumberOfBytesNeeded  // bytes required
	 */
	public static byte[] RawReadEventLog(int handle, int flags, int offset) throws IOException, COMException {
		NakedByteStream nbs = new NakedByteStream();
		LittleEndianOutputStream leos = new LittleEndianOutputStream(nbs);
		//top level args
		leos.writeInt(handle);
		leos.writeInt(flags);
		leos.writeInt(offset);
		byte[] sizeRes = fpREL.invoke("IIIAkAA:T8:L24n4", 28,
				nbs, null, ReturnFlags.CHECK_NONE); // to get the buffer size
		
		int size = StructConverter.bytesIntoInt(sizeRes, 0);
		leos.writeInt(size);
		byte[] res = fpREL.invoke("IIIM" + size + "IAA:T3:L12n" + size,
				28, nbs, null, ReturnFlags.CHECK_FALSE);
		return res;
	}

	public static EVENTLOGRECORD ReadEventLog(int handle, int flags, int offset) throws IOException, COMException {
		byte[] rawRecord = RawReadEventLog(handle, flags, offset);
		return new EVENTLOGRECORD(rawRecord);
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -