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