00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00026 #ifndef OW32_EventLog_h
00027 #define OW32_EventLog_h
00028
00029 #include <OW32/XHKEY.h>
00030 #include <tchar.h>
00031 #include <cstdio>
00032
00033 namespace OW32
00034 {
00035
00037 class OW32_LIB_EXPORT CEventLog
00038 {
00039 private:
00040 HANDLE m_hEventLog;
00041
00042 public:
00044 CEventLog() : m_hEventLog(NULL) {}
00045
00047 ~CEventLog() { Close(); }
00048
00054 BOOL Open(LPCTSTR SourceName, LPCTSTR Computer = NULL)
00055 {
00056 m_hEventLog = RegisterEventSource(Computer, SourceName);
00057 return (m_hEventLog != NULL);
00058 }
00059
00062 void Close()
00063 {
00064 if (m_hEventLog != NULL) {
00065 DeregisterEventSource(m_hEventLog);
00066 m_hEventLog = NULL;
00067 }
00068 }
00069
00081 BOOL ReportEvent(WORD wType,
00082 DWORD dwEventID,
00083 WORD wCategory = 0,
00084 WORD wNumStrings = 0,
00085 LPCTSTR* lpStrings = NULL,
00086 DWORD dwDataSize = 0,
00087 LPVOID lpRawData = NULL,
00088 PSID lpUserSID = NULL)
00089 {
00090 if (!m_hEventLog)
00091 return FALSE;
00092
00093 return ::ReportEvent(m_hEventLog, wType, wCategory, dwEventID, lpUserSID,
00094 wNumStrings, dwDataSize, lpStrings, lpRawData);
00095 }
00096
00105 BOOL ReportInformation(DWORD dwEventID, LPCTSTR str1=0, LPCTSTR str2=0, LPCTSTR str3=0, LPCTSTR str4=0)
00106 {
00107 LPCTSTR lpStrings[5] = {str1, str2, str3, str4, 0};
00108 int NumStrings=0;
00109 for ( ; lpStrings[NumStrings] != 0; NumStrings++)
00110 ;
00111 return ReportEvent((WORD)EVENTLOG_INFORMATION_TYPE, dwEventID, (WORD)0, (WORD)NumStrings, lpStrings);
00112 }
00113
00122 BOOL ReportWarning(DWORD dwEventID, LPCTSTR str1=0, LPCTSTR str2=0, LPCTSTR str3=0, LPCTSTR str4=0)
00123 {
00124 LPCTSTR lpStrings[5] = {str1, str2, str3, str4, 0};
00125 int NumStrings=0;
00126 for ( ; lpStrings[NumStrings] != 0; NumStrings++)
00127 ;
00128 return ReportEvent((WORD)EVENTLOG_WARNING_TYPE, dwEventID, (WORD)0, (WORD)NumStrings, lpStrings);
00129 }
00130
00139 BOOL ReportError(DWORD dwEventID, LPCTSTR str1=0, LPCTSTR str2=0, LPCTSTR str3=0, LPCTSTR str4=0)
00140 {
00141 LPCTSTR lpStrings[5] = {str1, str2, str3, str4, 0};
00142 int NumStrings=0;
00143 for ( ; lpStrings[NumStrings] != 0; NumStrings++)
00144 ;
00145 return ReportEvent((WORD)EVENTLOG_ERROR_TYPE, dwEventID, (WORD)0, (WORD)NumStrings, lpStrings);
00146 }
00147
00153 BOOL ReportAPIError(DWORD dwEventID, DWORD dwErr, LPCTSTR str1=0, LPCTSTR str2=0, LPCTSTR str3=0)
00154 {
00155 LPTSTR lpMessage=NULL;
00156 if (FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
00157 0,
00158 dwErr,
00159 0,
00160 (LPTSTR)&lpMessage,
00161 0,
00162 NULL) == 0) {
00163 TCHAR num[32];
00164 _sntprintf(num, sizeof(num)/sizeof(num[0]), _T("%d"), dwErr);
00165 return ReportError(dwEventID, num, str1, str2, str3);
00166 }
00167 BOOL bRet = ReportError(dwEventID, lpMessage, str1, str2, str3);
00168 LocalFree(lpMessage);
00169 return bRet;
00170 }
00171
00183 static LONG RegisterMessageDLL(LPCTSTR SourceName, LPCTSTR MessageDLLPath=NULL, HMODULE hModule=NULL,
00184 DWORD TypesSupported = EVENTLOG_ERROR_TYPE | EVENTLOG_WARNING_TYPE | EVENTLOG_INFORMATION_TYPE)
00185 {
00186 TCHAR EventLogKeyName[MAX_PATH];
00187 _sntprintf(EventLogKeyName, MAX_PATH, _T("SYSTEM\\CurrentControlSet\\Services\\EventLog\\Application\\%s"),
00188 SourceName);
00189
00190
00191 XHKEY hk;
00192 LONG lErr;
00193 if ((lErr=RegCreateKey(HKEY_LOCAL_MACHINE,
00194 EventLogKeyName,
00195 &hk)) != ERROR_SUCCESS) {
00196 return lErr;
00197 }
00198
00199
00200 TCHAR path[MAX_PATH];
00201 if (!MessageDLLPath) {
00202 if (!GetModuleFileName(hModule, path, MAX_PATH))
00203 return ::GetLastError();
00204 MessageDLLPath = path;
00205 }
00206 if ((lErr=RegSetValueEx(hk, _T("EventMessageFile"), 0, REG_EXPAND_SZ, (LPBYTE)MessageDLLPath, (DWORD)(_tcslen(MessageDLLPath)+1)*sizeof(TCHAR))) !=
00207 ERROR_SUCCESS) {
00208 return lErr;
00209 }
00210
00211
00212 if ((lErr=RegSetValueEx(hk, _T("TypesSupported"), 0, REG_DWORD, (LPBYTE)&TypesSupported, sizeof(TypesSupported))) !=
00213 ERROR_SUCCESS) {
00214 return lErr;
00215 }
00216 return ERROR_SUCCESS;
00217 }
00218
00223 static LONG UnregisterMessageDLL(LPCTSTR SourceName)
00224 {
00225
00226 TCHAR EventLogKeyName[MAX_PATH];
00227 _sntprintf(EventLogKeyName, MAX_PATH, _T("SYSTEM\\CurrentControlSet\\Services\\EventLog\\Application\\%s"), SourceName);
00228
00229
00230 LONG lErr;
00231 if ((lErr=RegDeleteKey(HKEY_LOCAL_MACHINE, EventLogKeyName)) != ERROR_SUCCESS) {
00232 return lErr;
00233 }
00234 return ERROR_SUCCESS;
00235 }
00236 };
00237
00238 }
00239
00240 #endif // OW32_EventLog_h