7a4acb5cc07eba70db4879696cde78ff4fc7ba84
[reactos.git] / reactos / dll / win32 / advapi32 / service / eventlog.c
1 /*
2 * Win32 advapi functions
3 *
4 * Copyright 1995 Sven Verdoolaege
5 * Copyright 1998 Juergen Schmied
6 * Copyright 2003 Mike Hearn
7 * Copyright 2007 Hervé Poussineau
8 *
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
13 *
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 */
23
24 #include <advapi32.h>
25 #include "wine/debug.h"
26
27 WINE_DEFAULT_DEBUG_CHANNEL(advapi);
28
29 static RPC_UNICODE_STRING EmptyString = { 0, 0, L"" };
30
31
32 handle_t __RPC_USER
33 EVENTLOG_HANDLE_A_bind(EVENTLOG_HANDLE_A UNCServerName)
34 {
35 handle_t hBinding = NULL;
36 UCHAR *pszStringBinding;
37 RPC_STATUS status;
38
39 TRACE("EVENTLOG_HANDLE_A_bind() called\n");
40
41 status = RpcStringBindingComposeA(NULL,
42 (UCHAR *)"ncacn_np",
43 (UCHAR *)UNCServerName,
44 (UCHAR *)"\\pipe\\ntsvcs",
45 NULL,
46 (UCHAR **)&pszStringBinding);
47 if (status)
48 {
49 ERR("RpcStringBindingCompose returned 0x%x\n", status);
50 return NULL;
51 }
52
53 /* Set the binding handle that will be used to bind to the server. */
54 status = RpcBindingFromStringBindingA(pszStringBinding,
55 &hBinding);
56 if (status)
57 {
58 ERR("RpcBindingFromStringBinding returned 0x%x\n", status);
59 }
60
61 status = RpcStringFreeA(&pszStringBinding);
62 if (status)
63 {
64 ERR("RpcStringFree returned 0x%x\n", status);
65 }
66
67 return hBinding;
68 }
69
70
71 void __RPC_USER
72 EVENTLOG_HANDLE_A_unbind(EVENTLOG_HANDLE_A UNCServerName,
73 handle_t hBinding)
74 {
75 RPC_STATUS status;
76
77 TRACE("EVENTLOG_HANDLE_A_unbind() called\n");
78
79 status = RpcBindingFree(&hBinding);
80 if (status)
81 {
82 ERR("RpcBindingFree returned 0x%x\n", status);
83 }
84 }
85
86
87 handle_t __RPC_USER
88 EVENTLOG_HANDLE_W_bind(EVENTLOG_HANDLE_W UNCServerName)
89 {
90 handle_t hBinding = NULL;
91 LPWSTR pszStringBinding;
92 RPC_STATUS status;
93
94 TRACE("EVENTLOG_HANDLE_W_bind() called\n");
95
96 status = RpcStringBindingComposeW(NULL,
97 L"ncacn_np",
98 (LPWSTR)UNCServerName,
99 L"\\pipe\\EventLog",
100 NULL,
101 &pszStringBinding);
102 if (status)
103 {
104 ERR("RpcStringBindingCompose returned 0x%x\n", status);
105 return NULL;
106 }
107
108 /* Set the binding handle that will be used to bind to the server. */
109 status = RpcBindingFromStringBindingW(pszStringBinding,
110 &hBinding);
111 if (status)
112 {
113 ERR("RpcBindingFromStringBinding returned 0x%x\n", status);
114 }
115
116 status = RpcStringFreeW(&pszStringBinding);
117 if (status)
118 {
119 ERR("RpcStringFree returned 0x%x\n", status);
120 }
121
122 return hBinding;
123 }
124
125
126 void __RPC_USER
127 EVENTLOG_HANDLE_W_unbind(EVENTLOG_HANDLE_W UNCServerName,
128 handle_t hBinding)
129 {
130 RPC_STATUS status;
131
132 TRACE("EVENTLOG_HANDLE_W_unbind() called\n");
133
134 status = RpcBindingFree(&hBinding);
135 if (status)
136 {
137 ERR("RpcBindingFree returned 0x%x\n", status);
138 }
139 }
140
141
142 /******************************************************************************
143 * BackupEventLogA [ADVAPI32.@]
144 */
145 BOOL WINAPI
146 BackupEventLogA(IN HANDLE hEventLog,
147 IN LPCSTR lpBackupFileName)
148 {
149 RPC_STRING BackupFileName;
150 NTSTATUS Status;
151
152 TRACE("%p, %s\n", hEventLog, lpBackupFileName);
153
154 BackupFileName.Buffer = (LPSTR)lpBackupFileName;
155 BackupFileName.Length = BackupFileName.MaximumLength =
156 lpBackupFileName ? strlen(lpBackupFileName) : 0;
157 BackupFileName.MaximumLength += sizeof(CHAR);
158
159 RpcTryExcept
160 {
161 Status = ElfrBackupELFA(hEventLog,
162 &BackupFileName);
163 }
164 RpcExcept(EXCEPTION_EXECUTE_HANDLER)
165 {
166 Status = I_RpcMapWin32Status(RpcExceptionCode());
167 }
168 RpcEndExcept;
169
170 if (!NT_SUCCESS(Status))
171 {
172 SetLastError(RtlNtStatusToDosError(Status));
173 return FALSE;
174 }
175
176 return TRUE;
177 }
178
179 /******************************************************************************
180 * BackupEventLogW [ADVAPI32.@]
181 *
182 * PARAMS
183 * hEventLog []
184 * lpBackupFileName []
185 */
186 BOOL WINAPI
187 BackupEventLogW(IN HANDLE hEventLog,
188 IN LPCWSTR lpBackupFileName)
189 {
190 RPC_UNICODE_STRING BackupFileName;
191 NTSTATUS Status;
192
193 TRACE("%p, %s\n", hEventLog, debugstr_w(lpBackupFileName));
194
195 BackupFileName.Buffer = (LPWSTR)lpBackupFileName;
196 BackupFileName.Length = BackupFileName.MaximumLength =
197 lpBackupFileName ? wcslen(lpBackupFileName) * sizeof(WCHAR) : 0;
198 BackupFileName.MaximumLength += sizeof(WCHAR);
199
200 RpcTryExcept
201 {
202 Status = ElfrBackupELFW(hEventLog,
203 &BackupFileName);
204 }
205 RpcExcept(EXCEPTION_EXECUTE_HANDLER)
206 {
207 Status = I_RpcMapWin32Status(RpcExceptionCode());
208 }
209 RpcEndExcept;
210
211 if (!NT_SUCCESS(Status))
212 {
213 SetLastError(RtlNtStatusToDosError(Status));
214 return FALSE;
215 }
216
217 return TRUE;
218 }
219
220
221 /******************************************************************************
222 * ClearEventLogA [ADVAPI32.@]
223 */
224 BOOL WINAPI
225 ClearEventLogA(IN HANDLE hEventLog,
226 IN LPCSTR lpBackupFileName)
227 {
228 RPC_STRING BackupFileName;
229 NTSTATUS Status;
230
231 TRACE("%p, %s\n", hEventLog, lpBackupFileName);
232
233 BackupFileName.Buffer = (LPSTR)lpBackupFileName;
234 BackupFileName.Length = BackupFileName.MaximumLength =
235 lpBackupFileName ? strlen(lpBackupFileName) : 0;
236 BackupFileName.MaximumLength += sizeof(CHAR);
237
238 RpcTryExcept
239 {
240 Status = ElfrClearELFA(hEventLog,
241 &BackupFileName);
242 }
243 RpcExcept(EXCEPTION_EXECUTE_HANDLER)
244 {
245 Status = I_RpcMapWin32Status(RpcExceptionCode());
246 }
247 RpcEndExcept;
248
249 if (!NT_SUCCESS(Status))
250 {
251 SetLastError(RtlNtStatusToDosError(Status));
252 return FALSE;
253 }
254
255 return TRUE;
256 }
257
258
259 /******************************************************************************
260 * ClearEventLogW [ADVAPI32.@]
261 */
262 BOOL WINAPI
263 ClearEventLogW(IN HANDLE hEventLog,
264 IN LPCWSTR lpBackupFileName)
265 {
266 RPC_UNICODE_STRING BackupFileName;
267 NTSTATUS Status;
268
269 TRACE("%p, %s\n", hEventLog, debugstr_w(lpBackupFileName));
270
271 BackupFileName.Buffer = (LPWSTR)lpBackupFileName;
272 BackupFileName.Length = BackupFileName.MaximumLength =
273 lpBackupFileName ? wcslen(lpBackupFileName) * sizeof(WCHAR) : 0;
274 BackupFileName.MaximumLength += sizeof(WCHAR);
275
276 RpcTryExcept
277 {
278 Status = ElfrClearELFW(hEventLog,
279 &BackupFileName);
280 }
281 RpcExcept(EXCEPTION_EXECUTE_HANDLER)
282 {
283 Status = I_RpcMapWin32Status(RpcExceptionCode());
284 }
285 RpcEndExcept;
286
287 if (!NT_SUCCESS(Status))
288 {
289 SetLastError(RtlNtStatusToDosError(Status));
290 return FALSE;
291 }
292
293 return TRUE;
294 }
295
296
297 /******************************************************************************
298 * CloseEventLog [ADVAPI32.@]
299 */
300 BOOL WINAPI
301 CloseEventLog(IN HANDLE hEventLog)
302 {
303 NTSTATUS Status;
304
305 TRACE("%p\n", hEventLog);
306
307 RpcTryExcept
308 {
309 Status = ElfrCloseEL(&hEventLog);
310 }
311 RpcExcept(EXCEPTION_EXECUTE_HANDLER)
312 {
313 Status = I_RpcMapWin32Status(RpcExceptionCode());
314 }
315 RpcEndExcept;
316
317 if (!NT_SUCCESS(Status))
318 {
319 SetLastError(RtlNtStatusToDosError(Status));
320 return FALSE;
321 }
322
323 return TRUE;
324 }
325
326
327 /******************************************************************************
328 * DeregisterEventSource [ADVAPI32.@]
329 * Closes a handle to the specified event log
330 *
331 * PARAMS
332 * hEventLog [I] Handle to event log
333 *
334 * RETURNS STD
335 */
336 BOOL WINAPI
337 DeregisterEventSource(IN HANDLE hEventLog)
338 {
339 NTSTATUS Status;
340
341 TRACE("%p\n", hEventLog);
342
343 RpcTryExcept
344 {
345 Status = ElfrDeregisterEventSource(&hEventLog);
346 }
347 RpcExcept(EXCEPTION_EXECUTE_HANDLER)
348 {
349 Status = I_RpcMapWin32Status(RpcExceptionCode());
350 }
351 RpcEndExcept;
352
353 if (!NT_SUCCESS(Status))
354 {
355 SetLastError(RtlNtStatusToDosError(Status));
356 return FALSE;
357 }
358
359 return TRUE;
360 }
361
362
363 /******************************************************************************
364 * GetEventLogInformation [ADVAPI32.@]
365 *
366 * PARAMS
367 * hEventLog [I] Handle to event log
368 * dwInfoLevel [I] Level of event log information to return
369 * lpBuffer [O] Buffer that receives the event log information
370 * cbBufSize [I] Size of the lpBuffer buffer
371 * pcbBytesNeeded [O] Required buffer size
372 */
373 BOOL WINAPI
374 GetEventLogInformation(IN HANDLE hEventLog,
375 IN DWORD dwInfoLevel,
376 OUT LPVOID lpBuffer,
377 IN DWORD cbBufSize,
378 OUT LPDWORD pcbBytesNeeded)
379 {
380 UNIMPLEMENTED;
381 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
382 return FALSE;
383 }
384
385
386 /******************************************************************************
387 * GetNumberOfEventLogRecords [ADVAPI32.@]
388 *
389 * PARAMS
390 * hEventLog []
391 * NumberOfRecords []
392 */
393 BOOL WINAPI
394 GetNumberOfEventLogRecords(IN HANDLE hEventLog,
395 OUT PDWORD NumberOfRecords)
396 {
397 NTSTATUS Status;
398 DWORD Records;
399
400 TRACE("%p, %p\n", hEventLog, NumberOfRecords);
401
402 if(!NumberOfRecords)
403 {
404 SetLastError(ERROR_INVALID_PARAMETER);
405 return FALSE;
406 }
407
408 RpcTryExcept
409 {
410 Status = ElfrNumberOfRecords(hEventLog,
411 &Records);
412 }
413 RpcExcept(EXCEPTION_EXECUTE_HANDLER)
414 {
415 Status = I_RpcMapWin32Status(RpcExceptionCode());
416 }
417 RpcEndExcept;
418
419 if (!NT_SUCCESS(Status))
420 {
421 SetLastError(RtlNtStatusToDosError(Status));
422 return FALSE;
423 }
424
425 *NumberOfRecords = Records;
426
427 return TRUE;
428 }
429
430
431 /******************************************************************************
432 * GetOldestEventLogRecord [ADVAPI32.@]
433 *
434 * PARAMS
435 * hEventLog []
436 * OldestRecord []
437 */
438 BOOL WINAPI
439 GetOldestEventLogRecord(IN HANDLE hEventLog,
440 OUT PDWORD OldestRecord)
441 {
442 NTSTATUS Status;
443 DWORD Oldest;
444
445 TRACE("%p, %p\n", hEventLog, OldestRecord);
446
447 if(!OldestRecord)
448 {
449 SetLastError(ERROR_INVALID_PARAMETER);
450 return FALSE;
451 }
452
453 RpcTryExcept
454 {
455 Status = ElfrOldestRecord(hEventLog,
456 &Oldest);
457 }
458 RpcExcept(EXCEPTION_EXECUTE_HANDLER)
459 {
460 Status = I_RpcMapWin32Status(RpcExceptionCode());
461 }
462 RpcEndExcept;
463
464 if (!NT_SUCCESS(Status))
465 {
466 SetLastError(RtlNtStatusToDosError(Status));
467 return FALSE;
468 }
469
470 *OldestRecord = Oldest;
471
472 return TRUE;
473 }
474
475
476 /******************************************************************************
477 * NotifyChangeEventLog [ADVAPI32.@]
478 *
479 * PARAMS
480 * hEventLog []
481 * hEvent []
482 */
483 BOOL WINAPI
484 NotifyChangeEventLog(IN HANDLE hEventLog,
485 IN HANDLE hEvent)
486 {
487 /* Use ElfrChangeNotify */
488 UNIMPLEMENTED;
489 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
490 return FALSE;
491 }
492
493
494 /******************************************************************************
495 * OpenBackupEventLogA [ADVAPI32.@]
496 */
497 HANDLE WINAPI
498 OpenBackupEventLogA(IN LPCSTR lpUNCServerName,
499 IN LPCSTR lpFileName)
500 {
501 UNICODE_STRING UNCServerName;
502 UNICODE_STRING FileName;
503 HANDLE Handle;
504
505 TRACE("%s, %s\n", lpUNCServerName, lpFileName);
506
507 if (!RtlCreateUnicodeStringFromAsciiz(&UNCServerName, lpUNCServerName))
508 {
509 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
510 return NULL;
511 }
512
513 if (!RtlCreateUnicodeStringFromAsciiz(&FileName, lpFileName))
514 {
515 RtlFreeUnicodeString(&UNCServerName);
516 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
517 return NULL;
518 }
519
520 Handle = OpenBackupEventLogW(UNCServerName.Buffer,
521 FileName.Buffer);
522
523 RtlFreeUnicodeString(&UNCServerName);
524 RtlFreeUnicodeString(&FileName);
525
526 return Handle;
527 }
528
529
530 /******************************************************************************
531 * OpenBackupEventLogW [ADVAPI32.@]
532 *
533 * PARAMS
534 * lpUNCServerName []
535 * lpFileName []
536 */
537 HANDLE WINAPI
538 OpenBackupEventLogW(IN LPCWSTR lpUNCServerName,
539 IN LPCWSTR lpFileName)
540 {
541 RPC_UNICODE_STRING FileName;
542 IELF_HANDLE LogHandle;
543 NTSTATUS Status;
544
545 TRACE("%s, %s\n", debugstr_w(lpUNCServerName), debugstr_w(lpFileName));
546
547 FileName.Buffer = (LPWSTR)lpFileName;
548 FileName.Length = FileName.MaximumLength =
549 lpFileName ? wcslen(lpFileName) * sizeof(WCHAR) : 0;
550 FileName.MaximumLength += sizeof(WCHAR);
551
552 RpcTryExcept
553 {
554 Status = ElfrOpenBELW((LPWSTR)lpUNCServerName,
555 &FileName,
556 1,
557 1,
558 &LogHandle);
559 }
560 RpcExcept(EXCEPTION_EXECUTE_HANDLER)
561 {
562 Status = I_RpcMapWin32Status(RpcExceptionCode());
563 }
564 RpcEndExcept;
565
566 if (!NT_SUCCESS(Status))
567 {
568 SetLastError(RtlNtStatusToDosError(Status));
569 return NULL;
570 }
571
572 return (HANDLE)LogHandle;
573 }
574
575
576 /******************************************************************************
577 * OpenEventLogA [ADVAPI32.@]
578 */
579 HANDLE WINAPI
580 OpenEventLogA(IN LPCSTR lpUNCServerName,
581 IN LPCSTR lpSourceName)
582 {
583 UNICODE_STRING UNCServerName;
584 UNICODE_STRING SourceName;
585 HANDLE Handle;
586
587 if (!RtlCreateUnicodeStringFromAsciiz(&UNCServerName, lpUNCServerName))
588 {
589 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
590 return NULL;
591 }
592
593 if (!RtlCreateUnicodeStringFromAsciiz(&SourceName, lpSourceName))
594 {
595 RtlFreeUnicodeString(&UNCServerName);
596 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
597 return NULL;
598 }
599
600 Handle = OpenEventLogW(UNCServerName.Buffer,
601 SourceName.Buffer);
602
603 RtlFreeUnicodeString(&UNCServerName);
604 RtlFreeUnicodeString(&SourceName);
605
606 return Handle;
607 }
608
609
610 /******************************************************************************
611 * OpenEventLogW [ADVAPI32.@]
612 *
613 * PARAMS
614 * lpUNCServerName []
615 * lpSourceName []
616 */
617 HANDLE WINAPI
618 OpenEventLogW(IN LPCWSTR lpUNCServerName,
619 IN LPCWSTR lpSourceName)
620 {
621 RPC_UNICODE_STRING SourceName;
622 IELF_HANDLE LogHandle;
623 NTSTATUS Status;
624
625 TRACE("%s, %s\n", debugstr_w(lpUNCServerName), debugstr_w(lpSourceName));
626
627 SourceName.Buffer = (LPWSTR)lpSourceName;
628 SourceName.Length = SourceName.MaximumLength =
629 lpSourceName ? wcslen(lpSourceName) * sizeof(WCHAR) : 0;
630 SourceName.MaximumLength += sizeof(WCHAR);
631
632 RpcTryExcept
633 {
634 Status = ElfrOpenELW((LPWSTR)lpUNCServerName,
635 &SourceName,
636 &EmptyString,
637 1,
638 1,
639 &LogHandle);
640 }
641 RpcExcept(EXCEPTION_EXECUTE_HANDLER)
642 {
643 Status = I_RpcMapWin32Status(RpcExceptionCode());
644 }
645 RpcEndExcept;
646
647 if (!NT_SUCCESS(Status))
648 {
649 SetLastError(RtlNtStatusToDosError(Status));
650 return NULL;
651 }
652
653 return (HANDLE)LogHandle;
654 }
655
656
657 /******************************************************************************
658 * ReadEventLogA [ADVAPI32.@]
659 */
660 BOOL WINAPI
661 ReadEventLogA(IN HANDLE hEventLog,
662 IN DWORD dwReadFlags,
663 IN DWORD dwRecordOffset,
664 OUT LPVOID lpBuffer,
665 IN DWORD nNumberOfBytesToRead,
666 OUT DWORD *pnBytesRead,
667 OUT DWORD *pnMinNumberOfBytesNeeded)
668 {
669 NTSTATUS Status;
670 DWORD bytesRead, minNumberOfBytesNeeded;
671
672 TRACE("%p, %lu, %lu, %p, %lu, %p, %p\n",
673 hEventLog, dwReadFlags, dwRecordOffset, lpBuffer,
674 nNumberOfBytesToRead, pnBytesRead, pnMinNumberOfBytesNeeded);
675
676 /* If buffer is NULL set nNumberOfBytesToRead to 0 to prevent rpcrt4 from
677 trying to access a null pointer */
678 if (!lpBuffer)
679 {
680 nNumberOfBytesToRead = 0;
681 }
682
683 RpcTryExcept
684 {
685 Status = ElfrReadELA(hEventLog,
686 dwReadFlags,
687 dwRecordOffset,
688 nNumberOfBytesToRead,
689 lpBuffer,
690 &bytesRead,
691 &minNumberOfBytesNeeded);
692 }
693 RpcExcept(EXCEPTION_EXECUTE_HANDLER)
694 {
695 Status = I_RpcMapWin32Status(RpcExceptionCode());
696 }
697 RpcEndExcept;
698
699 *pnBytesRead = (DWORD)bytesRead;
700 *pnMinNumberOfBytesNeeded = (DWORD)minNumberOfBytesNeeded;
701
702 if (!NT_SUCCESS(Status))
703 {
704 SetLastError(RtlNtStatusToDosError(Status));
705 return FALSE;
706 }
707
708 return TRUE;
709 }
710
711
712 /******************************************************************************
713 * ReadEventLogW [ADVAPI32.@]
714 *
715 * PARAMS
716 * hEventLog []
717 * dwReadFlags []
718 * dwRecordOffset []
719 * lpBuffer []
720 * nNumberOfBytesToRead []
721 * pnBytesRead []
722 * pnMinNumberOfBytesNeeded []
723 */
724 BOOL WINAPI
725 ReadEventLogW(IN HANDLE hEventLog,
726 IN DWORD dwReadFlags,
727 IN DWORD dwRecordOffset,
728 OUT LPVOID lpBuffer,
729 IN DWORD nNumberOfBytesToRead,
730 OUT DWORD *pnBytesRead,
731 OUT DWORD *pnMinNumberOfBytesNeeded)
732 {
733 NTSTATUS Status;
734 DWORD bytesRead, minNumberOfBytesNeeded;
735
736 TRACE("%p, %lu, %lu, %p, %lu, %p, %p\n",
737 hEventLog, dwReadFlags, dwRecordOffset, lpBuffer,
738 nNumberOfBytesToRead, pnBytesRead, pnMinNumberOfBytesNeeded);
739
740 /* If buffer is NULL set nNumberOfBytesToRead to 0 to prevent rpcrt4 from
741 trying to access a null pointer */
742 if (!lpBuffer)
743 {
744 nNumberOfBytesToRead = 0;
745 }
746
747 RpcTryExcept
748 {
749 Status = ElfrReadELW(hEventLog,
750 dwReadFlags,
751 dwRecordOffset,
752 nNumberOfBytesToRead,
753 lpBuffer,
754 &bytesRead,
755 &minNumberOfBytesNeeded);
756 }
757 RpcExcept(EXCEPTION_EXECUTE_HANDLER)
758 {
759 Status = I_RpcMapWin32Status(RpcExceptionCode());
760 }
761 RpcEndExcept;
762
763 *pnBytesRead = (DWORD)bytesRead;
764 *pnMinNumberOfBytesNeeded = (DWORD)minNumberOfBytesNeeded;
765
766 if (!NT_SUCCESS(Status))
767 {
768 SetLastError(RtlNtStatusToDosError(Status));
769 return FALSE;
770 }
771
772 return TRUE;
773 }
774
775
776 /******************************************************************************
777 * RegisterEventSourceA [ADVAPI32.@]
778 */
779 HANDLE WINAPI
780 RegisterEventSourceA(IN LPCSTR lpUNCServerName,
781 IN LPCSTR lpSourceName)
782 {
783 UNICODE_STRING UNCServerName;
784 UNICODE_STRING SourceName;
785 HANDLE Handle;
786
787 TRACE("%s, %s\n", lpUNCServerName, lpSourceName);
788
789 if (!RtlCreateUnicodeStringFromAsciiz(&UNCServerName, lpUNCServerName))
790 {
791 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
792 return NULL;
793 }
794
795 if (!RtlCreateUnicodeStringFromAsciiz(&SourceName, lpSourceName))
796 {
797 RtlFreeUnicodeString(&UNCServerName);
798 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
799 return NULL;
800 }
801
802 Handle = RegisterEventSourceW(UNCServerName.Buffer,
803 SourceName.Buffer);
804
805 RtlFreeUnicodeString(&UNCServerName);
806 RtlFreeUnicodeString(&SourceName);
807
808 return Handle;
809 }
810
811
812 /******************************************************************************
813 * RegisterEventSourceW [ADVAPI32.@]
814 * Returns a registered handle to an event log
815 *
816 * PARAMS
817 * lpUNCServerName [I] Server name for source
818 * lpSourceName [I] Source name for registered handle
819 *
820 * RETURNS
821 * Success: Handle
822 * Failure: NULL
823 */
824 HANDLE WINAPI
825 RegisterEventSourceW(IN LPCWSTR lpUNCServerName,
826 IN LPCWSTR lpSourceName)
827 {
828 RPC_UNICODE_STRING SourceName;
829 IELF_HANDLE LogHandle;
830 NTSTATUS Status;
831
832 TRACE("%s, %s\n", debugstr_w(lpUNCServerName), debugstr_w(lpSourceName));
833
834 SourceName.Buffer = (LPWSTR)lpSourceName;
835 SourceName.Length = SourceName.MaximumLength =
836 lpSourceName ? wcslen(lpSourceName) * sizeof(WCHAR) : 0;
837 SourceName.MaximumLength += sizeof(WCHAR);
838
839 RpcTryExcept
840 {
841 Status = ElfrRegisterEventSourceW((LPWSTR)lpUNCServerName,
842 &SourceName,
843 &EmptyString,
844 1,
845 1,
846 &LogHandle);
847 }
848 RpcExcept(EXCEPTION_EXECUTE_HANDLER)
849 {
850 Status = I_RpcMapWin32Status(RpcExceptionCode());
851 }
852 RpcEndExcept;
853
854 if (!NT_SUCCESS(Status))
855 {
856 SetLastError(RtlNtStatusToDosError(Status));
857 return NULL;
858 }
859
860 return (HANDLE)LogHandle;
861 }
862
863
864 /******************************************************************************
865 * ReportEventA [ADVAPI32.@]
866 */
867 BOOL WINAPI
868 ReportEventA(IN HANDLE hEventLog,
869 IN WORD wType,
870 IN WORD wCategory,
871 IN DWORD dwEventID,
872 IN PSID lpUserSid,
873 IN WORD wNumStrings,
874 IN DWORD dwDataSize,
875 IN LPCSTR *lpStrings,
876 IN LPVOID lpRawData)
877 {
878 LPCWSTR *wideStrArray;
879 UNICODE_STRING str;
880 WORD i;
881 BOOL ret;
882
883 if (wNumStrings == 0)
884 return TRUE;
885
886 if (lpStrings == NULL)
887 return TRUE;
888
889 wideStrArray = HeapAlloc(GetProcessHeap(),
890 HEAP_ZERO_MEMORY,
891 sizeof(LPCWSTR) * wNumStrings);
892
893 for (i = 0; i < wNumStrings; i++)
894 {
895 if (!RtlCreateUnicodeStringFromAsciiz(&str, (PSTR)lpStrings[i]))
896 break;
897 wideStrArray[i] = str.Buffer;
898 }
899
900 if (i == wNumStrings)
901 {
902 ret = ReportEventW(hEventLog,
903 wType,
904 wCategory,
905 dwEventID,
906 lpUserSid,
907 wNumStrings,
908 dwDataSize,
909 wideStrArray,
910 lpRawData);
911 }
912 else
913 {
914 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
915 ret = FALSE;
916 }
917
918 for (i = 0; i < wNumStrings; i++)
919 {
920 if (wideStrArray[i])
921 {
922 HeapFree(GetProcessHeap(),
923 0,
924 (PVOID)wideStrArray[i]);
925 }
926 }
927
928 HeapFree(GetProcessHeap(),
929 0,
930 (PVOID)wideStrArray);
931
932 return ret;
933 }
934
935
936 /******************************************************************************
937 * ReportEventW [ADVAPI32.@]
938 *
939 * PARAMS
940 * hEventLog []
941 * wType []
942 * wCategory []
943 * dwEventID []
944 * lpUserSid []
945 * wNumStrings []
946 * dwDataSize []
947 * lpStrings []
948 * lpRawData []
949 */
950 BOOL WINAPI
951 ReportEventW(IN HANDLE hEventLog,
952 IN WORD wType,
953 IN WORD wCategory,
954 IN DWORD dwEventID,
955 IN PSID lpUserSid,
956 IN WORD wNumStrings,
957 IN DWORD dwDataSize,
958 IN LPCWSTR *lpStrings,
959 IN LPVOID lpRawData)
960 {
961 NTSTATUS Status;
962 UNICODE_STRING *Strings;
963 UNICODE_STRING ComputerName;
964 WORD i;
965
966 TRACE("%p, %u, %u, %lu, %p, %u, %lu, %p, %p\n",
967 hEventLog, wType, wCategory, dwEventID, lpUserSid,
968 wNumStrings, dwDataSize, lpStrings, lpRawData);
969
970 Strings = HeapAlloc(GetProcessHeap(),
971 0,
972 wNumStrings * sizeof(UNICODE_STRING));
973 if (!Strings)
974 {
975 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
976 return FALSE;
977 }
978
979 for (i = 0; i < wNumStrings; i++)
980 RtlInitUnicodeString(&Strings[i], lpStrings[i]);
981
982 /*FIXME: ComputerName */
983 RtlInitUnicodeString(&ComputerName, L"");
984
985 RpcTryExcept
986 {
987 Status = ElfrReportEventW(hEventLog,
988 0, /* FIXME: Time */
989 wType,
990 wCategory,
991 dwEventID,
992 wNumStrings,
993 dwDataSize,
994 (PRPC_UNICODE_STRING) &ComputerName,
995 lpUserSid,
996 (PRPC_UNICODE_STRING*) &Strings,
997 lpRawData,
998 0,
999 NULL,
1000 NULL);
1001 }
1002 RpcExcept(EXCEPTION_EXECUTE_HANDLER)
1003 {
1004 Status = I_RpcMapWin32Status(RpcExceptionCode());
1005 }
1006 RpcEndExcept;
1007
1008 HeapFree(GetProcessHeap(), 0, Strings);
1009
1010 if (!NT_SUCCESS(Status))
1011 {
1012 SetLastError(RtlNtStatusToDosError(Status));
1013 return FALSE;
1014 }
1015
1016 return TRUE;
1017 }