GetNumberOfEventLogRecords returns incorrect number of event logs

后端 未结 2 1464
无人及你
无人及你 2020-12-22 12:42

I have this C++ code to read the event log records

DWORD GetLogRecords(LPCWSTR wsLogFile)
{
  HANDLE hEvt = OpenEventLog(NULL, wsLogFile);
  if (hEvt==NULL)          


        
2条回答
  •  再見小時候
    2020-12-22 13:27

    You are not checking the result of GetNumberOfEventLogRecords() for an error. And you are leaking the log handle. Try this instead:

    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;
    }
    

提交回复
热议问题