Add parameters check to ReadEventLog.
[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(!pnBytesRead || !pnMinNumberOfBytesNeeded)
677 {
678 SetLastError(ERROR_INVALID_PARAMETER);
679 return FALSE;
680 }
681
682 /* If buffer is NULL set nNumberOfBytesToRead to 0 to prevent rpcrt4 from
683 trying to access a null pointer */
684 if (!lpBuffer)
685 {
686 nNumberOfBytesToRead = 0;
687 }
688
689 RpcTryExcept
690 {
691 Status = ElfrReadELA(hEventLog,
692 dwReadFlags,
693 dwRecordOffset,
694 nNumberOfBytesToRead,
695 lpBuffer,
696 &bytesRead,
697 &minNumberOfBytesNeeded);
698 }
699 RpcExcept(EXCEPTION_EXECUTE_HANDLER)
700 {
701 Status = I_RpcMapWin32Status(RpcExceptionCode());
702 }
703 RpcEndExcept;
704
705 *pnBytesRead = (DWORD)bytesRead;
706 *pnMinNumberOfBytesNeeded = (DWORD)minNumberOfBytesNeeded;
707
708 if (!NT_SUCCESS(Status))
709 {
710 SetLastError(RtlNtStatusToDosError(Status));
711 return FALSE;
712 }
713
714 return TRUE;
715 }
716
717
718 /******************************************************************************
719 * ReadEventLogW [ADVAPI32.@]
720 *
721 * PARAMS
722 * hEventLog []
723 * dwReadFlags []
724 * dwRecordOffset []
725 * lpBuffer []
726 * nNumberOfBytesToRead []
727 * pnBytesRead []
728 * pnMinNumberOfBytesNeeded []
729 */
730 BOOL WINAPI
731 ReadEventLogW(IN HANDLE hEventLog,
732 IN DWORD dwReadFlags,
733 IN DWORD dwRecordOffset,
734 OUT LPVOID lpBuffer,
735 IN DWORD nNumberOfBytesToRead,
736 OUT DWORD *pnBytesRead,
737 OUT DWORD *pnMinNumberOfBytesNeeded)
738 {
739 NTSTATUS Status;
740 DWORD bytesRead, minNumberOfBytesNeeded;
741
742 TRACE("%p, %lu, %lu, %p, %lu, %p, %p\n",
743 hEventLog, dwReadFlags, dwRecordOffset, lpBuffer,
744 nNumberOfBytesToRead, pnBytesRead, pnMinNumberOfBytesNeeded);
745
746 if(!pnBytesRead || !pnMinNumberOfBytesNeeded)
747 {
748 SetLastError(ERROR_INVALID_PARAMETER);
749 return FALSE;
750 }
751
752 /* If buffer is NULL set nNumberOfBytesToRead to 0 to prevent rpcrt4 from
753 trying to access a null pointer */
754 if (!lpBuffer)
755 {
756 nNumberOfBytesToRead = 0;
757 }
758
759 RpcTryExcept
760 {
761 Status = ElfrReadELW(hEventLog,
762 dwReadFlags,
763 dwRecordOffset,
764 nNumberOfBytesToRead,
765 lpBuffer,
766 &bytesRead,
767 &minNumberOfBytesNeeded);
768 }
769 RpcExcept(EXCEPTION_EXECUTE_HANDLER)
770 {
771 Status = I_RpcMapWin32Status(RpcExceptionCode());
772 }
773 RpcEndExcept;
774
775 *pnBytesRead = (DWORD)bytesRead;
776 *pnMinNumberOfBytesNeeded = (DWORD)minNumberOfBytesNeeded;
777
778 if (!NT_SUCCESS(Status))
779 {
780 SetLastError(RtlNtStatusToDosError(Status));
781 return FALSE;
782 }
783
784 return TRUE;
785 }
786
787
788 /******************************************************************************
789 * RegisterEventSourceA [ADVAPI32.@]
790 */
791 HANDLE WINAPI
792 RegisterEventSourceA(IN LPCSTR lpUNCServerName,
793 IN LPCSTR lpSourceName)
794 {
795 UNICODE_STRING UNCServerName;
796 UNICODE_STRING SourceName;
797 HANDLE Handle;
798
799 TRACE("%s, %s\n", lpUNCServerName, lpSourceName);
800
801 if (!RtlCreateUnicodeStringFromAsciiz(&UNCServerName, lpUNCServerName))
802 {
803 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
804 return NULL;
805 }
806
807 if (!RtlCreateUnicodeStringFromAsciiz(&SourceName, lpSourceName))
808 {
809 RtlFreeUnicodeString(&UNCServerName);
810 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
811 return NULL;
812 }
813
814 Handle = RegisterEventSourceW(UNCServerName.Buffer,
815 SourceName.Buffer);
816
817 RtlFreeUnicodeString(&UNCServerName);
818 RtlFreeUnicodeString(&SourceName);
819
820 return Handle;
821 }
822
823
824 /******************************************************************************
825 * RegisterEventSourceW [ADVAPI32.@]
826 * Returns a registered handle to an event log
827 *
828 * PARAMS
829 * lpUNCServerName [I] Server name for source
830 * lpSourceName [I] Source name for registered handle
831 *
832 * RETURNS
833 * Success: Handle
834 * Failure: NULL
835 */
836 HANDLE WINAPI
837 RegisterEventSourceW(IN LPCWSTR lpUNCServerName,
838 IN LPCWSTR lpSourceName)
839 {
840 RPC_UNICODE_STRING SourceName;
841 IELF_HANDLE LogHandle;
842 NTSTATUS Status;
843
844 TRACE("%s, %s\n", debugstr_w(lpUNCServerName), debugstr_w(lpSourceName));
845
846 SourceName.Buffer = (LPWSTR)lpSourceName;
847 SourceName.Length = SourceName.MaximumLength =
848 lpSourceName ? wcslen(lpSourceName) * sizeof(WCHAR) : 0;
849 SourceName.MaximumLength += sizeof(WCHAR);
850
851 RpcTryExcept
852 {
853 Status = ElfrRegisterEventSourceW((LPWSTR)lpUNCServerName,
854 &SourceName,
855 &EmptyString,
856 1,
857 1,
858 &LogHandle);
859 }
860 RpcExcept(EXCEPTION_EXECUTE_HANDLER)
861 {
862 Status = I_RpcMapWin32Status(RpcExceptionCode());
863 }
864 RpcEndExcept;
865
866 if (!NT_SUCCESS(Status))
867 {
868 SetLastError(RtlNtStatusToDosError(Status));
869 return NULL;
870 }
871
872 return (HANDLE)LogHandle;
873 }
874
875
876 /******************************************************************************
877 * ReportEventA [ADVAPI32.@]
878 */
879 BOOL WINAPI
880 ReportEventA(IN HANDLE hEventLog,
881 IN WORD wType,
882 IN WORD wCategory,
883 IN DWORD dwEventID,
884 IN PSID lpUserSid,
885 IN WORD wNumStrings,
886 IN DWORD dwDataSize,
887 IN LPCSTR *lpStrings,
888 IN LPVOID lpRawData)
889 {
890 LPCWSTR *wideStrArray;
891 UNICODE_STRING str;
892 WORD i;
893 BOOL ret;
894
895 if (wNumStrings == 0)
896 return TRUE;
897
898 if (lpStrings == NULL)
899 return TRUE;
900
901 wideStrArray = HeapAlloc(GetProcessHeap(),
902 HEAP_ZERO_MEMORY,
903 sizeof(LPCWSTR) * wNumStrings);
904
905 for (i = 0; i < wNumStrings; i++)
906 {
907 if (!RtlCreateUnicodeStringFromAsciiz(&str, (PSTR)lpStrings[i]))
908 break;
909 wideStrArray[i] = str.Buffer;
910 }
911
912 if (i == wNumStrings)
913 {
914 ret = ReportEventW(hEventLog,
915 wType,
916 wCategory,
917 dwEventID,
918 lpUserSid,
919 wNumStrings,
920 dwDataSize,
921 wideStrArray,
922 lpRawData);
923 }
924 else
925 {
926 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
927 ret = FALSE;
928 }
929
930 for (i = 0; i < wNumStrings; i++)
931 {
932 if (wideStrArray[i])
933 {
934 HeapFree(GetProcessHeap(),
935 0,
936 (PVOID)wideStrArray[i]);
937 }
938 }
939
940 HeapFree(GetProcessHeap(),
941 0,
942 (PVOID)wideStrArray);
943
944 return ret;
945 }
946
947
948 /******************************************************************************
949 * ReportEventW [ADVAPI32.@]
950 *
951 * PARAMS
952 * hEventLog []
953 * wType []
954 * wCategory []
955 * dwEventID []
956 * lpUserSid []
957 * wNumStrings []
958 * dwDataSize []
959 * lpStrings []
960 * lpRawData []
961 */
962 BOOL WINAPI
963 ReportEventW(IN HANDLE hEventLog,
964 IN WORD wType,
965 IN WORD wCategory,
966 IN DWORD dwEventID,
967 IN PSID lpUserSid,
968 IN WORD wNumStrings,
969 IN DWORD dwDataSize,
970 IN LPCWSTR *lpStrings,
971 IN LPVOID lpRawData)
972 {
973 NTSTATUS Status;
974 UNICODE_STRING *Strings;
975 UNICODE_STRING ComputerName;
976 WORD i;
977
978 TRACE("%p, %u, %u, %lu, %p, %u, %lu, %p, %p\n",
979 hEventLog, wType, wCategory, dwEventID, lpUserSid,
980 wNumStrings, dwDataSize, lpStrings, lpRawData);
981
982 Strings = HeapAlloc(GetProcessHeap(),
983 0,
984 wNumStrings * sizeof(UNICODE_STRING));
985 if (!Strings)
986 {
987 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
988 return FALSE;
989 }
990
991 for (i = 0; i < wNumStrings; i++)
992 RtlInitUnicodeString(&Strings[i], lpStrings[i]);
993
994 /*FIXME: ComputerName */
995 RtlInitUnicodeString(&ComputerName, L"");
996
997 RpcTryExcept
998 {
999 Status = ElfrReportEventW(hEventLog,
1000 0, /* FIXME: Time */
1001 wType,
1002 wCategory,
1003 dwEventID,
1004 wNumStrings,
1005 dwDataSize,
1006 (PRPC_UNICODE_STRING) &ComputerName,
1007 lpUserSid,
1008 (PRPC_UNICODE_STRING*) &Strings,
1009 lpRawData,
1010 0,
1011 NULL,
1012 NULL);
1013 }
1014 RpcExcept(EXCEPTION_EXECUTE_HANDLER)
1015 {
1016 Status = I_RpcMapWin32Status(RpcExceptionCode());
1017 }
1018 RpcEndExcept;
1019
1020 HeapFree(GetProcessHeap(), 0, Strings);
1021
1022 if (!NT_SUCCESS(Status))
1023 {
1024 SetLastError(RtlNtStatusToDosError(Status));
1025 return FALSE;
1026 }
1027
1028 return TRUE;
1029 }