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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include "wine/debug.h"
27 WINE_DEFAULT_DEBUG_CHANNEL(advapi
);
28 WINE_DECLARE_DEBUG_CHANNEL(eventlog
);
30 static RPC_UNICODE_STRING EmptyString
= { 0, 0, L
"" };
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;
161 Status
= ElfrBackupELFA(hEventLog
,
166 Status
= I_RpcMapWin32Status(RpcExceptionCode());
170 if (!NT_SUCCESS(Status
))
172 SetLastError(RtlNtStatusToDosError(Status
));
179 /******************************************************************************
180 * BackupEventLogW [ADVAPI32.@]
184 * lpBackupFileName []
187 BackupEventLogW(IN HANDLE hEventLog
,
188 IN LPCWSTR lpBackupFileName
)
190 RPC_UNICODE_STRING BackupFileName
;
193 TRACE("%p, %s\n", hEventLog
, debugstr_w(lpBackupFileName
));
195 BackupFileName
.Buffer
= (LPWSTR
)lpBackupFileName
;
196 BackupFileName
.Length
= BackupFileName
.MaximumLength
=
197 lpBackupFileName
? wcslen(lpBackupFileName
) * sizeof(WCHAR
) : 0;
201 Status
= ElfrBackupELFW(hEventLog
,
206 Status
= I_RpcMapWin32Status(RpcExceptionCode());
210 if (!NT_SUCCESS(Status
))
212 SetLastError(RtlNtStatusToDosError(Status
));
220 /******************************************************************************
221 * ClearEventLogA [ADVAPI32.@]
224 ClearEventLogA(IN HANDLE hEventLog
,
225 IN LPCSTR lpBackupFileName
)
227 RPC_STRING BackupFileName
;
230 TRACE("%p, %s\n", hEventLog
, lpBackupFileName
);
232 BackupFileName
.Buffer
= (LPSTR
)lpBackupFileName
;
233 BackupFileName
.Length
= BackupFileName
.MaximumLength
=
234 lpBackupFileName
? strlen(lpBackupFileName
) : 0;
238 Status
= ElfrClearELFA(hEventLog
,
243 Status
= I_RpcMapWin32Status(RpcExceptionCode());
247 if (!NT_SUCCESS(Status
))
249 SetLastError(RtlNtStatusToDosError(Status
));
257 /******************************************************************************
258 * ClearEventLogW [ADVAPI32.@]
261 ClearEventLogW(IN HANDLE hEventLog
,
262 IN LPCWSTR lpBackupFileName
)
264 RPC_UNICODE_STRING BackupFileName
;
267 TRACE("%p, %s\n", hEventLog
, debugstr_w(lpBackupFileName
));
269 BackupFileName
.Buffer
= (LPWSTR
)lpBackupFileName
;
270 BackupFileName
.Length
= BackupFileName
.MaximumLength
=
271 lpBackupFileName
? wcslen(lpBackupFileName
) * sizeof(WCHAR
) : 0;
275 Status
= ElfrClearELFW(hEventLog
,
280 Status
= I_RpcMapWin32Status(RpcExceptionCode());
284 if (!NT_SUCCESS(Status
))
286 SetLastError(RtlNtStatusToDosError(Status
));
294 /******************************************************************************
295 * CloseEventLog [ADVAPI32.@]
298 CloseEventLog(IN HANDLE hEventLog
)
302 TRACE("%p\n", hEventLog
);
306 Status
= ElfrCloseEL(&hEventLog
);
310 Status
= I_RpcMapWin32Status(RpcExceptionCode());
314 if (!NT_SUCCESS(Status
))
316 SetLastError(RtlNtStatusToDosError(Status
));
324 /******************************************************************************
325 * DeregisterEventSource [ADVAPI32.@]
326 * Closes a handle to the specified event log
329 * hEventLog [I] Handle to event log
334 DeregisterEventSource(IN HANDLE hEventLog
)
338 TRACE("%p\n", hEventLog
);
342 Status
= ElfrDeregisterEventSource(&hEventLog
);
346 Status
= I_RpcMapWin32Status(RpcExceptionCode());
350 if (!NT_SUCCESS(Status
))
352 SetLastError(RtlNtStatusToDosError(Status
));
360 /******************************************************************************
361 * GetNumberOfEventLogRecords [ADVAPI32.@]
368 GetNumberOfEventLogRecords(IN HANDLE hEventLog
,
369 OUT PDWORD NumberOfRecords
)
374 TRACE("%p, %p\n", hEventLog
, NumberOfRecords
);
378 Status
= ElfrNumberOfRecords(hEventLog
,
383 Status
= I_RpcMapWin32Status(RpcExceptionCode());
387 if (!NT_SUCCESS(Status
))
389 SetLastError(RtlNtStatusToDosError(Status
));
393 *NumberOfRecords
= Records
;
399 /******************************************************************************
400 * GetOldestEventLogRecord [ADVAPI32.@]
407 GetOldestEventLogRecord(IN HANDLE hEventLog
,
408 OUT PDWORD OldestRecord
)
413 TRACE("%p, %p\n", hEventLog
, OldestRecord
);
417 Status
= ElfrOldestRecord(hEventLog
,
422 Status
= I_RpcMapWin32Status(RpcExceptionCode());
426 if (!NT_SUCCESS(Status
))
428 SetLastError(RtlNtStatusToDosError(Status
));
432 *OldestRecord
= Oldest
;
438 /******************************************************************************
439 * NotifyChangeEventLog [ADVAPI32.@]
446 NotifyChangeEventLog(IN HANDLE hEventLog
,
449 /* Use ElfrChangeNotify */
451 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
456 /******************************************************************************
457 * OpenBackupEventLogA [ADVAPI32.@]
460 OpenBackupEventLogA(IN LPCSTR lpUNCServerName
,
461 IN LPCSTR lpFileName
)
463 UNICODE_STRING UNCServerName
;
464 UNICODE_STRING FileName
;
467 TRACE("%s, %s\n", lpUNCServerName
, lpFileName
);
469 if (!RtlCreateUnicodeStringFromAsciiz(&UNCServerName
, lpUNCServerName
))
471 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
475 if (!RtlCreateUnicodeStringFromAsciiz(&FileName
, lpFileName
))
477 RtlFreeUnicodeString(&UNCServerName
);
478 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
482 Handle
= OpenBackupEventLogW(UNCServerName
.Buffer
,
485 RtlFreeUnicodeString(&UNCServerName
);
486 RtlFreeUnicodeString(&FileName
);
492 /******************************************************************************
493 * OpenBackupEventLogW [ADVAPI32.@]
500 OpenBackupEventLogW(IN LPCWSTR lpUNCServerName
,
501 IN LPCWSTR lpFileName
)
503 RPC_UNICODE_STRING FileName
;
504 IELF_HANDLE LogHandle
;
507 TRACE("%s, %s\n", debugstr_w(lpUNCServerName
), debugstr_w(lpFileName
));
509 FileName
.Buffer
= (LPWSTR
)lpFileName
;
510 FileName
.Length
= FileName
.MaximumLength
=
511 lpFileName
? wcslen(lpFileName
) * sizeof(WCHAR
) : 0;
515 Status
= ElfrOpenBELW((LPWSTR
)lpUNCServerName
,
523 Status
= I_RpcMapWin32Status(RpcExceptionCode());
527 if (!NT_SUCCESS(Status
))
529 SetLastError(RtlNtStatusToDosError(Status
));
533 return (HANDLE
)LogHandle
;
537 /******************************************************************************
538 * OpenEventLogA [ADVAPI32.@]
541 OpenEventLogA(IN LPCSTR lpUNCServerName
,
542 IN LPCSTR lpSourceName
)
544 UNICODE_STRING UNCServerName
;
545 UNICODE_STRING SourceName
;
548 if (!RtlCreateUnicodeStringFromAsciiz(&UNCServerName
, lpUNCServerName
))
550 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
554 if (!RtlCreateUnicodeStringFromAsciiz(&SourceName
, lpSourceName
))
556 RtlFreeUnicodeString(&UNCServerName
);
557 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
561 Handle
= OpenEventLogW(UNCServerName
.Buffer
,
564 RtlFreeUnicodeString(&UNCServerName
);
565 RtlFreeUnicodeString(&SourceName
);
571 /******************************************************************************
572 * OpenEventLogW [ADVAPI32.@]
579 OpenEventLogW(IN LPCWSTR lpUNCServerName
,
580 IN LPCWSTR lpSourceName
)
582 RPC_UNICODE_STRING SourceName
;
583 IELF_HANDLE LogHandle
;
586 TRACE("%s, %s\n", debugstr_w(lpUNCServerName
), debugstr_w(lpSourceName
));
588 SourceName
.Buffer
= (LPWSTR
)lpSourceName
;
589 SourceName
.Length
= SourceName
.MaximumLength
=
590 lpSourceName
? wcslen(lpSourceName
) * sizeof(WCHAR
) : 0;
594 Status
= ElfrOpenELW((LPWSTR
)lpUNCServerName
,
603 Status
= I_RpcMapWin32Status(RpcExceptionCode());
607 if (!NT_SUCCESS(Status
))
609 SetLastError(RtlNtStatusToDosError(Status
));
613 return (HANDLE
)LogHandle
;
617 /******************************************************************************
618 * ReadEventLogA [ADVAPI32.@]
621 ReadEventLogA(IN HANDLE hEventLog
,
622 IN DWORD dwReadFlags
,
623 IN DWORD dwRecordOffset
,
625 IN DWORD nNumberOfBytesToRead
,
626 OUT DWORD
*pnBytesRead
,
627 OUT DWORD
*pnMinNumberOfBytesNeeded
)
630 DWORD bytesRead
, minNumberOfBytesNeeded
;
632 TRACE("%p, %lu, %lu, %p, %lu, %p, %p\n",
633 hEventLog
, dwReadFlags
, dwRecordOffset
, lpBuffer
,
634 nNumberOfBytesToRead
, pnBytesRead
, pnMinNumberOfBytesNeeded
);
638 Status
= ElfrReadELA(hEventLog
,
641 nNumberOfBytesToRead
,
644 &minNumberOfBytesNeeded
);
648 Status
= I_RpcMapWin32Status(RpcExceptionCode());
652 if (!NT_SUCCESS(Status
))
654 SetLastError(RtlNtStatusToDosError(Status
));
658 *pnBytesRead
= (DWORD
)bytesRead
;
659 *pnMinNumberOfBytesNeeded
= (DWORD
)minNumberOfBytesNeeded
;
665 /******************************************************************************
666 * ReadEventLogW [ADVAPI32.@]
673 * nNumberOfBytesToRead []
675 * pnMinNumberOfBytesNeeded []
678 ReadEventLogW(IN HANDLE hEventLog
,
679 IN DWORD dwReadFlags
,
680 IN DWORD dwRecordOffset
,
682 IN DWORD nNumberOfBytesToRead
,
683 OUT DWORD
*pnBytesRead
,
684 OUT DWORD
*pnMinNumberOfBytesNeeded
)
687 DWORD bytesRead
, minNumberOfBytesNeeded
;
689 TRACE("%p, %lu, %lu, %p, %lu, %p, %p\n",
690 hEventLog
, dwReadFlags
, dwRecordOffset
, lpBuffer
,
691 nNumberOfBytesToRead
, pnBytesRead
, pnMinNumberOfBytesNeeded
);
695 Status
= ElfrReadELW(hEventLog
,
698 nNumberOfBytesToRead
,
701 &minNumberOfBytesNeeded
);
705 Status
= I_RpcMapWin32Status(RpcExceptionCode());
709 if (!NT_SUCCESS(Status
))
711 SetLastError(RtlNtStatusToDosError(Status
));
715 *pnBytesRead
= (DWORD
)bytesRead
;
716 *pnMinNumberOfBytesNeeded
= (DWORD
)minNumberOfBytesNeeded
;
722 /******************************************************************************
723 * RegisterEventSourceA [ADVAPI32.@]
726 RegisterEventSourceA(IN LPCSTR lpUNCServerName
,
727 IN LPCSTR lpSourceName
)
729 UNICODE_STRING UNCServerName
;
730 UNICODE_STRING SourceName
;
733 TRACE("%s, %s\n", lpUNCServerName
, lpSourceName
);
735 if (!RtlCreateUnicodeStringFromAsciiz(&UNCServerName
, lpUNCServerName
))
737 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
741 if (!RtlCreateUnicodeStringFromAsciiz(&SourceName
, lpSourceName
))
743 RtlFreeUnicodeString(&UNCServerName
);
744 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
748 Handle
= RegisterEventSourceW(UNCServerName
.Buffer
,
751 RtlFreeUnicodeString(&UNCServerName
);
752 RtlFreeUnicodeString(&SourceName
);
758 /******************************************************************************
759 * RegisterEventSourceW [ADVAPI32.@]
760 * Returns a registered handle to an event log
763 * lpUNCServerName [I] Server name for source
764 * lpSourceName [I] Source name for registered handle
771 RegisterEventSourceW(IN LPCWSTR lpUNCServerName
,
772 IN LPCWSTR lpSourceName
)
774 RPC_UNICODE_STRING SourceName
;
775 IELF_HANDLE LogHandle
;
778 TRACE("%s, %s\n", debugstr_w(lpUNCServerName
), debugstr_w(lpSourceName
));
780 SourceName
.Buffer
= (LPWSTR
)lpSourceName
;
781 SourceName
.Length
= SourceName
.MaximumLength
=
782 lpSourceName
? wcslen(lpSourceName
) * sizeof(WCHAR
) : 0;
786 Status
= ElfrRegisterEventSourceW((LPWSTR
)lpUNCServerName
,
795 Status
= I_RpcMapWin32Status(RpcExceptionCode());
799 if (!NT_SUCCESS(Status
))
801 SetLastError(RtlNtStatusToDosError(Status
));
805 return (HANDLE
)LogHandle
;
809 /******************************************************************************
810 * ReportEventA [ADVAPI32.@]
813 ReportEventA(IN HANDLE hEventLog
,
820 IN LPCSTR
*lpStrings
,
823 LPCWSTR
*wideStrArray
;
828 if (wNumStrings
== 0)
831 if (lpStrings
== NULL
)
834 wideStrArray
= HeapAlloc(GetProcessHeap(),
836 sizeof(LPCWSTR
) * wNumStrings
);
838 for (i
= 0; i
< wNumStrings
; i
++)
840 if (!RtlCreateUnicodeStringFromAsciiz(&str
, (PSTR
)lpStrings
[i
]))
842 wideStrArray
[i
] = str
.Buffer
;
845 if (i
== wNumStrings
)
847 ret
= ReportEventW(hEventLog
,
859 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
863 for (i
= 0; i
< wNumStrings
; i
++)
867 HeapFree(GetProcessHeap(),
869 (PVOID
)wideStrArray
[i
]);
873 HeapFree(GetProcessHeap(),
881 /******************************************************************************
882 * ReportEventW [ADVAPI32.@]
896 ReportEventW(IN HANDLE hEventLog
,
903 IN LPCWSTR
*lpStrings
,
908 UNICODE_STRING
*Strings
;
911 TRACE("%p, %u, %u, %lu, %p, %u, %lu, %p, %p\n",
912 hEventLog
, wType
, wCategory
, dwEventID
, lpUserSid
,
913 wNumStrings
, dwDataSize
, lpStrings
, lpRawData
);
915 Strings
= HeapAlloc(GetProcessHeap(),
917 wNumStrings
* sizeof(UNICODE_STRING
));
920 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
924 for (i
= 0; i
< wNumStrings
; i
++)
925 RtlInitUnicodeString(&Strings
[i
], lpStrings
[i
]);
929 Status
= ElfrReportEventW(hEventLog
,
936 L
"", /* FIXME: ComputerName */
938 (LPWSTR
*)lpStrings
, /* FIXME: should be Strings */
946 Status
= I_RpcMapWin32Status(RpcExceptionCode());
950 HeapFree(GetProcessHeap(), 0, Strings
);
952 if (!NT_SUCCESS(Status
))
954 SetLastError(RtlNtStatusToDosError(Status
));
964 if (wNumStrings
== 0)
967 if (lpStrings
== NULL
)
970 for (i
= 0; i
< wNumStrings
; i
++)
974 case EVENTLOG_SUCCESS
:
975 TRACE_(eventlog
)("Success: %S\n", lpStrings
[i
]);
978 case EVENTLOG_ERROR_TYPE
:
979 ERR_(eventlog
)("Error: %S\n", lpStrings
[i
]);
982 case EVENTLOG_WARNING_TYPE
:
983 WARN_(eventlog
)("Warning: %S\n", lpStrings
[i
]);
986 case EVENTLOG_INFORMATION_TYPE
:
987 TRACE_(eventlog
)("Info: %S\n", lpStrings
[i
]);
991 TRACE_(eventlog
)("Type %hu: %S\n", wType
, lpStrings
[i
]);