[SIGAR-80] Fix EventLog.message to handle multiple EventMessageFiles

This commit is contained in:
Doug MacEachern 2007-12-14 21:15:36 +00:00
parent f8a4710385
commit 23aa02766c
1 changed files with 43 additions and 33 deletions

View File

@ -88,55 +88,65 @@ static int get_messagefile_dll(const char *app, char *source, char *dllfile)
static int get_formatted_message(EVENTLOGRECORD *pevlr, char *dllfile, static int get_formatted_message(EVENTLOGRECORD *pevlr, char *dllfile,
char *msg) char *msg)
{ {
HINSTANCE hlib; LPVOID msgbuf = NULL;
LPVOID msgbuf;
char msgdll[MAX_MSG_LENGTH]; char msgdll[MAX_MSG_LENGTH];
char *insert_strs[56], *ch; char *insert_strs[56], *ptr;
int i, max = sizeof(insert_strs) / sizeof(char *); int i, max = sizeof(insert_strs) / sizeof(char *);
DWORD result; const DWORD flags =
FORMAT_MESSAGE_FROM_HMODULE |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_ARGUMENT_ARRAY |
FORMAT_MESSAGE_MAX_WIDTH_MASK;
if (!ExpandEnvironmentStrings(dllfile, msgdll, MAX_PATH)) if (!ExpandEnvironmentStrings(dllfile, msgdll, sizeof(msgdll))) {
return GetLastError();
if (!(hlib = LoadLibraryEx(msgdll, NULL,
LOAD_LIBRARY_AS_DATAFILE)))
return GetLastError(); return GetLastError();
}
memset(insert_strs, '\0', sizeof(insert_strs)); memset(insert_strs, '\0', sizeof(insert_strs));
ch = (char *)((LPBYTE)pevlr + pevlr->StringOffset); ptr = (char *)((LPBYTE)pevlr + pevlr->StringOffset);
for (i = 0; i < pevlr->NumStrings && i < max; i++) { for (i = 0; i < pevlr->NumStrings && i < max; i++) {
insert_strs[i] = ch; insert_strs[i] = ptr;
if (ch == NULL) { ptr += strlen(ptr) + 1;
break;
}
ch += strlen(ch) + 1;
} }
result = ptr = msgdll;
FormatMessage(FORMAT_MESSAGE_FROM_HMODULE | while (ptr) {
FORMAT_MESSAGE_FROM_SYSTEM | HINSTANCE hlib;
FORMAT_MESSAGE_ALLOCATE_BUFFER | char *delim = strchr(ptr, ';');
FORMAT_MESSAGE_ARGUMENT_ARRAY,
hlib,
pevlr->EventID,
MAKELANGID(LANG_NEUTRAL, SUBLANG_ENGLISH_US),
(LPTSTR) &msgbuf,
MAX_MSG_LENGTH,
insert_strs);
if (result) { if (delim) {
*delim++ = '\0';
}
hlib = LoadLibraryEx(ptr, NULL,
LOAD_LIBRARY_AS_DATAFILE);
if (hlib) {
FormatMessage(flags,
hlib,
pevlr->EventID,
MAKELANGID(LANG_NEUTRAL, SUBLANG_ENGLISH_US),
(LPTSTR) &msgbuf,
sizeof(msgbuf),
insert_strs);
FreeLibrary(hlib);
if (msgbuf) {
break;
}
}
ptr = delim;
}
if (msgbuf) {
strncpy(msg, msgbuf, MAX_MSG_LENGTH); strncpy(msg, msgbuf, MAX_MSG_LENGTH);
msg[MAX_MSG_LENGTH-1] = '\0'; msg[MAX_MSG_LENGTH-1] = '\0';
result = ERROR_SUCCESS;
LocalFree(msgbuf); LocalFree(msgbuf);
return ERROR_SUCCESS;
} }
else { else {
result = GetLastError(); return !ERROR_SUCCESS;
} }
FreeLibrary(hlib);
return result;
} }
JNIEXPORT void SIGAR_JNI(win32_EventLog_openlog) JNIEXPORT void SIGAR_JNI(win32_EventLog_openlog)