GetNumberOfEventLogRecords返回的事件日志数量不正确

我有这个C ++代码来读取事件日志logging

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:\Windows\system32\winevt\logs\ACEEventLog.evtx - 23499 Total Records atlTraceGeneral - C:\Windows\system32\winevt\logs\Application.evtx - 23499 Total Records atlTraceGeneral - C:\Windows\system32\winevt\logs\ConnectionInfo.evtx - 23499 Total Records atlTraceGeneral - C:\Windows\system32\winevt\logs\Error.evtx - 23499 Total Records atlTraceGeneral - C:\Windows\system32\winevt\logs\HardwareEvents.evtx - 23499 Total Records atlTraceGeneral - C:\Windows\system32\winevt\logs\Internet Explorer.evtx - 23499 Total Records atlTraceGeneral - C:\Windows\system32\winevt\logs\Key Management Service.evtx - 23499 Total Records ... 

我已经在我的计算机(150个日志文件)中用所有.EVTX日志文件的完整path调用了这个函数。 每次它返回23499! 我的日志文件有不​​同的大小和一些0,为什么我总是得到23499?

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

更新:正如雷米Lebeaubuild议,但仍然是相同的结果。

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

如果您使用无效的源名称(包括提供路径名称)来调用OpenEventLog ,那么按照记录,它将打开Application日志:

如果您指定自定义日志并且找不到,则事件日志记录服务将打开应用程序日志。

您没有检查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; }