GetNumberOfEventLogRecords返回错误的事件日志数

GetNumberOfEventLogRecords returns incorrect number of event logs

本文关键字:日志 事件 返回 错误 GetNumberOfEventLogRecords      更新时间:2023-10-16

我有这个c++代码来读取事件日志记录

DWORD GetLogRecords(LPCWSTR wsLogFile)
{
  HANDLE hEvt = OpenEventLog(NULL, wsLogFile);
  if (hEvt==NULL) return 0;
  DWORD dwTotalRecords;
  BOOL res = GetNumberOfEventLogRecords(hEvt, &dwTotalRecords);
  CloseEventLog(hEvt);
  return (res != 0) ? dwTotalRecords : 0;
}
结果

atlTraceGeneral - C:Windowssystem32winevtlogsACEEventLog.evtx - 23499 Total Records
atlTraceGeneral - C:Windowssystem32winevtlogsApplication.evtx - 23499 Total Records
atlTraceGeneral - C:Windowssystem32winevtlogsConnectionInfo.evtx - 23499 Total Records
atlTraceGeneral - C:Windowssystem32winevtlogsError.evtx - 23499 Total Records
atlTraceGeneral - C:Windowssystem32winevtlogsHardwareEvents.evtx - 23499 Total Records
atlTraceGeneral - C:Windowssystem32winevtlogsInternet Explorer.evtx - 23499 Total Records
atlTraceGeneral - C:Windowssystem32winevtlogsKey Management Service.evtx - 23499 Total Records
 ...

我用我计算机上所有. evtx日志文件(150个日志文件)的完整路径调用了这个函数。每次它都返回23499 !我的日志文件大小不一,有些是0,为什么我总是得到23499 ?

UPDATE2:在我清除了应用程序日志之后,现在我得到所有.evtx日志文件的0。我认为它总是得到应用程序日志,而不是指定的。evtx文件。

更新:正如Remy Lebeau建议的那样,但结果仍然相同。

为了其他人的利益,这个问题的解决方案是OpenEventLog不接受路径名。相反,您必须为它指定事件日志的源名称(例如"HardwareEvents")。

如果您使用无效的源名称调用OpenEventLog(其中包括提供路径名),那么根据文档,它将打开Application日志:

如果您指定的自定义日志无法找到,则事件日志service打开应用程序日志

您没有检查GetNumberOfEventLogRecords()的结果是否有错误。你的木头把手漏了。试试这个:

DWORD GetLogRecords(LPCWSTR wsLogFile)
{
  HANDLE hEvt = OpenEventLog(NULL, wsLogFile);
  if (hEvt==NULL) return 0;
  DWORD dwTotalRecords;
  BOOL res = GetNumberOfEventLogRecords(hEvt, &dwTotalRecords);
  CloseEventLog(hEvt);
  return (res != 0) ? dwTotalRecords : 0;
}