- Implement RCreateServiceA.
[reactos.git] / reactos / dll / win32 / advapi32 / service / eventlog.c
index c0f28f8..2ad4544 100644 (file)
  *
  * You should have received a copy of the GNU Lesser General Public
  * License along with this library; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
  */
 
 #include <advapi32.h>
 #include "wine/debug.h"
 
 WINE_DEFAULT_DEBUG_CHANNEL(advapi);
-WINE_DECLARE_DEBUG_CHANNEL(eventlog);
 
-typedef struct _LOG_INFO
+static RPC_UNICODE_STRING EmptyString = { 0, 0, L"" };
+
+static inline LPWSTR SERV_dup( LPCSTR str )
 {
-    RPC_BINDING_HANDLE BindingHandle;
-    IELF_HANDLE LogHandle;
-    BOOL bLocal;
-} LOG_INFO, *PLOG_INFO;
+    UINT len;
+    LPWSTR wstr;
+
+    if( !str )
+        return NULL;
+    len = MultiByteToWideChar( CP_ACP, 0, str, -1, NULL, 0 );
+    wstr = HeapAlloc( GetProcessHeap(), 0, len*sizeof (WCHAR) );
+    MultiByteToWideChar( CP_ACP, 0, str, -1, wstr, len );
+    return wstr;
+}
+
+handle_t __RPC_USER
+EVENTLOG_HANDLE_A_bind(EVENTLOG_HANDLE_A UNCServerName)
+{
+    handle_t hBinding = NULL;
+    UCHAR *pszStringBinding;
+    RPC_STATUS status;
+
+    TRACE("EVENTLOG_HANDLE_A_bind() called\n");
+
+    status = RpcStringBindingComposeA(NULL,
+                                      (UCHAR *)"ncacn_np",
+                                      (UCHAR *)UNCServerName,
+                                      (UCHAR *)"\\pipe\\ntsvcs",
+                                      NULL,
+                                      (UCHAR **)&pszStringBinding);
+    if (status)
+    {
+        ERR("RpcStringBindingCompose returned 0x%x\n", status);
+        return NULL;
+    }
+
+    /* Set the binding handle that will be used to bind to the server. */
+    status = RpcBindingFromStringBindingA(pszStringBinding,
+                                          &hBinding);
+    if (status)
+    {
+        ERR("RpcBindingFromStringBinding returned 0x%x\n", status);
+    }
+
+    status = RpcStringFreeA(&pszStringBinding);
+    if (status)
+    {
+        ERR("RpcStringFree returned 0x%x\n", status);
+    }
+
+    return hBinding;
+}
+
+
+void __RPC_USER
+EVENTLOG_HANDLE_A_unbind(EVENTLOG_HANDLE_A UNCServerName,
+                         handle_t hBinding)
+{
+    RPC_STATUS status;
+
+    TRACE("EVENTLOG_HANDLE_A_unbind() called\n");
+
+    status = RpcBindingFree(&hBinding);
+    if (status)
+    {
+        ERR("RpcBindingFree returned 0x%x\n", status);
+    }
+}
+
+
+handle_t __RPC_USER
+EVENTLOG_HANDLE_W_bind(EVENTLOG_HANDLE_W UNCServerName)
+{
+    handle_t hBinding = NULL;
+    LPWSTR pszStringBinding;
+    RPC_STATUS status;
+
+    TRACE("EVENTLOG_HANDLE_W_bind() called\n");
+
+    status = RpcStringBindingComposeW(NULL,
+                                      L"ncacn_np",
+                                      (LPWSTR)UNCServerName,
+                                      L"\\pipe\\EventLog",
+                                      NULL,
+                                      &pszStringBinding);
+    if (status)
+    {
+        ERR("RpcStringBindingCompose returned 0x%x\n", status);
+        return NULL;
+    }
+
+    /* Set the binding handle that will be used to bind to the server. */
+    status = RpcBindingFromStringBindingW(pszStringBinding,
+                                          &hBinding);
+    if (status)
+    {
+        ERR("RpcBindingFromStringBinding returned 0x%x\n", status);
+    }
+
+    status = RpcStringFreeW(&pszStringBinding);
+    if (status)
+    {
+        ERR("RpcStringFree returned 0x%x\n", status);
+    }
+
+    return hBinding;
+}
+
+
+void __RPC_USER
+EVENTLOG_HANDLE_W_unbind(EVENTLOG_HANDLE_W UNCServerName,
+                         handle_t hBinding)
+{
+    RPC_STATUS status;
+
+    TRACE("EVENTLOG_HANDLE_W_unbind() called\n");
+
+    status = RpcBindingFree(&hBinding);
+    if (status)
+    {
+        ERR("RpcBindingFree returned 0x%x\n", status);
+    }
+}
 
-static RPC_UNICODE_STRING EmptyString = { 0, 0, L"" };
 
 /******************************************************************************
  * BackupEventLogA [ADVAPI32.@]
  */
 BOOL WINAPI
-BackupEventLogA(
-    IN HANDLE hEventLog,
-    IN LPCSTR lpBackupFileName)
+BackupEventLogA(IN HANDLE hEventLog,
+                IN LPCSTR lpBackupFileName)
 {
-    PLOG_INFO pLog;
-    NTSTATUS Status;
     RPC_STRING BackupFileName;
+    NTSTATUS Status;
 
     TRACE("%p, %s\n", hEventLog, lpBackupFileName);
 
     BackupFileName.Buffer = (LPSTR)lpBackupFileName;
     BackupFileName.Length = BackupFileName.MaximumLength =
         lpBackupFileName ? strlen(lpBackupFileName) : 0;
+       BackupFileName.MaximumLength += sizeof(CHAR);
 
-    pLog = (PLOG_INFO)hEventLog;
-    if (!pLog)
+    RpcTryExcept
     {
-        SetLastError(ERROR_INVALID_HANDLE);
-        return FALSE;
+        Status = ElfrBackupELFA(hEventLog,
+                                &BackupFileName);
+    }
+    RpcExcept(EXCEPTION_EXECUTE_HANDLER)
+    {
+        Status = I_RpcMapWin32Status(RpcExceptionCode());
     }
-    Status = ElfrBackupELFA(
-        pLog->BindingHandle,
-        pLog->LogHandle,
-        &BackupFileName);
+    RpcEndExcept;
 
     if (!NT_SUCCESS(Status))
     {
         SetLastError(RtlNtStatusToDosError(Status));
         return FALSE;
     }
+
     return TRUE;
 }
 
