1 ////////////////////////////////////////////////////////////////////
4 // E V E N T L O G G I N G F U N C T I O N S
7 ////////////////////////////////////////////////////////////////////
13 #include "afsd_eventlog.h"
15 #define SZSERVICEPATH "System\\CurrentControlSet\\Services\\"
16 #define SZSERVICENAME "TransarcAFSDaemon"
18 static CHAR szKeyName[] = "System\\CurrentControlSet\\Services\\EventLog\\Application\\TransarcAFSDaemon";
20 static BOOL GetServicePath(LPTSTR lpPathBuf, PDWORD pdwPathBufSize);
21 static BOOL AddEventSource(void);
24 GetServicePath(LPTSTR lpPathBuf, PDWORD pdwPathBufSize)
27 UCHAR szBuf[MAX_PATH];
32 // Prepare path in Registry
33 memset(szBuf, '\0', MAX_PATH);
34 strcpy(szBuf, SZSERVICEPATH);
35 strcat(szBuf, "TransarcAFSDaemon");
38 if ( RegOpenKeyEx( HKEY_LOCAL_MACHINE, szBuf, 0, KEY_QUERY_VALUE, &hKey ) )
44 // prepare user's buffer and read into it
45 dwData = *pdwPathBufSize;
46 memset(lpPathBuf, '\0', dwData);
48 hKey, // handle to key
49 "ImagePath", // value name
52 (LPBYTE) lpPathBuf, // data buffer
53 &dwData)) // size of data buffer
59 *pdwPathBufSize = dwData;
70 // Ensure name for message file is in proper location in Registry.
76 UCHAR szBuf[MAX_PATH];
77 DWORD dwData, dwDisposition;
81 // Let's see if key already exists as a subkey under the
82 // Application key in the EventLog registry key. If not,
84 if ( RegOpenKeyEx( HKEY_LOCAL_MACHINE, szKeyName, 0,
85 KEY_QUERY_VALUE, &hKey ) )
88 if ( RegCreateKeyEx(HKEY_LOCAL_MACHINE, szKeyName, 0,
89 NULL, REG_OPTION_NON_VOLATILE,
90 KEY_ALL_ACCESS, NULL, &hKey,
97 // Set the name of the message file
98 // Get "ImagePath" from TransarcAFSDaemon service
99 memset(szBuf, '\0', MAX_PATH);
101 GetServicePath(szBuf, &dwData);
103 // Add the name to the EventMessageFile subkey.
105 hKey, // subkey handle
106 "EventMessageFile", // value name
108 REG_EXPAND_SZ, // value type
109 (LPBYTE) szBuf, // pointer to value data
110 strlen(szBuf) + 1)) // length of value data
116 // Set the supported event types in the TypesSupported subkey.
117 dwData = EVENTLOG_ERROR_TYPE | EVENTLOG_WARNING_TYPE |
118 EVENTLOG_INFORMATION_TYPE;
121 hKey, // subkey handle
122 "TypesSupported", // value name
124 REG_DWORD, // value type
125 (LPBYTE) &dwData, // pointer to value data
126 sizeof(DWORD))) // length of value data
135 // key was opened - read it
136 memset(szBuf, '\0', MAX_PATH);
138 if ( RegQueryValueEx(
139 hKey, // handle to key
140 "EventMessageFile", // value name
143 (LPBYTE) szBuf, // data buffer
144 &dwData)) // size of data buffer
159 // Log an event with a formatted system message as the (only) substitution
160 // string, from the given message ID.
162 LogEventMessage(WORD wEventType, DWORD dwEventID, DWORD dwMessageID)
166 FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM
167 | FORMAT_MESSAGE_ALLOCATE_BUFFER,
168 NULL, dwMessageID, 0, (LPTSTR)&msgBuf, 0, NULL);
169 LogEvent(wEventType, dwEventID, msgBuf, NULL);
174 // Use the ReportEvent API to write an entry to the system event log.
176 #define MAXSTRINGARGS 100
178 LogEvent(WORD wEventType, DWORD dwEventID, LPTSTR lpString, ...)
182 LPTSTR lpStrings[MAXSTRINGARGS];
185 // Ensure that our event source is properly initialized.
186 if (!AddEventSource())
189 // Get a handle to the event log.
190 hEventSource = RegisterEventSource(NULL, SZSERVICENAME);
191 if (hEventSource == NULL)
194 // Construct the array of substitution strings.
195 va_start(listStrings, lpString);
196 for (wNumStrings = 0;
197 lpString != NULL && wNumStrings < MAXSTRINGARGS;
200 lpStrings[wNumStrings] = lpString;
201 // Advance to the next argument.
202 lpString = va_arg(listStrings, LPTSTR);
206 // Make sure we were not given too many args.
207 if (wNumStrings >= MAXSTRINGARGS)
211 ReportEvent(hEventSource, // handle of event source
212 wEventType, // event type
214 dwEventID, // event ID
215 NULL, // current user's SID
216 wNumStrings, // strings in lpszStrings
217 0, // no bytes of raw data
218 lpStrings, // array of error strings
219 NULL); // no raw data
221 DeregisterEventSource(hEventSource);