2 * Win32 advapi functions
4 * Copyright 1995 Sven Verdoolaege
5 * Copyright 1998 Juergen Schmied
6 * Copyright 2003 Mike Hearn
7 * Copyright 2007 Hervé Poussineau
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.
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.
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
25 #include "wine/debug.h"
27 WINE_DEFAULT_DEBUG_CHANNEL(advapi
);
29 static RPC_UNICODE_STRING EmptyStringU
= { 0, 0, L
"" };
30 static RPC_STRING EmptyStringA
= { 0, 0, "" };
34 EVENTLOG_HANDLE_A_bind(EVENTLOG_HANDLE_A UNCServerName
)
36 handle_t hBinding
= NULL
;
37 UCHAR
*pszStringBinding
;
40 TRACE("EVENTLOG_HANDLE_A_bind() called\n");
42 status
= RpcStringBindingComposeA(NULL
,
44 (UCHAR
*)UNCServerName
,
45 (UCHAR
*)"\\pipe\\EventLog",
47 (UCHAR
**)&pszStringBinding
);
50 ERR("RpcStringBindingCompose returned 0x%x\n", status
);
54 /* Set the binding handle that will be used to bind to the server. */
55 status
= RpcBindingFromStringBindingA(pszStringBinding
,
59 ERR("RpcBindingFromStringBinding returned 0x%x\n", status
);
62 status
= RpcStringFreeA(&pszStringBinding
);
65 ERR("RpcStringFree returned 0x%x\n", status
);
73 EVENTLOG_HANDLE_A_unbind(EVENTLOG_HANDLE_A UNCServerName
,
78 TRACE("EVENTLOG_HANDLE_A_unbind() called\n");
80 status
= RpcBindingFree(&hBinding
);
83 ERR("RpcBindingFree returned 0x%x\n", status
);
89 EVENTLOG_HANDLE_W_bind(EVENTLOG_HANDLE_W UNCServerName
)
91 handle_t hBinding
= NULL
;
92 LPWSTR pszStringBinding
;
95 TRACE("EVENTLOG_HANDLE_W_bind() called\n");
97 status
= RpcStringBindingComposeW(NULL
,
99 (LPWSTR
)UNCServerName
,
105 ERR("RpcStringBindingCompose returned 0x%x\n", status
);
109 /* Set the binding handle that will be used to bind to the server. */
110 status
= RpcBindingFromStringBindingW(pszStringBinding
,
114 ERR("RpcBindingFromStringBinding returned 0x%x\n", status
);
117 status
= RpcStringFreeW(&pszStringBinding
);
120 ERR("RpcStringFree returned 0x%x\n", status
);
128 EVENTLOG_HANDLE_W_unbind(EVENTLOG_HANDLE_W UNCServerName
,
133 TRACE("EVENTLOG_HANDLE_W_unbind() called\n");
135 status
= RpcBindingFree(&hBinding
);
138 ERR("RpcBindingFree returned 0x%x\n", status
);
143 /******************************************************************************
144 * BackupEventLogA [ADVAPI32.@]
147 BackupEventLogA(IN HANDLE hEventLog
,
148 IN LPCSTR lpBackupFileName
)
150 ANSI_STRING BackupFileName
;
153 TRACE("%p, %s\n", hEventLog
, lpBackupFileName
);
155 RtlInitAnsiString(&BackupFileName
, lpBackupFileName
);
159 Status
= ElfrBackupELFA(hEventLog
,
160 (PRPC_STRING
)&BackupFileName
);
162 RpcExcept(EXCEPTION_EXECUTE_HANDLER
)
164 Status
= I_RpcMapWin32Status(RpcExceptionCode());
168 if (!NT_SUCCESS(Status
))
170 SetLastError(RtlNtStatusToDosError(Status
));
177 /******************************************************************************
178 * BackupEventLogW [ADVAPI32.@]
182 * lpBackupFileName []
185 BackupEventLogW(IN HANDLE hEventLog
,
186 IN LPCWSTR lpBackupFileName
)
188 UNICODE_STRING BackupFileName
;
191 TRACE("%p, %s\n", hEventLog
, debugstr_w(lpBackupFileName
));
193 RtlInitUnicodeString(&BackupFileName
, lpBackupFileName
);
197 Status
= ElfrBackupELFW(hEventLog
,
198 (PRPC_UNICODE_STRING
)&BackupFileName
);
200 RpcExcept(EXCEPTION_EXECUTE_HANDLER
)
202 Status
= I_RpcMapWin32Status(RpcExceptionCode());
206 if (!NT_SUCCESS(Status
))
208 SetLastError(RtlNtStatusToDosError(Status
));
216 /******************************************************************************
217 * ClearEventLogA [ADVAPI32.@]
220 ClearEventLogA(IN HANDLE hEventLog
,
221 IN LPCSTR lpBackupFileName
)
223 ANSI_STRING BackupFileName
;
226 TRACE("%p, %s\n", hEventLog
, lpBackupFileName
);
228 RtlInitAnsiString(&BackupFileName
, lpBackupFileName
);
232 Status
= ElfrClearELFA(hEventLog
,
233 (PRPC_STRING
)&BackupFileName
);
235 RpcExcept(EXCEPTION_EXECUTE_HANDLER
)
237 Status
= I_RpcMapWin32Status(RpcExceptionCode());
241 if (!NT_SUCCESS(Status
))
243 SetLastError(RtlNtStatusToDosError(Status
));
251 /******************************************************************************
252 * ClearEventLogW [ADVAPI32.@]
255 ClearEventLogW(IN HANDLE hEventLog
,
256 IN LPCWSTR lpBackupFileName
)
258 UNICODE_STRING BackupFileName
;
261 TRACE("%p, %s\n", hEventLog
, debugstr_w(lpBackupFileName
));
263 RtlInitUnicodeString(&BackupFileName
,lpBackupFileName
);
267 Status
= ElfrClearELFW(hEventLog
,
268 (PRPC_UNICODE_STRING
)&BackupFileName
);
270 RpcExcept(EXCEPTION_EXECUTE_HANDLER
)
272 Status
= I_RpcMapWin32Status(RpcExceptionCode());
276 if (!NT_SUCCESS(Status
))
278 SetLastError(RtlNtStatusToDosError(Status
));
286 /******************************************************************************
287 * CloseEventLog [ADVAPI32.@]
290 CloseEventLog(IN HANDLE hEventLog
)
294 TRACE("%p\n", hEventLog
);
298 Status
= ElfrCloseEL(&hEventLog
);
300 RpcExcept(EXCEPTION_EXECUTE_HANDLER
)
302 Status
= I_RpcMapWin32Status(RpcExceptionCode());
306 if (!NT_SUCCESS(Status
))
308 SetLastError(RtlNtStatusToDosError(Status
));
316 /******************************************************************************
317 * DeregisterEventSource [ADVAPI32.@]
318 * Closes a handle to the specified event log
321 * hEventLog [I] Handle to event log
326 DeregisterEventSource(IN HANDLE hEventLog
)
330 TRACE("%p\n", hEventLog
);
334 Status
= ElfrDeregisterEventSource(&hEventLog
);
336 RpcExcept(EXCEPTION_EXECUTE_HANDLER
)
338 Status
= I_RpcMapWin32Status(RpcExceptionCode());
342 if (!NT_SUCCESS(Status
))
344 SetLastError(RtlNtStatusToDosError(Status
));
352 /******************************************************************************
353 * GetEventLogInformation [ADVAPI32.@]
356 * hEventLog [I] Handle to event log
357 * dwInfoLevel [I] Level of event log information to return
358 * lpBuffer [O] Buffer that receives the event log information
359 * cbBufSize [I] Size of the lpBuffer buffer
360 * pcbBytesNeeded [O] Required buffer size
363 GetEventLogInformation(IN HANDLE hEventLog
,
364 IN DWORD dwInfoLevel
,
367 OUT LPDWORD pcbBytesNeeded
)
371 if (dwInfoLevel
!= EVENTLOG_FULL_INFO
)
373 SetLastError(ERROR_INVALID_LEVEL
);
379 Status
= ElfrGetLogInformation(hEventLog
,
385 RpcExcept(EXCEPTION_EXECUTE_HANDLER
)
387 Status
= I_RpcMapWin32Status(RpcExceptionCode());
391 if (!NT_SUCCESS(Status
))
393 SetLastError(RtlNtStatusToDosError(Status
));
401 /******************************************************************************
402 * GetNumberOfEventLogRecords [ADVAPI32.@]
409 GetNumberOfEventLogRecords(IN HANDLE hEventLog
,
410 OUT PDWORD NumberOfRecords
)
415 TRACE("%p, %p\n", hEventLog
, NumberOfRecords
);
419 SetLastError(ERROR_INVALID_PARAMETER
);
425 Status
= ElfrNumberOfRecords(hEventLog
,
428 RpcExcept(EXCEPTION_EXECUTE_HANDLER
)
430 Status
= I_RpcMapWin32Status(RpcExceptionCode());
434 if (!NT_SUCCESS(Status
))
436 SetLastError(RtlNtStatusToDosError(Status
));
440 *NumberOfRecords
= Records
;
446 /******************************************************************************
447 * GetOldestEventLogRecord [ADVAPI32.@]
454 GetOldestEventLogRecord(IN HANDLE hEventLog
,
455 OUT PDWORD OldestRecord
)
460 TRACE("%p, %p\n", hEventLog
, OldestRecord
);
464 SetLastError(ERROR_INVALID_PARAMETER
);
470 Status
= ElfrOldestRecord(hEventLog
,
473 RpcExcept(EXCEPTION_EXECUTE_HANDLER
)
475 Status
= I_RpcMapWin32Status(RpcExceptionCode());
479 if (!NT_SUCCESS(Status
))
481 SetLastError(RtlNtStatusToDosError(Status
));
485 *OldestRecord
= Oldest
;
491 /******************************************************************************
492 * NotifyChangeEventLog [ADVAPI32.@]
499 NotifyChangeEventLog(IN HANDLE hEventLog
,
502 /* Use ElfrChangeNotify */
504 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
509 /******************************************************************************
510 * OpenBackupEventLogA [ADVAPI32.@]
513 OpenBackupEventLogA(IN LPCSTR lpUNCServerName
,
514 IN LPCSTR lpFileName
)
516 ANSI_STRING FileName
;
517 IELF_HANDLE LogHandle
;
520 TRACE("%s, %s\n", lpUNCServerName
, lpFileName
);
522 RtlInitAnsiString(&FileName
, lpFileName
);
526 Status
= ElfrOpenBELA((LPSTR
)lpUNCServerName
,
527 (PRPC_STRING
)&FileName
,
532 RpcExcept(EXCEPTION_EXECUTE_HANDLER
)
534 Status
= I_RpcMapWin32Status(RpcExceptionCode());
538 if (!NT_SUCCESS(Status
))
540 SetLastError(RtlNtStatusToDosError(Status
));
544 return (HANDLE
)LogHandle
;
548 /******************************************************************************
549 * OpenBackupEventLogW [ADVAPI32.@]
556 OpenBackupEventLogW(IN LPCWSTR lpUNCServerName
,
557 IN LPCWSTR lpFileName
)
559 UNICODE_STRING FileName
;
560 IELF_HANDLE LogHandle
;
563 TRACE("%s, %s\n", debugstr_w(lpUNCServerName
), debugstr_w(lpFileName
));
565 RtlInitUnicodeString(&FileName
, lpFileName
);
569 Status
= ElfrOpenBELW((LPWSTR
)lpUNCServerName
,
570 (PRPC_UNICODE_STRING
)&FileName
,
575 RpcExcept(EXCEPTION_EXECUTE_HANDLER
)
577 Status
= I_RpcMapWin32Status(RpcExceptionCode());
581 if (!NT_SUCCESS(Status
))
583 SetLastError(RtlNtStatusToDosError(Status
));
587 return (HANDLE
)LogHandle
;
591 /******************************************************************************
592 * OpenEventLogA [ADVAPI32.@]
594 * Opens a handle to the specified event log.
597 * lpUNCServerName [I] UNC name of the server on which the event log is
599 * lpSourceName [I] Name of the log.
602 * Success: Handle to an event log.
606 OpenEventLogA(IN LPCSTR lpUNCServerName
,
607 IN LPCSTR lpSourceName
)
610 ANSI_STRING SourceName
;
611 IELF_HANDLE LogHandle
= NULL
;
614 TRACE("%s, %s\n", lpUNCServerName
, lpSourceName
);
616 if (lpSourceName
== NULL
)
618 SetLastError(ERROR_INVALID_PARAMETER
);
622 if (lpUNCServerName
== NULL
|| *lpUNCServerName
== 0)
623 UNCServerName
= NULL
;
625 UNCServerName
= (LPSTR
)lpUNCServerName
;
627 RtlInitAnsiString(&SourceName
, lpSourceName
);
631 Status
= ElfrOpenELA(UNCServerName
,
632 (PRPC_STRING
)&SourceName
,
638 RpcExcept(EXCEPTION_EXECUTE_HANDLER
)
640 Status
= I_RpcMapWin32Status(RpcExceptionCode());
644 if (!NT_SUCCESS(Status
))
646 SetLastError(RtlNtStatusToDosError(Status
));
650 return (HANDLE
)LogHandle
;
654 /******************************************************************************
655 * OpenEventLogW [ADVAPI32.@]
662 OpenEventLogW(IN LPCWSTR lpUNCServerName
,
663 IN LPCWSTR lpSourceName
)
665 LPWSTR UNCServerName
;
666 UNICODE_STRING SourceName
;
667 IELF_HANDLE LogHandle
;
670 TRACE("%s, %s\n", debugstr_w(lpUNCServerName
), debugstr_w(lpSourceName
));
672 if (lpSourceName
== NULL
)
674 SetLastError(ERROR_INVALID_PARAMETER
);
678 if (lpUNCServerName
== NULL
|| *lpUNCServerName
== 0)
679 UNCServerName
= NULL
;
681 UNCServerName
= (LPWSTR
)lpUNCServerName
;
683 RtlInitUnicodeString(&SourceName
, lpSourceName
);
687 Status
= ElfrOpenELW(UNCServerName
,
688 (PRPC_UNICODE_STRING
)&SourceName
,
694 RpcExcept(EXCEPTION_EXECUTE_HANDLER
)
696 Status
= I_RpcMapWin32Status(RpcExceptionCode());
700 if (!NT_SUCCESS(Status
))
702 SetLastError(RtlNtStatusToDosError(Status
));
706 return (HANDLE
)LogHandle
;
710 /******************************************************************************
711 * ReadEventLogA [ADVAPI32.@]
714 ReadEventLogA(IN HANDLE hEventLog
,
715 IN DWORD dwReadFlags
,
716 IN DWORD dwRecordOffset
,
718 IN DWORD nNumberOfBytesToRead
,
719 OUT DWORD
*pnBytesRead
,
720 OUT DWORD
*pnMinNumberOfBytesNeeded
)
723 DWORD bytesRead
, minNumberOfBytesNeeded
;
725 TRACE("%p, %lu, %lu, %p, %lu, %p, %p\n",
726 hEventLog
, dwReadFlags
, dwRecordOffset
, lpBuffer
,
727 nNumberOfBytesToRead
, pnBytesRead
, pnMinNumberOfBytesNeeded
);
729 if(!pnBytesRead
|| !pnMinNumberOfBytesNeeded
)
731 SetLastError(ERROR_INVALID_PARAMETER
);
735 /* If buffer is NULL set nNumberOfBytesToRead to 0 to prevent rpcrt4 from
736 trying to access a null pointer */
739 nNumberOfBytesToRead
= 0;
744 Status
= ElfrReadELA(hEventLog
,
747 nNumberOfBytesToRead
,
750 &minNumberOfBytesNeeded
);
752 RpcExcept(EXCEPTION_EXECUTE_HANDLER
)
754 Status
= I_RpcMapWin32Status(RpcExceptionCode());
758 *pnBytesRead
= (DWORD
)bytesRead
;
759 *pnMinNumberOfBytesNeeded
= (DWORD
)minNumberOfBytesNeeded
;
761 if (!NT_SUCCESS(Status
))
763 SetLastError(RtlNtStatusToDosError(Status
));
771 /******************************************************************************
772 * ReadEventLogW [ADVAPI32.@]
779 * nNumberOfBytesToRead []
781 * pnMinNumberOfBytesNeeded []
784 ReadEventLogW(IN HANDLE hEventLog
,
785 IN DWORD dwReadFlags
,
786 IN DWORD dwRecordOffset
,
788 IN DWORD nNumberOfBytesToRead
,
789 OUT DWORD
*pnBytesRead
,
790 OUT DWORD
*pnMinNumberOfBytesNeeded
)
793 DWORD bytesRead
, minNumberOfBytesNeeded
;
795 TRACE("%p, %lu, %lu, %p, %lu, %p, %p\n",
796 hEventLog
, dwReadFlags
, dwRecordOffset
, lpBuffer
,
797 nNumberOfBytesToRead
, pnBytesRead
, pnMinNumberOfBytesNeeded
);
799 if(!pnBytesRead
|| !pnMinNumberOfBytesNeeded
)
801 SetLastError(ERROR_INVALID_PARAMETER
);
805 /* If buffer is NULL set nNumberOfBytesToRead to 0 to prevent rpcrt4 from
806 trying to access a null pointer */
809 nNumberOfBytesToRead
= 0;
814 Status
= ElfrReadELW(hEventLog
,
817 nNumberOfBytesToRead
,
820 &minNumberOfBytesNeeded
);
822 RpcExcept(EXCEPTION_EXECUTE_HANDLER
)
824 Status
= I_RpcMapWin32Status(RpcExceptionCode());
828 *pnBytesRead
= (DWORD
)bytesRead
;
829 *pnMinNumberOfBytesNeeded
= (DWORD
)minNumberOfBytesNeeded
;
831 if (!NT_SUCCESS(Status
))
833 SetLastError(RtlNtStatusToDosError(Status
));
841 /******************************************************************************
842 * RegisterEventSourceA [ADVAPI32.@]
845 RegisterEventSourceA(IN LPCSTR lpUNCServerName
,
846 IN LPCSTR lpSourceName
)
848 ANSI_STRING SourceName
;
849 IELF_HANDLE LogHandle
;
852 TRACE("%s, %s\n", lpUNCServerName
, lpSourceName
);
854 RtlInitAnsiString(&SourceName
, lpSourceName
);
858 Status
= ElfrRegisterEventSourceA((LPSTR
)lpUNCServerName
,
859 (PRPC_STRING
)&SourceName
,
865 RpcExcept(EXCEPTION_EXECUTE_HANDLER
)
867 Status
= I_RpcMapWin32Status(RpcExceptionCode());
871 if (!NT_SUCCESS(Status
))
873 SetLastError(RtlNtStatusToDosError(Status
));
877 return (HANDLE
)LogHandle
;
881 /******************************************************************************
882 * RegisterEventSourceW [ADVAPI32.@]
883 * Returns a registered handle to an event log
886 * lpUNCServerName [I] Server name for source
887 * lpSourceName [I] Source name for registered handle
894 RegisterEventSourceW(IN LPCWSTR lpUNCServerName
,
895 IN LPCWSTR lpSourceName
)
897 UNICODE_STRING SourceName
;
898 IELF_HANDLE LogHandle
;
901 TRACE("%s, %s\n", debugstr_w(lpUNCServerName
), debugstr_w(lpSourceName
));
903 RtlInitUnicodeString(&SourceName
, lpSourceName
);
907 Status
= ElfrRegisterEventSourceW((LPWSTR
)lpUNCServerName
,
908 (PRPC_UNICODE_STRING
)&SourceName
,
914 RpcExcept(EXCEPTION_EXECUTE_HANDLER
)
916 Status
= I_RpcMapWin32Status(RpcExceptionCode());
920 if (!NT_SUCCESS(Status
))
922 SetLastError(RtlNtStatusToDosError(Status
));
926 return (HANDLE
)LogHandle
;
930 /******************************************************************************
931 * ReportEventA [ADVAPI32.@]
934 ReportEventA(IN HANDLE hEventLog
,
941 IN LPCSTR
*lpStrings
,
945 ANSI_STRING
*Strings
;
946 ANSI_STRING ComputerName
;
948 CHAR szComputerName
[MAX_COMPUTERNAME_LENGTH
+ 1];
951 TRACE("%p, %u, %u, %lu, %p, %u, %lu, %p, %p\n",
952 hEventLog
, wType
, wCategory
, dwEventID
, lpUserSid
,
953 wNumStrings
, dwDataSize
, lpStrings
, lpRawData
);
955 Strings
= HeapAlloc(GetProcessHeap(),
957 wNumStrings
* sizeof(ANSI_STRING
));
960 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
964 for (i
= 0; i
< wNumStrings
; i
++)
965 RtlInitAnsiString(&Strings
[i
], lpStrings
[i
]);
967 dwSize
= MAX_COMPUTERNAME_LENGTH
+ 1;
968 GetComputerNameA(szComputerName
, &dwSize
);
969 RtlInitAnsiString(&ComputerName
, szComputerName
);
973 Status
= ElfrReportEventA(hEventLog
,
980 (PRPC_STRING
) &ComputerName
,
982 (PRPC_STRING
*) &Strings
,
988 RpcExcept(EXCEPTION_EXECUTE_HANDLER
)
990 Status
= I_RpcMapWin32Status(RpcExceptionCode());
994 HeapFree(GetProcessHeap(), 0, Strings
);
996 if (!NT_SUCCESS(Status
))
998 SetLastError(RtlNtStatusToDosError(Status
));
1006 /******************************************************************************
1007 * ReportEventW [ADVAPI32.@]
1021 ReportEventW(IN HANDLE hEventLog
,
1026 IN WORD wNumStrings
,
1027 IN DWORD dwDataSize
,
1028 IN LPCWSTR
*lpStrings
,
1029 IN LPVOID lpRawData
)
1032 UNICODE_STRING
*Strings
;
1033 UNICODE_STRING ComputerName
;
1035 WCHAR szComputerName
[MAX_COMPUTERNAME_LENGTH
+ 1];
1038 TRACE("%p, %u, %u, %lu, %p, %u, %lu, %p, %p\n",
1039 hEventLog
, wType
, wCategory
, dwEventID
, lpUserSid
,
1040 wNumStrings
, dwDataSize
, lpStrings
, lpRawData
);
1042 Strings
= HeapAlloc(GetProcessHeap(),
1044 wNumStrings
* sizeof(UNICODE_STRING
));
1047 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1051 for (i
= 0; i
< wNumStrings
; i
++)
1052 RtlInitUnicodeString(&Strings
[i
], lpStrings
[i
]);
1054 dwSize
= MAX_COMPUTERNAME_LENGTH
+ 1;
1055 GetComputerNameW(szComputerName
, &dwSize
);
1056 RtlInitUnicodeString(&ComputerName
, szComputerName
);
1060 Status
= ElfrReportEventW(hEventLog
,
1061 0, /* FIXME: Time */
1067 (PRPC_UNICODE_STRING
) &ComputerName
,
1069 (PRPC_UNICODE_STRING
*) &Strings
,
1075 RpcExcept(EXCEPTION_EXECUTE_HANDLER
)
1077 Status
= I_RpcMapWin32Status(RpcExceptionCode());
1081 HeapFree(GetProcessHeap(), 0, Strings
);
1083 if (!NT_SUCCESS(Status
))
1085 SetLastError(RtlNtStatusToDosError(Status
));