@@ -81,36 +196,36 @@ BackupEventLogA(
  *   lpBackupFileName []
  */
 BOOL WINAPI
-BackupEventLogW(
-    IN HANDLE hEventLog,
-    IN LPCWSTR lpBackupFileName)
+BackupEventLogW(IN HANDLE hEventLog,
+                IN LPCWSTR lpBackupFileName)
 {
-    PLOG_INFO pLog;
-    NTSTATUS Status;
     RPC_UNICODE_STRING BackupFileName;
+    NTSTATUS Status;
 
     TRACE("%p, %s\n", hEventLog, debugstr_w(lpBackupFileName));
 
     BackupFileName.Buffer = (LPWSTR)lpBackupFileName;
     BackupFileName.Length = BackupFileName.MaximumLength =
         lpBackupFileName ? wcslen(lpBackupFileName) * sizeof(WCHAR) : 0;
+    BackupFileName.MaximumLength += sizeof(WCHAR);
 
-    pLog = (PLOG_INFO)hEventLog;
-    if (!pLog)
+    RpcTryExcept
     {
-        SetLastError(ERROR_INVALID_HANDLE);
-        return FALSE;
+        Status = ElfrBackupELFW(hEventLog,
+                                &BackupFileName);
     }
-    Status = ElfrBackupELFW(
-        pLog->BindingHandle,
-        pLog->LogHandle,
-        &BackupFileName);
+    RpcExcept(EXCEPTION_EXECUTE_HANDLER)
+    {
+        Status = I_RpcMapWin32Status(RpcExceptionCode());
+    }
+    RpcEndExcept;
 
     if (!NT_SUCCESS(Status))
     {
         SetLastError(RtlNtStatusToDosError(Status));
         return FALSE;
     }
+
     return TRUE;
 }
 
@@ -119,36 +234,36 @@ BackupEventLogW(
  * ClearEventLogA [ADVAPI32.@]
  */
 BOOL WINAPI
-ClearEventLogA(
-    IN HANDLE hEventLog,
-    IN LPCSTR lpBackupFileName)
+ClearEventLogA(IN HANDLE hEventLog,
+               IN LPCSTR lpBackupFileName)
 {
-    PLOG_INFO pLog;
-    NTSTATUS Status;
     RPC_STRING BackupFileName;
+    NTSTATUS Status;
 
     TRACE("%p, %s\n", hEventLog, lpBackupFileName);
 
     BackupFileName.Buffer = (LPSTR)lpBackupFileName;
     BackupFileName.Length = BackupFileName.MaximumLength =
         lpBackupFileName ? strlen(lpBackupFileName) : 0;
+    BackupFileName.MaximumLength += sizeof(CHAR);
 
-    pLog = (PLOG_INFO)hEventLog;
-    if (!pLog)
+    RpcTryExcept
     {
-        SetLastError(ERROR_INVALID_HANDLE);
-        return FALSE;
+        Status = ElfrClearELFA(hEventLog,
+                               &BackupFileName);
+    }
+    RpcExcept(EXCEPTION_EXECUTE_HANDLER)
+    {
+        Status = I_RpcMapWin32Status(RpcExceptionCode());
     }
-    Status = ElfrClearELFA(
-        pLog->BindingHandle,
-        pLog->LogHandle,
-        &BackupFileName);
+    RpcEndExcept;
 
     if (!NT_SUCCESS(Status))
     {
         SetLastError(RtlNtStatusToDosError(Status));
         return FALSE;
     }
+
     return TRUE;
 }
 
@@ -157,36 +272,36 @@ ClearEventLogA(
  * ClearEventLogW [ADVAPI32.@]
  */
 BOOL WINAPI
-ClearEventLogW(
-    IN HANDLE hEventLog,
-    IN LPCWSTR lpBackupFileName)
+ClearEventLogW(IN HANDLE hEventLog,
+               IN LPCWSTR lpBackupFileName)
 {
-    PLOG_INFO pLog;
-    NTSTATUS Status;
     RPC_UNICODE_STRING BackupFileName;
+    NTSTATUS Status;
 
     TRACE("%p, %s\n", hEventLog, debugstr_w(lpBackupFileName));
 
     BackupFileName.Buffer = (LPWSTR)lpBackupFileName;
     BackupFileName.Length = BackupFileName.MaximumLength =
         lpBackupFileName ? wcslen(lpBackupFileName) * sizeof(WCHAR) : 0;
+    BackupFileName.MaximumLength += sizeof(WCHAR);
 
-    pLog = (PLOG_INFO)hEventLog;
-    if (!pLog)
+    RpcTryExcept
     {
-        SetLastError(ERROR_INVALID_HANDLE);
-        return FALSE;
+        Status = ElfrClearELFW(hEventLog,
+                               &BackupFileName);
     }
-    Status = ElfrClearELFW(
-        pLog->BindingHandle,
-        pLog->LogHandle,
-        &BackupFileName);
+    RpcExcept(EXCEPTION_EXECUTE_HANDLER)
+    {
+        Status = I_RpcMapWin32Status(RpcExceptionCode());
+    }
+    RpcEndExcept;
 
     if (!NT_SUCCESS(Status))
     {
         SetLastError(RtlNtStatusToDosError(Status));
         return FALSE;
     }
+
     return TRUE;
 }
 
@@ -195,39 +310,27 @@ ClearEventLogW(
  * CloseEventLog [ADVAPI32.@]
  */
 BOOL WINAPI
-CloseEventLog(
-    IN HANDLE hEventLog)
+CloseEventLog(IN HANDLE hEventLog)
 {
-    PLOG_INFO pLog;
     NTSTATUS Status;
 
     TRACE("%p\n", hEventLog);
 
-    pLog = (PLOG_INFO)hEventLog;
-    if (!pLog)
-        return TRUE;
-
-    if (pLog->bLocal == FALSE)
+    RpcTryExcept
     {
-        if (!EvtUnbindRpc(pLog->BindingHandle))
-        {
-            SetLastError(ERROR_ACCESS_DENIED);
-            return FALSE;
-        }
+        Status = ElfrCloseEL(&hEventLog);
     }
-    else
+    RpcExcept(EXCEPTION_EXECUTE_HANDLER)
     {
-        Status = ElfrCloseEL(
-            pLog->BindingHandle,
-            &pLog->LogHandle);
-        if (!NT_SUCCESS(Status))
-        {
-            SetLastError(RtlNtStatusToDosError(Status));
-            return FALSE;
-        }
+        Status = I_RpcMapWin32Status(RpcExceptionCode());
     }
+    RpcEndExcept;
 
-    HeapFree(GetProcessHeap(), 0, pLog);
+    if (!NT_SUCCESS(Status))
+    {
+        SetLastError(RtlNtStatusToDosError(Status));
+        return FALSE;
+    }
 
     return TRUE;
 }
@@ -243,30 +346,55 @@ CloseEventLog(
  * RETURNS STD
  */
 BOOL WINAPI
-DeregisterEventSource(
-    IN HANDLE hEventLog)
+DeregisterEventSource(IN HANDLE hEventLog)
 {
-    PLOG_INFO pLog;
     NTSTATUS Status;
 
     TRACE("%p\n", hEventLog);
 
-    pLog = (PLOG_INFO)hEventLog;
-    if (!pLog)
-        return TRUE;
+    RpcTryExcept
+    {
+        Status = ElfrDeregisterEventSource(&hEventLog);
+    }
+    RpcExcept(EXCEPTION_EXECUTE_HANDLER)
+    {
+        Status = I_RpcMapWin32Status(RpcExceptionCode());
+    }
+    RpcEndExcept;
 
-    Status = ElfrDeregisterEventSource(
-        pLog->BindingHandle,
-        &pLog->LogHandle);
     if (!NT_SUCCESS(Status))
     {
         SetLastError(RtlNtStatusToDosError(Status));
         return FALSE;
     }
+
     return TRUE;
 }
 
 
+/******************************************************************************
+ * GetEventLogInformation [ADVAPI32.@]
+ *
+ * PARAMS
+ *   hEventLog      [I] Handle to event log
+ *   dwInfoLevel    [I] Level of event log information to return
+ *   lpBuffer       [O] Buffer that receives the event log information
+ *   cbBufSize      [I] Size of the lpBuffer buffer
+ *   pcbBytesNeeded [O] Required buffer size
+ */
+BOOL WINAPI
+GetEventLogInformation(IN HANDLE hEventLog,
+                       IN DWORD dwInfoLevel,
+                       OUT LPVOID lpBuffer,
+                       IN DWORD cbBufSize,
+                       OUT LPDWORD pcbBytesNeeded)
+{
+    UNIMPLEMENTED;
+    SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
+    return FALSE;
+}
+
+
 /******************************************************************************
  * GetNumberOfEventLogRecords [ADVAPI32.@]
  *
@@ -275,26 +403,31 @@ DeregisterEventSource(
  *   NumberOfRecords []
  */
 BOOL WINAPI
-GetNumberOfEventLogRecords(
-    IN HANDLE hEventLog,
-    OUT PDWORD NumberOfRecords)
+GetNumberOfEventLogRecords(IN HANDLE hEventLog,
+                           OUT PDWORD NumberOfRecords)
 {
-    PLOG_INFO pLog;
     NTSTATUS Status;
     DWORD Records;
 
     TRACE("%p, %p\n", hEventLog, NumberOfRecords);
 
-    pLog = (PLOG_INFO)hEventLog;
-    if (!pLog)
+    if(!NumberOfRecords)
     {
-        SetLastError(ERROR_INVALID_HANDLE);
+        SetLastError(ERROR_INVALID_PARAMETER);
         return FALSE;
     }
-    Status = ElfrNumberOfRecords(
-        pLog->BindingHandle,
-        pLog->LogHandle,
-        &Records);
+    
+    RpcTryExcept
+    {
+        Status = ElfrNumberOfRecords(hEventLog,
+                                     &Records);
+    }
+    RpcExcept(EXCEPTION_EXECUTE_HANDLER)
+    {
+        Status = I_RpcMapWin32Status(RpcExceptionCode());
+    }
+    RpcEndExcept;
+
     if (!NT_SUCCESS(Status))
     {
         SetLastError(RtlNtStatusToDosError(Status));
@@ -302,6 +435,7 @@ GetNumberOfEventLogRecords(
     }
 
     *NumberOfRecords = Records;
+
     return TRUE;
 }
 
@@ -314,26 +448,31 @@ GetNumberOfEventLogRecords(
  *   OldestRecord []
  */
 BOOL WINAPI
-GetOldestEventLogRecord(
-    IN HANDLE hEventLog,
-    OUT PDWORD OldestRecord)
+GetOldestEventLogRecord(IN HANDLE hEventLog,
+                        OUT PDWORD OldestRecord)
 {
-    PLOG_INFO pLog;
     NTSTATUS Status;
     DWORD Oldest;
 
     TRACE("%p, %p\n", hEventLog, OldestRecord);
 
-    pLog = (PLOG_INFO)hEventLog;
-    if (!pLog)
+    if(!OldestRecord)
     {
-        SetLastError(ERROR_INVALID_HANDLE);
+        SetLastError(ERROR_INVALID_PARAMETER);
         return FALSE;
     }
-    Status = ElfrOldestRecord(
-        pLog->BindingHandle,
-        pLog->LogHandle,
-        &Oldest);
+    
+    RpcTryExcept
+    {
+        Status = ElfrOldestRecord(hEventLog,
+                                  &Oldest);
+    }
+    RpcExcept(EXCEPTION_EXECUTE_HANDLER)
+    {
+        Status = I_RpcMapWin32Status(RpcExceptionCode());
+    }
+    RpcEndExcept;
+
     if (!NT_SUCCESS(Status))
     {
         SetLastError(RtlNtStatusToDosError(Status));
@@ -341,6 +480,7 @@ GetOldestEventLogRecord(
     }
 
     *OldestRecord = Oldest;
+
     return TRUE;
 }
 
@@ -353,9 +493,8 @@ GetOldestEventLogRecord(
  *   hEvent    []
  */
 BOOL WINAPI
-NotifyChangeEventLog(
-    IN HANDLE hEventLog,
-    IN HANDLE hEvent)
+NotifyChangeEventLog(IN HANDLE hEventLog,
+                     IN HANDLE hEvent)
 {
     /* Use ElfrChangeNotify */
     UNIMPLEMENTED;
@@ -368,9 +507,8 @@ NotifyChangeEventLog(
  * OpenBackupEventLogA [ADVAPI32.@]
  */
 HANDLE WINAPI
-OpenBackupEventLogA(
-    IN LPCSTR lpUNCServerName,
-    IN LPCSTR lpFileName)
+OpenBackupEventLogA(IN LPCSTR lpUNCServerName,
+                    IN LPCSTR lpFileName)
 {
     UNICODE_STRING UNCServerName;
     UNICODE_STRING FileName;
@@ -383,6 +521,7 @@ OpenBackupEventLogA(
         SetLastError(ERROR_NOT_ENOUGH_MEMORY);
         return NULL;
     }
+
     if (!RtlCreateUnicodeStringFromAsciiz(&FileName, lpFileName))
     {
         RtlFreeUnicodeString(&UNCServerName);
@@ -390,9 +529,8 @@ OpenBackupEventLogA(
         return NULL;
     }
 
-    Handle = OpenBackupEventLogW(
-        UNCServerName.Buffer,
-        FileName.Buffer);
+    Handle = OpenBackupEventLogW(UNCServerName.Buffer,
+                                 FileName.Buffer);
 
     RtlFreeUnicodeString(&UNCServerName);
     RtlFreeUnicodeString(&FileName);
@@ -409,99 +547,72 @@ OpenBackupEventLogA(
  *   lpFileName      []
  */
 HANDLE WINAPI
-OpenBackupEventLogW(
-    IN LPCWSTR lpUNCServerName,
-    IN LPCWSTR lpFileName)
+OpenBackupEventLogW(IN LPCWSTR lpUNCServerName,
+                    IN LPCWSTR lpFileName)
 {
-    PLOG_INFO pLog;
-    NTSTATUS Status;
     RPC_UNICODE_STRING FileName;
+    IELF_HANDLE LogHandle;
+    NTSTATUS Status;
 
     TRACE("%s, %s\n", debugstr_w(lpUNCServerName), debugstr_w(lpFileName));
 
     FileName.Buffer = (LPWSTR)lpFileName;
     FileName.Length = FileName.MaximumLength =
         lpFileName ? wcslen(lpFileName) * sizeof(WCHAR) : 0;
+    FileName.MaximumLength += sizeof(WCHAR);
 
-    pLog = HeapAlloc(GetProcessHeap(), 0, sizeof(LOG_INFO));
-    if (!pLog)
+    RpcTryExcept
     {
-        SetLastError(ERROR_NOT_ENOUGH_MEMORY);
-        return NULL;
+        Status = ElfrOpenBELW((LPWSTR)lpUNCServerName,
+                              &FileName,
+                              1,
+                              1,
+                              &LogHandle);
     }
-    ZeroMemory(pLog, sizeof(LOG_INFO));
-
-    if (lpUNCServerName == NULL || *lpUNCServerName == 0)
+    RpcExcept(EXCEPTION_EXECUTE_HANDLER)
     {
-        pLog->bLocal = TRUE;
-
-        if (!EvtGetLocalHandle(&pLog->BindingHandle))
-        {
-            HeapFree(GetProcessHeap(), 0, pLog);
-            SetLastError(ERROR_GEN_FAILURE);
-            return NULL;
-        }
-    }
-    else
-    {
-        pLog->bLocal = FALSE;
-
-        if (!EvtBindRpc(lpUNCServerName, &pLog->BindingHandle))
-        {
-            HeapFree(GetProcessHeap(), 0, pLog);
-            SetLastError(ERROR_INVALID_COMPUTERNAME);
-            return NULL;
-        }
+        Status = I_RpcMapWin32Status(RpcExceptionCode());
     }
-
-    Status = ElfrOpenBELW(
-        pLog->BindingHandle,
-        (LPWSTR)lpUNCServerName,
-        &FileName,
-        0,
-        0,
-        &pLog->LogHandle);
+    RpcEndExcept;
 
     if (!NT_SUCCESS(Status))
     {
         SetLastError(RtlNtStatusToDosError(Status));
-        HeapFree(GetProcessHeap(), 0, pLog);
         return NULL;
     }
-    return pLog;
+
+    return (HANDLE)LogHandle;
 }
 
 
 /******************************************************************************
  * OpenEventLogA [ADVAPI32.@]
+ *
+ * Opens a handle to the specified event log.
+ *
+ * PARAMS
+ *  lpUNCServerName [I] UNC name of the server on which the event log is
+ *                      opened.
+ *  lpSourceName    [I] Name of the log.
+ *
+ * RETURNS
+ *  Success: Handle to an event log.
+ *  Failure: NULL
  */
 HANDLE WINAPI
-OpenEventLogA(
-    IN LPCSTR lpUNCServerName,
-    IN LPCSTR lpSourceName)
+OpenEventLogA(IN LPCSTR uncname,
+              IN LPCSTR source)
 {
-    UNICODE_STRING UNCServerName;
-    UNICODE_STRING SourceName;
-    HANDLE Handle;
+    LPWSTR uncnameW, sourceW;
+    HANDLE handle;
 
-    if (!RtlCreateUnicodeStringFromAsciiz(&UNCServerName, lpUNCServerName))
-    {
-        SetLastError(ERROR_NOT_ENOUGH_MEMORY);
-        return NULL;
-    }
-    if (!RtlCreateUnicodeStringFromAsciiz(&SourceName, lpSourceName))
-    {
-        RtlFreeUnicodeString(&UNCServerName);
-        SetLastError(ERROR_NOT_ENOUGH_MEMORY);
-        return NULL;
-    }
-
-    Handle = OpenEventLogW(UNCServerName.Buffer, SourceName.Buffer);
+    uncnameW = SERV_dup(uncname);
+    sourceW = SERV_dup(source);
+    handle = OpenEventLogW(uncnameW, sourceW);
+    HeapFree(GetProcessHeap(), 0, uncnameW);
+    HeapFree(GetProcessHeap(), 0, sourceW);
 
-    RtlFreeUnicodeString(&UNCServerName);
-    RtlFreeUnicodeString(&SourceName);
-
-    return Handle;
+    return handle;
 }
 
 
@@ -513,67 +624,42 @@ OpenEventLogA(
  *   lpSourceName    []
  */
 HANDLE WINAPI
-OpenEventLogW(
-    IN LPCWSTR lpUNCServerName,
-    IN LPCWSTR lpSourceName)
+OpenEventLogW(IN LPCWSTR lpUNCServerName,
+              IN LPCWSTR lpSourceName)
 {
-    PLOG_INFO pLog;
-    NTSTATUS Status;
     RPC_UNICODE_STRING SourceName;
+    IELF_HANDLE LogHandle;
+    NTSTATUS Status;
 
     TRACE("%s, %s\n", debugstr_w(lpUNCServerName), debugstr_w(lpSourceName));
 
     SourceName.Buffer = (LPWSTR)lpSourceName;
     SourceName.Length = SourceName.MaximumLength =
         lpSourceName ? wcslen(lpSourceName) * sizeof(WCHAR) : 0;
+    SourceName.MaximumLength += sizeof(WCHAR);
 
-    pLog = HeapAlloc(GetProcessHeap(), 0, sizeof(LOG_INFO));
-    if (!pLog)
-    {
-        SetLastError(ERROR_NOT_ENOUGH_MEMORY);
-        return NULL;
-    }
-    ZeroMemory(pLog, sizeof(LOG_INFO));
-
-    if (lpUNCServerName == NULL || *lpUNCServerName == 0)
+    RpcTryExcept
     {
-        pLog->bLocal = TRUE;
-
-        if (!EvtGetLocalHandle(&pLog->BindingHandle))
-        {
-            HeapFree(GetProcessHeap(), 0, pLog);
-            SetLastError(ERROR_GEN_FAILURE);
-            return NULL;
-        }
+        Status = ElfrOpenELW((LPWSTR)lpUNCServerName,
+                             &SourceName,
+                             &EmptyString,
+                             1,
+                             1,
+                             &LogHandle);
     }
-    else
+    RpcExcept(EXCEPTION_EXECUTE_HANDLER)
     {
-        pLog->bLocal = FALSE;
-
-        if (!EvtBindRpc(lpUNCServerName, &pLog->BindingHandle))
-        {
-            HeapFree(GetProcessHeap(), 0, pLog);
-            SetLastError(ERROR_INVALID_COMPUTERNAME);
-            return NULL;
-        }
+        Status = I_RpcMapWin32Status(RpcExceptionCode());
     }
-
-    Status = ElfrOpenELW(
-        pLog->BindingHandle,
-        (LPWSTR)lpUNCServerName,
-        &SourceName,
-        &EmptyString,
-        0,
-        0,
-        &pLog->LogHandle);
+    RpcEndExcept;
 
     if (!NT_SUCCESS(Status))
     {
         SetLastError(RtlNtStatusToDosError(Status));
-        HeapFree(GetProcessHeap(), 0, pLog);
         return NULL;
     }
-    return pLog;
+
+    return (HANDLE)LogHandle;
 }
 
 
@@ -581,16 +667,14 @@ OpenEventLogW(
  * ReadEventLogA [ADVAPI32.@]
  */
 BOOL WINAPI
-ReadEventLogA(
-    IN HANDLE hEventLog,
-    IN DWORD dwReadFlags,
-    IN DWORD dwRecordOffset,
-    OUT LPVOID lpBuffer,
-    IN DWORD nNumberOfBytesToRead,
-    OUT DWORD *pnBytesRead,
-    OUT DWORD *pnMinNumberOfBytesNeeded)
+ReadEventLogA(IN HANDLE hEventLog,
+              IN DWORD dwReadFlags,
+              IN DWORD dwRecordOffset,
+              OUT LPVOID lpBuffer,
+              IN DWORD nNumberOfBytesToRead,
+              OUT DWORD *pnBytesRead,
+              OUT DWORD *pnMinNumberOfBytesNeeded)
 {
-    PLOG_INFO pLog;
     NTSTATUS Status;
     DWORD bytesRead, minNumberOfBytesNeeded;
 
@@ -598,29 +682,44 @@ ReadEventLogA(
         hEventLog, dwReadFlags, dwRecordOffset, lpBuffer,
         nNumberOfBytesToRead, pnBytesRead, pnMinNumberOfBytesNeeded);
 
-    pLog = (PLOG_INFO)hEventLog;
-    if (!pLog)
+    if(!pnBytesRead || !pnMinNumberOfBytesNeeded)
     {
-        SetLastError(ERROR_INVALID_HANDLE);
+        SetLastError(ERROR_INVALID_PARAMETER);
         return FALSE;
     }
-    Status = ElfrReadELA(
-        pLog->BindingHandle,
-        pLog->LogHandle,
-        dwReadFlags,
-        dwRecordOffset,
-        nNumberOfBytesToRead,
-        lpBuffer,
-        &bytesRead,
-        &minNumberOfBytesNeeded);
+
+    /* If buffer is NULL set nNumberOfBytesToRead to 0 to prevent rpcrt4 from
+       trying to access a null pointer */
+    if (!lpBuffer)
+    {
+        nNumberOfBytesToRead = 0;
+    }
+
+    RpcTryExcept
+    {
+        Status = ElfrReadELA(hEventLog,
+                             dwReadFlags,
+                             dwRecordOffset,
+                             nNumberOfBytesToRead,
+                             lpBuffer,
+                             &bytesRead,
+                             &minNumberOfBytesNeeded);
+    }
+    RpcExcept(EXCEPTION_EXECUTE_HANDLER)
+    {
+        Status = I_RpcMapWin32Status(RpcExceptionCode());
+    }
+    RpcEndExcept;
+
+    *pnBytesRead = (DWORD)bytesRead;
+    *pnMinNumberOfBytesNeeded = (DWORD)minNumberOfBytesNeeded;
+
     if (!NT_SUCCESS(Status))
     {
         SetLastError(RtlNtStatusToDosError(Status));
         return FALSE;
     }
 
-    *pnBytesRead = (DWORD)bytesRead;
-    *pnMinNumberOfBytesNeeded = (DWORD)minNumberOfBytesNeeded;
     return TRUE;
 }
 
@@ -638,16 +737,14 @@ ReadEventLogA(
  *   pnMinNumberOfBytesNeeded []
  */
 BOOL WINAPI
-ReadEventLogW(
-    IN HANDLE hEventLog,
-    IN DWORD dwReadFlags,
-    IN DWORD dwRecordOffset,
-    OUT LPVOID lpBuffer,
-    IN DWORD nNumberOfBytesToRead,
-    OUT DWORD *pnBytesRead,
-    OUT DWORD *pnMinNumberOfBytesNeeded)
+ReadEventLogW(IN HANDLE hEventLog,
+              IN DWORD dwReadFlags,
+              IN DWORD dwRecordOffset,
+              OUT LPVOID lpBuffer,
+              IN DWORD nNumberOfBytesToRead,
+              OUT DWORD *pnBytesRead,
+              OUT DWORD *pnMinNumberOfBytesNeeded)
 {
-    PLOG_INFO pLog;
     NTSTATUS Status;
     DWORD bytesRead, minNumberOfBytesNeeded;
 
@@ -655,29 +752,44 @@ ReadEventLogW(
         hEventLog, dwReadFlags, dwRecordOffset, lpBuffer,
         nNumberOfBytesToRead, pnBytesRead, pnMinNumberOfBytesNeeded);
 
-    pLog = (PLOG_INFO)hEventLog;
-    if (!pLog)
+    if(!pnBytesRead || !pnMinNumberOfBytesNeeded)
     {
-        SetLastError(ERROR_INVALID_HANDLE);
+        SetLastError(ERROR_INVALID_PARAMETER);
         return FALSE;
     }
-    Status = ElfrReadELW(
-        pLog->BindingHandle,
-        pLog->LogHandle,
-        dwReadFlags,
-        dwRecordOffset,
-        nNumberOfBytesToRead,
-        lpBuffer,
-        &bytesRead,
-        &minNumberOfBytesNeeded);
+
+    /* If buffer is NULL set nNumberOfBytesToRead to 0 to prevent rpcrt4 from
+       trying to access a null pointer */
+    if (!lpBuffer)
+    {
+        nNumberOfBytesToRead = 0;
+    }
+
+    RpcTryExcept
+    {
+        Status = ElfrReadELW(hEventLog,
+                             dwReadFlags,
+                             dwRecordOffset,
+                             nNumberOfBytesToRead,
+                             lpBuffer,
+                             &bytesRead,
+                             &minNumberOfBytesNeeded);
+    }
+    RpcExcept(EXCEPTION_EXECUTE_HANDLER)
+    {
+        Status = I_RpcMapWin32Status(RpcExceptionCode());
+    }
+    RpcEndExcept;
+
+    *pnBytesRead = (DWORD)bytesRead;
+    *pnMinNumberOfBytesNeeded = (DWORD)minNumberOfBytesNeeded;
+
     if (!NT_SUCCESS(Status))
     {
         SetLastError(RtlNtStatusToDosError(Status));
         return FALSE;
     }
 
-    *pnBytesRead = (DWORD)bytesRead;
-    *pnMinNumberOfBytesNeeded = (DWORD)minNumberOfBytesNeeded;
     return TRUE;
 }
 
@@ -686,9 +798,8 @@ ReadEventLogW(
  * RegisterEventSourceA [ADVAPI32.@]
  */
 HANDLE WINAPI
-RegisterEventSourceA(
-    IN LPCSTR lpUNCServerName,
-    IN LPCSTR lpSourceName)
+RegisterEventSourceA(IN LPCSTR lpUNCServerName,
+                     IN LPCSTR lpSourceName)
 {
     UNICODE_STRING UNCServerName;
     UNICODE_STRING SourceName;
@@ -701,6 +812,7 @@ RegisterEventSourceA(
         SetLastError(ERROR_NOT_ENOUGH_MEMORY);
         return NULL;
     }
+
     if (!RtlCreateUnicodeStringFromAsciiz(&SourceName, lpSourceName))
     {
         RtlFreeUnicodeString(&UNCServerName);
@@ -708,7 +820,8 @@ RegisterEventSourceA(
         return NULL;
     }
 
-    Handle = RegisterEventSourceW(UNCServerName.Buffer, SourceName.Buffer);
+    Handle = RegisterEventSourceW(UNCServerName.Buffer,
+                                  SourceName.Buffer);
 
     RtlFreeUnicodeString(&UNCServerName);
     RtlFreeUnicodeString(&SourceName);
@@ -730,67 +843,42 @@ RegisterEventSourceA(
  *    Failure: NULL
  */
 HANDLE WINAPI
-RegisterEventSourceW(
-    IN LPCWSTR lpUNCServerName,
-    IN LPCWSTR lpSourceName)
+RegisterEventSourceW(IN LPCWSTR lpUNCServerName,
+                     IN LPCWSTR lpSourceName)
 {
-    PLOG_INFO pLog;
-    NTSTATUS Status;
     RPC_UNICODE_STRING SourceName;
+    IELF_HANDLE LogHandle;
+    NTSTATUS Status;
 
     TRACE("%s, %s\n", debugstr_w(lpUNCServerName), debugstr_w(lpSourceName));
 
     SourceName.Buffer = (LPWSTR)lpSourceName;
     SourceName.Length = SourceName.MaximumLength =
         lpSourceName ? wcslen(lpSourceName) * sizeof(WCHAR) : 0;
+    SourceName.MaximumLength += sizeof(WCHAR);
 
-    pLog = HeapAlloc(GetProcessHeap(), 0, sizeof(LOG_INFO));
-    if (!pLog)
+    RpcTryExcept
     {
-        SetLastError(ERROR_NOT_ENOUGH_MEMORY);
-        return NULL;
+        Status = ElfrRegisterEventSourceW((LPWSTR)lpUNCServerName,
+                                          &SourceName,
+                                          &EmptyString,
+                                          1,
+                                          1,
+                                          &LogHandle);
     }
-    ZeroMemory(pLog, sizeof(LOG_INFO));
-
-    if (lpUNCServerName == NULL || *lpUNCServerName == 0)
+    RpcExcept(EXCEPTION_EXECUTE_HANDLER)
     {
-        pLog->bLocal = TRUE;
-
-        if (!EvtGetLocalHandle(&pLog->BindingHandle))
-        {
-            HeapFree(GetProcessHeap(), 0, pLog);
-            SetLastError(ERROR_GEN_FAILURE);
-            return NULL;
-        }
+        Status = I_RpcMapWin32Status(RpcExceptionCode());
     }
-    else
-    {
-        pLog->bLocal = FALSE;
-
-        if (!EvtBindRpc(lpUNCServerName, &pLog->BindingHandle))
-        {
-            HeapFree(GetProcessHeap(), 0, pLog);
-            SetLastError(ERROR_INVALID_COMPUTERNAME);
-            return NULL;
-        }
-    }
-
-    Status = ElfrRegisterEventSourceW(
-        pLog->BindingHandle,
-        (LPWSTR)lpUNCServerName,
-        &SourceName,
-        &EmptyString,
-        0,
-        0,
-        &pLog->LogHandle);
+    RpcEndExcept;
 
     if (!NT_SUCCESS(Status))
     {
         SetLastError(RtlNtStatusToDosError(Status));
-        HeapFree(GetProcessHeap(), 0, pLog);
         return NULL;
     }
-    return pLog;
+
+    return (HANDLE)LogHandle;
 }
 
 
@@ -798,16 +886,15 @@ RegisterEventSourceW(
  * ReportEventA [ADVAPI32.@]
  */
 BOOL WINAPI
-ReportEventA(
-    IN HANDLE hEventLog,
-    IN WORD wType,
-    IN WORD wCategory,
-    IN DWORD dwEventID,
-    IN PSID lpUserSid,
-    IN WORD wNumStrings,
-    IN DWORD dwDataSize,
-    IN LPCSTR *lpStrings,
-    IN LPVOID lpRawData)
+ReportEventA(IN HANDLE hEventLog,
+             IN WORD wType,
+             IN WORD wCategory,
+             IN DWORD dwEventID,
+             IN PSID lpUserSid,
+             IN WORD wNumStrings,
+             IN DWORD dwDataSize,
+             IN LPCSTR *lpStrings,
+             IN LPVOID lpRawData)
 {
     LPCWSTR *wideStrArray;
     UNICODE_STRING str;
@@ -820,10 +907,9 @@ ReportEventA(
     if (lpStrings == NULL)
         return TRUE;
 
-    wideStrArray = HeapAlloc(
-        GetProcessHeap(),
-        HEAP_ZERO_MEMORY,
-        sizeof(LPCWSTR) * wNumStrings);
+    wideStrArray = HeapAlloc(GetProcessHeap(),
+                             HEAP_ZERO_MEMORY,
+                             sizeof(LPCWSTR) * wNumStrings);
 
     for (i = 0; i < wNumStrings; i++)
     {
@@ -834,16 +920,15 @@ ReportEventA(
 
     if (i == wNumStrings)
     {
-        ret = ReportEventW(
-            hEventLog,
-            wType,
-            wCategory,
-            dwEventID,
-            lpUserSid,
-            wNumStrings,
-            dwDataSize,
-            wideStrArray,
-            lpRawData);
+        ret = ReportEventW(hEventLog,
+                           wType,
+                           wCategory,
+                           dwEventID,
+                           lpUserSid,
+                           wNumStrings,
+                           dwDataSize,
+                           wideStrArray,
+                           lpRawData);
     }
     else
     {
@@ -855,17 +940,15 @@ ReportEventA(
     {
         if (wideStrArray[i])
         {
-            HeapFree(
-                GetProcessHeap(),
-                0,
-                (PVOID)wideStrArray[i]);
+            HeapFree(GetProcessHeap(),
+                     0,
+                     (PVOID)wideStrArray[i]);
         }
     }
 
-    HeapFree(
-        GetProcessHeap(),
-        0,
-        wideStrArray);
+    HeapFree(GetProcessHeap(),
+             0,
+             (PVOID)wideStrArray);
 
     return ret;
 }
@@ -886,62 +969,63 @@ ReportEventA(
  *   lpRawData   []
  */
 BOOL WINAPI
-ReportEventW(
-    IN HANDLE hEventLog,
-    IN WORD wType,
-    IN WORD wCategory,
-    IN DWORD dwEventID,
-    IN PSID lpUserSid,
-    IN WORD wNumStrings,
-    IN DWORD dwDataSize,
-    IN LPCWSTR *lpStrings,
-    IN LPVOID lpRawData)
+ReportEventW(IN HANDLE hEventLog,
+             IN WORD wType,
+             IN WORD wCategory,
+             IN DWORD dwEventID,
+             IN PSID lpUserSid,
+             IN WORD wNumStrings,
+             IN DWORD dwDataSize,
+             IN LPCWSTR *lpStrings,
+             IN LPVOID lpRawData)
 {
-#if 0
-    PLOG_INFO pLog;
     NTSTATUS Status;
     UNICODE_STRING *Strings;
+    UNICODE_STRING ComputerName;
     WORD i;
 
     TRACE("%p, %u, %u, %lu, %p, %u, %lu, %p, %p\n",
-        hEventLog, wType, wCategory, dwEventID, lpUserSid,
-        wNumStrings, dwDataSize, lpStrings, lpRawData);
-
-    pLog = (PLOG_INFO)hEventLog;
-    if (!pLog)
-    {
-        SetLastError(ERROR_INVALID_HANDLE);
-        return FALSE;
-    }
+          hEventLog, wType, wCategory, dwEventID, lpUserSid,
+          wNumStrings, dwDataSize, lpStrings, lpRawData);
 
-    Strings = HeapAlloc(
-        GetProcessHeap(),
-        0,
-        wNumStrings * sizeof(UNICODE_STRING));
+    Strings = HeapAlloc(GetProcessHeap(),
+                        0,
+                        wNumStrings * sizeof(UNICODE_STRING));
     if (!Strings)
     {
         SetLastError(ERROR_NOT_ENOUGH_MEMORY);
         return FALSE;
     }
+
     for (i = 0; i < wNumStrings; i++)
         RtlInitUnicodeString(&Strings[i], lpStrings[i]);
 
-    Status = ElfrReportEventW(
-        pLog->BindingHandle,
-        pLog->LogHandle,
-        0, /* FIXME: Time */
-        wType,
-        wCategory,
-        dwEventID,
-        wNumStrings,
-        dwDataSize,
-        L"", /* FIXME: ComputerName */
-        lpUserSid,
-        (LPWSTR *)lpStrings, /* FIXME: should be Strings */
-        lpRawData,
-        0,
-        NULL,
-        NULL);
+    /*FIXME: ComputerName */
+    RtlInitUnicodeString(&ComputerName, L"");
+
+    RpcTryExcept
+    {
+        Status = ElfrReportEventW(hEventLog,
+                                  0, /* FIXME: Time */
+                                  wType,
+                                  wCategory,
+                                  dwEventID,
+                                  wNumStrings,
+                                  dwDataSize,
+                                  (PRPC_UNICODE_STRING) &ComputerName,
+                                  lpUserSid,
+                                  (PRPC_UNICODE_STRING*) &Strings,
+                                  lpRawData,
+                                  0,
+                                  NULL,
+                                  NULL);
+    }
+    RpcExcept(EXCEPTION_EXECUTE_HANDLER)
+    {
+        Status = I_RpcMapWin32Status(RpcExceptionCode());
+    }
+    RpcEndExcept;
+
     HeapFree(GetProcessHeap(), 0, Strings);
 
     if (!NT_SUCCESS(Status))
@@ -949,44 +1033,6 @@ ReportEventW(
         SetLastError(RtlNtStatusToDosError(Status));
         return FALSE;
     }
-    return TRUE;
-#else
-  int i;
-
-    /* partial stub */
-
-  if (wNumStrings == 0)
-    return TRUE;
 
-  if (lpStrings == NULL)
     return TRUE;
-
-  for (i = 0; i < wNumStrings; i++)
-    {
-      switch (wType)
-        {
-        case EVENTLOG_SUCCESS:
-            TRACE_(eventlog)("Success: %S\n", lpStrings[i]);
-            break;
-
-        case EVENTLOG_ERROR_TYPE:
-            ERR_(eventlog)("Error: %S\n", lpStrings[i]);
-            break;
-
-        case EVENTLOG_WARNING_TYPE:
-            WARN_(eventlog)("Warning: %S\n", lpStrings[i]);
-            break;
-
-        case EVENTLOG_INFORMATION_TYPE:
-            TRACE_(eventlog)("Info: %S\n", lpStrings[i]);
-            break;
-
-        default:
-            TRACE_(eventlog)("Type %hu: %S\n", wType, lpStrings[i]);
-            break;
-        }
-    }
-
-  return TRUE;
-#endif
 }