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
);
29 static RPC_UNICODE_STRING EmptyString
= { 0, 0, L
"" };
33 EVENTLOG_HANDLE_A_bind(EVENTLOG_HANDLE_A UNCServerName
)
35 handle_t hBinding
= NULL
;
36 UCHAR
*pszStringBinding
;
39 TRACE("EVENTLOG_HANDLE_A_bind() called\n");
41 status
= RpcStringBindingComposeA(NULL
,
43 (UCHAR
*)UNCServerName
,
44 (UCHAR
*)"\\pipe\\ntsvcs",
46 (UCHAR
**)&pszStringBinding
);
49 ERR("RpcStringBindingCompose returned 0x%x\n", status
);
53 /* Set the binding handle that will be used to bind to the server. */
54 status
= RpcBindingFromStringBindingA(pszStringBinding
,
58 ERR("RpcBindingFromStringBinding returned 0x%x\n", status
);
61 status
= RpcStringFreeA(&pszStringBinding
);
64 ERR("RpcStringFree returned 0x%x\n", status
);
72 EVENTLOG_HANDLE_A_unbind(EVENTLOG_HANDLE_A UNCServerName
,
77 TRACE("EVENTLOG_HANDLE_A_unbind() called\n");
79 status
= RpcBindingFree(&hBinding
);
82 ERR("RpcBindingFree returned 0x%x\n", status
);
88 EVENTLOG_HANDLE_W_bind(EVENTLOG_HANDLE_W UNCServerName
)
90 handle_t hBinding
= NULL
;
91 LPWSTR pszStringBinding
;
94 TRACE("EVENTLOG_HANDLE_W_bind() called\n");
96 status
= RpcStringBindingComposeW(NULL
,
98 (LPWSTR
)UNCServerName
,
104 ERR("RpcStringBindingCompose returned 0x%x\n", status
);
108 /* Set the binding handle that will be used to bind to the server. */
109 status
= RpcBindingFromStringBindingW(pszStringBinding
,
113 ERR("RpcBindingFromStringBinding returned 0x%x\n", status
);
116 status
= RpcStringFreeW(&pszStringBinding
);
119 ERR("RpcStringFree returned 0x%x\n", status
);
127 EVENTLOG_HANDLE_W_unbind(EVENTLOG_HANDLE_W UNCServerName
,
132 TRACE("EVENTLOG_HANDLE_W_unbind() called\n");
134 status
= RpcBindingFree(&hBinding
);
137 ERR("RpcBindingFree returned 0x%x\n", status
);
142 /******************************************************************************
143 * BackupEventLogA [ADVAPI32.@]
146 BackupEventLogA(IN HANDLE hEventLog
,
147 IN LPCSTR lpBackupFileName
)
149 RPC_STRING BackupFileName
;
152 TRACE("%p, %s\n", hEventLog
, lpBackupFileName
);
154 BackupFileName
.Buffer
= (LPSTR
)lpBackupFileName
;
155 BackupFileName
.Length
= BackupFileName
.MaximumLength
=
156 lpBackupFileName
? strlen(lpBackupFileName
) : 0;
160 Status
= ElfrBackupELFA(hEventLog
,
163 RpcExcept(EXCEPTION_EXECUTE_HANDLER
)
165 Status
= I_RpcMapWin32Status(RpcExceptionCode());
169 if (!NT_SUCCESS(Status
))
171 SetLastError(RtlNtStatusToDosError(Status
));
178 /******************************************************************************
179 * BackupEventLogW [ADVAPI32.@]
183 * lpBackupFileName []
186 BackupEventLogW(IN HANDLE hEventLog
,
187 IN LPCWSTR lpBackupFileName
)
189 RPC_UNICODE_STRING BackupFileName
;
192 TRACE("%p, %s\n", hEventLog
, debugstr_w(lpBackupFileName
));
194 BackupFileName
.Buffer
= (LPWSTR
)lpBackupFileName
;
195 BackupFileName
.Length
= BackupFileName
.MaximumLength
=
196 lpBackupFileName
? wcslen(lpBackupFileName
) * sizeof(WCHAR
) : 0;
200 Status
= ElfrBackupELFW(hEventLog
,
203 RpcExcept(EXCEPTION_EXECUTE_HANDLER
)
205 Status
= I_RpcMapWin32Status(RpcExceptionCode());
209 if (!NT_SUCCESS(Status
))
211 SetLastError(RtlNtStatusToDosError(Status
));
219 /******************************************************************************
220 * ClearEventLogA [ADVAPI32.@]
223 ClearEventLogA(IN HANDLE hEventLog
,
224 IN LPCSTR lpBackupFileName
)
226 RPC_STRING BackupFileName
;
229 TRACE("%p, %s\n", hEventLog
, lpBackupFileName
);
231 BackupFileName
.Buffer
= (LPSTR
)lpBackupFileName
;
232 BackupFileName
.Length
= BackupFileName
.MaximumLength
=
233 lpBackupFileName
? strlen(lpBackupFileName
) : 0;
237 Status
= ElfrClearELFA(hEventLog
,
240 RpcExcept(EXCEPTION_EXECUTE_HANDLER
)
242 Status
= I_RpcMapWin32Status(RpcExceptionCode());
246 if (!NT_SUCCESS(Status
))
248 SetLastError(RtlNtStatusToDosError(Status
));
256 /******************************************************************************
257 * ClearEventLogW [ADVAPI32.@]
260 ClearEventLogW(IN HANDLE hEventLog
,
261 IN LPCWSTR lpBackupFileName
)
263 RPC_UNICODE_STRING BackupFileName
;
266 TRACE("%p, %s\n", hEventLog
, debugstr_w(lpBackupFileName
));
268 BackupFileName
.Buffer
= (LPWSTR
)lpBackupFileName
;
269 BackupFileName
.Length
= BackupFileName
.MaximumLength
=
270 lpBackupFileName
? wcslen(lpBackupFileName
) * sizeof(WCHAR
) : 0;
274 Status
= ElfrClearELFW(hEventLog
,
277 RpcExcept(EXCEPTION_EXECUTE_HANDLER
)
279 Status
= I_RpcMapWin32Status(RpcExceptionCode());
283 if (!NT_SUCCESS(Status
))
285 SetLastError(RtlNtStatusToDosError(Status
));
293 /******************************************************************************
294 * CloseEventLog [ADVAPI32.@]
297 CloseEventLog(IN HANDLE hEventLog
)
301 TRACE("%p\n", hEventLog
);
305 Status
= ElfrCloseEL(&hEventLog
);
307 RpcExcept(EXCEPTION_EXECUTE_HANDLER
)
309 Status
= I_RpcMapWin32Status(RpcExceptionCode());
313 if (!NT_SUCCESS(Status
))
315 SetLastError(RtlNtStatusToDosError(Status
));
323 /******************************************************************************
324 * DeregisterEventSource [ADVAPI32.@]
325 * Closes a handle to the specified event log
328 * hEventLog [I] Handle to event log
333 DeregisterEventSource(IN HANDLE hEventLog
)
337 TRACE("%p\n", hEventLog
);
341 Status
= ElfrDeregisterEventSource(&hEventLog
);
343 RpcExcept(EXCEPTION_EXECUTE_HANDLER
)
345 Status
= I_RpcMapWin32Status(RpcExceptionCode());
349 if (!NT_SUCCESS(Status
))
351 SetLastError(RtlNtStatusToDosError(Status
));
359 /******************************************************************************
360 * GetNumberOfEventLogRecords [ADVAPI32.@]
367 GetNumberOfEventLogRecords(IN HANDLE hEventLog
,
368 OUT PDWORD NumberOfRecords
)
373 TRACE("%p, %p\n", hEventLog
, NumberOfRecords
);
377 Status
= ElfrNumberOfRecords(hEventLog
,
380 RpcExcept(EXCEPTION_EXECUTE_HANDLER
)
382 Status
= I_RpcMapWin32Status(RpcExceptionCode());
386 if (!NT_SUCCESS(Status
))
388 SetLastError(RtlNtStatusToDosError(Status
));
392 *NumberOfRecords
= Records
;
398 /******************************************************************************
399 * GetOldestEventLogRecord [ADVAPI32.@]
406 GetOldestEventLogRecord(IN HANDLE hEventLog
,
407 OUT PDWORD OldestRecord
)
412 TRACE("%p, %p\n", hEventLog
, OldestRecord
);
416 Status
= ElfrOldestRecord(hEventLog
,
419 RpcExcept(EXCEPTION_EXECUTE_HANDLER
)
421 Status
= I_RpcMapWin32Status(RpcExceptionCode());
425 if (!NT_SUCCESS(Status
))
427 SetLastError(RtlNtStatusToDosError(Status
));
431 *OldestRecord
= Oldest
;
437 /******************************************************************************
438 * NotifyChangeEventLog [ADVAPI32.@]
445 NotifyChangeEventLog(IN HANDLE hEventLog
,
448 /* Use ElfrChangeNotify */
450 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
455 /******************************************************************************
456 * OpenBackupEventLogA [ADVAPI32.@]
459 OpenBackupEventLogA(IN LPCSTR lpUNCServerName
,
460 IN LPCSTR lpFileName
)
462 UNICODE_STRING UNCServerName
;
463 UNICODE_STRING FileName
;
466 TRACE("%s, %s\n", lpUNCServerName
, lpFileName
);
468 if (!RtlCreateUnicodeStringFromAsciiz(&UNCServerName
, lpUNCServerName
))
470 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
474 if (!RtlCreateUnicodeStringFromAsciiz(&FileName
, lpFileName
))
476 RtlFreeUnicodeString(&UNCServerName
);
477 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
481 Handle
= OpenBackupEventLogW(UNCServerName
.Buffer
,
484 RtlFreeUnicodeString(&UNCServerName
);
485 RtlFreeUnicodeString(&FileName
);
491 /******************************************************************************
492 * OpenBackupEventLogW [ADVAPI32.@]
499 OpenBackupEventLogW(IN LPCWSTR lpUNCServerName
,
500 IN LPCWSTR lpFileName
)
502 RPC_UNICODE_STRING FileName
;
503 IELF_HANDLE LogHandle
;
506 TRACE("%s, %s\n", debugstr_w(lpUNCServerName
), debugstr_w(lpFileName
));
508 FileName
.Buffer
= (LPWSTR
)lpFileName
;
509 FileName
.Length
= FileName
.MaximumLength
=
510 lpFileName
? wcslen(lpFileName
) * sizeof(WCHAR
) : 0;
514 Status
= ElfrOpenBELW((LPWSTR
)lpUNCServerName
,
520 RpcExcept(EXCEPTION_EXECUTE_HANDLER
)
522 Status
= I_RpcMapWin32Status(RpcExceptionCode());
526 if (!NT_SUCCESS(Status
))
528 SetLastError(RtlNtStatusToDosError(Status
));
532 return (HANDLE
)LogHandle
;
536 /******************************************************************************
537 * OpenEventLogA [ADVAPI32.@]
540 OpenEventLogA(IN LPCSTR lpUNCServerName
,
541 IN LPCSTR lpSourceName
)
543 UNICODE_STRING UNCServerName
;
544 UNICODE_STRING SourceName
;
547 if (!RtlCreateUnicodeStringFromAsciiz(&UNCServerName
, lpUNCServerName
))
549 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
553 if (!RtlCreateUnicodeStringFromAsciiz(&SourceName
, lpSourceName
))
555 RtlFreeUnicodeString(&UNCServerName
);
556 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
560 Handle
= OpenEventLogW(UNCServerName
.Buffer
,
563 RtlFreeUnicodeString(&UNCServerName
);
564 RtlFreeUnicodeString(&SourceName
);
570 /******************************************************************************
571 * OpenEventLogW [ADVAPI32.@]
578 OpenEventLogW(IN LPCWSTR lpUNCServerName
,
579 IN LPCWSTR lpSourceName
)
581 RPC_UNICODE_STRING SourceName
;
582 IELF_HANDLE LogHandle
;
585 TRACE("%s, %s\n", debugstr_w(lpUNCServerName
), debugstr_w(lpSourceName
));
587 SourceName
.Buffer
= (LPWSTR
)lpSourceName
;
588 SourceName
.Length
= SourceName
.MaximumLength
=
589 lpSourceName
? wcslen(lpSourceName
) * sizeof(WCHAR
) : 0;
593 Status
= ElfrOpenELW((LPWSTR
)lpUNCServerName
,
600 RpcExcept(EXCEPTION_EXECUTE_HANDLER
)
602 Status
= I_RpcMapWin32Status(RpcExceptionCode());
606 if (!NT_SUCCESS(Status
))
608 SetLastError(RtlNtStatusToDosError(Status
));
612 return (HANDLE
)LogHandle
;
616 /******************************************************************************
617 * ReadEventLogA [ADVAPI32.@]
620 ReadEventLogA(IN HANDLE hEventLog
,
621 IN DWORD dwReadFlags
,
622 IN DWORD dwRecordOffset
,
624 IN DWORD nNumberOfBytesToRead
,
625 OUT DWORD
*pnBytesRead
,
626 OUT DWORD
*pnMinNumberOfBytesNeeded
)
629 DWORD bytesRead
, minNumberOfBytesNeeded
;
631 TRACE("%p, %lu, %lu, %p, %lu, %p, %p\n",
632 hEventLog
, dwReadFlags
, dwRecordOffset
, lpBuffer
,
633 nNumberOfBytesToRead
, pnBytesRead
, pnMinNumberOfBytesNeeded
);
635 /* If buffer is NULL set nNumberOfBytesToRead to 0 to prevent rpcrt4 from
636 trying to access a null pointer */
639 nNumberOfBytesToRead
= 0;
644 Status
= ElfrReadELA(hEventLog
,
647 nNumberOfBytesToRead
,
650 &minNumberOfBytesNeeded
);
652 RpcExcept(EXCEPTION_EXECUTE_HANDLER
)
654 Status
= I_RpcMapWin32Status(RpcExceptionCode());
658 *pnBytesRead
= (DWORD
)bytesRead
;
659 *pnMinNumberOfBytesNeeded
= (DWORD
)minNumberOfBytesNeeded
;
661 if (!NT_SUCCESS(Status
))
663 SetLastError(RtlNtStatusToDosError(Status
));
671 /******************************************************************************
672 * ReadEventLogW [ADVAPI32.@]
679 * nNumberOfBytesToRead []
681 * pnMinNumberOfBytesNeeded []
684 ReadEventLogW(IN HANDLE hEventLog
,
685 IN DWORD dwReadFlags
,
686 IN DWORD dwRecordOffset
,
688 IN DWORD nNumberOfBytesToRead
,
689 OUT DWORD
*pnBytesRead
,
690 OUT DWORD
*pnMinNumberOfBytesNeeded
)
693 DWORD bytesRead
, minNumberOfBytesNeeded
;
695 TRACE("%p, %lu, %lu, %p, %lu, %p, %p\n",
696 hEventLog
, dwReadFlags
, dwRecordOffset
, lpBuffer
,
697 nNumberOfBytesToRead
, pnBytesRead
, pnMinNumberOfBytesNeeded
);
699 /* If buffer is NULL set nNumberOfBytesToRead to 0 to prevent rpcrt4 from
700 trying to access a null pointer */
703 nNumberOfBytesToRead
= 0;
708 Status
= ElfrReadELW(hEventLog
,
711 nNumberOfBytesToRead
,
714 &minNumberOfBytesNeeded
);
716 RpcExcept(EXCEPTION_EXECUTE_HANDLER
)
718 Status
= I_RpcMapWin32Status(RpcExceptionCode());
722 *pnBytesRead
= (DWORD
)bytesRead
;
723 *pnMinNumberOfBytesNeeded
= (DWORD
)minNumberOfBytesNeeded
;
725 if (!NT_SUCCESS(Status
))
727 SetLastError(RtlNtStatusToDosError(Status
));
735 /******************************************************************************
736 * RegisterEventSourceA [ADVAPI32.@]
739 RegisterEventSourceA(IN LPCSTR lpUNCServerName
,
740 IN LPCSTR lpSourceName
)
742 UNICODE_STRING UNCServerName
;
743 UNICODE_STRING SourceName
;
746 TRACE("%s, %s\n", lpUNCServerName
, lpSourceName
);
748 if (!RtlCreateUnicodeStringFromAsciiz(&UNCServerName
, lpUNCServerName
))
750 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
754 if (!RtlCreateUnicodeStringFromAsciiz(&SourceName
, lpSourceName
))
756 RtlFreeUnicodeString(&UNCServerName
);
757 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
761 Handle
= RegisterEventSourceW(UNCServerName
.Buffer
,
764 RtlFreeUnicodeString(&UNCServerName
);
765 RtlFreeUnicodeString(&SourceName
);
771 /******************************************************************************
772 * RegisterEventSourceW [ADVAPI32.@]
773 * Returns a registered handle to an event log
776 * lpUNCServerName [I] Server name for source
777 * lpSourceName [I] Source name for registered handle
784 RegisterEventSourceW(IN LPCWSTR lpUNCServerName
,
785 IN LPCWSTR lpSourceName
)
787 RPC_UNICODE_STRING SourceName
;
788 IELF_HANDLE LogHandle
;
791 TRACE("%s, %s\n", debugstr_w(lpUNCServerName
), debugstr_w(lpSourceName
));
793 SourceName
.Buffer
= (LPWSTR
)lpSourceName
;
794 SourceName
.Length
= SourceName
.MaximumLength
=
795 lpSourceName
? wcslen(lpSourceName
) * sizeof(WCHAR
) : 0;
799 Status
= ElfrRegisterEventSourceW((LPWSTR
)lpUNCServerName
,
806 RpcExcept(EXCEPTION_EXECUTE_HANDLER
)
808 Status
= I_RpcMapWin32Status(RpcExceptionCode());
812 if (!NT_SUCCESS(Status
))
814 SetLastError(RtlNtStatusToDosError(Status
));
818 return (HANDLE
)LogHandle
;
822 /******************************************************************************
823 * ReportEventA [ADVAPI32.@]
826 ReportEventA(IN HANDLE hEventLog
,
833 IN LPCSTR
*lpStrings
,
836 LPCWSTR
*wideStrArray
;
841 if (wNumStrings
== 0)
844 if (lpStrings
== NULL
)
847 wideStrArray
= HeapAlloc(GetProcessHeap(),
849 sizeof(LPCWSTR
) * wNumStrings
);
851 for (i
= 0; i
< wNumStrings
; i
++)
853 if (!RtlCreateUnicodeStringFromAsciiz(&str
, (PSTR
)lpStrings
[i
]))
855 wideStrArray
[i
] = str
.Buffer
;
858 if (i
== wNumStrings
)
860 ret
= ReportEventW(hEventLog
,
872 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
876 for (i
= 0; i
< wNumStrings
; i
++)
880 HeapFree(GetProcessHeap(),
882 (PVOID
)wideStrArray
[i
]);
886 HeapFree(GetProcessHeap(),
888 (PVOID
)wideStrArray
);
894 /******************************************************************************
895 * ReportEventW [ADVAPI32.@]
909 ReportEventW(IN HANDLE hEventLog
,
916 IN LPCWSTR
*lpStrings
,
920 UNICODE_STRING
*Strings
;
921 UNICODE_STRING ComputerName
;
924 TRACE("%p, %u, %u, %lu, %p, %u, %lu, %p, %p\n",
925 hEventLog
, wType
, wCategory
, dwEventID
, lpUserSid
,
926 wNumStrings
, dwDataSize
, lpStrings
, lpRawData
);
928 Strings
= HeapAlloc(GetProcessHeap(),
930 wNumStrings
* sizeof(UNICODE_STRING
));
933 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
937 for (i
= 0; i
< wNumStrings
; i
++)
938 RtlInitUnicodeString(&Strings
[i
], lpStrings
[i
]);
940 /*FIXME: ComputerName */
941 RtlInitEmptyUnicodeString(&ComputerName
, NULL
, 0);
945 Status
= ElfrReportEventW(hEventLog
,
952 (PRPC_UNICODE_STRING
) &ComputerName
,
954 (PRPC_UNICODE_STRING
*) &Strings
,
960 RpcExcept(EXCEPTION_EXECUTE_HANDLER
)
962 Status
= I_RpcMapWin32Status(RpcExceptionCode());
966 HeapFree(GetProcessHeap(), 0, Strings
);
968 if (!NT_SUCCESS(Status
))
970 SetLastError(RtlNtStatusToDosError(Status
));