comctl32/listview: Free ID array when removing all items.
[wine] / dlls / advapi32 / eventlog.c
1 /*
2  * Win32 advapi functions
3  *
4  * Copyright 1995 Sven Verdoolaege
5  * Copyright 1998 Juergen Schmied
6  * Copyright 2003 Mike Hearn
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21  */
22
23 #include <stdarg.h>
24
25 #include "windef.h"
26 #include "winbase.h"
27 #include "winerror.h"
28 #include "winternl.h"
29 #include "wmistr.h"
30 #include "evntrace.h"
31
32 #include "wine/debug.h"
33
34 WINE_DEFAULT_DEBUG_CHANNEL(advapi);
35 WINE_DECLARE_DEBUG_CHANNEL(eventlog);
36
37 /******************************************************************************
38  * BackupEventLogA [ADVAPI32.@]
39  *
40  * Saves the event log to a backup file.
41  *
42  * PARAMS
43  *  hEventLog        [I] Handle to event log to backup.
44  *  lpBackupFileName [I] Name of the backup file.
45  *
46  * RETURNS
47  *  Success: nonzero. File lpBackupFileName will contain the contents of
48  *           hEvenLog.
49  *  Failure: zero.
50  */
51 BOOL WINAPI BackupEventLogA( HANDLE hEventLog, LPCSTR lpBackupFileName )
52 {
53         FIXME("(%p,%s) stub\n", hEventLog, debugstr_a(lpBackupFileName));
54         return TRUE;
55 }
56
57 /******************************************************************************
58  * BackupEventLogW [ADVAPI32.@]
59  *
60  * See BackupEventLogA.
61  */
62 BOOL WINAPI BackupEventLogW( HANDLE hEventLog, LPCWSTR lpBackupFileName )
63 {
64         FIXME("(%p,%s) stub\n", hEventLog, debugstr_w(lpBackupFileName));
65         return TRUE;
66 }
67
68 /******************************************************************************
69  * ClearEventLogA [ADVAPI32.@]
70  *
71  * Clears the event log and/or saves the log to a backup file.
72  *
73  * PARAMS
74  *  hEvenLog         [I] Handle to event log to clear.
75  *  lpBackupFileName [I] Name of the backup file.
76  *
77  * RETURNS
78  *  Success: nonzero. if lpBackupFileName != NULL, lpBackupFileName will 
79  *           contain the contents of hEvenLog and the log will be cleared.
80  *  Failure: zero. Fails if the event log is empty or if lpBackupFileName
81  *           exists.
82  */
83 BOOL WINAPI ClearEventLogA( HANDLE hEventLog, LPCSTR lpBackupFileName )
84 {
85         FIXME("(%p,%s) stub\n", hEventLog, debugstr_a(lpBackupFileName));
86         return TRUE;
87 }
88
89 /******************************************************************************
90  * ClearEventLogW [ADVAPI32.@]
91  *
92  * See ClearEventLogA.
93  */
94 BOOL WINAPI ClearEventLogW( HANDLE hEventLog, LPCWSTR lpBackupFileName )
95 {
96         FIXME("(%p,%s) stub\n", hEventLog, debugstr_w(lpBackupFileName));
97         return TRUE;
98 }
99
100 /******************************************************************************
101  * CloseEventLog [ADVAPI32.@]
102  *
103  * Closes a read handle to the event log.
104  *
105  * PARAMS
106  *  hEventLog [I/O] Handle of the event log to close.
107  *
108  * RETURNS
109  *  Success: nonzero
110  *  Failure: zero
111  */
112 BOOL WINAPI CloseEventLog( HANDLE hEventLog )
113 {
114         FIXME("(%p) stub\n", hEventLog);
115         return TRUE;
116 }
117
118 /******************************************************************************
119  * ControlTraceW [ADVAPI32.@]
120  *
121  * Control a givel event trace session
122  *
123  */
124 ULONG WINAPI ControlTraceW( TRACEHANDLE hSession, LPCWSTR SessionName, PEVENT_TRACE_PROPERTIES Properties, ULONG control )
125 {
126     FIXME("(%s, %s, %p, %d) stub\n", wine_dbgstr_longlong(hSession), debugstr_w(SessionName), Properties, control);
127     return ERROR_SUCCESS;
128 }
129
130 /******************************************************************************
131  * ControlTraceA [ADVAPI32.@]
132  *
133  * See ControlTraceW.
134  *
135  */
136 ULONG WINAPI ControlTraceA( TRACEHANDLE hSession, LPCSTR SessionName, PEVENT_TRACE_PROPERTIES Properties, ULONG control )
137 {
138     FIXME("(%s, %s, %p, %d) stub\n", wine_dbgstr_longlong(hSession), debugstr_a(SessionName), Properties, control);
139     return ERROR_SUCCESS;
140 }
141
142 /******************************************************************************
143  * DeregisterEventSource [ADVAPI32.@]
144  * 
145  * Closes a write handle to an event log
146  *
147  * PARAMS
148  *  hEventLog [I/O] Handle of the event log.
149  *
150  * RETURNS
151  *  Success: nonzero
152  *  Failure: zero
153  */
154 BOOL WINAPI DeregisterEventSource( HANDLE hEventLog )
155 {
156     FIXME("(%p) stub\n", hEventLog);
157     return TRUE;
158 }
159
160 /******************************************************************************
161  * EnableTrace [ADVAPI32.@]
162  */
163 ULONG WINAPI EnableTrace( ULONG enable, ULONG flag, ULONG level, LPCGUID guid, TRACEHANDLE hSession )
164 {
165     FIXME("(%d, 0x%x, %d, %s, %s): stub\n", enable, flag, level,
166             debugstr_guid(guid), wine_dbgstr_longlong(hSession));
167
168     return ERROR_SUCCESS;
169 }
170
171 /******************************************************************************
172  * GetNumberOfEventLogRecords [ADVAPI32.@]
173  *
174  * Retrieves the number of records in an event log.
175  *
176  * PARAMS
177  *  hEventLog       [I] Handle to an open event log.
178  *  NumberOfRecords [O] Number of records in the log.
179  *
180  * RETURNS
181  *  Success: nonzero. NumberOfRecords will contain the number of records in
182  *           the log.
183  *  Failure: zero
184  */
185 BOOL WINAPI GetNumberOfEventLogRecords( HANDLE hEventLog, PDWORD NumberOfRecords )
186 {
187     FIXME("(%p,%p) stub\n", hEventLog, NumberOfRecords);
188
189     if (!NumberOfRecords) return FALSE;
190     *NumberOfRecords = 0;
191
192     return TRUE;
193 }
194
195 /******************************************************************************
196  * GetOldestEventLogRecord [ADVAPI32.@]
197  *
198  * Retrieves the absolute record number of the oldest record in an even log.
199  *
200  * PARAMS
201  *  hEventLog    [I] Handle to an open event log.
202  *  OldestRecord [O] Absolute record number of the oldest record.
203  *
204  * RETURNS
205  *  Success: nonzero. OldestRecord contains the record number of the oldest
206  *           record in the log.
207  *  Failure: zero 
208  */
209 BOOL WINAPI GetOldestEventLogRecord( HANDLE hEventLog, PDWORD OldestRecord )
210 {
211     FIXME("(%p,%p) stub\n", hEventLog, OldestRecord);
212
213     if (!OldestRecord) return FALSE;
214     *OldestRecord = 0;
215
216     return TRUE;
217 }
218
219 /******************************************************************************
220  * NotifyChangeEventLog [ADVAPI32.@]
221  *
222  * Enables an application to receive notification when an event is written
223  * to an event log.
224  *
225  * PARAMS
226  *  hEventLog [I] Handle to an event log.
227  *  hEvent    [I] Handle to a manual-reset event object.
228  *
229  * RETURNS
230  *  Success: nonzero
231  *  Failure: zero
232  */
233 BOOL WINAPI NotifyChangeEventLog( HANDLE hEventLog, HANDLE hEvent )
234 {
235         FIXME("(%p,%p) stub\n", hEventLog, hEvent);
236         return TRUE;
237 }
238
239 /******************************************************************************
240  * OpenBackupEventLogA [ADVAPI32.@]
241  *
242  * Opens a handle to a backup event log.
243  *
244  * PARAMS
245  *  lpUNCServerName [I] Universal Naming Convention name of the server on which
246  *                      this will be performed.
247  *  lpFileName      [I] Specifies the name of the backup file.
248  *
249  * RETURNS
250  *  Success: Handle to the backup event log.
251  *  Failure: NULL
252  */
253 HANDLE WINAPI OpenBackupEventLogA( LPCSTR lpUNCServerName, LPCSTR lpFileName )
254 {
255         FIXME("(%s,%s) stub\n", debugstr_a(lpUNCServerName), debugstr_a(lpFileName));
256         return (HANDLE)0xcafe4242;
257 }
258
259 /******************************************************************************
260  * OpenBackupEventLogW [ADVAPI32.@]
261  *
262  * See OpenBackupEventLogA.
263  */
264 HANDLE WINAPI OpenBackupEventLogW( LPCWSTR lpUNCServerName, LPCWSTR lpFileName )
265 {
266         FIXME("(%s,%s) stub\n", debugstr_w(lpUNCServerName), debugstr_w(lpFileName));
267         return (HANDLE)0xcafe4242;
268 }
269
270 /******************************************************************************
271  * OpenEventLogA [ADVAPI32.@]
272  *
273  * Opens a handle to the specified event log.
274  *
275  * PARAMS
276  *  lpUNCServerName [I] UNC name of the server on which the event log is
277  *                      opened.
278  *  lpSourceName    [I] Name of the log.
279  *
280  * RETURNS
281  *  Success: Handle to an event log.
282  *  Failure: NULL
283  */
284 HANDLE WINAPI OpenEventLogA( LPCSTR uncname, LPCSTR source )
285 {
286         FIXME("(%s,%s) stub\n", debugstr_a(uncname), debugstr_a(source));
287         return (HANDLE)0xcafe4242;
288 }
289
290 /******************************************************************************
291  * OpenEventLogW [ADVAPI32.@]
292  *
293  * See OpenEventLogA.
294  */
295 HANDLE WINAPI OpenEventLogW( LPCWSTR uncname, LPCWSTR source )
296 {
297         FIXME("(%s,%s) stub\n", debugstr_w(uncname), debugstr_w(source));
298         return (HANDLE)0xcafe4242;
299 }
300
301 /******************************************************************************
302  * QueryAllTracesW [ADVAPI32.@]
303  *
304  * Query informations for started event trace sessions
305  *
306  */
307 ULONG WINAPI QueryAllTracesW( PEVENT_TRACE_PROPERTIES * parray, ULONG arraycount, PULONG psessioncount )
308 {
309     FIXME("(%p, %d, %p) stub\n", parray, arraycount, psessioncount);
310
311     if (psessioncount) *psessioncount = 0;
312     return ERROR_SUCCESS;
313 }
314
315 /******************************************************************************
316  * QueryAllTracesA [ADVAPI32.@]
317  *
318  * See QueryAllTracesW.
319  */
320 ULONG WINAPI QueryAllTracesA( PEVENT_TRACE_PROPERTIES * parray, ULONG arraycount, PULONG psessioncount )
321 {
322     FIXME("(%p, %d, %p) stub\n", parray, arraycount, psessioncount);
323
324     if (psessioncount) *psessioncount = 0;
325     return ERROR_SUCCESS;
326 }
327
328 /******************************************************************************
329  * ReadEventLogA [ADVAPI32.@]
330  *
331  * Reads a whole number of entries from an event log.
332  *
333  * PARAMS
334  *  hEventLog                [I] Handle of the event log to read.
335  *  dwReadFlags              [I] see MSDN doc.
336  *  dwRecordOffset           [I] Log-entry record number to start at.
337  *  lpBuffer                 [O] Buffer for the data read.
338  *  nNumberOfBytesToRead     [I] Size of lpBuffer.
339  *  pnBytesRead              [O] Receives number of bytes read.
340  *  pnMinNumberOfBytesNeeded [O] Receives number of bytes required for the
341  *                               next log entry.
342  *
343  * RETURNS
344  *  Success: nonzero
345  *  Failure: zero
346  */
347 BOOL WINAPI ReadEventLogA( HANDLE hEventLog, DWORD dwReadFlags, DWORD dwRecordOffset,
348     LPVOID lpBuffer, DWORD nNumberOfBytesToRead, DWORD *pnBytesRead, DWORD *pnMinNumberOfBytesNeeded )
349 {
350     FIXME("(%p,0x%08x,0x%08x,%p,0x%08x,%p,%p) stub\n", hEventLog, dwReadFlags,
351           dwRecordOffset, lpBuffer, nNumberOfBytesToRead, pnBytesRead, pnMinNumberOfBytesNeeded);
352     return FALSE;
353 }
354
355 /******************************************************************************
356  * ReadEventLogW [ADVAPI32.@]
357  *
358  * See ReadEventLogA.
359  */
360 BOOL WINAPI ReadEventLogW( HANDLE hEventLog, DWORD dwReadFlags, DWORD dwRecordOffset,
361     LPVOID lpBuffer, DWORD nNumberOfBytesToRead, DWORD *pnBytesRead, DWORD *pnMinNumberOfBytesNeeded )
362 {
363     FIXME("(%p,0x%08x,0x%08x,%p,0x%08x,%p,%p) stub\n", hEventLog, dwReadFlags,
364           dwRecordOffset, lpBuffer, nNumberOfBytesToRead, pnBytesRead, pnMinNumberOfBytesNeeded);
365     return FALSE;
366 }
367
368 /******************************************************************************
369  * RegisterEventSourceA [ADVAPI32.@]
370  *
371  * Returns a registered handle to an event log.
372  *
373  * PARAMS
374  *  lpUNCServerName [I] UNC name of the source server.
375  *  lpSourceName    [I] Specifies the name of the event source to retrieve.
376  *
377  * RETURNS
378  *  Success: Handle to the event log.
379  *  Failure: NULL. Returns ERROR_INVALID_HANDLE if lpSourceName specifies the
380  *           Security event log.
381  */
382 HANDLE WINAPI RegisterEventSourceA( LPCSTR lpUNCServerName, LPCSTR lpSourceName )
383 {
384     UNICODE_STRING lpUNCServerNameW;
385     UNICODE_STRING lpSourceNameW;
386     HANDLE ret;
387
388     FIXME("(%s,%s): stub\n", debugstr_a(lpUNCServerName), debugstr_a(lpSourceName));
389
390     RtlCreateUnicodeStringFromAsciiz(&lpUNCServerNameW, lpUNCServerName);
391     RtlCreateUnicodeStringFromAsciiz(&lpSourceNameW, lpSourceName);
392     ret = RegisterEventSourceW(lpUNCServerNameW.Buffer,lpSourceNameW.Buffer);
393     RtlFreeUnicodeString (&lpUNCServerNameW);
394     RtlFreeUnicodeString (&lpSourceNameW);
395     return ret;
396 }
397
398 /******************************************************************************
399  * RegisterEventSourceW [ADVAPI32.@]
400  *
401  * See RegisterEventSourceA.
402  */
403 HANDLE WINAPI RegisterEventSourceW( LPCWSTR lpUNCServerName, LPCWSTR lpSourceName )
404 {
405     FIXME("(%s,%s): stub\n", debugstr_w(lpUNCServerName), debugstr_w(lpSourceName));
406     return (HANDLE)0xcafe4242;
407 }
408
409 /******************************************************************************
410  * ReportEventA [ADVAPI32.@]
411  *
412  * Writes an entry at the end of an event log.
413  *
414  * PARAMS
415  *  hEventLog   [I] Handle of an event log.
416  *  wType       [I] See MSDN doc.
417  *  wCategory   [I] Event category.
418  *  dwEventID   [I] Event identifier.
419  *  lpUserSid   [I] Current user's security identifier.
420  *  wNumStrings [I] Number of insert strings in lpStrings.
421  *  dwDataSize  [I] Size of event-specific raw data to write.
422  *  lpStrings   [I] Buffer containing an array of string to be merged.
423  *  lpRawData   [I] Buffer containing the binary data.
424  *
425  * RETURNS
426  *  Success: nonzero. Entry was written to the log.
427  *  Failure: zero.
428  *
429  * NOTES
430  *  The ReportEvent function adds the time, the entry's length, and the
431  *  offsets before storing the entry in the log. If lpUserSid != NULL, the
432  *  username is also logged.
433  */
434 BOOL WINAPI ReportEventA ( HANDLE hEventLog, WORD wType, WORD wCategory, DWORD dwEventID,
435     PSID lpUserSid, WORD wNumStrings, DWORD dwDataSize, LPCSTR *lpStrings, LPVOID lpRawData)
436 {
437     LPWSTR *wideStrArray;
438     UNICODE_STRING str;
439     int i;
440     BOOL ret;
441
442     FIXME("(%p,0x%04x,0x%04x,0x%08x,%p,0x%04x,0x%08x,%p,%p): stub\n", hEventLog,
443           wType, wCategory, dwEventID, lpUserSid, wNumStrings, dwDataSize, lpStrings, lpRawData);
444
445     if (wNumStrings == 0) return TRUE;
446     if (!lpStrings) return TRUE;
447
448     wideStrArray = HeapAlloc(GetProcessHeap(), 0, sizeof(LPWSTR) * wNumStrings);
449     for (i = 0; i < wNumStrings; i++)
450     {
451         RtlCreateUnicodeStringFromAsciiz(&str, lpStrings[i]);
452         wideStrArray[i] = str.Buffer;
453     }
454     ret = ReportEventW(hEventLog, wType, wCategory, dwEventID, lpUserSid,
455                        wNumStrings, dwDataSize, (LPCWSTR *)wideStrArray, lpRawData);
456     for (i = 0; i < wNumStrings; i++)
457     {
458         HeapFree( GetProcessHeap(), 0, wideStrArray[i] );
459     }
460     HeapFree(GetProcessHeap(), 0, wideStrArray);
461     return ret;
462 }
463
464 /******************************************************************************
465  * ReportEventW [ADVAPI32.@]
466  *
467  * See ReportEventA.
468  */
469 BOOL WINAPI ReportEventW( HANDLE hEventLog, WORD wType, WORD wCategory, DWORD dwEventID,
470     PSID lpUserSid, WORD wNumStrings, DWORD dwDataSize, LPCWSTR *lpStrings, LPVOID lpRawData )
471 {
472     int i;
473
474     FIXME("(%p,0x%04x,0x%04x,0x%08x,%p,0x%04x,0x%08x,%p,%p): stub\n", hEventLog,
475           wType, wCategory, dwEventID, lpUserSid, wNumStrings, dwDataSize, lpStrings, lpRawData);
476
477     /* partial stub */
478
479     if (wNumStrings == 0) return TRUE;
480     if (!lpStrings) return TRUE;
481
482     for (i = 0; i < wNumStrings; i++)
483     {
484         switch (wType)
485         {
486         case EVENTLOG_SUCCESS:
487             TRACE_(eventlog)("%s\n", debugstr_w(lpStrings[i]));
488             break;
489         case EVENTLOG_ERROR_TYPE:
490             ERR_(eventlog)("%s\n", debugstr_w(lpStrings[i]));
491             break;
492         case EVENTLOG_WARNING_TYPE:
493             WARN_(eventlog)("%s\n", debugstr_w(lpStrings[i]));
494             break;
495         default:
496             TRACE_(eventlog)("%s\n", debugstr_w(lpStrings[i]));
497             break;
498         }
499     }
500     return TRUE;
501 }
502
503 /******************************************************************************
504  * RegisterTraceGuidsW [ADVAPI32.@]
505  *
506  * Register an event trace provider and the event trace classes that it uses
507  * to generate events.
508  *
509  * PARAMS
510  *  RequestAddress     [I]   ControlCallback function
511  *  RequestContext     [I]   Optional provider-defined context
512  *  ControlGuid        [I]   GUID of the registering provider
513  *  GuidCount          [I]   Number of elements in the TraceGuidReg array
514  *  TraceGuidReg       [I/O] Array of TRACE_GUID_REGISTRATION structures
515  *  MofImagePath       [I]   not supported, set to NULL
516  *  MofResourceNmae    [I]   not supported, set to NULL
517  *  RegistrationHandle [O]   Provider's registration handle
518  *
519  * RETURNS
520  *  Success: ERROR_SUCCESS
521  *  Failure: System error code
522  *
523  * FIXME
524  *  Stub.
525  */
526 ULONG WINAPI RegisterTraceGuidsW( WMIDPREQUEST RequestAddress,
527                 PVOID RequestContext, LPCGUID ControlGuid, ULONG GuidCount,
528                 PTRACE_GUID_REGISTRATION TraceGuidReg, LPCWSTR MofImagePath,
529                 LPCWSTR MofResourceName, PTRACEHANDLE RegistrationHandle )
530 {
531     FIXME("%p %p %p %u %p %s %s %p\n", RequestAddress, RequestContext,
532           ControlGuid, GuidCount, TraceGuidReg, debugstr_w(MofImagePath),
533           debugstr_w(MofResourceName), RegistrationHandle);
534     return ERROR_CALL_NOT_IMPLEMENTED;
535 }
536
537 /******************************************************************************
538  * RegisterTraceGuidsA [ADVAPI32.@]
539  *
540  * See RegisterTraceGuidsW.
541  *
542  * FIXME
543  *  Stub.
544  */
545 ULONG WINAPI RegisterTraceGuidsA( WMIDPREQUEST RequestAddress,
546                 PVOID RequestContext, LPCGUID ControlGuid, ULONG GuidCount,
547                 PTRACE_GUID_REGISTRATION TraceGuidReg, LPCSTR MofImagePath,
548                 LPCSTR MofResourceName, PTRACEHANDLE RegistrationHandle )
549 {
550     FIXME("%p %p %p %u %p %s %s %p\n", RequestAddress, RequestContext,
551           ControlGuid, GuidCount, TraceGuidReg, debugstr_a(MofImagePath),
552           debugstr_a(MofResourceName), RegistrationHandle);
553     return ERROR_CALL_NOT_IMPLEMENTED;
554 }
555
556 /******************************************************************************
557  * StartTraceW [ADVAPI32.@]
558  *
559  * Register and start an event trace session
560  *
561  */
562 ULONG WINAPI StartTraceW( PTRACEHANDLE pSessionHandle, LPCWSTR SessionName, PEVENT_TRACE_PROPERTIES Properties )
563 {
564     FIXME("(%p, %s, %p) stub\n", pSessionHandle, debugstr_w(SessionName), Properties);
565     if (pSessionHandle) *pSessionHandle = 0xcafe4242;
566     return ERROR_SUCCESS;
567 }
568
569 /******************************************************************************
570  * StartTraceA [ADVAPI32.@]
571  *
572  * See StartTraceW.
573  *
574  */
575 ULONG WINAPI StartTraceA( PTRACEHANDLE pSessionHandle, LPCSTR SessionName, PEVENT_TRACE_PROPERTIES Properties )
576 {
577     FIXME("(%p, %s, %p) stub\n", pSessionHandle, debugstr_a(SessionName), Properties);
578     if (pSessionHandle) *pSessionHandle = 0xcafe4242;
579     return ERROR_SUCCESS;
580 }
581
582 /******************************************************************************
583  * TraceEvent [ADVAPI32.@]
584  */
585 ULONG WINAPI TraceEvent( TRACEHANDLE SessionHandle, PEVENT_TRACE_HEADER EventTrace )
586 {
587     FIXME("%s %p\n", wine_dbgstr_longlong(SessionHandle), EventTrace);
588     return ERROR_CALL_NOT_IMPLEMENTED;
589 }
590
591 /******************************************************************************
592  * UnregisterTraceGuids [ADVAPI32.@]
593  *
594  * See RegisterTraceGuids
595  *
596  * FIXME
597  *  Stub.
598  */
599 ULONG WINAPI UnregisterTraceGuids( TRACEHANDLE RegistrationHandle )
600 {
601     FIXME("%s: stub\n", wine_dbgstr_longlong(RegistrationHandle));
602     return ERROR_CALL_NOT_IMPLEMENTED;
603 }