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 EmptyString
= { 0, 0, L
"" };
31 static inline LPWSTR
SERV_dup( LPCSTR str
)
38 len
= MultiByteToWideChar( CP_ACP
, 0, str
, -1, NULL
, 0 );
39 wstr
= HeapAlloc( GetProcessHeap(), 0, len
*sizeof (WCHAR
) );
40 MultiByteToWideChar( CP_ACP
, 0, str
, -1, wstr
, len
);
45 EVENTLOG_HANDLE_A_bind(EVENTLOG_HANDLE_A UNCServerName
)
47 handle_t hBinding
= NULL
;
48 UCHAR
*pszStringBinding
;
51 TRACE("EVENTLOG_HANDLE_A_bind() called\n");
53 status
= RpcStringBindingComposeA(NULL
,
55 (UCHAR
*)UNCServerName
,
56 (UCHAR
*)"\\pipe\\ntsvcs",
58 (UCHAR
**)&pszStringBinding
);
61 ERR("RpcStringBindingCompose returned 0x%x\n", status
);
65 /* Set the binding handle that will be used to bind to the server. */
66 status
= RpcBindingFromStringBindingA(pszStringBinding
,
70 ERR("RpcBindingFromStringBinding returned 0x%x\n", status
);
73 status
= RpcStringFreeA(&pszStringBinding
);
76 ERR("RpcStringFree returned 0x%x\n", status
);
84 EVENTLOG_HANDLE_A_unbind(EVENTLOG_HANDLE_A UNCServerName
,
89 TRACE("EVENTLOG_HANDLE_A_unbind() called\n");
91 status
= RpcBindingFree(&hBinding
);
94 ERR("RpcBindingFree returned 0x%x\n", status
);
100 EVENTLOG_HANDLE_W_bind(EVENTLOG_HANDLE_W UNCServerName
)
102 handle_t hBinding
= NULL
;
103 LPWSTR pszStringBinding
;
106 TRACE("EVENTLOG_HANDLE_W_bind() called\n");
108 status
= RpcStringBindingComposeW(NULL
,
110 (LPWSTR
)UNCServerName
,
116 ERR("RpcStringBindingCompose returned 0x%x\n", status
);
120 /* Set the binding handle that will be used to bind to the server. */
121 status
= RpcBindingFromStringBindingW(pszStringBinding
,
125 ERR("RpcBindingFromStringBinding returned 0x%x\n", status
);
128 status
= RpcStringFreeW(&pszStringBinding
);
131 ERR("RpcStringFree returned 0x%x\n", status
);
139 EVENTLOG_HANDLE_W_unbind(EVENTLOG_HANDLE_W UNCServerName
,
144 TRACE("EVENTLOG_HANDLE_W_unbind() called\n");
146 status
= RpcBindingFree(&hBinding
);
149 ERR("RpcBindingFree returned 0x%x\n", status
);
154 /******************************************************************************
155 * BackupEventLogA [ADVAPI32.@]
158 BackupEventLogA(IN HANDLE hEventLog
,
159 IN LPCSTR lpBackupFileName
)
161 RPC_STRING BackupFileName
;
164 TRACE("%p, %s\n", hEventLog
, lpBackupFileName
);
166 BackupFileName
.Buffer
= (LPSTR
)lpBackupFileName
;
167 BackupFileName
.Length
= BackupFileName
.MaximumLength
=
168 lpBackupFileName
? strlen(lpBackupFileName
) : 0;
169 BackupFileName
.MaximumLength
+= sizeof(CHAR
);
173 Status
= ElfrBackupELFA(hEventLog
,
176 RpcExcept(EXCEPTION_EXECUTE_HANDLER
)
178 Status
= I_RpcMapWin32Status(RpcExceptionCode());
182 if (!NT_SUCCESS(Status
))
184 SetLastError(RtlNtStatusToDosError(Status
));
191 /******************************************************************************
192 * BackupEventLogW [ADVAPI32.@]
196 * lpBackupFileName []
199 BackupEventLogW(IN HANDLE hEventLog
,
200 IN LPCWSTR lpBackupFileName
)
202 RPC_UNICODE_STRING BackupFileName
;
205 TRACE("%p, %s\n", hEventLog
, debugstr_w(lpBackupFileName
));
207 BackupFileName
.Buffer
= (LPWSTR
)lpBackupFileName
;
208 BackupFileName
.Length
= BackupFileName
.MaximumLength
=
209 lpBackupFileName
? wcslen(lpBackupFileName
) * sizeof(WCHAR
) : 0;
210 BackupFileName
.MaximumLength
+= sizeof(WCHAR
);
214 Status
= ElfrBackupELFW(hEventLog
,
217 RpcExcept(EXCEPTION_EXECUTE_HANDLER
)
219 Status
= I_RpcMapWin32Status(RpcExceptionCode());
223 if (!NT_SUCCESS(Status
))
225 SetLastError(RtlNtStatusToDosError(Status
));
233 /******************************************************************************
234 * ClearEventLogA [ADVAPI32.@]
237 ClearEventLogA(IN HANDLE hEventLog
,
238 IN LPCSTR lpBackupFileName
)
240 RPC_STRING BackupFileName
;
243 TRACE("%p, %s\n", hEventLog
, lpBackupFileName
);
245 BackupFileName
.Buffer
= (LPSTR
)lpBackupFileName
;
246 BackupFileName
.Length
= BackupFileName
.MaximumLength
=
247 lpBackupFileName
? strlen(lpBackupFileName
) : 0;
248 BackupFileName
.MaximumLength
+= sizeof(CHAR
);
252 Status
= ElfrClearELFA(hEventLog
,
255 RpcExcept(EXCEPTION_EXECUTE_HANDLER
)
257 Status
= I_RpcMapWin32Status(RpcExceptionCode());
261 if (!NT_SUCCESS(Status
))
263 SetLastError(RtlNtStatusToDosError(Status
));
271 /******************************************************************************
272 * ClearEventLogW [ADVAPI32.@]
275 ClearEventLogW(IN HANDLE hEventLog
,
276 IN LPCWSTR lpBackupFileName
)
278 RPC_UNICODE_STRING BackupFileName
;
281 TRACE("%p, %s\n", hEventLog
, debugstr_w(lpBackupFileName
));
283 BackupFileName
.Buffer
= (LPWSTR
)lpBackupFileName
;
284 BackupFileName
.Length
= BackupFileName
.MaximumLength
=
285 lpBackupFileName
? wcslen(lpBackupFileName
) * sizeof(WCHAR
) : 0;
286 BackupFileName
.MaximumLength
+= sizeof(WCHAR
);
290 Status
= ElfrClearELFW(hEventLog
,
293 RpcExcept(EXCEPTION_EXECUTE_HANDLER
)
295 Status
= I_RpcMapWin32Status(RpcExceptionCode());
299 if (!NT_SUCCESS(Status
))
301 SetLastError(RtlNtStatusToDosError(Status
));
309 /******************************************************************************
310 * CloseEventLog [ADVAPI32.@]
313 CloseEventLog(IN HANDLE hEventLog
)
317 TRACE("%p\n", hEventLog
);
321 Status
= ElfrCloseEL(&hEventLog
);
323 RpcExcept(EXCEPTION_EXECUTE_HANDLER
)
325 Status
= I_RpcMapWin32Status(RpcExceptionCode());
329 if (!NT_SUCCESS(Status
))
331 SetLastError(RtlNtStatusToDosError(Status
));
339 /******************************************************************************
340 * DeregisterEventSource [ADVAPI32.@]
341 * Closes a handle to the specified event log
344 * hEventLog [I] Handle to event log
349 DeregisterEventSource(IN HANDLE hEventLog
)
353 TRACE("%p\n", hEventLog
);
357 Status
= ElfrDeregisterEventSource(&hEventLog
);
359 RpcExcept(EXCEPTION_EXECUTE_HANDLER
)
361 Status
= I_RpcMapWin32Status(RpcExceptionCode());
365 if (!NT_SUCCESS(Status
))
367 SetLastError(RtlNtStatusToDosError(Status
));
375 /******************************************************************************
376 * GetEventLogInformation [ADVAPI32.@]
379 * hEventLog [I] Handle to event log
380 * dwInfoLevel [I] Level of event log information to return
381 * lpBuffer [O] Buffer that receives the event log information
382 * cbBufSize [I] Size of the lpBuffer buffer
383 * pcbBytesNeeded [O] Required buffer size
386 GetEventLogInformation(IN HANDLE hEventLog
,
387 IN DWORD dwInfoLevel
,
390 OUT LPDWORD pcbBytesNeeded
)
393 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
398 /******************************************************************************
399 * GetNumberOfEventLogRecords [ADVAPI32.@]
406 GetNumberOfEventLogRecords(IN HANDLE hEventLog
,
407 OUT PDWORD NumberOfRecords
)
412 TRACE("%p, %p\n", hEventLog
, NumberOfRecords
);
416 SetLastError(ERROR_INVALID_PARAMETER
);
422 Status
= ElfrNumberOfRecords(hEventLog
,
425 RpcExcept(EXCEPTION_EXECUTE_HANDLER
)
427 Status
= I_RpcMapWin32Status(RpcExceptionCode());
431 if (!NT_SUCCESS(Status
))
433 SetLastError(RtlNtStatusToDosError(Status
));
437 *NumberOfRecords
= Records
;
443 /******************************************************************************
444 * GetOldestEventLogRecord [ADVAPI32.@]
451 GetOldestEventLogRecord(IN HANDLE hEventLog
,
452 OUT PDWORD OldestRecord
)
457 TRACE("%p, %p\n", hEventLog
, OldestRecord
);
461 SetLastError(ERROR_INVALID_PARAMETER
);
467 Status
= ElfrOldestRecord(hEventLog
,
470 RpcExcept(EXCEPTION_EXECUTE_HANDLER
)
472 Status
= I_RpcMapWin32Status(RpcExceptionCode());
476 if (!NT_SUCCESS(Status
))
478 SetLastError(RtlNtStatusToDosError(Status
));
482 *OldestRecord
= Oldest
;
488 /******************************************************************************
489 * NotifyChangeEventLog [ADVAPI32.@]
496 NotifyChangeEventLog(IN HANDLE hEventLog
,
499 /* Use ElfrChangeNotify */
501 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
506 /******************************************************************************
507 * OpenBackupEventLogA [ADVAPI32.@]
510 OpenBackupEventLogA(IN LPCSTR lpUNCServerName
,
511 IN LPCSTR lpFileName
)
513 UNICODE_STRING UNCServerName
;
514 UNICODE_STRING FileName
;
517 TRACE("%s, %s\n", lpUNCServerName
, lpFileName
);
519 if (!RtlCreateUnicodeStringFromAsciiz(&UNCServerName
, lpUNCServerName
))
521 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
525 if (!RtlCreateUnicodeStringFromAsciiz(&FileName
, lpFileName
))
527 RtlFreeUnicodeString(&UNCServerName
);
528 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
532 Handle
= OpenBackupEventLogW(UNCServerName
.Buffer
,
535 RtlFreeUnicodeString(&UNCServerName
);
536 RtlFreeUnicodeString(&FileName
);
542 /******************************************************************************
543 * OpenBackupEventLogW [ADVAPI32.@]
550 OpenBackupEventLogW(IN LPCWSTR lpUNCServerName
,
551 IN LPCWSTR lpFileName
)
553 RPC_UNICODE_STRING FileName
;
554 IELF_HANDLE LogHandle
;
557 TRACE("%s, %s\n", debugstr_w(lpUNCServerName
), debugstr_w(lpFileName
));
559 FileName
.Buffer
= (LPWSTR
)lpFileName
;
560 FileName
.Length
= FileName
.MaximumLength
=
561 lpFileName
? wcslen(lpFileName
) * sizeof(WCHAR
) : 0;
562 FileName
.MaximumLength
+= sizeof(WCHAR
);
566 Status
= ElfrOpenBELW((LPWSTR
)lpUNCServerName
,
572 RpcExcept(EXCEPTION_EXECUTE_HANDLER
)
574 Status
= I_RpcMapWin32Status(RpcExceptionCode());
578 if (!NT_SUCCESS(Status
))
580 SetLastError(RtlNtStatusToDosError(Status
));
584 return (HANDLE
)LogHandle
;
588 /******************************************************************************
589 * OpenEventLogA [ADVAPI32.@]
591 * Opens a handle to the specified event log.
594 * lpUNCServerName [I] UNC name of the server on which the event log is
596 * lpSourceName [I] Name of the log.
599 * Success: Handle to an event log.
603 OpenEventLogA(IN LPCSTR uncname
,
606 LPWSTR uncnameW
, sourceW
;
609 uncnameW
= SERV_dup(uncname
);
610 sourceW
= SERV_dup(source
);
611 handle
= OpenEventLogW(uncnameW
, sourceW
);
612 HeapFree(GetProcessHeap(), 0, uncnameW
);
613 HeapFree(GetProcessHeap(), 0, sourceW
);
619 /******************************************************************************
620 * OpenEventLogW [ADVAPI32.@]
627 OpenEventLogW(IN LPCWSTR lpUNCServerName
,
628 IN LPCWSTR lpSourceName
)
630 RPC_UNICODE_STRING SourceName
;
631 IELF_HANDLE LogHandle
;
634 TRACE("%s, %s\n", debugstr_w(lpUNCServerName
), debugstr_w(lpSourceName
));
636 SourceName
.Buffer
= (LPWSTR
)lpSourceName
;
637 SourceName
.Length
= SourceName
.MaximumLength
=
638 lpSourceName
? wcslen(lpSourceName
) * sizeof(WCHAR
) : 0;
639 SourceName
.MaximumLength
+= sizeof(WCHAR
);
643 Status
= ElfrOpenELW((LPWSTR
)lpUNCServerName
,
650 RpcExcept(EXCEPTION_EXECUTE_HANDLER
)
652 Status
= I_RpcMapWin32Status(RpcExceptionCode());
656 if (!NT_SUCCESS(Status
))
658 SetLastError(RtlNtStatusToDosError(Status
));
662 return (HANDLE
)LogHandle
;
666 /******************************************************************************
667 * ReadEventLogA [ADVAPI32.@]
670 ReadEventLogA(IN HANDLE hEventLog
,
671 IN DWORD dwReadFlags
,
672 IN DWORD dwRecordOffset
,
674 IN DWORD nNumberOfBytesToRead
,
675 OUT DWORD
*pnBytesRead
,
676 OUT DWORD
*pnMinNumberOfBytesNeeded
)
679 DWORD bytesRead
, minNumberOfBytesNeeded
;
681 TRACE("%p, %lu, %lu, %p, %lu, %p, %p\n",
682 hEventLog
, dwReadFlags
, dwRecordOffset
, lpBuffer
,
683 nNumberOfBytesToRead
, pnBytesRead
, pnMinNumberOfBytesNeeded
);
685 if(!pnBytesRead
|| !pnMinNumberOfBytesNeeded
)
687 SetLastError(ERROR_INVALID_PARAMETER
);
691 /* If buffer is NULL set nNumberOfBytesToRead to 0 to prevent rpcrt4 from
692 trying to access a null pointer */
695 nNumberOfBytesToRead
= 0;
700 Status
= ElfrReadELA(hEventLog
,
703 nNumberOfBytesToRead
,
706 &minNumberOfBytesNeeded
);
708 RpcExcept(EXCEPTION_EXECUTE_HANDLER
)
710 Status
= I_RpcMapWin32Status(RpcExceptionCode());
714 *pnBytesRead
= (DWORD
)bytesRead
;
715 *pnMinNumberOfBytesNeeded
= (DWORD
)minNumberOfBytesNeeded
;
717 if (!NT_SUCCESS(Status
))
719 SetLastError(RtlNtStatusToDosError(Status
));
727 /******************************************************************************
728 * ReadEventLogW [ADVAPI32.@]
735 * nNumberOfBytesToRead []
737 * pnMinNumberOfBytesNeeded []
740 ReadEventLogW(IN HANDLE hEventLog
,
741 IN DWORD dwReadFlags
,
742 IN DWORD dwRecordOffset
,
744 IN DWORD nNumberOfBytesToRead
,
745 OUT DWORD
*pnBytesRead
,
746 OUT DWORD
*pnMinNumberOfBytesNeeded
)
749 DWORD bytesRead
, minNumberOfBytesNeeded
;
751 TRACE("%p, %lu, %lu, %p, %lu, %p, %p\n",
752 hEventLog
, dwReadFlags
, dwRecordOffset
, lpBuffer
,
753 nNumberOfBytesToRead
, pnBytesRead
, pnMinNumberOfBytesNeeded
);
755 if(!pnBytesRead
|| !pnMinNumberOfBytesNeeded
)
757 SetLastError(ERROR_INVALID_PARAMETER
);
761 /* If buffer is NULL set nNumberOfBytesToRead to 0 to prevent rpcrt4 from
762 trying to access a null pointer */
765 nNumberOfBytesToRead
= 0;
770 Status
= ElfrReadELW(hEventLog
,
773 nNumberOfBytesToRead
,
776 &minNumberOfBytesNeeded
);
778 RpcExcept(EXCEPTION_EXECUTE_HANDLER
)
780 Status
= I_RpcMapWin32Status(RpcExceptionCode());
784 *pnBytesRead
= (DWORD
)bytesRead
;
785 *pnMinNumberOfBytesNeeded
= (DWORD
)minNumberOfBytesNeeded
;
787 if (!NT_SUCCESS(Status
))
789 SetLastError(RtlNtStatusToDosError(Status
));
797 /******************************************************************************
798 * RegisterEventSourceA [ADVAPI32.@]
801 RegisterEventSourceA(IN LPCSTR lpUNCServerName
,
802 IN LPCSTR lpSourceName
)
804 UNICODE_STRING UNCServerName
;
805 UNICODE_STRING SourceName
;
808 TRACE("%s, %s\n", lpUNCServerName
, lpSourceName
);
810 if (!RtlCreateUnicodeStringFromAsciiz(&UNCServerName
, lpUNCServerName
))
812 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
816 if (!RtlCreateUnicodeStringFromAsciiz(&SourceName
, lpSourceName
))
818 RtlFreeUnicodeString(&UNCServerName
);
819 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
823 Handle
= RegisterEventSourceW(UNCServerName
.Buffer
,
826 RtlFreeUnicodeString(&UNCServerName
);
827 RtlFreeUnicodeString(&SourceName
);
833 /******************************************************************************
834 * RegisterEventSourceW [ADVAPI32.@]
835 * Returns a registered handle to an event log
838 * lpUNCServerName [I] Server name for source
839 * lpSourceName [I] Source name for registered handle
846 RegisterEventSourceW(IN LPCWSTR lpUNCServerName
,
847 IN LPCWSTR lpSourceName
)
849 RPC_UNICODE_STRING SourceName
;
850 IELF_HANDLE LogHandle
;
853 TRACE("%s, %s\n", debugstr_w(lpUNCServerName
), debugstr_w(lpSourceName
));
855 SourceName
.Buffer
= (LPWSTR
)lpSourceName
;
856 SourceName
.Length
= SourceName
.MaximumLength
=
857 lpSourceName
? wcslen(lpSourceName
) * sizeof(WCHAR
) : 0;
858 SourceName
.MaximumLength
+= sizeof(WCHAR
);
862 Status
= ElfrRegisterEventSourceW((LPWSTR
)lpUNCServerName
,
869 RpcExcept(EXCEPTION_EXECUTE_HANDLER
)
871 Status
= I_RpcMapWin32Status(RpcExceptionCode());
875 if (!NT_SUCCESS(Status
))
877 SetLastError(RtlNtStatusToDosError(Status
));
881 return (HANDLE
)LogHandle
;
885 /******************************************************************************
886 * ReportEventA [ADVAPI32.@]
889 ReportEventA(IN HANDLE hEventLog
,
896 IN LPCSTR
*lpStrings
,
899 LPCWSTR
*wideStrArray
;
904 if (wNumStrings
== 0)
907 if (lpStrings
== NULL
)
910 wideStrArray
= HeapAlloc(GetProcessHeap(),
912 sizeof(LPCWSTR
) * wNumStrings
);
914 for (i
= 0; i
< wNumStrings
; i
++)
916 if (!RtlCreateUnicodeStringFromAsciiz(&str
, (PSTR
)lpStrings
[i
]))
918 wideStrArray
[i
] = str
.Buffer
;
921 if (i
== wNumStrings
)
923 ret
= ReportEventW(hEventLog
,
935 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
939 for (i
= 0; i
< wNumStrings
; i
++)
943 HeapFree(GetProcessHeap(),
945 (PVOID
)wideStrArray
[i
]);
949 HeapFree(GetProcessHeap(),
951 (PVOID
)wideStrArray
);
957 /******************************************************************************
958 * ReportEventW [ADVAPI32.@]
972 ReportEventW(IN HANDLE hEventLog
,
979 IN LPCWSTR
*lpStrings
,
983 UNICODE_STRING
*Strings
;
984 UNICODE_STRING ComputerName
;
987 TRACE("%p, %u, %u, %lu, %p, %u, %lu, %p, %p\n",
988 hEventLog
, wType
, wCategory
, dwEventID
, lpUserSid
,
989 wNumStrings
, dwDataSize
, lpStrings
, lpRawData
);
991 Strings
= HeapAlloc(GetProcessHeap(),
993 wNumStrings
* sizeof(UNICODE_STRING
));
996 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1000 for (i
= 0; i
< wNumStrings
; i
++)
1001 RtlInitUnicodeString(&Strings
[i
], lpStrings
[i
]);
1003 /*FIXME: ComputerName */
1004 RtlInitUnicodeString(&ComputerName
, L
"");
1008 Status
= ElfrReportEventW(hEventLog
,
1009 0, /* FIXME: Time */
1015 (PRPC_UNICODE_STRING
) &ComputerName
,
1017 (PRPC_UNICODE_STRING
*) &Strings
,
1023 RpcExcept(EXCEPTION_EXECUTE_HANDLER
)
1025 Status
= I_RpcMapWin32Status(RpcExceptionCode());
1029 HeapFree(GetProcessHeap(), 0, Strings
);
1031 if (!NT_SUCCESS(Status
))
1033 SetLastError(RtlNtStatusToDosError(Status
));