some fixes for EventLogRecord.stringData

This commit is contained in:
Doug MacEachern 2005-09-22 17:10:27 +00:00
parent e65b3f3068
commit c28ab5253a
3 changed files with 26 additions and 20 deletions

View File

@ -3,7 +3,7 @@
#include "win32bindings.h" #include "win32bindings.h"
#define MAX_INSERT_STRS 8 #define MAX_INSERT_STRS 8
#define MAX_MSG_LENGTH 4096 #define MAX_MSG_LENGTH 8192
#define MAX_ERROR_LENGTH 1024 #define MAX_ERROR_LENGTH 1024
#define REG_MSGFILE_ROOT "SYSTEM\\CurrentControlSet\\Services\\EventLog\\" #define REG_MSGFILE_ROOT "SYSTEM\\CurrentControlSet\\Services\\EventLog\\"
@ -40,29 +40,31 @@ static HANDLE win32_get_pointer(JNIEnv *env, jobject obj)
return h; return h;
} }
static int get_messagefile_dll(char *app, char *source, char *dllfile) static int get_messagefile_dll(const char *app, char *source, char *dllfile)
{ {
HKEY hk; HKEY hk;
DWORD type, data; DWORD type, data;
char buf[MAX_MSG_LENGTH]; char buf[MAX_MSG_LENGTH];
LONG rc;
sprintf(buf, "%s%s\\%s", REG_MSGFILE_ROOT, app, source); sprintf(buf, "%s%s\\%s", REG_MSGFILE_ROOT, app, source);
rc = RegOpenKey(HKEY_LOCAL_MACHINE, buf, &hk);
if (RegOpenKey(HKEY_LOCAL_MACHINE, buf, &hk)) { if (rc) {
return GetLastError(); return rc;
} }
if (RegQueryValueEx(hk, "EventMessageFile", NULL, &type, rc = RegQueryValueEx(hk, "EventMessageFile", NULL, &type,
(UCHAR *)buf, &data)) { (UCHAR *)buf, &data);
if (rc) {
RegCloseKey(hk); RegCloseKey(hk);
return GetLastError(); return rc;
} }
strncpy(dllfile, buf, sizeof(dllfile)); strncpy(dllfile, buf, sizeof(dllfile));
RegCloseKey(hk); RegCloseKey(hk);
return 0; return ERROR_SUCCESS;
} }
static int get_formatted_message(EVENTLOGRECORD *pevlr, char *dllfile, static int get_formatted_message(EVENTLOGRECORD *pevlr, char *dllfile,
@ -163,11 +165,12 @@ JNIEXPORT jint SIGAR_JNI(win32_EventLog_getOldestRecord)
} }
JNIEXPORT jobject SIGAR_JNI(win32_EventLog_read) JNIEXPORT jobject SIGAR_JNI(win32_EventLog_read)
(JNIEnv *env, jobject obj, jint recordOffset) (JNIEnv *env, jobject obj, jstring jname, jint recordOffset)
{ {
EVENTLOGRECORD *pevlr; EVENTLOGRECORD *pevlr;
BYTE buffer[8192]; BYTE buffer[8192];
char dllfile[1024]; char dllfile[1024];
char msg[MAX_MSG_LENGTH];
DWORD dwRead, dwNeeded; DWORD dwRead, dwNeeded;
LPSTR source, machineName; LPSTR source, machineName;
HANDLE h; HANDLE h;
@ -175,6 +178,7 @@ JNIEXPORT jobject SIGAR_JNI(win32_EventLog_read)
jclass cls = WIN32_FIND_CLASS("EventLogRecord"); jclass cls = WIN32_FIND_CLASS("EventLogRecord");
jobject eventObj; /* Actual instance of the EventLogRecord */ jobject eventObj; /* Actual instance of the EventLogRecord */
jfieldID id; jfieldID id;
const char *name;
h = win32_get_pointer(env, obj); h = win32_get_pointer(env, obj);
@ -225,21 +229,22 @@ JNIEXPORT jobject SIGAR_JNI(win32_EventLog_read)
source = (LPSTR)((LPBYTE)pevlr + sizeof(EVENTLOGRECORD)); source = (LPSTR)((LPBYTE)pevlr + sizeof(EVENTLOGRECORD));
SetStringField(env, eventObj, id, source); SetStringField(env, eventObj, id, source);
/* Get the formatted message */ name = JENV->GetStringUTFChars(env, jname, 0);
if (!get_messagefile_dll("Application", source, dllfile)) {
char msg[MAX_MSG_LENGTH];
if (!get_formatted_message(pevlr, dllfile, msg)) {
/* Get the formatted message */
if ((get_messagefile_dll(name, source, dllfile) == ERROR_SUCCESS) &&
(get_formatted_message(pevlr, dllfile, msg) == ERROR_SUCCESS))
{
id = JENV->GetFieldID(env, cls, "stringData", id = JENV->GetFieldID(env, cls, "stringData",
"Ljava/lang/String;"); "Ljava/lang/String;");
SetStringField(env, eventObj, id, msg); SetStringField(env, eventObj, id, msg);
}
} else if (pevlr->StringOffset > 0) { } else if (pevlr->StringOffset > 0) {
/* Work around some applications not using a message file */ /* Work around some applications not using a message file */
char *tmp = (LPSTR)((LPBYTE)pevlr + pevlr->StringOffset); char *tmp = (LPSTR)((LPBYTE)pevlr + pevlr->StringOffset);
id = JENV->GetFieldID(env, cls, "stringData", "Ljava/lang/String;"); id = JENV->GetFieldID(env, cls, "stringData", "Ljava/lang/String;");
SetStringField(env, eventObj, id, tmp); SetStringField(env, eventObj, id, tmp);
} }
JENV->ReleaseStringUTFChars(env, jname, name);
/* Increment up to the machine name. */ /* Increment up to the machine name. */
id = JENV->GetFieldID(env, cls, "computerName", "Ljava/lang/String;"); id = JENV->GetFieldID(env, cls, "computerName", "Ljava/lang/String;");

View File

@ -65,12 +65,13 @@ public class EventLog extends Win32 {
* EVENTLOG_SEEK_READ flag, no sequential reading is currently * EVENTLOG_SEEK_READ flag, no sequential reading is currently
* supported. * supported.
* *
* @param logName The event log name
* @param recordOffset The record offset to read. * @param recordOffset The record offset to read.
* @exception Win32Exception If the event log is not open, or * @exception Win32Exception If the event log is not open, or
* if the specified record could not be * if the specified record could not be
* found * found
*/ */
public native EventLogRecord read(int recordOffset) public native EventLogRecord read(String logName, int recordOffset)
throws Win32Exception; throws Win32Exception;
/** /**

View File

@ -101,7 +101,7 @@ public class EventLogThread implements Runnable {
EventLogRecord record; EventLogRecord record;
try { try {
record = log.read(i); record = log.read(this.logName, i);
} catch (Win32Exception e) { } catch (Win32Exception e) {
logger.error("Unable to read event id " + i + ": " + e); logger.error("Unable to read event id " + i + ": " + e);
continue; continue;