PLOGHANDLE ElfCreateEventLogHandle(LPCWSTR Name, BOOL Create) { PLOGHANDLE lpLogHandle; PLOGFILE currentLogFile = NULL; INT i, LogsActive; PEVENTSOURCE pEventSource; DPRINT("ElfCreateEventLogHandle(Name: %S)\n", Name); lpLogHandle = HeapAlloc(GetProcessHeap(), 0, sizeof(LOGHANDLE) + ((wcslen(Name) + 1) * sizeof(WCHAR))); if (!lpLogHandle) { DPRINT1("Failed to allocate Heap!\n"); return NULL; } wcscpy(lpLogHandle->szName, Name); /* Get the number of Log Files the EventLog service found */ LogsActive = LogfListItemCount(); if (LogsActive == 0) { DPRINT1("EventLog service reports no log files!\n"); goto Cleanup; } /* If Creating, default to the Application Log in case we fail, as documented on MSDN */ if (Create == TRUE) { pEventSource = GetEventSourceByName(Name); DPRINT("EventSource: %p\n", pEventSource); if (pEventSource) { DPRINT("EventSource LogFile: %p\n", pEventSource->LogFile); lpLogHandle->LogFile = pEventSource->LogFile; } else { DPRINT("EventSource LogFile: Application log file\n"); lpLogHandle->LogFile = LogfListItemByName(L"Application"); } DPRINT("LogHandle LogFile: %p\n", lpLogHandle->LogFile); } else { lpLogHandle->LogFile = NULL; for (i = 1; i <= LogsActive; i++) { currentLogFile = LogfListItemByIndex(i); if (_wcsicmp(Name, currentLogFile->LogName) == 0) { lpLogHandle->LogFile = LogfListItemByIndex(i); lpLogHandle->CurrentRecord = LogfGetOldestRecord(lpLogHandle->LogFile); break; } } } if (!lpLogHandle->LogFile) goto Cleanup; /* Append log handle */ InsertTailList(&LogHandleListHead, &lpLogHandle->LogHandleListEntry); return lpLogHandle; Cleanup: HeapFree(GetProcessHeap(), 0, lpLogHandle); return NULL; }
static NTSTATUS ElfCreateEventLogHandle(PLOGHANDLE* LogHandle, PUNICODE_STRING LogName, BOOLEAN Create) { NTSTATUS Status = STATUS_SUCCESS; PLOGHANDLE pLogHandle; PLOGFILE currentLogFile = NULL; DWORD i, LogsActive; PEVENTSOURCE pEventSource; DPRINT("ElfCreateEventLogHandle(%wZ)\n", LogName); *LogHandle = NULL; i = (LogName->Length + sizeof(UNICODE_NULL)) / sizeof(WCHAR); pLogHandle = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, FIELD_OFFSET(LOGHANDLE, szName[i])); if (!pLogHandle) { DPRINT1("Failed to allocate Heap!\n"); return STATUS_NO_MEMORY; } StringCchCopyW(pLogHandle->szName, i, LogName->Buffer); /* Get the number of Log Files the EventLog service found */ // NOTE: We could just as well loop only once within the list of logs // and retrieve what the code below that calls LogfListItemByIndex, does!! LogsActive = LogfListItemCount(); if (LogsActive == 0) { DPRINT1("EventLog service reports no log files!\n"); Status = STATUS_UNSUCCESSFUL; goto Done; } /* If Creating, default to the Application Log in case we fail, as documented on MSDN */ if (Create) { pEventSource = GetEventSourceByName(LogName->Buffer); DPRINT("EventSource: %p\n", pEventSource); if (pEventSource) { DPRINT("EventSource LogFile: %p\n", pEventSource->LogFile); pLogHandle->LogFile = pEventSource->LogFile; } else { DPRINT("EventSource LogFile: Application log file\n"); pLogHandle->LogFile = LogfListItemByName(L"Application"); } DPRINT("LogHandle LogFile: %p\n", pLogHandle->LogFile); } else { pLogHandle->LogFile = NULL; for (i = 1; i <= LogsActive; i++) { currentLogFile = LogfListItemByIndex(i); if (_wcsicmp(LogName->Buffer, currentLogFile->LogName) == 0) { pLogHandle->LogFile = currentLogFile; break; } } /* Use the application log if the desired log does not exist */ if (pLogHandle->LogFile == NULL) { pLogHandle->LogFile = LogfListItemByName(L"Application"); if (pLogHandle->LogFile == NULL) { DPRINT1("Application log is missing!\n"); Status = STATUS_UNSUCCESSFUL; goto Done; } } /* Reset the current record */ pLogHandle->CurrentRecord = 0; } if (!pLogHandle->LogFile) Status = STATUS_UNSUCCESSFUL; Done: if (NT_SUCCESS(Status)) { /* Append log handle */ EnterCriticalSection(&LogHandleListCs); InsertTailList(&LogHandleListHead, &pLogHandle->LogHandleListEntry); LeaveCriticalSection(&LogHandleListCs); *LogHandle = pLogHandle; } else { HeapFree(GetProcessHeap(), 0, pLogHandle); } return Status; }