- ReadEventLogA/ReadEventLogW: Check for lpBuffer for NULL and if so set nNumberOfByt...
[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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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
158 RpcTryExcept
159 {
160 Status = ElfrBackupELFA(hEventLog,
161 &BackupFileName);
162 }
163 RpcExcept(EXCEPTION_EXECUTE_HANDLER)
164 {
165 Status = I_RpcMapWin32Status(RpcExceptionCode());
166 }
167 RpcEndExcept;
168
169 if (!NT_SUCCESS(Status))
170 {
171 SetLastError(RtlNtStatusToDosError(Status));
172 return FALSE;
173 }
174
175 return TRUE;
176 }
177
178 /******************************************************************************
179 * BackupEventLogW [ADVAPI32.@]
180 *
181 * PARAMS
182 * hEventLog []
183 * lpBackupFileName []
184 */
185 BOOL WINAPI
186 BackupEventLogW(IN HANDLE hEventLog,
187 IN LPCWSTR lpBackupFileName)
188 {
189 RPC_UNICODE_STRING BackupFileName;
190 NTSTATUS Status;
191
192 TRACE("%p, %s\n", hEventLog, debugstr_w(lpBackupFileName));
193
194 BackupFileName.Buffer = (LPWSTR)lpBackupFileName;
195 BackupFileName.Length = BackupFileName.MaximumLength =
196 lpBackupFileName ? wcslen(lpBackupFileName) * sizeof(WCHAR) : 0;
197
198 RpcTryExcept
199 {
200 Status = ElfrBackupELFW(hEventLog,
201 &BackupFileName);
202 }
203 RpcExcept(EXCEPTION_EXECUTE_HANDLER)
204 {
205 Status = I_RpcMapWin32Status(RpcExceptionCode());
206 }
207 RpcEndExcept;
208
209 if (!NT_SUCCESS(Status))
210 {
211 SetLastError(RtlNtStatusToDosError(Status));
212 return FALSE;
213 }
214
215 return TRUE;
216 }
217
218
219 /******************************************************************************
220 * ClearEventLogA [ADVAPI32.@]
221 */
222 BOOL WINAPI
223 ClearEventLogA(IN HANDLE hEventLog,
224 IN LPCSTR lpBackupFileName)
225 {
226 RPC_STRING BackupFileName;
227 NTSTATUS Status;
228
229 TRACE("%p, %s\n", hEventLog, lpBackupFileName);
230
231 BackupFileName.Buffer = (LPSTR)lpBackupFileName;
232 BackupFileName.Length = BackupFileName.MaximumLength =
233 lpBackupFileName ? strlen(lpBackupFileName) : 0;
234
235 RpcTryExcept
236 {
237 Status = ElfrClearELFA(hEventLog,
238 &BackupFileName);
239 }
240 RpcExcept(EXCEPTION_EXECUTE_HANDLER)
241 {
242 Status = I_RpcMapWin32Status(RpcExceptionCode());
243 }
244 RpcEndExcept;
245
246 if (!NT_SUCCESS(Status))
247 {
248 SetLastError(RtlNtStatusToDosError(Status));
249 return FALSE;
250 }
251
252 return TRUE;
253 }
254
255
256 /******************************************************************************
257 * ClearEventLogW [ADVAPI32.@]
258 */
259 BOOL WINAPI
260 ClearEventLogW(IN HANDLE hEventLog,
261 IN LPCWSTR lpBackupFileName)
262 {
263 RPC_UNICODE_STRING BackupFileName;
264 NTSTATUS Status;
265
266 TRACE("%p, %s\n", hEventLog, debugstr_w(lpBackupFileName));
267
268 BackupFileName.Buffer = (LPWSTR)lpBackupFileName;
269 BackupFileName.Length = BackupFileName.MaximumLength =
270 lpBackupFileName ? wcslen(lpBackupFileName) * sizeof(WCHAR) : 0;
271
272 RpcTryExcept
273 {
274 Status = ElfrClearELFW(hEventLog,
275 &BackupFileName);
276 }
277 RpcExcept(EXCEPTION_EXECUTE_HANDLER)
278 {
279 Status = I_RpcMapWin32Status(RpcExceptionCode());
280 }
281 RpcEndExcept;
282
283 if (!NT_SUCCESS(Status))
284 {
285 SetLastError(RtlNtStatusToDosError(Status));
286 return FALSE;
287 }
288
289 return TRUE;
290 }
291
292
293 /******************************************************************************
294 * CloseEventLog [ADVAPI32.@]
295 */
296 BOOL WINAPI
297 CloseEventLog(IN HANDLE hEventLog)
298 {
299 NTSTATUS Status;
300
301 TRACE("%p\n", hEventLog);
302
303 RpcTryExcept
304 {
305 Status = ElfrCloseEL(&hEventLog);
306 }
307 RpcExcept(EXCEPTION_EXECUTE_HANDLER)
308 {
309 Status = I_RpcMapWin32Status(RpcExceptionCode());
310 }
311 RpcEndExcept;
312
313 if (!NT_SUCCESS(Status))
314 {
315 SetLastError(RtlNtStatusToDosError(Status));
316 return FALSE;
317 }
318
319 return TRUE;
320 }
321
322
323 /******************************************************************************
324 * DeregisterEventSource [ADVAPI32.@]
325 * Closes a handle to the specified event log
326 *
327 * PARAMS
328 * hEventLog [I] Handle to event log
329 *
330 * RETURNS STD
331 */
332 BOOL WINAPI
333 DeregisterEventSource(IN HANDLE hEventLog)
334 {
335 NTSTATUS Status;
336
337 TRACE("%p\n", hEventLog);
338
339 RpcTryExcept
340 {
341 Status = ElfrDeregisterEventSource(&hEventLog);
342 }
343 RpcExcept(EXCEPTION_EXECUTE_HANDLER)
344 {
345 Status = I_RpcMapWin32Status(RpcExceptionCode());
346 }
347 RpcEndExcept;
348
349 if (!NT_SUCCESS(Status))
350 {
351 SetLastError(RtlNtStatusToDosError(Status));
352 return FALSE;
353 }
354
355 return TRUE;
356 }
357
358
359 /******************************************************************************
360 * GetNumberOfEventLogRecords [ADVAPI32.@]
361 *
362 * PARAMS
363 * hEventLog []
364 * NumberOfRecords []
365 */
366 BOOL WINAPI
367 GetNumberOfEventLogRecords(IN HANDLE hEventLog,
368 OUT PDWORD NumberOfRecords)
369 {
370 NTSTATUS Status;
371 DWORD Records;
372
373 TRACE("%p, %p\n", hEventLog, NumberOfRecords);
374
375 RpcTryExcept
376 {
377 Status = ElfrNumberOfRecords(hEventLog,
378 &Records);
379 }
380 RpcExcept(EXCEPTION_EXECUTE_HANDLER)
381 {
382 Status = I_RpcMapWin32Status(RpcExceptionCode());
383 }
384 RpcEndExcept;
385
386 if (!NT_SUCCESS(Status))
387 {
388 SetLastError(RtlNtStatusToDosError(Status));
389 return FALSE;
390 }
391
392 *NumberOfRecords = Records;
393
394 return TRUE;
395 }
396
397
398 /******************************************************************************
399 * GetOldestEventLogRecord [ADVAPI32.@]
400 *
401 * PARAMS
402 * hEventLog []
403 * OldestRecord []
404 */
405 BOOL WINAPI
406 GetOldestEventLogRecord(IN HANDLE hEventLog,
407 OUT PDWORD OldestRecord)
408 {
409 NTSTATUS Status;
410 DWORD Oldest;
411
412 TRACE("%p, %p\n", hEventLog, OldestRecord);
413
414 RpcTryExcept
415 {
416 Status = ElfrOldestRecord(hEventLog,
417 &Oldest);
418 }
419 RpcExcept(EXCEPTION_EXECUTE_HANDLER)
420 {
421 Status = I_RpcMapWin32Status(RpcExceptionCode());
422 }
423 RpcEndExcept;
424
425 if (!NT_SUCCESS(Status))
426 {
427 SetLastError(RtlNtStatusToDosError(Status));
428 return FALSE;
429 }
430
431 *OldestRecord = Oldest;
432
433 return TRUE;
434 }
435
436
437 /******************************************************************************
438 * NotifyChangeEventLog [ADVAPI32.@]
439 *
440 * PARAMS
441 * hEventLog []
442 * hEvent []
443 */
444 BOOL WINAPI
445 NotifyChangeEventLog(IN HANDLE hEventLog,
446 IN HANDLE hEvent)
447 {
448 /* Use ElfrChangeNotify */
449 UNIMPLEMENTED;
450 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
451 return FALSE;
452 }
453
454
455 /******************************************************************************
456 * OpenBackupEventLogA [ADVAPI32.@]
457 */
458 HANDLE WINAPI
459 OpenBackupEventLogA(IN LPCSTR lpUNCServerName,
460 IN LPCSTR lpFileName)
461 {
462 UNICODE_STRING UNCServerName;
463 UNICODE_STRING FileName;
464 HANDLE Handle;
465
466 TRACE("%s, %s\n", lpUNCServerName, lpFileName);
467
468 if (!RtlCreateUnicodeStringFromAsciiz(&UNCServerName, lpUNCServerName))
469 {
470 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
471 return NULL;
472 }
473
474 if (!RtlCreateUnicodeStringFromAsciiz(&FileName, lpFileName))
475 {
476 RtlFreeUnicodeString(&UNCServerName);
477 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
478 return NULL;
479 }
480
481 Handle = OpenBackupEventLogW(UNCServerName.Buffer,
482 FileName.Buffer);
483
484 RtlFreeUnicodeString(&UNCServerName);
485 RtlFreeUnicodeString(&FileName);
486
487 return Handle;
488 }
489
490
491 /******************************************************************************
492 * OpenBackupEventLogW [ADVAPI32.@]
493 *
494 * PARAMS
495 * lpUNCServerName []
496 * lpFileName []
497 */
498 HANDLE WINAPI
499 OpenBackupEventLogW(IN LPCWSTR lpUNCServerName,
500 IN LPCWSTR lpFileName)
501 {
502 RPC_UNICODE_STRING FileName;
503 IELF_HANDLE LogHandle;
504 NTSTATUS Status;
505
506 TRACE("%s, %s\n", debugstr_w(lpUNCServerName), debugstr_w(lpFileName));
507
508 FileName.Buffer = (LPWSTR)lpFileName;
509 FileName.Length = FileName.MaximumLength =
510 lpFileName ? wcslen(lpFileName) * sizeof(WCHAR) : 0;
511
512 RpcTryExcept
513 {
514 Status = ElfrOpenBELW((LPWSTR)lpUNCServerName,
515 &FileName,
516 0,
517 0,
518 &LogHandle);
519 }
520 RpcExcept(EXCEPTION_EXECUTE_HANDLER)
521 {
522 Status = I_RpcMapWin32Status(RpcExceptionCode());
523 }
524 RpcEndExcept;
525
526 if (!NT_SUCCESS(Status))
527 {
528 SetLastError(RtlNtStatusToDosError(Status));
529 return NULL;
530 }
531
532 return (HANDLE)LogHandle;
533 }
534
535
536 /******************************************************************************
537 * OpenEventLogA [ADVAPI32.@]
538 */
539 HANDLE WINAPI
540 OpenEventLogA(IN LPCSTR lpUNCServerName,
541 IN LPCSTR lpSourceName)
542 {
543 UNICODE_STRING UNCServerName;
544 UNICODE_STRING SourceName;
545 HANDLE Handle;
546
547 if (!RtlCreateUnicodeStringFromAsciiz(&UNCServerName, lpUNCServerName))
548 {
549 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
550 return NULL;
551 }
552
553 if (!RtlCreateUnicodeStringFromAsciiz(&SourceName, lpSourceName))
554 {
555 RtlFreeUnicodeString(&UNCServerName);
556 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
557 return NULL;
558 }
559
560 Handle = OpenEventLogW(UNCServerName.Buffer,
561 SourceName.Buffer);
562
563 RtlFreeUnicodeString(&UNCServerName);
564 RtlFreeUnicodeString(&SourceName);
565
566 return Handle;
567 }
568
569
570 /******************************************************************************
571 * OpenEventLogW [ADVAPI32.@]
572 *
573 * PARAMS
574 * lpUNCServerName []
575 * lpSourceName []
576 */
577 HANDLE WINAPI
578 OpenEventLogW(IN LPCWSTR lpUNCServerName,
579 IN LPCWSTR lpSourceName)
580 {
581 RPC_UNICODE_STRING SourceName;
582 IELF_HANDLE LogHandle;
583 NTSTATUS Status;
584
585 TRACE("%s, %s\n", debugstr_w(lpUNCServerName), debugstr_w(lpSourceName));
586
587 SourceName.Buffer = (LPWSTR)lpSourceName;
588 SourceName.Length = SourceName.MaximumLength =
589 lpSourceName ? wcslen(lpSourceName) * sizeof(WCHAR) : 0;
590
591 RpcTryExcept
592 {
593 Status = ElfrOpenELW((LPWSTR)lpUNCServerName,
594 &SourceName,
595 &EmptyString,
596 0,
597 0,
598 &LogHandle);
599 }
600 RpcExcept(EXCEPTION_EXECUTE_HANDLER)
601 {
602 Status = I_RpcMapWin32Status(RpcExceptionCode());
603 }
604 RpcEndExcept;
605
606 if (!NT_SUCCESS(Status))
607 {
608 SetLastError(RtlNtStatusToDosError(Status));
609 return NULL;
610 }
611
612 return (HANDLE)LogHandle;
613 }
614
615
616 /******************************************************************************
617 * ReadEventLogA [ADVAPI32.@]
618 */
619 BOOL WINAPI
620 ReadEventLogA(IN HANDLE hEventLog,
621 IN DWORD dwReadFlags,
622 IN DWORD dwRecordOffset,
623 OUT LPVOID lpBuffer,
624 IN DWORD nNumberOfBytesToRead,
625 OUT DWORD *pnBytesRead,
626 OUT DWORD *pnMinNumberOfBytesNeeded)
627 {
628 NTSTATUS Status;
629 DWORD bytesRead, minNumberOfBytesNeeded;
630
631 TRACE("%p, %lu, %lu, %p, %lu, %p, %p\n",
632 hEventLog, dwReadFlags, dwRecordOffset, lpBuffer,
633 nNumberOfBytesToRead, pnBytesRead, pnMinNumberOfBytesNeeded);
634
635 /* If buffer is NULL set nNumberOfBytesToRead to 0 to prevent rpcrt4 from
636 trying to access a null pointer */
637 if (!lpBuffer)
638 {
639 nNumberOfBytesToRead = 0;
640 }
641
642 RpcTryExcept
643 {
644 Status = ElfrReadELA(hEventLog,
645 dwReadFlags,
646 dwRecordOffset,
647 nNumberOfBytesToRead,
648 lpBuffer,
649 &bytesRead,
650 &minNumberOfBytesNeeded);
651 }
652 RpcExcept(EXCEPTION_EXECUTE_HANDLER)
653 {
654 Status = I_RpcMapWin32Status(RpcExceptionCode());
655 }
656 RpcEndExcept;
657
658 *pnBytesRead = (DWORD)bytesRead;
659 *pnMinNumberOfBytesNeeded = (DWORD)minNumberOfBytesNeeded;
660
661 if (!NT_SUCCESS(Status))
662 {
663 SetLastError(RtlNtStatusToDosError(Status));
664 return FALSE;
665 }
666
667 return TRUE;
668 }
669
670
671 /******************************************************************************
672 * ReadEventLogW [ADVAPI32.@]
673 *
674 * PARAMS
675 * hEventLog []
676 * dwReadFlags []
677 * dwRecordOffset []
678 * lpBuffer []
679 * nNumberOfBytesToRead []
680 * pnBytesRead []
681 * pnMinNumberOfBytesNeeded []
682 */
683 BOOL WINAPI
684 ReadEventLogW(IN HANDLE hEventLog,
685 IN DWORD dwReadFlags,
686 IN DWORD dwRecordOffset,
687 OUT LPVOID lpBuffer,
688 IN DWORD nNumberOfBytesToRead,
689 OUT DWORD *pnBytesRead,
690 OUT DWORD *pnMinNumberOfBytesNeeded)
691 {
692 NTSTATUS Status;
693 DWORD bytesRead, minNumberOfBytesNeeded;
694
695 TRACE("%p, %lu, %lu, %p, %lu, %p, %p\n",
696 hEventLog, dwReadFlags, dwRecordOffset, lpBuffer,
697 nNumberOfBytesToRead, pnBytesRead, pnMinNumberOfBytesNeeded);
698
699 /* If buffer is NULL set nNumberOfBytesToRead to 0 to prevent rpcrt4 from
700 trying to access a null pointer */
701 if (!lpBuffer)
702 {
703 nNumberOfBytesToRead = 0;
704 }
705
706 RpcTryExcept
707 {
708 Status = ElfrReadELW(hEventLog,
709 dwReadFlags,
710 dwRecordOffset,
711 nNumberOfBytesToRead,
712 lpBuffer,
713 &bytesRead,
714 &minNumberOfBytesNeeded);
715 }
716 RpcExcept(EXCEPTION_EXECUTE_HANDLER)
717 {
718 Status = I_RpcMapWin32Status(RpcExceptionCode());
719 }
720 RpcEndExcept;
721
722 *pnBytesRead = (DWORD)bytesRead;
723 *pnMinNumberOfBytesNeeded = (DWORD)minNumberOfBytesNeeded;
724
725 if (!NT_SUCCESS(Status))
726 {
727 SetLastError(RtlNtStatusToDosError(Status));
728 return FALSE;
729 }
730
731 return TRUE;
732 }
733
734
735 /******************************************************************************
736 * RegisterEventSourceA [ADVAPI32.@]
737 */
738 HANDLE WINAPI
739 RegisterEventSourceA(IN LPCSTR lpUNCServerName,
740 IN LPCSTR lpSourceName)
741 {
742 UNICODE_STRING UNCServerName;
743 UNICODE_STRING SourceName;
744 HANDLE Handle;
745
746 TRACE("%s, %s\n", lpUNCServerName, lpSourceName);
747
748 if (!RtlCreateUnicodeStringFromAsciiz(&UNCServerName, lpUNCServerName))
749 {
750 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
751 return NULL;
752 }
753
754 if (!RtlCreateUnicodeStringFromAsciiz(&SourceName, lpSourceName))
755 {
756 RtlFreeUnicodeString(&UNCServerName);
757 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
758 return NULL;
759 }
760
761 Handle = RegisterEventSourceW(UNCServerName.Buffer,
762 SourceName.Buffer);
763
764 RtlFreeUnicodeString(&UNCServerName);
765 RtlFreeUnicodeString(&SourceName);
766
767 return Handle;
768 }
769
770
771 /******************************************************************************
772 * RegisterEventSourceW [ADVAPI32.@]
773 * Returns a registered handle to an event log
774 *
775 * PARAMS
776 * lpUNCServerName [I] Server name for source
777 * lpSourceName [I] Source name for registered handle
778 *
779 * RETURNS
780 * Success: Handle
781 * Failure: NULL
782 */
783 HANDLE WINAPI
784 RegisterEventSourceW(IN LPCWSTR lpUNCServerName,
785 IN LPCWSTR lpSourceName)
786 {
787 RPC_UNICODE_STRING SourceName;
788 IELF_HANDLE LogHandle;
789 NTSTATUS Status;
790
791 TRACE("%s, %s\n", debugstr_w(lpUNCServerName), debugstr_w(lpSourceName));
792
793 SourceName.Buffer = (LPWSTR)lpSourceName;
794 SourceName.Length = SourceName.MaximumLength =
795 lpSourceName ? wcslen(lpSourceName) * sizeof(WCHAR) : 0;
796
797 RpcTryExcept
798 {
799 Status = ElfrRegisterEventSourceW((LPWSTR)lpUNCServerName,
800 &SourceName,
801 &EmptyString,
802 0,
803 0,
804 &LogHandle);
805 }
806 RpcExcept(EXCEPTION_EXECUTE_HANDLER)
807 {
808 Status = I_RpcMapWin32Status(RpcExceptionCode());
809 }
810 RpcEndExcept;
811
812 if (!NT_SUCCESS(Status))
813 {
814 SetLastError(RtlNtStatusToDosError(Status));
815 return NULL;
816 }
817
818 return (HANDLE)LogHandle;
819 }
820
821
822 /******************************************************************************
823 * ReportEventA [ADVAPI32.@]
824 */
825 BOOL WINAPI
826 ReportEventA(IN HANDLE hEventLog,
827 IN WORD wType,
828 IN WORD wCategory,
829 IN DWORD dwEventID,
830 IN PSID lpUserSid,
831 IN WORD wNumStrings,
832 IN DWORD dwDataSize,
833 IN LPCSTR *lpStrings,
834 IN LPVOID lpRawData)
835 {
836 LPCWSTR *wideStrArray;
837 UNICODE_STRING str;
838 WORD i;
839 BOOL ret;
840
841 if (wNumStrings == 0)
842 return TRUE;
843
844 if (lpStrings == NULL)
845 return TRUE;
846
847 wideStrArray = HeapAlloc(GetProcessHeap(),
848 HEAP_ZERO_MEMORY,
849 sizeof(LPCWSTR) * wNumStrings);
850
851 for (i = 0; i < wNumStrings; i++)
852 {
853 if (!RtlCreateUnicodeStringFromAsciiz(&str, (PSTR)lpStrings[i]))
854 break;
855 wideStrArray[i] = str.Buffer;
856 }
857
858 if (i == wNumStrings)
859 {
860 ret = ReportEventW(hEventLog,
861 wType,
862 wCategory,
863 dwEventID,
864 lpUserSid,
865 wNumStrings,
866 dwDataSize,
867 wideStrArray,
868 lpRawData);
869 }
870 else
871 {
872 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
873 ret = FALSE;
874 }
875
876 for (i = 0; i < wNumStrings; i++)
877 {
878 if (wideStrArray[i])
879 {
880 HeapFree(GetProcessHeap(),
881 0,
882 (PVOID)wideStrArray[i]);
883 }
884 }
885
886 HeapFree(GetProcessHeap(),
887 0,
888 (PVOID)wideStrArray);
889
890 return ret;
891 }
892
893
894 /******************************************************************************
895 * ReportEventW [ADVAPI32.@]
896 *
897 * PARAMS
898 * hEventLog []
899 * wType []
900 * wCategory []
901 * dwEventID []
902 * lpUserSid []
903 * wNumStrings []
904 * dwDataSize []
905 * lpStrings []
906 * lpRawData []
907 */
908 BOOL WINAPI
909 ReportEventW(IN HANDLE hEventLog,
910 IN WORD wType,
911 IN WORD wCategory,
912 IN DWORD dwEventID,
913 IN PSID lpUserSid,
914 IN WORD wNumStrings,
915 IN DWORD dwDataSize,
916 IN LPCWSTR *lpStrings,
917 IN LPVOID lpRawData)
918 {
919 NTSTATUS Status;
920 UNICODE_STRING *Strings;
921 UNICODE_STRING ComputerName;
922 WORD i;
923
924 TRACE("%p, %u, %u, %lu, %p, %u, %lu, %p, %p\n",
925 hEventLog, wType, wCategory, dwEventID, lpUserSid,
926 wNumStrings, dwDataSize, lpStrings, lpRawData);
927
928 Strings = HeapAlloc(GetProcessHeap(),
929 0,
930 wNumStrings * sizeof(UNICODE_STRING));
931 if (!Strings)
932 {
933 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
934 return FALSE;
935 }
936
937 for (i = 0; i < wNumStrings; i++)
938 RtlInitUnicodeString(&Strings[i], lpStrings[i]);
939
940 /*FIXME: ComputerName */
941 RtlInitEmptyUnicodeString(&ComputerName, NULL, 0);
942
943 RpcTryExcept
944 {
945 Status = ElfrReportEventW(hEventLog,
946 0, /* FIXME: Time */
947 wType,
948 wCategory,
949 dwEventID,
950 wNumStrings,
951 dwDataSize,
952 (PRPC_UNICODE_STRING) &ComputerName,
953 lpUserSid,
954 (PRPC_UNICODE_STRING*) &Strings,
955 lpRawData,
956 0,
957 NULL,
958 NULL);
959 }
960 RpcExcept(EXCEPTION_EXECUTE_HANDLER)
961 {
962 Status = I_RpcMapWin32Status(RpcExceptionCode());
963 }
964 RpcEndExcept;
965
966 HeapFree(GetProcessHeap(), 0, Strings);
967
968 if (!NT_SUCCESS(Status))
969 {
970 SetLastError(RtlNtStatusToDosError(Status));
971 return FALSE;
972 }
973
974 return TRUE;
975 }