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\\ntsvcs",
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 RPC_STRING BackupFileName
;
153 TRACE("%p, %s\n", hEventLog
, lpBackupFileName
);
155 BackupFileName
.Buffer
= (LPSTR
)lpBackupFileName
;
156 BackupFileName
.Length
= BackupFileName
.MaximumLength
=
157 lpBackupFileName
? strlen(lpBackupFileName
) : 0;
158 BackupFileName
.MaximumLength
+= sizeof(CHAR
);
162 Status
= ElfrBackupELFA(hEventLog
,
165 RpcExcept(EXCEPTION_EXECUTE_HANDLER
)
167 Status
= I_RpcMapWin32Status(RpcExceptionCode());
171 if (!NT_SUCCESS(Status
))
173 SetLastError(RtlNtStatusToDosError(Status
));
180 /******************************************************************************
181 * BackupEventLogW [ADVAPI32.@]
185 * lpBackupFileName []
188 BackupEventLogW(IN HANDLE hEventLog
,
189 IN LPCWSTR lpBackupFileName
)
191 RPC_UNICODE_STRING BackupFileName
;
194 TRACE("%p, %s\n", hEventLog
, debugstr_w(lpBackupFileName
));
196 BackupFileName
.Buffer
= (LPWSTR
)lpBackupFileName
;
197 BackupFileName
.Length
= BackupFileName
.MaximumLength
=
198 lpBackupFileName
? wcslen(lpBackupFileName
) * sizeof(WCHAR
) : 0;
199 BackupFileName
.MaximumLength
+= sizeof(WCHAR
);
203 Status
= ElfrBackupELFW(hEventLog
,
206 RpcExcept(EXCEPTION_EXECUTE_HANDLER
)
208 Status
= I_RpcMapWin32Status(RpcExceptionCode());
212 if (!NT_SUCCESS(Status
))
214 SetLastError(RtlNtStatusToDosError(Status
));
222 /******************************************************************************
223 * ClearEventLogA [ADVAPI32.@]
226 ClearEventLogA(IN HANDLE hEventLog
,
227 IN LPCSTR lpBackupFileName
)
229 RPC_STRING BackupFileName
;
232 TRACE("%p, %s\n", hEventLog
, lpBackupFileName
);
234 BackupFileName
.Buffer
= (LPSTR
)lpBackupFileName
;
235 BackupFileName
.Length
= BackupFileName
.MaximumLength
=
236 lpBackupFileName
? strlen(lpBackupFileName
) : 0;
237 BackupFileName
.MaximumLength
+= sizeof(CHAR
);
241 Status
= ElfrClearELFA(hEventLog
,
244 RpcExcept(EXCEPTION_EXECUTE_HANDLER
)
246 Status
= I_RpcMapWin32Status(RpcExceptionCode());
250 if (!NT_SUCCESS(Status
))
252 SetLastError(RtlNtStatusToDosError(Status
));
260 /******************************************************************************
261 * ClearEventLogW [ADVAPI32.@]
264 ClearEventLogW(IN HANDLE hEventLog
,
265 IN LPCWSTR lpBackupFileName
)
267 RPC_UNICODE_STRING BackupFileName
;
270 TRACE("%p, %s\n", hEventLog
, debugstr_w(lpBackupFileName
));
272 BackupFileName
.Buffer
= (LPWSTR
)lpBackupFileName
;
273 BackupFileName
.Length
= BackupFileName
.MaximumLength
=
274 lpBackupFileName
? wcslen(lpBackupFileName
) * sizeof(WCHAR
) : 0;
275 BackupFileName
.MaximumLength
+= sizeof(WCHAR
);
279 Status
= ElfrClearELFW(hEventLog
,
282 RpcExcept(EXCEPTION_EXECUTE_HANDLER
)
284 Status
= I_RpcMapWin32Status(RpcExceptionCode());
288 if (!NT_SUCCESS(Status
))
290 SetLastError(RtlNtStatusToDosError(Status
));
298 /******************************************************************************
299 * CloseEventLog [ADVAPI32.@]
302 CloseEventLog(IN HANDLE hEventLog
)
306 TRACE("%p\n", hEventLog
);
310 Status
= ElfrCloseEL(&hEventLog
);
312 RpcExcept(EXCEPTION_EXECUTE_HANDLER
)
314 Status
= I_RpcMapWin32Status(RpcExceptionCode());
318 if (!NT_SUCCESS(Status
))
320 SetLastError(RtlNtStatusToDosError(Status
));
328 /******************************************************************************
329 * DeregisterEventSource [ADVAPI32.@]
330 * Closes a handle to the specified event log
333 * hEventLog [I] Handle to event log
338 DeregisterEventSource(IN HANDLE hEventLog
)
342 TRACE("%p\n", hEventLog
);
346 Status
= ElfrDeregisterEventSource(&hEventLog
);
348 RpcExcept(EXCEPTION_EXECUTE_HANDLER
)
350 Status
= I_RpcMapWin32Status(RpcExceptionCode());
354 if (!NT_SUCCESS(Status
))
356 SetLastError(RtlNtStatusToDosError(Status
));
364 /******************************************************************************
365 * GetEventLogInformation [ADVAPI32.@]
368 * hEventLog [I] Handle to event log
369 * dwInfoLevel [I] Level of event log information to return
370 * lpBuffer [O] Buffer that receives the event log information
371 * cbBufSize [I] Size of the lpBuffer buffer
372 * pcbBytesNeeded [O] Required buffer size
375 GetEventLogInformation(IN HANDLE hEventLog
,
376 IN DWORD dwInfoLevel
,
379 OUT LPDWORD pcbBytesNeeded
)
385 Status
= ElfrGetLogInformation(hEventLog
,
391 RpcExcept(EXCEPTION_EXECUTE_HANDLER
)
393 Status
= I_RpcMapWin32Status(RpcExceptionCode());
397 if (!NT_SUCCESS(Status
))
399 SetLastError(RtlNtStatusToDosError(Status
));
407 /******************************************************************************
408 * GetNumberOfEventLogRecords [ADVAPI32.@]
415 GetNumberOfEventLogRecords(IN HANDLE hEventLog
,
416 OUT PDWORD NumberOfRecords
)
421 TRACE("%p, %p\n", hEventLog
, NumberOfRecords
);
425 SetLastError(ERROR_INVALID_PARAMETER
);
431 Status
= ElfrNumberOfRecords(hEventLog
,
434 RpcExcept(EXCEPTION_EXECUTE_HANDLER
)
436 Status
= I_RpcMapWin32Status(RpcExceptionCode());
440 if (!NT_SUCCESS(Status
))
442 SetLastError(RtlNtStatusToDosError(Status
));
446 *NumberOfRecords
= Records
;
452 /******************************************************************************
453 * GetOldestEventLogRecord [ADVAPI32.@]
460 GetOldestEventLogRecord(IN HANDLE hEventLog
,
461 OUT PDWORD OldestRecord
)
466 TRACE("%p, %p\n", hEventLog
, OldestRecord
);
470 SetLastError(ERROR_INVALID_PARAMETER
);
476 Status
= ElfrOldestRecord(hEventLog
,
479 RpcExcept(EXCEPTION_EXECUTE_HANDLER
)
481 Status
= I_RpcMapWin32Status(RpcExceptionCode());
485 if (!NT_SUCCESS(Status
))
487 SetLastError(RtlNtStatusToDosError(Status
));
491 *OldestRecord
= Oldest
;
497 /******************************************************************************
498 * NotifyChangeEventLog [ADVAPI32.@]
505 NotifyChangeEventLog(IN HANDLE hEventLog
,
508 /* Use ElfrChangeNotify */
510 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
515 /******************************************************************************
516 * OpenBackupEventLogA [ADVAPI32.@]
519 OpenBackupEventLogA(IN LPCSTR lpUNCServerName
,
520 IN LPCSTR lpFileName
)
522 ANSI_STRING FileName
;
523 IELF_HANDLE LogHandle
;
526 TRACE("%s, %s\n", lpUNCServerName
, lpFileName
);
528 RtlInitAnsiString(&FileName
, lpFileName
);
532 Status
= ElfrOpenBELA((LPSTR
)lpUNCServerName
,
533 (PRPC_STRING
)&FileName
,
538 RpcExcept(EXCEPTION_EXECUTE_HANDLER
)
540 Status
= I_RpcMapWin32Status(RpcExceptionCode());
544 if (!NT_SUCCESS(Status
))
546 SetLastError(RtlNtStatusToDosError(Status
));
550 return (HANDLE
)LogHandle
;
554 /******************************************************************************
555 * OpenBackupEventLogW [ADVAPI32.@]
562 OpenBackupEventLogW(IN LPCWSTR lpUNCServerName
,
563 IN LPCWSTR lpFileName
)
565 RPC_UNICODE_STRING FileName
;
566 IELF_HANDLE LogHandle
;
569 TRACE("%s, %s\n", debugstr_w(lpUNCServerName
), debugstr_w(lpFileName
));
571 FileName
.Buffer
= (LPWSTR
)lpFileName
;
572 FileName
.Length
= FileName
.MaximumLength
=
573 lpFileName
? wcslen(lpFileName
) * sizeof(WCHAR
) : 0;
574 FileName
.MaximumLength
+= sizeof(WCHAR
);
578 Status
= ElfrOpenBELW((LPWSTR
)lpUNCServerName
,
584 RpcExcept(EXCEPTION_EXECUTE_HANDLER
)
586 Status
= I_RpcMapWin32Status(RpcExceptionCode());
590 if (!NT_SUCCESS(Status
))
592 SetLastError(RtlNtStatusToDosError(Status
));
596 return (HANDLE
)LogHandle
;
600 /******************************************************************************
601 * OpenEventLogA [ADVAPI32.@]
603 * Opens a handle to the specified event log.
606 * lpUNCServerName [I] UNC name of the server on which the event log is
608 * lpSourceName [I] Name of the log.
611 * Success: Handle to an event log.
615 OpenEventLogA(IN LPCSTR lpUNCServerName
,
616 IN LPCSTR lpSourceName
)
619 ANSI_STRING SourceName
;
620 IELF_HANDLE LogHandle
= NULL
;
623 TRACE("%s, %s\n", lpUNCServerName
, lpSourceName
);
625 if (lpSourceName
== NULL
)
627 SetLastError(ERROR_INVALID_PARAMETER
);
631 if (lpUNCServerName
== NULL
|| *lpUNCServerName
== 0)
632 UNCServerName
= NULL
;
634 UNCServerName
= (LPSTR
)lpUNCServerName
;
636 RtlInitAnsiString(&SourceName
, lpSourceName
);
640 Status
= ElfrOpenELA(UNCServerName
,
641 (PRPC_STRING
)&SourceName
,
647 RpcExcept(EXCEPTION_EXECUTE_HANDLER
)
649 Status
= I_RpcMapWin32Status(RpcExceptionCode());
653 if (!NT_SUCCESS(Status
))
655 SetLastError(RtlNtStatusToDosError(Status
));
659 return (HANDLE
)LogHandle
;
663 /******************************************************************************
664 * OpenEventLogW [ADVAPI32.@]
671 OpenEventLogW(IN LPCWSTR lpUNCServerName
,
672 IN LPCWSTR lpSourceName
)
674 LPWSTR UNCServerName
;
675 UNICODE_STRING SourceName
;
676 IELF_HANDLE LogHandle
;
679 TRACE("%s, %s\n", debugstr_w(lpUNCServerName
), debugstr_w(lpSourceName
));
681 if (lpSourceName
== NULL
)
683 SetLastError(ERROR_INVALID_PARAMETER
);
687 if (lpUNCServerName
== NULL
|| *lpUNCServerName
== 0)
688 UNCServerName
= NULL
;
690 UNCServerName
= (LPWSTR
)lpUNCServerName
;
692 RtlInitUnicodeString(&SourceName
, lpSourceName
);
696 Status
= ElfrOpenELW(UNCServerName
,
697 (PRPC_UNICODE_STRING
)&SourceName
,
703 RpcExcept(EXCEPTION_EXECUTE_HANDLER
)
705 Status
= I_RpcMapWin32Status(RpcExceptionCode());
709 if (!NT_SUCCESS(Status
))
711 SetLastError(RtlNtStatusToDosError(Status
));
715 return (HANDLE
)LogHandle
;
719 /******************************************************************************
720 * ReadEventLogA [ADVAPI32.@]
723 ReadEventLogA(IN HANDLE hEventLog
,
724 IN DWORD dwReadFlags
,
725 IN DWORD dwRecordOffset
,
727 IN DWORD nNumberOfBytesToRead
,
728 OUT DWORD
*pnBytesRead
,
729 OUT DWORD
*pnMinNumberOfBytesNeeded
)
732 DWORD bytesRead
, minNumberOfBytesNeeded
;
734 TRACE("%p, %lu, %lu, %p, %lu, %p, %p\n",
735 hEventLog
, dwReadFlags
, dwRecordOffset
, lpBuffer
,
736 nNumberOfBytesToRead
, pnBytesRead
, pnMinNumberOfBytesNeeded
);
738 if(!pnBytesRead
|| !pnMinNumberOfBytesNeeded
)
740 SetLastError(ERROR_INVALID_PARAMETER
);
744 /* If buffer is NULL set nNumberOfBytesToRead to 0 to prevent rpcrt4 from
745 trying to access a null pointer */
748 nNumberOfBytesToRead
= 0;
753 Status
= ElfrReadELA(hEventLog
,
756 nNumberOfBytesToRead
,
759 &minNumberOfBytesNeeded
);
761 RpcExcept(EXCEPTION_EXECUTE_HANDLER
)
763 Status
= I_RpcMapWin32Status(RpcExceptionCode());
767 *pnBytesRead
= (DWORD
)bytesRead
;
768 *pnMinNumberOfBytesNeeded
= (DWORD
)minNumberOfBytesNeeded
;
770 if (!NT_SUCCESS(Status
))
772 SetLastError(RtlNtStatusToDosError(Status
));
780 /******************************************************************************
781 * ReadEventLogW [ADVAPI32.@]
788 * nNumberOfBytesToRead []
790 * pnMinNumberOfBytesNeeded []
793 ReadEventLogW(IN HANDLE hEventLog
,
794 IN DWORD dwReadFlags
,
795 IN DWORD dwRecordOffset
,
797 IN DWORD nNumberOfBytesToRead
,
798 OUT DWORD
*pnBytesRead
,
799 OUT DWORD
*pnMinNumberOfBytesNeeded
)
802 DWORD bytesRead
, minNumberOfBytesNeeded
;
804 TRACE("%p, %lu, %lu, %p, %lu, %p, %p\n",
805 hEventLog
, dwReadFlags
, dwRecordOffset
, lpBuffer
,
806 nNumberOfBytesToRead
, pnBytesRead
, pnMinNumberOfBytesNeeded
);
808 if(!pnBytesRead
|| !pnMinNumberOfBytesNeeded
)
810 SetLastError(ERROR_INVALID_PARAMETER
);
814 /* If buffer is NULL set nNumberOfBytesToRead to 0 to prevent rpcrt4 from
815 trying to access a null pointer */
818 nNumberOfBytesToRead
= 0;
823 Status
= ElfrReadELW(hEventLog
,
826 nNumberOfBytesToRead
,
829 &minNumberOfBytesNeeded
);
831 RpcExcept(EXCEPTION_EXECUTE_HANDLER
)
833 Status
= I_RpcMapWin32Status(RpcExceptionCode());
837 *pnBytesRead
= (DWORD
)bytesRead
;
838 *pnMinNumberOfBytesNeeded
= (DWORD
)minNumberOfBytesNeeded
;
840 if (!NT_SUCCESS(Status
))
842 SetLastError(RtlNtStatusToDosError(Status
));
850 /******************************************************************************
851 * RegisterEventSourceA [ADVAPI32.@]
854 RegisterEventSourceA(IN LPCSTR lpUNCServerName
,
855 IN LPCSTR lpSourceName
)
857 ANSI_STRING SourceName
;
858 IELF_HANDLE LogHandle
;
861 TRACE("%s, %s\n", lpUNCServerName
, lpSourceName
);
863 RtlInitAnsiString(&SourceName
, lpSourceName
);
867 Status
= ElfrRegisterEventSourceA((LPSTR
)lpUNCServerName
,
868 (PRPC_STRING
)&SourceName
,
874 RpcExcept(EXCEPTION_EXECUTE_HANDLER
)
876 Status
= I_RpcMapWin32Status(RpcExceptionCode());
880 if (!NT_SUCCESS(Status
))
882 SetLastError(RtlNtStatusToDosError(Status
));
886 return (HANDLE
)LogHandle
;
890 /******************************************************************************
891 * RegisterEventSourceW [ADVAPI32.@]
892 * Returns a registered handle to an event log
895 * lpUNCServerName [I] Server name for source
896 * lpSourceName [I] Source name for registered handle
903 RegisterEventSourceW(IN LPCWSTR lpUNCServerName
,
904 IN LPCWSTR lpSourceName
)
906 RPC_UNICODE_STRING SourceName
;
907 IELF_HANDLE LogHandle
;
910 TRACE("%s, %s\n", debugstr_w(lpUNCServerName
), debugstr_w(lpSourceName
));
912 SourceName
.Buffer
= (LPWSTR
)lpSourceName
;
913 SourceName
.Length
= SourceName
.MaximumLength
=
914 lpSourceName
? wcslen(lpSourceName
) * sizeof(WCHAR
) : 0;
915 SourceName
.MaximumLength
+= sizeof(WCHAR
);
919 Status
= ElfrRegisterEventSourceW((LPWSTR
)lpUNCServerName
,
926 RpcExcept(EXCEPTION_EXECUTE_HANDLER
)
928 Status
= I_RpcMapWin32Status(RpcExceptionCode());
932 if (!NT_SUCCESS(Status
))
934 SetLastError(RtlNtStatusToDosError(Status
));
938 return (HANDLE
)LogHandle
;
942 /******************************************************************************
943 * ReportEventA [ADVAPI32.@]
946 ReportEventA(IN HANDLE hEventLog
,
953 IN LPCSTR
*lpStrings
,
957 ANSI_STRING
*Strings
;
958 ANSI_STRING ComputerName
;
961 TRACE("%p, %u, %u, %lu, %p, %u, %lu, %p, %p\n",
962 hEventLog
, wType
, wCategory
, dwEventID
, lpUserSid
,
963 wNumStrings
, dwDataSize
, lpStrings
, lpRawData
);
965 Strings
= HeapAlloc(GetProcessHeap(),
967 wNumStrings
* sizeof(ANSI_STRING
));
970 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
974 for (i
= 0; i
< wNumStrings
; i
++)
975 RtlInitAnsiString(&Strings
[i
], lpStrings
[i
]);
977 /*FIXME: ComputerName */
978 RtlInitAnsiString(&ComputerName
, "");
982 Status
= ElfrReportEventA(hEventLog
,
989 (PRPC_STRING
) &ComputerName
,
991 (PRPC_STRING
*) &Strings
,
997 RpcExcept(EXCEPTION_EXECUTE_HANDLER
)
999 Status
= I_RpcMapWin32Status(RpcExceptionCode());
1003 HeapFree(GetProcessHeap(), 0, Strings
);
1005 if (!NT_SUCCESS(Status
))
1007 SetLastError(RtlNtStatusToDosError(Status
));
1015 /******************************************************************************
1016 * ReportEventW [ADVAPI32.@]
1030 ReportEventW(IN HANDLE hEventLog
,
1035 IN WORD wNumStrings
,
1036 IN DWORD dwDataSize
,
1037 IN LPCWSTR
*lpStrings
,
1038 IN LPVOID lpRawData
)
1041 UNICODE_STRING
*Strings
;
1042 UNICODE_STRING ComputerName
;
1045 TRACE("%p, %u, %u, %lu, %p, %u, %lu, %p, %p\n",
1046 hEventLog
, wType
, wCategory
, dwEventID
, lpUserSid
,
1047 wNumStrings
, dwDataSize
, lpStrings
, lpRawData
);
1049 Strings
= HeapAlloc(GetProcessHeap(),
1051 wNumStrings
* sizeof(UNICODE_STRING
));
1054 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1058 for (i
= 0; i
< wNumStrings
; i
++)
1059 RtlInitUnicodeString(&Strings
[i
], lpStrings
[i
]);
1061 /*FIXME: ComputerName */
1062 RtlInitUnicodeString(&ComputerName
, L
"");
1066 Status
= ElfrReportEventW(hEventLog
,
1067 0, /* FIXME: Time */
1073 (PRPC_UNICODE_STRING
) &ComputerName
,
1075 (PRPC_UNICODE_STRING
*) &Strings
,
1081 RpcExcept(EXCEPTION_EXECUTE_HANDLER
)
1083 Status
= I_RpcMapWin32Status(RpcExceptionCode());
1087 HeapFree(GetProcessHeap(), 0, Strings
);
1089 if (!NT_SUCCESS(Status
))
1091 SetLastError(RtlNtStatusToDosError(Status
));