GetServiceDisplayNameA/W and GetServiceKeyNameA/W must return an empty String if...
[reactos.git] / reactos / dll / win32 / advapi32 / service / scm.c
index 5d31111..2e3c79f 100644 (file)
 
 #include <advapi32.h>
 
-#define NDEBUG
-#include <debug.h>
+#include "wine/debug.h"
+
+WINE_DEFAULT_DEBUG_CHANNEL(advapi);
+
 
 /* FUNCTIONS *****************************************************************/
 
-handle_t BindingHandle = NULL;
+handle_t __RPC_USER
+SVCCTL_HANDLEA_bind(SVCCTL_HANDLEA szMachineName)
+{
+    handle_t hBinding = NULL;
+    UCHAR *pszStringBinding;
+    RPC_STATUS status;
+
+    TRACE("SVCCTL_HANDLEA_bind() called\n");
 
-static VOID
-HandleBind(VOID)
+    status = RpcStringBindingComposeA(NULL,
+                                      (UCHAR *)"ncacn_np",
+                                      (UCHAR *)szMachineName,
+                                      (UCHAR *)"\\pipe\\ntsvcs",
+                                      NULL,
+                                      (UCHAR **)&pszStringBinding);
+    if (status != RPC_S_OK)
+    {
+        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 != RPC_S_OK)
+    {
+        ERR("RpcBindingFromStringBinding returned 0x%x\n", status);
+    }
+
+    status = RpcStringFreeA(&pszStringBinding);
+    if (status != RPC_S_OK)
+    {
+        ERR("RpcStringFree returned 0x%x\n", status);
+    }
+
+    return hBinding;
+}
+
+
+void __RPC_USER
+SVCCTL_HANDLEA_unbind(SVCCTL_HANDLEA szMachineName,
+                      handle_t hBinding)
+{
+    RPC_STATUS status;
+
+    TRACE("SVCCTL_HANDLEA_unbind() called\n");
+
+    status = RpcBindingFree(&hBinding);
+    if (status != RPC_S_OK)
+    {
+        ERR("RpcBindingFree returned 0x%x\n", status);
+    }
+}
+
+
+handle_t __RPC_USER
+SVCCTL_HANDLEW_bind(SVCCTL_HANDLEW szMachineName)
+{
+    handle_t hBinding = NULL;
+    LPWSTR pszStringBinding;
+    RPC_STATUS status;
+
+    TRACE("SVCCTL_HANDLEW_bind() called\n");
+
+    status = RpcStringBindingComposeW(NULL,
+                                      L"ncacn_np",
+                                      szMachineName,
+                                      L"\\pipe\\ntsvcs",
+                                      NULL,
+                                      &pszStringBinding);
+    if (status != RPC_S_OK)
+    {
+        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 != RPC_S_OK)
+    {
+        ERR("RpcBindingFromStringBinding returned 0x%x\n", status);
+    }
+
+    status = RpcStringFreeW(&pszStringBinding);
+    if (status != RPC_S_OK)
+    {
+        ERR("RpcStringFree returned 0x%x\n", status);
+    }
+
+    return hBinding;
+}
+
+
+void __RPC_USER
+SVCCTL_HANDLEW_unbind(SVCCTL_HANDLEW szMachineName,
+                      handle_t hBinding)
+{
+    RPC_STATUS status;
+
+    TRACE("SVCCTL_HANDLEW_unbind() called\n");
+
+    status = RpcBindingFree(&hBinding);
+    if (status != RPC_S_OK)
+    {
+        ERR("RpcBindingFree returned 0x%x\n", status);
+    }
+}
+
+
+handle_t __RPC_USER
+RPC_SERVICE_STATUS_HANDLE_bind(RPC_SERVICE_STATUS_HANDLE hServiceStatus)
 {
+    handle_t hBinding = NULL;
     LPWSTR pszStringBinding;
     RPC_STATUS status;
 
-    if (BindingHandle != NULL)
-        return;
+    TRACE("RPC_SERVICE_STATUS_HANDLE_bind() called\n");
 
     status = RpcStringBindingComposeW(NULL,
                                       L"ncacn_np",
@@ -36,44 +146,66 @@ HandleBind(VOID)
                                       L"\\pipe\\ntsvcs",
                                       NULL,
                                       &pszStringBinding);
-    if (status)
+    if (status != RPC_S_OK)
     {
-        DPRINT1("RpcStringBindingCompose returned 0x%x\n", status);
-        return;
+        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,
-                                          &BindingHandle);
-    if (status)
+                                          &hBinding);
+    if (status != RPC_S_OK)
     {
-        DPRINT1("RpcBindingFromStringBinding returned 0x%x\n", status);
+        ERR("RpcBindingFromStringBinding returned 0x%x\n", status);
     }
 
     status = RpcStringFreeW(&pszStringBinding);
-    if (status)
+    if (status != RPC_S_OK)
     {
-        DPRINT1("RpcStringFree returned 0x%x\n", status);
+        ERR("RpcStringFree returned 0x%x\n", status);
     }
+
+    return hBinding;
 }
 
 
-#if 0
-static VOID
-HandleUnbind(VOID)
+void __RPC_USER
+RPC_SERVICE_STATUS_HANDLE_unbind(RPC_SERVICE_STATUS_HANDLE hServiceStatus,
+                                 handle_t hBinding)
 {
     RPC_STATUS status;
 
-    if (BindingHandle == NULL)
-        return;
+    TRACE("RPC_SERVICE_STATUS_HANDLE_unbind() called\n");
 
-    status = RpcBindingFree(&BindingHandle);
-    if (status)
+    status = RpcBindingFree(&hBinding);
+    if (status != RPC_S_OK)
     {
-        DPRINT1("RpcBindingFree returned 0x%x\n", status);
+        ERR("RpcBindingFree returned 0x%x\n", status);
+    }
+}
+
+
+DWORD
+ScmRpcStatusToWinError(RPC_STATUS Status)
+{
+    switch (Status)
+    {
+        case RPC_S_INVALID_BINDING:
+        case RPC_X_SS_IN_NULL_CONTEXT:
+            return ERROR_INVALID_HANDLE;
+
+        case RPC_X_ENUM_VALUE_OUT_OF_RANGE:
+        case RPC_X_BYTE_COUNT_TOO_SMALL:
+            return ERROR_INVALID_PARAMETER;
+
+        case RPC_X_NULL_REF_POINTER:
+            return ERROR_INVALID_ADDRESS;
+
+        default:
+            return (DWORD)Status;
     }
 }
-#endif
 
 
 /**********************************************************************
@@ -86,24 +218,26 @@ ChangeServiceConfig2A(SC_HANDLE hService,
                       DWORD dwInfoLevel,
                       LPVOID lpInfo)
 {
-    DWORD lpInfoSize;
+    SC_RPC_CONFIG_INFOA Info;
     DWORD dwError;
 
-    DPRINT("ChangeServiceConfig2A() called\n");
+    TRACE("ChangeServiceConfig2A() called\n");
 
-    /* Determine the length of the lpInfo parameter */
+    /* Fill relevent field of the Info structure */
+    Info.dwInfoLevel = dwInfoLevel;
     switch (dwInfoLevel)
     {
         case SERVICE_CONFIG_DESCRIPTION:
-            lpInfoSize = sizeof(SERVICE_DESCRIPTIONA);
+            Info.psd = (LPSERVICE_DESCRIPTIONA)&lpInfo;
+            Info.lpDescription = ((LPSERVICE_DESCRIPTIONA)lpInfo)->lpDescription; //HACK
             break;
 
         case SERVICE_CONFIG_FAILURE_ACTIONS:
-            lpInfoSize = sizeof(SERVICE_FAILURE_ACTIONSA);
+            Info.psfa = (LPSERVICE_FAILURE_ACTIONSA)lpInfo;
             break;
 
         default:
-            DPRINT1("Unknown info level 0x%lx\n", dwInfoLevel);
+            WARN("Unknown info level 0x%lx\n", dwInfoLevel);
             SetLastError(ERROR_INVALID_PARAMETER);
             return FALSE;
     }
@@ -111,16 +245,20 @@ ChangeServiceConfig2A(SC_HANDLE hService,
     if (lpInfo == NULL)
         return TRUE;
 
-    HandleBind();
+    RpcTryExcept
+    {
+        dwError = RChangeServiceConfig2A((SC_RPC_HANDLE)hService,
+                                         Info);
+    }
+    RpcExcept(EXCEPTION_EXECUTE_HANDLER)
+    {
+        dwError = ScmRpcStatusToWinError(RpcExceptionCode());
+    }
+    RpcEndExcept;
 
-    dwError = ScmrChangeServiceConfig2A(BindingHandle,
-                                        (unsigned int)hService,
-                                        dwInfoLevel,
-                                        lpInfo,
-                                        lpInfoSize);
     if (dwError != ERROR_SUCCESS)
     {
-        DPRINT1("ScmrChangeServiceConfig2A() failed (Error %lu)\n", dwError);
+        ERR("RChangeServiceConfig2A() failed (Error %lu)\n", dwError);
         SetLastError(dwError);
         return FALSE;
     }
@@ -139,24 +277,25 @@ ChangeServiceConfig2W(SC_HANDLE hService,
                       DWORD dwInfoLevel,
                       LPVOID lpInfo)
 {
-    DWORD lpInfoSize;
+    SC_RPC_CONFIG_INFOW Info;
     DWORD dwError;
 
-    DPRINT("ChangeServiceConfig2W() called\n");
+    TRACE("ChangeServiceConfig2W() called\n");
 
-    /* Determine the length of the lpInfo parameter */
+    /* Fill relevent field of the Info structure */
+    Info.dwInfoLevel = dwInfoLevel;
     switch (dwInfoLevel)
     {
         case SERVICE_CONFIG_DESCRIPTION:
-            lpInfoSize = sizeof(SERVICE_DESCRIPTIONW);
+            Info.psd = (LPSERVICE_DESCRIPTIONW)&lpInfo;
             break;
 
         case SERVICE_CONFIG_FAILURE_ACTIONS:
-            lpInfoSize = sizeof(SERVICE_FAILURE_ACTIONSW);
+            Info.psfa = (LPSERVICE_FAILURE_ACTIONSW)&lpInfo;
             break;
 
         default:
-            DPRINT1("Unknown info level 0x%lx\n", dwInfoLevel);
+            WARN("Unknown info level 0x%lx\n", dwInfoLevel);
             SetLastError(ERROR_INVALID_PARAMETER);
             return FALSE;
     }
@@ -164,16 +303,20 @@ ChangeServiceConfig2W(SC_HANDLE hService,
     if (lpInfo == NULL)
         return TRUE;
 
-    HandleBind();
+    RpcTryExcept
+    {
+        dwError = RChangeServiceConfig2W((SC_RPC_HANDLE)hService,
+                                         Info);
+    }
+    RpcExcept(EXCEPTION_EXECUTE_HANDLER)
+    {
+        dwError = ScmRpcStatusToWinError(RpcExceptionCode());
+    }
+    RpcEndExcept;
 
-    dwError = ScmrChangeServiceConfig2W(BindingHandle,
-                                        (unsigned int)hService,
-                                        dwInfoLevel,
-                                        lpInfo,
-                                        lpInfoSize);
     if (dwError != ERROR_SUCCESS)
     {
-        DPRINT1("ScmrChangeServiceConfig2W() failed (Error %lu)\n", dwError);
+        ERR("RChangeServiceConfig2W() failed (Error %lu)\n", dwError);
         SetLastError(dwError);
         return FALSE;
     }
@@ -187,7 +330,7 @@ ChangeServiceConfig2W(SC_HANDLE hService,
  *
  * @implemented
  */
-BOOL STDCALL
+BOOL WINAPI
 ChangeServiceConfigA(SC_HANDLE hService,
                      DWORD dwServiceType,
                      DWORD dwStartType,
@@ -205,7 +348,7 @@ ChangeServiceConfigA(SC_HANDLE hService,
     DWORD dwLength;
     LPSTR lpStr;
 
-    DPRINT("ChangeServiceConfigA() called\n");
+    TRACE("ChangeServiceConfigA() called\n");
 
     /* Calculate the Dependencies length*/
     if (lpDependencies != NULL)
@@ -222,26 +365,32 @@ ChangeServiceConfigA(SC_HANDLE hService,
 
     /* FIXME: Encrypt the password */
 
-    HandleBind();
+    RpcTryExcept
+    {
+        /* Call to services.exe using RPC */
+        dwError = RChangeServiceConfigA((SC_RPC_HANDLE)hService,
+                                        dwServiceType,
+                                        dwStartType,
+                                        dwErrorControl,
+                                        (LPSTR)lpBinaryPathName,
+                                        (LPSTR)lpLoadOrderGroup,
+                                        lpdwTagId,
+                                        (LPSTR)lpDependencies,
+                                        dwDependenciesLength,
+                                        (LPSTR)lpServiceStartName,
+                                        NULL,              /* FIXME: lpPassword */
+                                        0,                 /* FIXME: dwPasswordLength */
+                                        (LPSTR)lpDisplayName);
+    }
+    RpcExcept(EXCEPTION_EXECUTE_HANDLER)
+    {
+        dwError = ScmRpcStatusToWinError(RpcExceptionCode());
+    }
+    RpcEndExcept;
 
-    /* Call to services.exe using RPC */
-    dwError = ScmrChangeServiceConfigA(BindingHandle,
-                                       (unsigned int)hService,
-                                       dwServiceType,
-                                       dwStartType,
-                                       dwErrorControl,
-                                       (LPSTR)lpBinaryPathName,
-                                       (LPSTR)lpLoadOrderGroup,
-                                       lpdwTagId,
-                                       (LPSTR)lpDependencies,
-                                       dwDependenciesLength,
-                                       (LPSTR)lpServiceStartName,
-                                       NULL,              /* FIXME: lpPassword */
-                                       0,                 /* FIXME: dwPasswordLength */
-                                       (LPSTR)lpDisplayName);
     if (dwError != ERROR_SUCCESS)
     {
-        DPRINT1("ScmrChangeServiceConfigA() failed (Error %lu)\n", dwError);
+        ERR("RChangeServiceConfigA() failed (Error %lu)\n", dwError);
         SetLastError(dwError);
         return FALSE;
     }
@@ -255,7 +404,7 @@ ChangeServiceConfigA(SC_HANDLE hService,
  *
  * @implemented
  */
-BOOL STDCALL
+BOOL WINAPI
 ChangeServiceConfigW(SC_HANDLE hService,
                      DWORD dwServiceType,
                      DWORD dwStartType,
@@ -273,7 +422,7 @@ ChangeServiceConfigW(SC_HANDLE hService,
     DWORD dwLength;
     LPWSTR lpStr;
 
-    DPRINT("ChangeServiceConfigW() called\n");
+    TRACE("ChangeServiceConfigW() called\n");
 
     /* Calculate the Dependencies length*/
     if (lpDependencies != NULL)
@@ -290,26 +439,32 @@ ChangeServiceConfigW(SC_HANDLE hService,
 
     /* FIXME: Encrypt the password */
 
-    HandleBind();
+    RpcTryExcept
+    {
+        /* Call to services.exe using RPC */
+        dwError = RChangeServiceConfigW((SC_RPC_HANDLE)hService,
+                                        dwServiceType,
+                                        dwStartType,
+                                        dwErrorControl,
+                                        (LPWSTR)lpBinaryPathName,
+                                        (LPWSTR)lpLoadOrderGroup,
+                                        lpdwTagId,
+                                        (LPBYTE)lpDependencies,
+                                        dwDependenciesLength,
+                                        (LPWSTR)lpServiceStartName,
+                                        NULL,              /* FIXME: lpPassword */
+                                        0,                 /* FIXME: dwPasswordLength */
+                                        (LPWSTR)lpDisplayName);
+    }
+    RpcExcept(EXCEPTION_EXECUTE_HANDLER)
+    {
+        dwError = ScmRpcStatusToWinError(RpcExceptionCode());
+    }
+    RpcEndExcept;
 
-    /* Call to services.exe using RPC */
-    dwError = ScmrChangeServiceConfigW(BindingHandle,
-                                       (unsigned int)hService,
-                                       dwServiceType,
-                                       dwStartType,
-                                       dwErrorControl,
-                                       (LPWSTR)lpBinaryPathName,
-                                       (LPWSTR)lpLoadOrderGroup,
-                                       lpdwTagId,
-                                       (LPWSTR)lpDependencies,
-                                       dwDependenciesLength,
-                                       (LPWSTR)lpServiceStartName,
-                                       NULL,              /* FIXME: lpPassword */
-                                       0,                 /* FIXME: dwPasswordLength */
-                                       (LPWSTR)lpDisplayName);
     if (dwError != ERROR_SUCCESS)
     {
-        DPRINT1("ScmrChangeServiceConfigW() failed (Error %lu)\n", dwError);
+        ERR("RChangeServiceConfigW() failed (Error %lu)\n", dwError);
         SetLastError(dwError);
         return FALSE;
     }
@@ -323,26 +478,38 @@ ChangeServiceConfigW(SC_HANDLE hService,
  *
  * @implemented
  */
-BOOL STDCALL
+BOOL WINAPI
 CloseServiceHandle(SC_HANDLE hSCObject)
 {
     DWORD dwError;
 
-    DPRINT("CloseServiceHandle() called\n");
+    TRACE("CloseServiceHandle() called\n");
+
+    if (!hSCObject)
+    {
+        SetLastError(ERROR_INVALID_HANDLE);
+        return FALSE;
+    }
 
-    HandleBind();
+    RpcTryExcept
+    {
+        /* Call to services.exe using RPC */
+        dwError = RCloseServiceHandle((LPSC_RPC_HANDLE)&hSCObject);
+    }
+    RpcExcept(EXCEPTION_EXECUTE_HANDLER)
+    {
+        dwError = ScmRpcStatusToWinError(RpcExceptionCode());
+    }
+    RpcEndExcept;
 
-    /* Call to services.exe using RPC */
-    dwError = ScmrCloseServiceHandle(BindingHandle,
-                                     (unsigned int)hSCObject);
     if (dwError)
     {
-        DPRINT1("ScmrCloseServiceHandle() failed (Error %lu)\n", dwError);
+        ERR("RCloseServiceHandle() failed (Error %lu)\n", dwError);
         SetLastError(dwError);
         return FALSE;
     }
 
-    DPRINT("CloseServiceHandle() done\n");
+    TRACE("CloseServiceHandle() done\n");
 
     return TRUE;
 }
@@ -353,31 +520,37 @@ CloseServiceHandle(SC_HANDLE hSCObject)
  *
  * @implemented
  */
-BOOL STDCALL
+BOOL WINAPI
 ControlService(SC_HANDLE hService,
                DWORD dwControl,
                LPSERVICE_STATUS lpServiceStatus)
 {
     DWORD dwError;
 
-    DPRINT("ControlService(%x, %x, %p)\n",
+    TRACE("ControlService(%x, %x, %p)\n",
            hService, dwControl, lpServiceStatus);
 
-    HandleBind();
+    RpcTryExcept
+    {
+        /* Call to services.exe using RPC */
+        dwError = RControlService((SC_RPC_HANDLE)hService,
+                                  dwControl,
+                                  lpServiceStatus);
+    }
+    RpcExcept(EXCEPTION_EXECUTE_HANDLER)
+    {
+        dwError = ScmRpcStatusToWinError(RpcExceptionCode());
+    }
+    RpcEndExcept;
 
-    /* Call to services.exe using RPC */
-    dwError = ScmrControlService(BindingHandle,
-                                 (unsigned int)hService,
-                                 dwControl,
-                                 lpServiceStatus);
     if (dwError != ERROR_SUCCESS)
     {
-        DPRINT1("ScmrControlService() failed (Error %lu)\n", dwError);
+        ERR("RControlService() failed (Error %lu)\n", dwError);
         SetLastError(dwError);
         return FALSE;
     }
 
-    DPRINT("ControlService() done\n");
+    TRACE("ControlService() done\n");
 
     return TRUE;
 }
@@ -388,26 +561,25 @@ ControlService(SC_HANDLE hService,
  *
  * @unimplemented
  */
-BOOL STDCALL
+BOOL WINAPI
 ControlServiceEx(IN SC_HANDLE hService,
                  IN DWORD dwControl,
                  IN DWORD dwInfoLevel,
                  IN OUT PVOID pControlParams)
 {
-    DPRINT1("ControlServiceEx(0x%p, 0x%x, 0x%x, 0x%p) UNIMPLEMENTED!\n",
+    FIXME("ControlServiceEx(0x%p, 0x%x, 0x%x, 0x%p) UNIMPLEMENTED!\n",
             hService, dwControl, dwInfoLevel, pControlParams);
     SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
     return FALSE;
 }
 
 
-
 /**********************************************************************
  *  CreateServiceA
  *
  * @implemented
  */
-SC_HANDLE STDCALL
+SC_HANDLE WINAPI
 CreateServiceA(SC_HANDLE hSCManager,
                LPCSTR lpServiceName,
                LPCSTR lpDisplayName,
@@ -422,54 +594,23 @@ CreateServiceA(SC_HANDLE hSCManager,
                LPCSTR lpServiceStartName,
                LPCSTR lpPassword)
 {
-    SC_HANDLE RetVal = NULL;
-    LPWSTR lpServiceNameW = NULL;
-    LPWSTR lpDisplayNameW = NULL;
-    LPWSTR lpBinaryPathNameW = NULL;
-    LPWSTR lpLoadOrderGroupW = NULL;
-    LPWSTR lpDependenciesW = NULL;
-    LPWSTR lpServiceStartNameW = NULL;
-    LPWSTR lpPasswordW = NULL;
+    SC_HANDLE hService = NULL;
     DWORD dwDependenciesLength = 0;
+    DWORD dwError;
     DWORD dwLength;
     LPSTR lpStr;
 
-    int len = MultiByteToWideChar(CP_ACP, 0, lpServiceName, -1, NULL, 0);
-    lpServiceNameW = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
-    if (!lpServiceNameW)
-    {
-        SetLastError(ERROR_NOT_ENOUGH_MEMORY);
-        goto cleanup;
-    }
-    MultiByteToWideChar(CP_ACP, 0, lpServiceName, -1, lpServiceNameW, len);
-
-    len = MultiByteToWideChar(CP_ACP, 0, lpDisplayName, -1, NULL, 0);
-    lpDisplayNameW = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
-    if (!lpDisplayNameW)
-    {
-        SetLastError(ERROR_NOT_ENOUGH_MEMORY);
-        goto cleanup;
-    }
-    MultiByteToWideChar(CP_ACP, 0, lpDisplayName, -1, lpDisplayNameW, len);
+    TRACE("CreateServiceA() called\n");
+    TRACE("%p %s %s\n", hSCManager, 
+          lpServiceName, lpDisplayName);
 
-    len = MultiByteToWideChar(CP_ACP, 0, lpBinaryPathName, -1, NULL, 0);
-    lpBinaryPathNameW = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
-    if (!lpBinaryPathNameW)
+    if (!hSCManager)
     {
-        SetLastError(ERROR_NOT_ENOUGH_MEMORY);
-        goto cleanup;
-    }
-    MultiByteToWideChar(CP_ACP, 0, lpDisplayName, -1, lpBinaryPathNameW, len);
-
-    len = MultiByteToWideChar(CP_ACP, 0, lpLoadOrderGroup, -1, NULL, 0);
-    lpLoadOrderGroupW = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
-    if (!lpLoadOrderGroupW)
-    {
-        SetLastError(ERROR_NOT_ENOUGH_MEMORY);
-        goto cleanup;
+        SetLastError(ERROR_INVALID_HANDLE);
+        return NULL;
     }
-    MultiByteToWideChar(CP_ACP, 0, lpLoadOrderGroup, -1, lpLoadOrderGroupW, len);
 
+    /* Calculate the Dependencies length*/
     if (lpDependencies != NULL)
     {
         lpStr = (LPSTR)lpDependencies;
@@ -482,56 +623,42 @@ CreateServiceA(SC_HANDLE hSCManager,
         dwDependenciesLength++;
     }
 
-    lpDependenciesW = HeapAlloc(GetProcessHeap(), 0, dwDependenciesLength * sizeof(WCHAR));
-    if (!lpDependenciesW)
+    /* FIXME: Encrypt the password */
+
+    RpcTryExcept
     {
-        SetLastError(ERROR_NOT_ENOUGH_MEMORY);
-        goto cleanup;
+        /* Call to services.exe using RPC */
+        dwError = RCreateServiceA((SC_RPC_HANDLE)hSCManager,
+                                  (LPSTR)lpServiceName,
+                                  (LPSTR)lpDisplayName,
+                                  dwDesiredAccess,
+                                  dwServiceType,
+                                  dwStartType,
+                                  dwErrorControl,
+                                  (LPSTR)lpBinaryPathName,
+                                  (LPSTR)lpLoadOrderGroup,
+                                  lpdwTagId,
+                                  (LPBYTE)lpDependencies,
+                                  dwDependenciesLength,
+                                  (LPSTR)lpServiceStartName,
+                                  NULL,              /* FIXME: lpPassword */
+                                  0,                 /* FIXME: dwPasswordLength */
+                                  (SC_RPC_HANDLE *)&hService);
     }
-    MultiByteToWideChar(CP_ACP, 0, lpDependencies, -1, lpDependenciesW, dwDependenciesLength);
-
-    len = MultiByteToWideChar(CP_ACP, 0, lpServiceStartName, -1, NULL, 0);
-    lpServiceStartName = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
-    if (!lpServiceStartNameW)
+    RpcExcept(EXCEPTION_EXECUTE_HANDLER)
     {
-        SetLastError(ERROR_NOT_ENOUGH_MEMORY);
-        goto cleanup;
+        dwError = ScmRpcStatusToWinError(RpcExceptionCode());
     }
-    MultiByteToWideChar(CP_ACP, 0, lpServiceStartName, -1, lpServiceStartNameW, len);
+    RpcEndExcept;
 
-    len = MultiByteToWideChar(CP_ACP, 0, lpPassword, -1, NULL, 0);
-    lpPasswordW = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
-    if (!lpPasswordW)
+    if (dwError != ERROR_SUCCESS)
     {
-        SetLastError(ERROR_NOT_ENOUGH_MEMORY);
-        goto cleanup;
-    }
-    MultiByteToWideChar(CP_ACP, 0, lpPassword, -1, lpPasswordW, len);
-
-    RetVal = CreateServiceW(hSCManager,
-                            lpServiceNameW,
-                            lpDisplayNameW,
-                            dwDesiredAccess,
-                            dwServiceType,
-                            dwStartType,
-                            dwErrorControl,
-                            lpBinaryPathNameW,
-                            lpLoadOrderGroupW,
-                            lpdwTagId,
-                            lpDependenciesW,
-                            lpServiceStartNameW,
-                            lpPasswordW);
-
-cleanup:
-    HeapFree(GetProcessHeap(), 0, lpServiceNameW);
-    HeapFree(GetProcessHeap(), 0, lpDisplayNameW);
-    HeapFree(GetProcessHeap(), 0, lpBinaryPathNameW);
-    HeapFree(GetProcessHeap(), 0, lpLoadOrderGroupW);
-    HeapFree(GetProcessHeap(), 0, lpDependenciesW);
-    HeapFree(GetProcessHeap(), 0, lpServiceStartNameW);
-    HeapFree(GetProcessHeap(), 0, lpPasswordW);
-
-    return RetVal;
+        ERR("RCreateServiceA() failed (Error %lu)\n", dwError);
+        SetLastError(dwError);
+        return NULL;
+    }
+
+    return hService;
 }
 
 
@@ -540,7 +667,7 @@ cleanup:
  *
  * @implemented
  */
-SC_HANDLE STDCALL
+SC_HANDLE WINAPI
 CreateServiceW(SC_HANDLE hSCManager,
                LPCWSTR lpServiceName,
                LPCWSTR lpDisplayName,
@@ -561,7 +688,15 @@ CreateServiceW(SC_HANDLE hSCManager,
     DWORD dwLength;
     LPWSTR lpStr;
 
-    DPRINT("CreateServiceW() called\n");
+    TRACE("CreateServiceW() called\n");
+    TRACE("%p %S %S\n", hSCManager, 
+          lpServiceName, lpDisplayName);
+
+    if (!hSCManager)
+    {
+        SetLastError(ERROR_INVALID_HANDLE);
+        return NULL;
+    }
 
     /* Calculate the Dependencies length*/
     if (lpDependencies != NULL)
@@ -574,33 +709,41 @@ CreateServiceW(SC_HANDLE hSCManager,
             lpStr = lpStr + dwLength;
         }
         dwDependenciesLength++;
+
+        dwDependenciesLength *= sizeof(WCHAR);
     }
 
     /* FIXME: Encrypt the password */
 
-    HandleBind();
-
-    /* Call to services.exe using RPC */
-    dwError = ScmrCreateServiceW(BindingHandle,
-                                 (unsigned int)hSCManager,
-                                 (LPWSTR)lpServiceName,
-                                 (LPWSTR)lpDisplayName,
-                                 dwDesiredAccess,
-                                 dwServiceType,
-                                 dwStartType,
-                                 dwErrorControl,
-                                 (LPWSTR)lpBinaryPathName,
-                                 (LPWSTR)lpLoadOrderGroup,
-                                 lpdwTagId,
-                                 (LPWSTR)lpDependencies,
-                                 dwDependenciesLength,
-                                 (LPWSTR)lpServiceStartName,
-                                 NULL,              /* FIXME: lpPassword */
-                                 0,                 /* FIXME: dwPasswordLength */
-                                 (unsigned int *)&hService);
+    RpcTryExcept
+    {
+        /* Call to services.exe using RPC */
+        dwError = RCreateServiceW((SC_RPC_HANDLE)hSCManager,
+                                  lpServiceName,
+                                  lpDisplayName,
+                                  dwDesiredAccess,
+                                  dwServiceType,
+                                  dwStartType,
+                                  dwErrorControl,
+                                  lpBinaryPathName,
+                                  lpLoadOrderGroup,
+                                  lpdwTagId,
+                                  (LPBYTE)lpDependencies,
+                                  dwDependenciesLength,
+                                  lpServiceStartName,
+                                  NULL,              /* FIXME: lpPassword */
+                                  0,                 /* FIXME: dwPasswordLength */
+                                  (SC_RPC_HANDLE *)&hService);
+    }
+    RpcExcept(EXCEPTION_EXECUTE_HANDLER)
+    {
+        dwError = ScmRpcStatusToWinError(RpcExceptionCode());
+    }
+    RpcEndExcept;
+
     if (dwError != ERROR_SUCCESS)
     {
-        DPRINT("ScmrCreateServiceW() failed (Error %lu)\n", dwError);
+        ERR("RCreateServiceW() failed (Error %lu)\n", dwError);
         SetLastError(dwError);
         return NULL;
     }
@@ -614,21 +757,27 @@ CreateServiceW(SC_HANDLE hSCManager,
  *
  * @implemented
  */
-BOOL STDCALL
+BOOL WINAPI
 DeleteService(SC_HANDLE hService)
 {
     DWORD dwError;
 
-    DPRINT("DeleteService(%x)\n", hService);
+    TRACE("DeleteService(%x)\n", hService);
 
-    HandleBind();
+    RpcTryExcept
+    {
+        /* Call to services.exe using RPC */
+        dwError = RDeleteService((SC_RPC_HANDLE)hService);
+    }
+    RpcExcept(EXCEPTION_EXECUTE_HANDLER)
+    {
+        dwError = ScmRpcStatusToWinError(RpcExceptionCode());
+    }
+    RpcEndExcept;
 
-    /* Call to services.exe using RPC */
-    dwError = ScmrDeleteService(BindingHandle,
-                                (unsigned int)hService);
     if (dwError != ERROR_SUCCESS)
     {
-        DPRINT1("ScmrDeleteService() failed (Error %lu)\n", dwError);
+        ERR("RDeleteService() failed (Error %lu)\n", dwError);
         SetLastError(dwError);
         return FALSE;
     }
@@ -642,7 +791,7 @@ DeleteService(SC_HANDLE hService)
  *
  * @implemented
  */
-BOOL STDCALL
+BOOL WINAPI
 EnumDependentServicesA(SC_HANDLE hService,
                        DWORD dwServiceState,
                        LPENUM_SERVICE_STATUSA lpServices,
@@ -651,20 +800,32 @@ EnumDependentServicesA(SC_HANDLE hService,
                        LPDWORD lpServicesReturned)
 {
     LPENUM_SERVICE_STATUSA lpStatusPtr;
-    DWORD dwError = ERROR_SUCCESS;
+    DWORD dwError;
     DWORD dwCount;
 
-    DPRINT("EnumServicesStatusA() called\n");
+    TRACE("EnumServicesStatusA() called\n");
 
-    HandleBind();
+    RpcTryExcept
+    {
+        dwError = REnumDependentServicesA((SC_RPC_HANDLE)hService,
+                                          dwServiceState,
+                                          (LPBYTE)lpServices,
+                                          cbBufSize,
+                                          pcbBytesNeeded,
+                                          lpServicesReturned);
+    }
+    RpcExcept(EXCEPTION_EXECUTE_HANDLER)
+    {
+        dwError = ScmRpcStatusToWinError(RpcExceptionCode());
+    }
+    RpcEndExcept;
 
-    dwError = ScmrEnumDependentServicesA(BindingHandle,
-                                         (unsigned int)hService,
-                                         dwServiceState,
-                                         (unsigned char *)lpServices,
-                                         cbBufSize,
-                                         pcbBytesNeeded,
-                                         lpServicesReturned);
+    if (dwError != ERROR_SUCCESS)
+    {
+        ERR("REnumDependentServicesA() failed (Error %lu)\n", dwError);
+        SetLastError(dwError);
+        return FALSE;
+    }
 
     lpStatusPtr = (LPENUM_SERVICE_STATUSA)lpServices;
     for (dwCount = 0; dwCount < *lpServicesReturned; dwCount++)
@@ -680,14 +841,7 @@ EnumDependentServicesA(SC_HANDLE hService,
         lpStatusPtr++;
     }
 
-    if (dwError != ERROR_SUCCESS)
-    {
-        DPRINT("ScmrEnumDependentServicesA() failed (Error %lu)\n", dwError);
-        SetLastError(dwError);
-        return FALSE;
-    }
-
-    DPRINT("EnumDependentServicesA() done\n");
+    TRACE("EnumDependentServicesA() done\n");
 
     return TRUE;
 }
@@ -698,7 +852,7 @@ EnumDependentServicesA(SC_HANDLE hService,
  *
  * @implemented
  */
-BOOL STDCALL
+BOOL WINAPI
 EnumDependentServicesW(SC_HANDLE hService,
                        DWORD dwServiceState,
                        LPENUM_SERVICE_STATUSW lpServices,
@@ -707,20 +861,32 @@ EnumDependentServicesW(SC_HANDLE hService,
                        LPDWORD lpServicesReturned)
 {
     LPENUM_SERVICE_STATUSW lpStatusPtr;
-    DWORD dwError = ERROR_SUCCESS;
+    DWORD dwError;
     DWORD dwCount;
 
-    DPRINT("EnumServicesStatusW() called\n");
+    TRACE("EnumServicesStatusW() called\n");
 
-    HandleBind();
+    RpcTryExcept
+    {
+        dwError = REnumDependentServicesW((SC_RPC_HANDLE)hService,
+                                          dwServiceState,
+                                          (LPBYTE)lpServices,
+                                          cbBufSize,
+                                          pcbBytesNeeded,
+                                          lpServicesReturned);
+    }
+    RpcExcept(EXCEPTION_EXECUTE_HANDLER)
+    {
+        dwError = ScmRpcStatusToWinError(RpcExceptionCode());
+    }
+    RpcEndExcept;
 
-    dwError = ScmrEnumDependentServicesW(BindingHandle,
-                                         (unsigned int)hService,
-                                         dwServiceState,
-                                         (unsigned char *)lpServices,
-                                         cbBufSize,
-                                         pcbBytesNeeded,
-                                         lpServicesReturned);
+    if (dwError != ERROR_SUCCESS)
+    {
+        ERR("REnumDependentServicesW() failed (Error %lu)\n", dwError);
+        SetLastError(dwError);
+        return FALSE;
+    }
 
     lpStatusPtr = (LPENUM_SERVICE_STATUSW)lpServices;
     for (dwCount = 0; dwCount < *lpServicesReturned; dwCount++)
@@ -736,14 +902,7 @@ EnumDependentServicesW(SC_HANDLE hService,
         lpStatusPtr++;
     }
 
-    if (dwError != ERROR_SUCCESS)
-    {
-        DPRINT("ScmrEnumDependentServicesW() failed (Error %lu)\n", dwError);
-        SetLastError(dwError);
-        return FALSE;
-    }
-
-    DPRINT("EnumDependentServicesW() done\n");
+    TRACE("EnumDependentServicesW() done\n");
 
     return TRUE;
 }
@@ -755,7 +914,7 @@ EnumDependentServicesW(SC_HANDLE hService,
  * @unimplemented
  */
 BOOL
-STDCALL
+WINAPI
 EnumServiceGroupW(
     SC_HANDLE               hSCManager,
     DWORD                   dwServiceType,
@@ -767,7 +926,7 @@ EnumServiceGroupW(
     LPDWORD                 lpResumeHandle,
     LPCWSTR                 lpGroup)
 {
-    DPRINT1("EnumServiceGroupW is unimplemented\n");
+    FIXME("EnumServiceGroupW is unimplemented\n");
     SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
     return FALSE;
 }
@@ -778,7 +937,7 @@ EnumServiceGroupW(
  *
  * @implemented
  */
-BOOL STDCALL
+BOOL WINAPI
 EnumServicesStatusA(SC_HANDLE hSCManager,
                     DWORD dwServiceType,
                     DWORD dwServiceState,
@@ -789,22 +948,76 @@ EnumServicesStatusA(SC_HANDLE hSCManager,
                     LPDWORD lpResumeHandle)
 {
     LPENUM_SERVICE_STATUSA lpStatusPtr;
-    DWORD dwError = ERROR_SUCCESS;
+    DWORD dwError;
     DWORD dwCount;
 
-    DPRINT("EnumServicesStatusA() called\n");
+    TRACE("EnumServicesStatusA() called\n");
 
-    HandleBind();
+    if (!hSCManager)
+    {
+        SetLastError(ERROR_INVALID_HANDLE);
+        return FALSE;
+    }
+
+    if (dwServiceType != SERVICE_DRIVER && dwServiceType != SERVICE_WIN32)
+    {
+        if (pcbBytesNeeded && lpServicesReturned)
+        {
+            *pcbBytesNeeded = 0;
+            *lpServicesReturned = 0;
+        }
+
+        SetLastError(ERROR_INVALID_PARAMETER);
+        return FALSE;
+    }
+
+    if (dwServiceState != SERVICE_ACTIVE && dwServiceState != SERVICE_INACTIVE && dwServiceState != SERVICE_STATE_ALL)
+    {
+            if (pcbBytesNeeded)
+                *pcbBytesNeeded = 0;
+
+            if (lpServicesReturned)
+                *lpServicesReturned = 0;
+
+        SetLastError(ERROR_INVALID_PARAMETER);
+        return FALSE;
+    }
+
+    if (!pcbBytesNeeded || !lpServicesReturned)
+    {
+        SetLastError(ERROR_INVALID_ADDRESS);
+        return FALSE;
+    }
+
+    if (!lpServices && cbBufSize != 0)
+    {
+        SetLastError(ERROR_INVALID_ADDRESS);
+        return FALSE;
+    }
+
+    RpcTryExcept
+    {
+        dwError = REnumServicesStatusA((SC_RPC_HANDLE)hSCManager,
+                                       dwServiceType,
+                                       dwServiceState,
+                                       (LPBYTE)lpServices,
+                                       cbBufSize,
+                                       pcbBytesNeeded,
+                                       lpServicesReturned,
+                                       lpResumeHandle);
+    }
+    RpcExcept(EXCEPTION_EXECUTE_HANDLER)
+    {
+        dwError = ScmRpcStatusToWinError(RpcExceptionCode());
+    }
+    RpcEndExcept;
 
-    dwError = ScmrEnumServicesStatusA(BindingHandle,
-                                      (unsigned int)hSCManager,
-                                      dwServiceType,
-                                      dwServiceState,
-                                      (unsigned char *)lpServices,
-                                      cbBufSize,
-                                      pcbBytesNeeded,
-                                      lpServicesReturned,
-                                      lpResumeHandle);
+    if (dwError != ERROR_SUCCESS)
+    {
+        ERR("REnumServicesStatusA() failed (Error %lu)\n", dwError);
+        SetLastError(dwError);
+        return FALSE;
+    }
 
     lpStatusPtr = (LPENUM_SERVICE_STATUSA)lpServices;
     for (dwCount = 0; dwCount < *lpServicesReturned; dwCount++)
@@ -820,14 +1033,7 @@ EnumServicesStatusA(SC_HANDLE hSCManager,
         lpStatusPtr++;
     }
 
-    if (dwError != ERROR_SUCCESS)
-    {
-        DPRINT("ScmrEnumServicesStatusA() failed (Error %lu)\n", dwError);
-        SetLastError(dwError);
-        return FALSE;
-    }
-
-    DPRINT("EnumServicesStatusA() done\n");
+    TRACE("EnumServicesStatusA() done\n");
 
     return TRUE;
 }
@@ -838,7 +1044,7 @@ EnumServicesStatusA(SC_HANDLE hSCManager,
  *
  * @implemented
  */
-BOOL STDCALL
+BOOL WINAPI
 EnumServicesStatusW(SC_HANDLE hSCManager,
                     DWORD dwServiceType,
                     DWORD dwServiceState,
@@ -849,22 +1055,40 @@ EnumServicesStatusW(SC_HANDLE hSCManager,
                     LPDWORD lpResumeHandle)
 {
     LPENUM_SERVICE_STATUSW lpStatusPtr;
-    DWORD dwError = ERROR_SUCCESS;
+    DWORD dwError;
     DWORD dwCount;
 
-    DPRINT("EnumServicesStatusW() called\n");
+    TRACE("EnumServicesStatusW() called\n");
 
-    HandleBind();
+    if (!hSCManager)
+    {
+        SetLastError(ERROR_INVALID_HANDLE);
+        return FALSE;
+    }
 
-    dwError = ScmrEnumServicesStatusW(BindingHandle,
-                                      (unsigned int)hSCManager,
-                                      dwServiceType,
-                                      dwServiceState,
-                                      (unsigned char *)lpServices,
-                                      cbBufSize,
-                                      pcbBytesNeeded,
-                                      lpServicesReturned,
-                                      lpResumeHandle);
+    RpcTryExcept
+    {
+        dwError = REnumServicesStatusW((SC_RPC_HANDLE)hSCManager,
+                                       dwServiceType,
+                                       dwServiceState,
+                                       (LPBYTE)lpServices,
+                                       cbBufSize,
+                                       pcbBytesNeeded,
+                                       lpServicesReturned,
+                                       lpResumeHandle);
+    }
+    RpcExcept(EXCEPTION_EXECUTE_HANDLER)
+    {
+        dwError = ScmRpcStatusToWinError(RpcExceptionCode());
+    }
+    RpcEndExcept;
+
+    if (dwError != ERROR_SUCCESS)
+    {
+        ERR("REnumServicesStatusW() failed (Error %lu)\n", dwError);
+        SetLastError(dwError);
+        return FALSE;
+    }
 
     lpStatusPtr = (LPENUM_SERVICE_STATUSW)lpServices;
     for (dwCount = 0; dwCount < *lpServicesReturned; dwCount++)
@@ -880,14 +1104,7 @@ EnumServicesStatusW(SC_HANDLE hSCManager,
         lpStatusPtr++;
     }
 
-    if (dwError != ERROR_SUCCESS)
-    {
-        DPRINT("ScmrEnumServicesStatusW() failed (Error %lu)\n", dwError);
-        SetLastError(dwError);
-        return FALSE;
-    }
-
-    DPRINT("EnumServicesStatusW() done\n");
+    TRACE("EnumServicesStatusW() done\n");
 
     return TRUE;
 }
@@ -898,7 +1115,7 @@ EnumServicesStatusW(SC_HANDLE hSCManager,
  *
  * @implemented
  */
-BOOL STDCALL
+BOOL WINAPI
 EnumServicesStatusExA(SC_HANDLE hSCManager,
                       SC_ENUM_TYPE InfoLevel,
                       DWORD dwServiceType,
@@ -911,47 +1128,73 @@ EnumServicesStatusExA(SC_HANDLE hSCManager,
                       LPCSTR pszGroupName)
 {
     LPENUM_SERVICE_STATUS_PROCESSA lpStatusPtr;
-    DWORD dwError = ERROR_SUCCESS;
+    DWORD dwError;
     DWORD dwCount;
 
-    DPRINT("EnumServicesStatusExA() called\n");
-
-    HandleBind();
+    TRACE("EnumServicesStatusExA() called\n");
 
-    dwError = ScmrEnumServicesStatusExA(BindingHandle,
-                                        (unsigned int)hSCManager,
-                                        (unsigned long)InfoLevel,
-                                        dwServiceType,
-                                        dwServiceState,
-                                        (unsigned char *)lpServices,
-                                        cbBufSize,
-                                        pcbBytesNeeded,
-                                        lpServicesReturned,
-                                        lpResumeHandle,
-                                        (char *)pszGroupName);
+    if (InfoLevel != SC_ENUM_PROCESS_INFO)
+    {
+        SetLastError(ERROR_INVALID_LEVEL);
+        return FALSE;
+    }
 
-    lpStatusPtr = (LPENUM_SERVICE_STATUS_PROCESSA)lpServices;
-    for (dwCount = 0; dwCount < *lpServicesReturned; dwCount++)
+    if (!hSCManager)
     {
-        if (lpStatusPtr->lpServiceName)
-            lpStatusPtr->lpServiceName =
-                (LPSTR)((ULONG_PTR)lpServices + (ULONG_PTR)lpStatusPtr->lpServiceName);
+        SetLastError(ERROR_INVALID_HANDLE);
+        return FALSE;
+    }
 
-        if (lpStatusPtr->lpDisplayName)
-            lpStatusPtr->lpDisplayName =
-                (LPSTR)((ULONG_PTR)lpServices + (ULONG_PTR)lpStatusPtr->lpDisplayName);
 
-        lpStatusPtr++;
+    RpcTryExcept
+    {
+        dwError = REnumServicesStatusExA((SC_RPC_HANDLE)hSCManager,
+                                         InfoLevel,
+                                         dwServiceType,
+                                         dwServiceState,
+                                         (LPBYTE)lpServices,
+                                         cbBufSize,
+                                         pcbBytesNeeded,
+                                         lpServicesReturned,
+                                         lpResumeHandle,
+                                         (LPSTR)pszGroupName);
+    }
+    RpcExcept(EXCEPTION_EXECUTE_HANDLER)
+    {
+        dwError = ScmRpcStatusToWinError(RpcExceptionCode());
     }
+    RpcEndExcept;
 
-    if (dwError != ERROR_SUCCESS)
+    if (dwError == ERROR_MORE_DATA)
     {
-        DPRINT1("ScmrEnumServicesStatusExA() failed (Error %lu)\n", dwError);
+        WARN("Required buffer size %ul\n", *pcbBytesNeeded);
         SetLastError(dwError);
         return FALSE;
     }
+    else if (dwError == ERROR_SUCCESS)
+    {
+        lpStatusPtr = (LPENUM_SERVICE_STATUS_PROCESSA)lpServices;
+        for (dwCount = 0; dwCount < *lpServicesReturned; dwCount++)
+        {
+            if (lpStatusPtr->lpServiceName)
+                lpStatusPtr->lpServiceName =
+                    (LPSTR)((ULONG_PTR)lpServices + (ULONG_PTR)lpStatusPtr->lpServiceName);
 
-    DPRINT("EnumServicesStatusExA() done\n");
+            if (lpStatusPtr->lpDisplayName)
+                lpStatusPtr->lpDisplayName =
+                    (LPSTR)((ULONG_PTR)lpServices + (ULONG_PTR)lpStatusPtr->lpDisplayName);
+
+            lpStatusPtr++;
+        }
+    }
+    else
+    {
+        ERR("REnumServicesStatusExA() failed (Error %lu)\n", dwError);
+        SetLastError(dwError);
+        return FALSE;
+    }
+
+    TRACE("EnumServicesStatusExA() done\n");
 
     return TRUE;
 }
@@ -962,7 +1205,7 @@ EnumServicesStatusExA(SC_HANDLE hSCManager,
  *
  * @implemented
  */
-BOOL STDCALL
+BOOL WINAPI
 EnumServicesStatusExW(SC_HANDLE hSCManager,
                       SC_ENUM_TYPE InfoLevel,
                       DWORD dwServiceType,
@@ -975,47 +1218,60 @@ EnumServicesStatusExW(SC_HANDLE hSCManager,
                       LPCWSTR pszGroupName)
 {
     LPENUM_SERVICE_STATUS_PROCESSW lpStatusPtr;
-    DWORD dwError = ERROR_SUCCESS;
+    DWORD dwError;
     DWORD dwCount;
 
-    DPRINT("EnumServicesStatusExW() called\n");
-
-    HandleBind();
+    TRACE("EnumServicesStatusExW() called\n");
 
-    dwError = ScmrEnumServicesStatusExW(BindingHandle,
-                                        (unsigned int)hSCManager,
-                                        (unsigned long)InfoLevel,
-                                        dwServiceType,
-                                        dwServiceState,
-                                        (unsigned char *)lpServices,
-                                        cbBufSize,
-                                        pcbBytesNeeded,
-                                        lpServicesReturned,
-                                        lpResumeHandle,
-                                        (wchar_t *)pszGroupName);
+    RpcTryExcept
+    {
+        dwError = REnumServicesStatusExW((SC_RPC_HANDLE)hSCManager,
+                                         InfoLevel,
+                                         dwServiceType,
+                                         dwServiceState,
+                                         (LPBYTE)lpServices,
+                                         cbBufSize,
+                                         pcbBytesNeeded,
+                                         lpServicesReturned,
+                                         lpResumeHandle,
+                                         (LPWSTR)pszGroupName);
+    }
+    RpcExcept(EXCEPTION_EXECUTE_HANDLER)
+    {
+        dwError = ScmRpcStatusToWinError(RpcExceptionCode());
+    }
+    RpcEndExcept;
 
-    lpStatusPtr = (LPENUM_SERVICE_STATUS_PROCESSW)lpServices;
-    for (dwCount = 0; dwCount < *lpServicesReturned; dwCount++)
+    if (dwError == ERROR_MORE_DATA)
     {
-        if (lpStatusPtr->lpServiceName)
-            lpStatusPtr->lpServiceName =
-                (LPWSTR)((ULONG_PTR)lpServices + (ULONG_PTR)lpStatusPtr->lpServiceName);
+        WARN("Required buffer size %ul\n", *pcbBytesNeeded);
+        SetLastError(dwError);
+        return FALSE;
+    }
+    else if (dwError == ERROR_SUCCESS)
+    {
+        lpStatusPtr = (LPENUM_SERVICE_STATUS_PROCESSW)lpServices;
+        for (dwCount = 0; dwCount < *lpServicesReturned; dwCount++)
+        {
+            if (lpStatusPtr->lpServiceName)
+                lpStatusPtr->lpServiceName =
+                    (LPWSTR)((ULONG_PTR)lpServices + (ULONG_PTR)lpStatusPtr->lpServiceName);
 
-        if (lpStatusPtr->lpDisplayName)
-            lpStatusPtr->lpDisplayName =
-                (LPWSTR)((ULONG_PTR)lpServices + (ULONG_PTR)lpStatusPtr->lpDisplayName);
+            if (lpStatusPtr->lpDisplayName)
+                lpStatusPtr->lpDisplayName =
+                    (LPWSTR)((ULONG_PTR)lpServices + (ULONG_PTR)lpStatusPtr->lpDisplayName);
 
-        lpStatusPtr++;
+            lpStatusPtr++;
+        }
     }
-
-    if (dwError != ERROR_SUCCESS)
+    else
     {
-        DPRINT1("ScmrEnumServicesStatusExW() failed (Error %lu)\n", dwError);
+        ERR("REnumServicesStatusExW() failed (Error %lu)\n", dwError);
         SetLastError(dwError);
         return FALSE;
     }
 
-    DPRINT("EnumServicesStatusExW() done\n");
+    TRACE("EnumServicesStatusExW() done\n");
 
     return TRUE;
 }
@@ -1026,32 +1282,57 @@ EnumServicesStatusExW(SC_HANDLE hSCManager,
  *
  * @implemented
  */
-BOOL STDCALL
+BOOL WINAPI
 GetServiceDisplayNameA(SC_HANDLE hSCManager,
                        LPCSTR lpServiceName,
                        LPSTR lpDisplayName,
                        LPDWORD lpcchBuffer)
 {
     DWORD dwError;
+    LPSTR lpNameBuffer;
+    CHAR szEmptyName[] = "";
 
-    DPRINT("GetServiceDisplayNameA() called\n");
+    TRACE("GetServiceDisplayNameA() called\n");
+    TRACE("%p %s %p %p\n", hSCManager,
+          debugstr_a(lpServiceName), lpDisplayName, lpcchBuffer);
 
-    HandleBind();
+    if (!hSCManager)
+    {
+        SetLastError(ERROR_INVALID_HANDLE);
+        return FALSE;
+    }
+
+    if (!lpDisplayName || *lpcchBuffer < sizeof(CHAR))
+    {
+        lpNameBuffer = szEmptyName;
+        *lpcchBuffer = sizeof(CHAR);
+    }
+    else
+    {
+        lpNameBuffer = lpDisplayName;
+    }
+
+    RpcTryExcept
+    {
+        dwError = RGetServiceDisplayNameA((SC_RPC_HANDLE)hSCManager,
+                                          lpServiceName,
+                                          lpNameBuffer,
+                                          lpcchBuffer);
+    }
+    RpcExcept(EXCEPTION_EXECUTE_HANDLER)
+    {
+        /* HACK: because of a problem with rpcrt4, rpcserver is hacked to return 6 for ERROR_SERVICE_DOES_NOT_EXIST */
+        dwError = ScmRpcStatusToWinError(RpcExceptionCode());
+    }
+    RpcEndExcept;
 
-    dwError = ScmrGetServiceDisplayNameA(BindingHandle,
-                                         (unsigned int)hSCManager,
-                                         (LPSTR)lpServiceName,
-                                         lpDisplayName,
-                                         lpcchBuffer);
     if (dwError != ERROR_SUCCESS)
     {
-        DPRINT1("ScmrGetServiceDisplayNameA() failed (Error %lu)\n", dwError);
+        ERR("RGetServiceDisplayNameA() failed (Error %lu)\n", dwError);
         SetLastError(dwError);
         return FALSE;
     }
 
-    (*lpcchBuffer)--;
-
     return TRUE;
 }
 
@@ -1061,32 +1342,54 @@ GetServiceDisplayNameA(SC_HANDLE hSCManager,
  *
  * @implemented
  */
-BOOL STDCALL
+BOOL WINAPI
 GetServiceDisplayNameW(SC_HANDLE hSCManager,
                        LPCWSTR lpServiceName,
                        LPWSTR lpDisplayName,
                        LPDWORD lpcchBuffer)
 {
     DWORD dwError;
+    LPWSTR lpNameBuffer;
+    WCHAR szEmptyName[] = L"";
 
-    DPRINT("GetServiceDisplayNameW() called\n");
+    TRACE("GetServiceDisplayNameW() called\n");
 
-    HandleBind();
+    if (!hSCManager)
+    {
+        SetLastError(ERROR_INVALID_HANDLE);
+        return FALSE;
+    }
+
+    if (!lpDisplayName || *lpcchBuffer < sizeof(WCHAR))
+    {
+        lpNameBuffer = szEmptyName;
+        *lpcchBuffer = sizeof(WCHAR);
+    }
+    else
+    {
+        lpNameBuffer = lpDisplayName;
+    }
+
+    RpcTryExcept
+    {
+        dwError = RGetServiceDisplayNameW((SC_RPC_HANDLE)hSCManager,
+                                          lpServiceName,
+                                          lpNameBuffer,
+                                          lpcchBuffer);
+    }
+    RpcExcept(EXCEPTION_EXECUTE_HANDLER)
+    {
+        dwError = ScmRpcStatusToWinError(RpcExceptionCode());
+    }
+    RpcEndExcept;
 
-    dwError = ScmrGetServiceDisplayNameW(BindingHandle,
-                                         (unsigned int)hSCManager,
-                                         (LPWSTR)lpServiceName,
-                                         lpDisplayName,
-                                         lpcchBuffer);
     if (dwError != ERROR_SUCCESS)
     {
-        DPRINT1("ScmrGetServiceDisplayNameW() failed (Error %lu)\n", dwError);
+        ERR("RGetServiceDisplayNameW() failed (Error %lu)\n", dwError);
         SetLastError(dwError);
         return FALSE;
     }
 
-    (*lpcchBuffer)--;
-
     return TRUE;
 }
 
@@ -1096,32 +1399,54 @@ GetServiceDisplayNameW(SC_HANDLE hSCManager,
  *
  * @implemented
  */
-BOOL STDCALL
+BOOL WINAPI
 GetServiceKeyNameA(SC_HANDLE hSCManager,
                    LPCSTR lpDisplayName,
                    LPSTR lpServiceName,
                    LPDWORD lpcchBuffer)
 {
     DWORD dwError;
+    LPSTR lpNameBuffer;
+    CHAR szEmptyName[] = "";
 
-    DPRINT("GetServiceKeyNameA() called\n");
+    TRACE("GetServiceKeyNameA() called\n");
+
+    if (!hSCManager)
+    {
+        SetLastError(ERROR_INVALID_HANDLE);
+        return FALSE;
+    }
+
+    if (!lpServiceName || *lpcchBuffer < sizeof(CHAR))
+    {
+        lpNameBuffer = szEmptyName;
+        *lpcchBuffer = sizeof(CHAR);
+    }
+    else
+    {
+        lpNameBuffer = lpServiceName;
+    }
 
-    HandleBind();
+    RpcTryExcept
+    {
+        dwError = RGetServiceKeyNameA((SC_RPC_HANDLE)hSCManager,
+                                      lpDisplayName,
+                                      lpNameBuffer,
+                                      lpcchBuffer);
+    }
+    RpcExcept(EXCEPTION_EXECUTE_HANDLER)
+    {
+        dwError = ScmRpcStatusToWinError(RpcExceptionCode());
+    }
+    RpcEndExcept;
 
-    dwError = ScmrGetServiceKeyNameA(BindingHandle,
-                                     (unsigned int)hSCManager,
-                                     (LPSTR)lpDisplayName,
-                                     lpServiceName,
-                                     lpcchBuffer);
     if (dwError != ERROR_SUCCESS)
     {
-        DPRINT1("ScmrGetServiceKeyNameA() failed (Error %lu)\n", dwError);
+        ERR("RGetServiceKeyNameA() failed (Error %lu)\n", dwError);
         SetLastError(dwError);
         return FALSE;
     }
 
-    (*lpcchBuffer)--;
-
     return TRUE;
 }
 
@@ -1131,32 +1456,54 @@ GetServiceKeyNameA(SC_HANDLE hSCManager,
  *
  * @implemented
  */
-BOOL STDCALL
+BOOL WINAPI
 GetServiceKeyNameW(SC_HANDLE hSCManager,
                    LPCWSTR lpDisplayName,
                    LPWSTR lpServiceName,
                    LPDWORD lpcchBuffer)
 {
     DWORD dwError;
+    LPWSTR lpNameBuffer;
+    WCHAR szEmptyName[] = L"";
+
+    TRACE("GetServiceKeyNameW() called\n");
 
-    DPRINT("GetServiceKeyNameW() called\n");
+    if (!hSCManager)
+    {
+        SetLastError(ERROR_INVALID_HANDLE);
+        return FALSE;
+    }
+
+    if (!lpServiceName || *lpcchBuffer < sizeof(WCHAR))
+    {
+        lpNameBuffer = szEmptyName;
+        *lpcchBuffer = sizeof(WCHAR);
+    }
+    else
+    {
+        lpNameBuffer = lpServiceName;
+    }
 
-    HandleBind();
+    RpcTryExcept
+    {
+        dwError = RGetServiceKeyNameW((SC_RPC_HANDLE)hSCManager,
+                                      lpDisplayName,
+                                      lpNameBuffer,
+                                      lpcchBuffer);
+    }
+    RpcExcept(EXCEPTION_EXECUTE_HANDLER)
+    {
+        dwError = ScmRpcStatusToWinError(RpcExceptionCode());
+    }
+    RpcEndExcept;
 
-    dwError = ScmrGetServiceKeyNameW(BindingHandle,
-                                     (unsigned int)hSCManager,
-                                     (LPWSTR)lpDisplayName,
-                                     lpServiceName,
-                                     lpcchBuffer);
     if (dwError != ERROR_SUCCESS)
     {
-        DPRINT1("ScmrGetServiceKeyNameW() failed (Error %lu)\n", dwError);
+        ERR("RGetServiceKeyNameW() failed (Error %lu)\n", dwError);
         SetLastError(dwError);
         return FALSE;
     }
 
-    (*lpcchBuffer)--;
-
     return TRUE;
 }
 
@@ -1166,28 +1513,34 @@ GetServiceKeyNameW(SC_HANDLE hSCManager,
  *
  * @implemented
  */
-SC_LOCK STDCALL
+SC_LOCK WINAPI
 LockServiceDatabase(SC_HANDLE hSCManager)
 {
     SC_LOCK hLock;
     DWORD dwError;
 
-    DPRINT("LockServiceDatabase(%x)\n", hSCManager);
+    TRACE("LockServiceDatabase(%x)\n", hSCManager);
 
-    HandleBind();
+    RpcTryExcept
+    {
+        /* Call to services.exe using RPC */
+        dwError = RLockServiceDatabase((SC_RPC_HANDLE)hSCManager,
+                                       (SC_RPC_LOCK *)&hLock);
+    }
+    RpcExcept(EXCEPTION_EXECUTE_HANDLER)
+    {
+        dwError = ScmRpcStatusToWinError(RpcExceptionCode());
+    }
+    RpcEndExcept;
 
-    /* Call to services.exe using RPC */
-    dwError = ScmrLockServiceDatabase(BindingHandle,
-                                      (unsigned int)hSCManager,
-                                      (unsigned int *)&hLock);
     if (dwError != ERROR_SUCCESS)
     {
-        DPRINT1("ScmrLockServiceDatabase() failed (Error %lu)\n", dwError);
+        ERR("RLockServiceDatabase() failed (Error %lu)\n", dwError);
         SetLastError(dwError);
         return NULL;
     }
 
-    DPRINT("hLock = %p\n", hLock);
+    TRACE("hLock = %p\n", hLock);
 
     return hLock;
 }
@@ -1198,12 +1551,12 @@ WaitForSCManager(VOID)
 {
     HANDLE hEvent;
 
-    DPRINT("WaitForSCManager() called\n");
+    TRACE("WaitForSCManager() called\n");
 
     /* Try to open the existing event */
     hEvent = OpenEventW(SYNCHRONIZE,
                         FALSE,
-                        L"SvcctrlStartEvent_A3725DX");
+                        L"SvcctrlStartEvent_A3752DX");
     if (hEvent == NULL)
     {
         if (GetLastError() != ERROR_FILE_NOT_FOUND)
@@ -1213,13 +1566,13 @@ WaitForSCManager(VOID)
         hEvent = CreateEventW(NULL,
                               TRUE,
                               FALSE,
-                              L"SvcctrlStartEvent_A3725DX");
+                              L"SvcctrlStartEvent_A3752DX");
         if (hEvent == NULL)
         {
             /* Try to open the existing event again */
             hEvent = OpenEventW(SYNCHRONIZE,
                                 FALSE,
-                                L"SvcctrlStartEvent_A3725DX");
+                                L"SvcctrlStartEvent_A3752DX");
             if (hEvent == NULL)
                 return;
         }
@@ -1229,7 +1582,7 @@ WaitForSCManager(VOID)
     WaitForSingleObject(hEvent, 180000);
     CloseHandle(hEvent);
 
-    DPRINT("ScmWaitForSCManager() done\n");
+    TRACE("ScmWaitForSCManager() done\n");
 }
 
 
@@ -1238,7 +1591,7 @@ WaitForSCManager(VOID)
  *
  * @implemented
  */
-SC_HANDLE STDCALL
+SC_HANDLE WINAPI
 OpenSCManagerA(LPCSTR lpMachineName,
                LPCSTR lpDatabaseName,
                DWORD dwDesiredAccess)
@@ -1246,27 +1599,33 @@ OpenSCManagerA(LPCSTR lpMachineName,
     SC_HANDLE hScm = NULL;
     DWORD dwError;
 
-    DPRINT("OpenSCManagerA(%s, %s, %lx)\n",
+    TRACE("OpenSCManagerA(%s, %s, %lx)\n",
            lpMachineName, lpDatabaseName, dwDesiredAccess);
 
     WaitForSCManager();
 
-    HandleBind();
+    RpcTryExcept
+    {
+        /* Call to services.exe using RPC */
+        dwError = ROpenSCManagerA((LPSTR)lpMachineName,
+                                  (LPSTR)lpDatabaseName,
+                                  dwDesiredAccess,
+                                  (SC_RPC_HANDLE *)&hScm);
+    }
+    RpcExcept(EXCEPTION_EXECUTE_HANDLER)
+    {
+        dwError = ScmRpcStatusToWinError(RpcExceptionCode());
+    }
+    RpcEndExcept;
 
-    /* Call to services.exe using RPC */
-    dwError = ScmrOpenSCManagerA(BindingHandle,
-                                 (LPSTR)lpMachineName,
-                                 (LPSTR)lpDatabaseName,
-                                 dwDesiredAccess,
-                                 (unsigned int*)&hScm);
     if (dwError != ERROR_SUCCESS)
     {
-        DPRINT1("ScmrOpenSCManagerA() failed (Error %lu)\n", dwError);
+        ERR("ROpenSCManagerA() failed (Error %lu)\n", dwError);
         SetLastError(dwError);
         return NULL;
     }
 
-    DPRINT("hScm = %p\n", hScm);
+    TRACE("hScm = %p\n", hScm);
 
     return hScm;
 }
@@ -1277,7 +1636,7 @@ OpenSCManagerA(LPCSTR lpMachineName,
  *
  * @implemented
  */
-SC_HANDLE STDCALL
+SC_HANDLE WINAPI
 OpenSCManagerW(LPCWSTR lpMachineName,
                LPCWSTR lpDatabaseName,
                DWORD dwDesiredAccess)
@@ -1285,27 +1644,33 @@ OpenSCManagerW(LPCWSTR lpMachineName,
     SC_HANDLE hScm = NULL;
     DWORD dwError;
 
-    DPRINT("OpenSCManagerW(%S, %S, %lx)\n",
+    TRACE("OpenSCManagerW(%S, %S, %lx)\n",
            lpMachineName, lpDatabaseName, dwDesiredAccess);
 
     WaitForSCManager();
 
-    HandleBind();
+    RpcTryExcept
+    {
+        /* Call to services.exe using RPC */
+        dwError = ROpenSCManagerW((LPWSTR)lpMachineName,
+                                  (LPWSTR)lpDatabaseName,
+                                  dwDesiredAccess,
+                                  (SC_RPC_HANDLE *)&hScm);
+    }
+    RpcExcept(EXCEPTION_EXECUTE_HANDLER)
+    {
+        dwError = ScmRpcStatusToWinError(RpcExceptionCode());
+    }
+    RpcEndExcept;
 
-    /* Call to services.exe using RPC */
-    dwError = ScmrOpenSCManagerW(BindingHandle,
-                                 (LPWSTR)lpMachineName,
-                                 (LPWSTR)lpDatabaseName,
-                                 dwDesiredAccess,
-                                 (unsigned int*)&hScm);
     if (dwError != ERROR_SUCCESS)
     {
-        DPRINT1("ScmrOpenSCManagerW() failed (Error %lu)\n", dwError);
+        ERR("ROpenSCManagerW() failed (Error %lu)\n", dwError);
         SetLastError(dwError);
         return NULL;
     }
 
-    DPRINT("hScm = %p\n", hScm);
+    TRACE("hScm = %p\n", hScm);
 
     return hScm;
 }
@@ -1316,7 +1681,7 @@ OpenSCManagerW(LPCWSTR lpMachineName,
  *
  * @implemented
  */
-SC_HANDLE STDCALL
+SC_HANDLE WINAPI
 OpenServiceA(SC_HANDLE hSCManager,
              LPCSTR lpServiceName,
              DWORD dwDesiredAccess)
@@ -1324,25 +1689,37 @@ OpenServiceA(SC_HANDLE hSCManager,
     SC_HANDLE hService = NULL;
     DWORD dwError;
 
-    DPRINT("OpenServiceA(%p, %s, %lx)\n",
+    TRACE("OpenServiceA(%p, %s, %lx)\n",
            hSCManager, lpServiceName, dwDesiredAccess);
 
-    HandleBind();
+    if (!hSCManager)
+    {
+        SetLastError(ERROR_INVALID_HANDLE);
+        return NULL;
+    }
+
+    RpcTryExcept
+    {
+        /* Call to services.exe using RPC */
+        dwError = ROpenServiceA((SC_RPC_HANDLE)hSCManager,
+                                (LPSTR)lpServiceName,
+                                dwDesiredAccess,
+                                (SC_RPC_HANDLE *)&hService);
+    }
+    RpcExcept(EXCEPTION_EXECUTE_HANDLER)
+    {
+        dwError = ScmRpcStatusToWinError(RpcExceptionCode());
+    }
+    RpcEndExcept;
 
-    /* Call to services.exe using RPC */
-    dwError = ScmrOpenServiceA(BindingHandle,
-                               (unsigned int)hSCManager,
-                               (LPSTR)lpServiceName,
-                               dwDesiredAccess,
-                               (unsigned int*)&hService);
     if (dwError != ERROR_SUCCESS)
     {
-        DPRINT1("ScmrOpenServiceA() failed (Error %lu)\n", dwError);
+        ERR("ROpenServiceA() failed (Error %lu)\n", dwError);
         SetLastError(dwError);
         return NULL;
     }
 
-    DPRINT("hService = %p\n", hService);
+    TRACE("hService = %p\n", hService);
 
     return hService;
 }
@@ -1353,7 +1730,7 @@ OpenServiceA(SC_HANDLE hSCManager,
  *
  * @implemented
  */
-SC_HANDLE STDCALL
+SC_HANDLE WINAPI
 OpenServiceW(SC_HANDLE hSCManager,
              LPCWSTR lpServiceName,
              DWORD dwDesiredAccess)
@@ -1361,25 +1738,40 @@ OpenServiceW(SC_HANDLE hSCManager,
     SC_HANDLE hService = NULL;
     DWORD dwError;
 
-    DPRINT("OpenServiceW(%p, %S, %lx)\n",
+    TRACE("OpenServiceW(%p, %S, %lx)\n",
            hSCManager, lpServiceName, dwDesiredAccess);
 
-    HandleBind();
+    if (!hSCManager)
+    {
+        SetLastError(ERROR_INVALID_HANDLE);
+        return NULL;
+    }
+
+    RpcTryExcept
+    {
+        /* Call to services.exe using RPC */
+        dwError = ROpenServiceW((SC_RPC_HANDLE)hSCManager,
+                                (LPWSTR)lpServiceName,
+                                dwDesiredAccess,
+                                (SC_RPC_HANDLE *)&hService);
+    }
+    RpcExcept(EXCEPTION_EXECUTE_HANDLER)
+    {
+        dwError = ScmRpcStatusToWinError(RpcExceptionCode());
+    }
+    RpcEndExcept;
 
-    /* Call to services.exe using RPC */
-    dwError = ScmrOpenServiceW(BindingHandle,
-                               (unsigned int)hSCManager,
-                               (LPWSTR)lpServiceName,
-                               dwDesiredAccess,
-                               (unsigned int*)&hService);
     if (dwError != ERROR_SUCCESS)
     {
-        DPRINT("ScmrOpenServiceW() failed (Error %lu)\n", dwError);
+        if (dwError == ERROR_SERVICE_DOES_NOT_EXIST)
+            WARN("ROpenServiceW() failed (Error %lu)\n", dwError);
+        else
+            ERR("ROpenServiceW() failed (Error %lu)\n", dwError);
         SetLastError(dwError);
         return NULL;
     }
 
-    DPRINT("hService = %p\n", hService);
+    TRACE("hService = %p\n", hService);
 
     return hService;
 }
@@ -1390,7 +1782,7 @@ OpenServiceW(SC_HANDLE hSCManager,
  *
  * @implemented
  */
-BOOL STDCALL
+BOOL WINAPI
 QueryServiceConfigA(SC_HANDLE hService,
                     LPQUERY_SERVICE_CONFIGA lpServiceConfig,
                     DWORD cbBufSize,
@@ -1398,20 +1790,26 @@ QueryServiceConfigA(SC_HANDLE hService,
 {
     DWORD dwError;
 
-    DPRINT("QueryServiceConfigA(%p, %p, %lu, %p)\n",
+    TRACE("QueryServiceConfigA(%p, %p, %lu, %p)\n",
            hService, lpServiceConfig, cbBufSize, pcbBytesNeeded);
 
-    HandleBind();
+    RpcTryExcept
+    {
+        /* Call to services.exe using RPC */
+        dwError = RQueryServiceConfigA((SC_RPC_HANDLE)hService,
+                                       (LPBYTE)lpServiceConfig,
+                                       cbBufSize,
+                                       pcbBytesNeeded);
+    }
+    RpcExcept(EXCEPTION_EXECUTE_HANDLER)
+    {
+        dwError = ScmRpcStatusToWinError(RpcExceptionCode());
+    }
+    RpcEndExcept;
 
-    /* Call to services.exe using RPC */
-    dwError = ScmrQueryServiceConfigA(BindingHandle,
-                                      (unsigned int)hService,
-                                      (unsigned char *)lpServiceConfig,
-                                      cbBufSize,
-                                      pcbBytesNeeded);
     if (dwError != ERROR_SUCCESS)
     {
-        DPRINT("ScmrQueryServiceConfigA() failed (Error %lu)\n", dwError);
+        ERR("RQueryServiceConfigA() failed (Error %lu)\n", dwError);
         SetLastError(dwError);
         return FALSE;
     }
@@ -1442,7 +1840,7 @@ QueryServiceConfigA(SC_HANDLE hService,
            (LPSTR)((ULONG_PTR)lpServiceConfig +
                    (ULONG_PTR)lpServiceConfig->lpDisplayName);
 
-    DPRINT("QueryServiceConfigA() done\n");
+    TRACE("QueryServiceConfigA() done\n");
 
     return TRUE;
 }
@@ -1453,7 +1851,7 @@ QueryServiceConfigA(SC_HANDLE hService,
  *
  * @implemented
  */
-BOOL STDCALL
+BOOL WINAPI
 QueryServiceConfigW(SC_HANDLE hService,
                     LPQUERY_SERVICE_CONFIGW lpServiceConfig,
                     DWORD cbBufSize,
@@ -1461,20 +1859,29 @@ QueryServiceConfigW(SC_HANDLE hService,
 {
     DWORD dwError;
 
-    DPRINT("QueryServiceConfigW(%p, %p, %lu, %p)\n",
+    TRACE("QueryServiceConfigW(%p, %p, %lu, %p)\n",
            hService, lpServiceConfig, cbBufSize, pcbBytesNeeded);
 
-    HandleBind();
+    RpcTryExcept
+    {
+        /* Call to services.exe using RPC */
+        dwError = RQueryServiceConfigW((SC_RPC_HANDLE)hService,
+                                       (LPBYTE)lpServiceConfig,
+                                       cbBufSize,
+                                       pcbBytesNeeded);
+    }
+    RpcExcept(EXCEPTION_EXECUTE_HANDLER)
+    {
+        dwError = ScmRpcStatusToWinError(RpcExceptionCode());
+    }
+    RpcEndExcept;
 
-    /* Call to services.exe using RPC */
-    dwError = ScmrQueryServiceConfigW(BindingHandle,
-                                      (unsigned int)hService,
-                                      (unsigned char *)lpServiceConfig,
-                                      cbBufSize,
-                                      pcbBytesNeeded);
     if (dwError != ERROR_SUCCESS)
     {
-        DPRINT("ScmrQueryServiceConfigW() failed (Error %lu)\n", dwError);
+        if (dwError == ERROR_INSUFFICIENT_BUFFER)
+            WARN("RQueryServiceConfigW() failed (Error %lu)\n", dwError);
+        else
+            ERR("RQueryServiceConfigW() failed (Error %lu)\n", dwError);
         SetLastError(dwError);
         return FALSE;
     }
@@ -1505,7 +1912,7 @@ QueryServiceConfigW(SC_HANDLE hService,
            (LPWSTR)((ULONG_PTR)lpServiceConfig +
                     (ULONG_PTR)lpServiceConfig->lpDisplayName);
 
-    DPRINT("QueryServiceConfigW() done\n");
+    TRACE("QueryServiceConfigW() done\n");
 
     return TRUE;
 }
@@ -1516,7 +1923,7 @@ QueryServiceConfigW(SC_HANDLE hService,
  *
  * @implemented
  */
-BOOL STDCALL
+BOOL WINAPI
 QueryServiceConfig2A(SC_HANDLE hService,
                      DWORD dwInfoLevel,
                      LPBYTE lpBuffer,
@@ -1525,21 +1932,41 @@ QueryServiceConfig2A(SC_HANDLE hService,
 {
     DWORD dwError;
 
-    DPRINT("QueryServiceConfig2A(%p, %lu, %p, %lu, %p)\n",
+    DbgPrint("QueryServiceConfig2A(hService %p, dwInfoLevel %lu, lpBuffer %p, cbBufSize %lu, pcbBytesNeeded %p)\n",
            hService, dwInfoLevel, lpBuffer, cbBufSize, pcbBytesNeeded);
 
-    HandleBind();
+    if (dwInfoLevel != SERVICE_CONFIG_DESCRIPTION &&
+        dwInfoLevel != SERVICE_CONFIG_FAILURE_ACTIONS)
+    {
+        SetLastError(ERROR_INVALID_LEVEL);
+        return FALSE;
+    }
+
+    if ((lpBuffer == NULL && cbBufSize != 0) ||
+        pcbBytesNeeded == NULL)
+    {
+        SetLastError(ERROR_INVALID_ADDRESS);
+        return FALSE;
+    }
+
+    RpcTryExcept
+    {
+        /* Call to services.exe using RPC */
+        dwError = RQueryServiceConfig2A((SC_RPC_HANDLE)hService,
+                                        dwInfoLevel,
+                                        lpBuffer,
+                                        cbBufSize,
+                                        pcbBytesNeeded);
+    }
+    RpcExcept(EXCEPTION_EXECUTE_HANDLER)
+    {
+        dwError = ScmRpcStatusToWinError(RpcExceptionCode());
+    }
+    RpcEndExcept;
 
-    /* Call to services.exe using RPC */
-    dwError = ScmrQueryServiceConfig2A(BindingHandle,
-                                       (unsigned int)hService,
-                                       dwInfoLevel,
-                                       (unsigned char *)lpBuffer,
-                                       cbBufSize,
-                                       pcbBytesNeeded);
     if (dwError != ERROR_SUCCESS)
     {
-        DPRINT("ScmrQueryServiceConfig2A() failed (Error %lu)\n", dwError);
+        ERR("RQueryServiceConfig2A() failed (Error %lu)\n", dwError);
         SetLastError(dwError);
         return FALSE;
     }
@@ -1575,12 +2002,12 @@ QueryServiceConfig2A(SC_HANDLE hService,
             break;
 
         default:
-            DPRINT1("Unknown info level 0x%lx\n", dwInfoLevel);
+            ERR("Unknown info level 0x%lx\n", dwInfoLevel);
             SetLastError(ERROR_INVALID_PARAMETER);
             return FALSE;
     }
 
-    DPRINT("QueryServiceConfig2A() done\n");
+    TRACE("QueryServiceConfig2A() done\n");
 
     return TRUE;
 }
@@ -1591,7 +2018,7 @@ QueryServiceConfig2A(SC_HANDLE hService,
  *
  * @implemented
  */
-BOOL STDCALL
+BOOL WINAPI
 QueryServiceConfig2W(SC_HANDLE hService,
                      DWORD dwInfoLevel,
                      LPBYTE lpBuffer,
@@ -1600,21 +2027,41 @@ QueryServiceConfig2W(SC_HANDLE hService,
 {
     DWORD dwError;
 
-    DPRINT("QueryServiceConfig2W(%p, %lu, %p, %lu, %p)\n",
+    TRACE("QueryServiceConfig2W(%p, %lu, %p, %lu, %p)\n",
            hService, dwInfoLevel, lpBuffer, cbBufSize, pcbBytesNeeded);
 
-    HandleBind();
+    if (dwInfoLevel != SERVICE_CONFIG_DESCRIPTION &&
+        dwInfoLevel != SERVICE_CONFIG_FAILURE_ACTIONS)
+    {
+        SetLastError(ERROR_INVALID_LEVEL);
+        return FALSE;
+    }
+
+    if ((lpBuffer == NULL && cbBufSize != 0) ||
+        pcbBytesNeeded == NULL)
+    {
+        SetLastError(ERROR_INVALID_ADDRESS);
+        return FALSE;
+    }
+
+    RpcTryExcept
+    {
+        /* Call to services.exe using RPC */
+        dwError = RQueryServiceConfig2W((SC_RPC_HANDLE)hService,
+                                        dwInfoLevel,
+                                        lpBuffer,
+                                        cbBufSize,
+                                        pcbBytesNeeded);
+    }
+    RpcExcept(EXCEPTION_EXECUTE_HANDLER)
+    {
+        dwError = ScmRpcStatusToWinError(RpcExceptionCode());
+    }
+    RpcEndExcept;
 
-    /* Call to services.exe using RPC */
-    dwError = ScmrQueryServiceConfig2W(BindingHandle,
-                                       (unsigned int)hService,
-                                       dwInfoLevel,
-                                       (unsigned char *)lpBuffer,
-                                       cbBufSize,
-                                       pcbBytesNeeded);
     if (dwError != ERROR_SUCCESS)
     {
-        DPRINT("ScmrQueryServiceConfig2W() failed (Error %lu)\n", dwError);
+        ERR("RQueryServiceConfig2W() failed (Error %lu)\n", dwError);
         SetLastError(dwError);
         return FALSE;
     }
@@ -1650,12 +2097,12 @@ QueryServiceConfig2W(SC_HANDLE hService,
             break;
 
         default:
-            DPRINT1("Unknown info level 0x%lx\n", dwInfoLevel);
+            WARN("Unknown info level 0x%lx\n", dwInfoLevel);
             SetLastError(ERROR_INVALID_PARAMETER);
             return FALSE;
     }
 
-    DPRINT("QueryServiceConfig2W() done\n");
+    TRACE("QueryServiceConfig2W() done\n");
 
     return TRUE;
 }
@@ -1666,7 +2113,7 @@ QueryServiceConfig2W(SC_HANDLE hService,
  *
  * @implemented
  */
-BOOL STDCALL
+BOOL WINAPI
 QueryServiceLockStatusA(SC_HANDLE hSCManager,
                         LPQUERY_SERVICE_LOCK_STATUSA lpLockStatus,
                         DWORD cbBufSize,
@@ -1674,19 +2121,25 @@ QueryServiceLockStatusA(SC_HANDLE hSCManager,
 {
     DWORD dwError;
 
-    DPRINT("QueryServiceLockStatusA() called\n");
+    TRACE("QueryServiceLockStatusA() called\n");
 
-    HandleBind();
+    RpcTryExcept
+    {
+        /* Call to services.exe using RPC */
+        dwError = RQueryServiceLockStatusA((SC_RPC_HANDLE)hSCManager,
+                                           lpLockStatus,
+                                           cbBufSize,
+                                           pcbBytesNeeded);
+    }
+    RpcExcept(EXCEPTION_EXECUTE_HANDLER)
+    {
+        dwError = ScmRpcStatusToWinError(RpcExceptionCode());
+    }
+    RpcEndExcept;
 
-    /* Call to services.exe using RPC */
-    dwError = ScmrQueryServiceLockStatusA(BindingHandle,
-                                          (unsigned int)hSCManager,
-                                          (unsigned char *)lpLockStatus,
-                                          cbBufSize,
-                                          pcbBytesNeeded);
     if (dwError != ERROR_SUCCESS)
     {
-        DPRINT("ScmrQueryServiceLockStatusA() failed (Error %lu)\n", dwError);
+        ERR("RQueryServiceLockStatusA() failed (Error %lu)\n", dwError);
         SetLastError(dwError);
         return FALSE;
     }
@@ -1697,7 +2150,7 @@ QueryServiceLockStatusA(SC_HANDLE hSCManager,
             (LPSTR)((UINT_PTR)lpLockStatus + (UINT_PTR)lpLockStatus->lpLockOwner);
     }
 
-    DPRINT("QueryServiceLockStatusA() done\n");
+    TRACE("QueryServiceLockStatusA() done\n");
 
     return TRUE;
 }
@@ -1708,7 +2161,7 @@ QueryServiceLockStatusA(SC_HANDLE hSCManager,
  *
  * @implemented
  */
-BOOL STDCALL
+BOOL WINAPI
 QueryServiceLockStatusW(SC_HANDLE hSCManager,
                         LPQUERY_SERVICE_LOCK_STATUSW lpLockStatus,
                         DWORD cbBufSize,
@@ -1716,19 +2169,25 @@ QueryServiceLockStatusW(SC_HANDLE hSCManager,
 {
     DWORD dwError;
 
-    DPRINT("QueryServiceLockStatusW() called\n");
+    TRACE("QueryServiceLockStatusW() called\n");
 
-    HandleBind();
+    RpcTryExcept
+    {
+        /* Call to services.exe using RPC */
+        dwError = RQueryServiceLockStatusW((SC_RPC_HANDLE)hSCManager,
+                                           lpLockStatus,
+                                           cbBufSize,
+                                           pcbBytesNeeded);
+    }
+    RpcExcept(EXCEPTION_EXECUTE_HANDLER)
+    {
+        dwError = ScmRpcStatusToWinError(RpcExceptionCode());
+    }
+    RpcEndExcept;
 
-    /* Call to services.exe using RPC */
-    dwError = ScmrQueryServiceLockStatusW(BindingHandle,
-                                          (unsigned int)hSCManager,
-                                          (unsigned char *)lpLockStatus,
-                                          cbBufSize,
-                                          pcbBytesNeeded);
     if (dwError != ERROR_SUCCESS)
     {
-        DPRINT("ScmrQueryServiceLockStatusW() failed (Error %lu)\n", dwError);
+        ERR("RQueryServiceLockStatusW() failed (Error %lu)\n", dwError);
         SetLastError(dwError);
         return FALSE;
     }
@@ -1739,7 +2198,7 @@ QueryServiceLockStatusW(SC_HANDLE hSCManager,
             (LPWSTR)((UINT_PTR)lpLockStatus + (UINT_PTR)lpLockStatus->lpLockOwner);
     }
 
-    DPRINT("QueryServiceLockStatusW() done\n");
+    TRACE("QueryServiceLockStatusW() done\n");
 
     return TRUE;
 }
@@ -1750,7 +2209,7 @@ QueryServiceLockStatusW(SC_HANDLE hSCManager,
  *
  * @implemented
  */
-BOOL STDCALL
+BOOL WINAPI
 QueryServiceObjectSecurity(SC_HANDLE hService,
                            SECURITY_INFORMATION dwSecurityInformation,
                            PSECURITY_DESCRIPTOR lpSecurityDescriptor,
@@ -1759,21 +2218,27 @@ QueryServiceObjectSecurity(SC_HANDLE hService,
 {
     DWORD dwError;
 
-    DPRINT("QueryServiceObjectSecurity(%p, %lu, %p)\n",
+    TRACE("QueryServiceObjectSecurity(%p, %lu, %p)\n",
            hService, dwSecurityInformation, lpSecurityDescriptor);
 
-    HandleBind();
+    RpcTryExcept
+    {
+        /* Call to services.exe using RPC */
+        dwError = RQueryServiceObjectSecurity((SC_RPC_HANDLE)hService,
+                                              dwSecurityInformation,
+                                              (LPBYTE)lpSecurityDescriptor,
+                                              cbBufSize,
+                                              pcbBytesNeeded);
+    }
+    RpcExcept(EXCEPTION_EXECUTE_HANDLER)
+    {
+        dwError = ScmRpcStatusToWinError(RpcExceptionCode());
+    }
+    RpcEndExcept;
 
-    /* Call to services.exe using RPC */
-    dwError = ScmrQueryServiceObjectSecurity(BindingHandle,
-                                             (unsigned int)hService,
-                                             dwSecurityInformation,
-                                             (unsigned char *)lpSecurityDescriptor,
-                                             cbBufSize,
-                                             pcbBytesNeeded);
     if (dwError != ERROR_SUCCESS)
     {
-        DPRINT1("QueryServiceObjectSecurity() failed (Error %lu)\n", dwError);
+        ERR("QueryServiceObjectSecurity() failed (Error %lu)\n", dwError);
         SetLastError(dwError);
         return FALSE;
     }
@@ -1781,30 +2246,67 @@ QueryServiceObjectSecurity(SC_HANDLE hService,
     return TRUE;
 }
 
-
 /**********************************************************************
- *  QueryServiceStatus
+ *  SetServiceObjectSecurity
  *
  * @implemented
  */
-BOOL STDCALL
-QueryServiceStatus(SC_HANDLE hService,
-                   LPSERVICE_STATUS lpServiceStatus)
+BOOL WINAPI
+SetServiceObjectSecurity(SC_HANDLE hService,
+                         SECURITY_INFORMATION dwSecurityInformation,
+                         PSECURITY_DESCRIPTOR lpSecurityDescriptor)
 {
+    PSECURITY_DESCRIPTOR SelfRelativeSD = NULL;
+    ULONG Length;
+    NTSTATUS Status;
     DWORD dwError;
 
-    DPRINT("QueryServiceStatus(%p, %p)\n",
-           hService, lpServiceStatus);
+    Length = 0;
+    Status = RtlMakeSelfRelativeSD(lpSecurityDescriptor,
+                                   SelfRelativeSD,
+                                   &Length);
+    if (Status != STATUS_BUFFER_TOO_SMALL)
+    {
+        SetLastError(ERROR_INVALID_PARAMETER);
+        return FALSE;
+    }
+
+    SelfRelativeSD = HeapAlloc(GetProcessHeap(), 0, Length);
+    if (SelfRelativeSD == NULL)
+    {
+        SetLastError(ERROR_NOT_ENOUGH_MEMORY);
+        return FALSE;
+    }
+
+    Status = RtlMakeSelfRelativeSD(lpSecurityDescriptor,
+                                   SelfRelativeSD,
+                                   &Length);
+    if (!NT_SUCCESS(Status))
+    {
+        HeapFree(GetProcessHeap(), 0, SelfRelativeSD);
+        SetLastError(RtlNtStatusToDosError(Status));
+        return FALSE;
+    }
+
+    RpcTryExcept
+    {
+        /* Call to services.exe using RPC */
+        dwError = RSetServiceObjectSecurity((SC_RPC_HANDLE)hService,
+                                            dwSecurityInformation,
+                                            (LPBYTE)SelfRelativeSD,
+                                            Length);
+    }
+    RpcExcept(EXCEPTION_EXECUTE_HANDLER)
+    {
+        dwError = ScmRpcStatusToWinError(RpcExceptionCode());
+    }
+    RpcEndExcept;
 
-    HandleBind();
+    HeapFree(GetProcessHeap(), 0, SelfRelativeSD);
 
-    /* Call to services.exe using RPC */
-    dwError = ScmrQueryServiceStatus(BindingHandle,
-                                     (unsigned int)hService,
-                                     lpServiceStatus);
     if (dwError != ERROR_SUCCESS)
     {
-        DPRINT1("ScmrQueryServiceStatus() failed (Error %lu)\n", dwError);
+        ERR("RServiceObjectSecurity() failed (Error %lu)\n", dwError);
         SetLastError(dwError);
         return FALSE;
     }
@@ -1814,33 +2316,40 @@ QueryServiceStatus(SC_HANDLE hService,
 
 
 /**********************************************************************
- *  QueryServiceStatusEx
+ *  QueryServiceStatus
  *
  * @implemented
  */
-BOOL STDCALL
-QueryServiceStatusEx(SC_HANDLE hService,
-                     SC_STATUS_TYPE InfoLevel,
-                     LPBYTE lpBuffer,
-                     DWORD cbBufSize,
-                     LPDWORD pcbBytesNeeded)
+BOOL WINAPI
+QueryServiceStatus(SC_HANDLE hService,
+                   LPSERVICE_STATUS lpServiceStatus)
 {
     DWORD dwError;
 
-    DPRINT("QueryServiceStatusEx() called\n");
+    TRACE("QueryServiceStatus(%p, %p)\n",
+           hService, lpServiceStatus);
 
-    HandleBind();
+    if (!hService)
+    {
+        SetLastError(ERROR_INVALID_HANDLE);
+        return FALSE;
+    }
+
+    RpcTryExcept
+    {
+        /* Call to services.exe using RPC */
+        dwError = RQueryServiceStatus((SC_RPC_HANDLE)hService,
+                                      lpServiceStatus);
+    }
+    RpcExcept(EXCEPTION_EXECUTE_HANDLER)
+    {
+        dwError = ScmRpcStatusToWinError(RpcExceptionCode());
+    }
+    RpcEndExcept;
 
-    /* Call to services.exe using RPC */
-    dwError = ScmrQueryServiceStatusEx(BindingHandle,
-                                       (unsigned int)hService,
-                                       InfoLevel,
-                                       lpBuffer,
-                                       cbBufSize,
-                                       pcbBytesNeeded);
     if (dwError != ERROR_SUCCESS)
     {
-        DPRINT("ScmrQueryServiceStatusEx() failed (Error %lu)\n", dwError);
+        ERR("RQueryServiceStatus() failed (Error %lu)\n", dwError);
         SetLastError(dwError);
         return FALSE;
     }
@@ -1850,61 +2359,45 @@ QueryServiceStatusEx(SC_HANDLE hService,
 
 
 /**********************************************************************
- *  SetServiceObjectSecurity
+ *  QueryServiceStatusEx
  *
  * @implemented
  */
-BOOL STDCALL
-SetServiceObjectSecurity(SC_HANDLE hService,
-                         SECURITY_INFORMATION dwSecurityInformation,
-                         PSECURITY_DESCRIPTOR lpSecurityDescriptor)
+BOOL WINAPI
+QueryServiceStatusEx(SC_HANDLE hService,
+                     SC_STATUS_TYPE InfoLevel,
+                     LPBYTE lpBuffer,
+                     DWORD cbBufSize,
+                     LPDWORD pcbBytesNeeded)
 {
-    PSECURITY_DESCRIPTOR SelfRelativeSD = NULL;
-    ULONG Length;
-    NTSTATUS Status;
     DWORD dwError;
 
-    Length = 0;
-    Status = RtlMakeSelfRelativeSD(lpSecurityDescriptor,
-                                   SelfRelativeSD,
-                                   &Length);
-    if (Status != STATUS_BUFFER_TOO_SMALL)
+    TRACE("QueryServiceStatusEx() called\n");
+
+    if (InfoLevel != SC_STATUS_PROCESS_INFO)
     {
-        SetLastError(ERROR_INVALID_PARAMETER);
+        SetLastError(ERROR_INVALID_LEVEL);
         return FALSE;
     }
 
-    SelfRelativeSD = HeapAlloc(GetProcessHeap(), 0, Length);
-    if (SelfRelativeSD == NULL)
+    RpcTryExcept
     {
-        SetLastError(ERROR_NOT_ENOUGH_MEMORY);
-        return FALSE;
+        /* Call to services.exe using RPC */
+        dwError = RQueryServiceStatusEx((SC_RPC_HANDLE)hService,
+                                        InfoLevel,
+                                        lpBuffer,
+                                        cbBufSize,
+                                        pcbBytesNeeded);
     }
-
-    Status = RtlMakeSelfRelativeSD(lpSecurityDescriptor,
-                                   SelfRelativeSD,
-                                   &Length);
-    if (!NT_SUCCESS(Status))
+    RpcExcept(EXCEPTION_EXECUTE_HANDLER)
     {
-        HeapFree(GetProcessHeap(), 0, SelfRelativeSD);
-        SetLastError(RtlNtStatusToDosError(Status));
-        return FALSE;
+        dwError = ScmRpcStatusToWinError(RpcExceptionCode());
     }
-
-    HandleBind();
-
-    /* Call to services.exe using RPC */
-    dwError = ScmrSetServiceObjectSecurity(BindingHandle,
-                                           (unsigned int)hService,
-                                           dwSecurityInformation,
-                                           (unsigned char *)SelfRelativeSD,
-                                           Length);
-
-    HeapFree(GetProcessHeap(), 0, SelfRelativeSD);
+    RpcEndExcept;
 
     if (dwError != ERROR_SUCCESS)
     {
-        DPRINT1("ScmrServiceObjectSecurity() failed (Error %lu)\n", dwError);
+        ERR("RQueryServiceStatusEx() failed (Error %lu)\n", dwError);
         SetLastError(dwError);
         return FALSE;
     }
@@ -1918,49 +2411,28 @@ SetServiceObjectSecurity(SC_HANDLE hService,
  *
  * @implemented
  */
-BOOL STDCALL
+BOOL WINAPI
 StartServiceA(SC_HANDLE hService,
               DWORD dwNumServiceArgs,
               LPCSTR *lpServiceArgVectors)
 {
-    LPSTR lpBuffer;
-    LPSTR lpStr;
     DWORD dwError;
-    DWORD dwBufSize;
-    DWORD i;
 
-    dwBufSize = 0;
-    for (i = 0; i < dwNumServiceArgs; i++)
+    RpcTryExcept
     {
-        dwBufSize += (strlen(lpServiceArgVectors[i]) + 1);
-    }
-    DPRINT1("dwBufSize: %lu\n", dwBufSize);
-
-    lpBuffer = HeapAlloc(GetProcessHeap(), 0, dwBufSize);
-    if (lpBuffer == NULL)
-    {
-        SetLastError(ERROR_NOT_ENOUGH_MEMORY);
-        return FALSE;
+        dwError = RStartServiceA((SC_RPC_HANDLE)hService,
+                                 dwNumServiceArgs,
+                                 (LPSTRING_PTRSA)lpServiceArgVectors);
     }
-
-    lpStr = lpBuffer;
-    for (i = 0; i < dwNumServiceArgs; i++)
+    RpcExcept(EXCEPTION_EXECUTE_HANDLER)
     {
-        strcpy(lpStr, lpServiceArgVectors[i]);
-        lpStr += (strlen(lpServiceArgVectors[i]) + 1);
+        dwError = ScmRpcStatusToWinError(RpcExceptionCode());
     }
-
-    dwError = ScmrStartServiceA(BindingHandle,
-                                (unsigned int)hService,
-                                dwNumServiceArgs,
-                                (unsigned char *)lpBuffer,
-                                dwBufSize);
-
-    HeapFree(GetProcessHeap(), 0, lpBuffer);
+    RpcEndExcept;
 
     if (dwError != ERROR_SUCCESS)
     {
-        DPRINT1("ScmrStartServiceA() failed (Error %lu)\n", dwError);
+        ERR("RStartServiceA() failed (Error %lu)\n", dwError);
         SetLastError(dwError);
         return FALSE;
     }
@@ -1974,49 +2446,28 @@ StartServiceA(SC_HANDLE hService,
  *
  * @implemented
  */
-BOOL STDCALL
+BOOL WINAPI
 StartServiceW(SC_HANDLE hService,
               DWORD dwNumServiceArgs,
               LPCWSTR *lpServiceArgVectors)
 {
-    LPWSTR lpBuffer;
-    LPWSTR lpStr;
     DWORD dwError;
-    DWORD dwBufSize;
-    DWORD i;
-
-    dwBufSize = 0;
-    for (i = 0; i < dwNumServiceArgs; i++)
-    {
-        dwBufSize += ((wcslen(lpServiceArgVectors[i]) + 1) * sizeof(WCHAR));
-    }
-    DPRINT1("dwBufSize: %lu\n", dwBufSize);
 
-    lpBuffer = HeapAlloc(GetProcessHeap(), 0, dwBufSize);
-    if (lpBuffer == NULL)
+    RpcTryExcept
     {
-        SetLastError(ERROR_NOT_ENOUGH_MEMORY);
-        return FALSE;
+        dwError = RStartServiceW((SC_RPC_HANDLE)hService,
+                                 dwNumServiceArgs,
+                                 (LPSTRING_PTRSW)lpServiceArgVectors);
     }
-
-    lpStr = lpBuffer;
-    for (i = 0; i < dwNumServiceArgs; i++)
+    RpcExcept(EXCEPTION_EXECUTE_HANDLER)
     {
-        wcscpy(lpStr, lpServiceArgVectors[i]);
-        lpStr += (wcslen(lpServiceArgVectors[i]) + 1);
+        dwError = ScmRpcStatusToWinError(RpcExceptionCode());
     }
-
-    dwError = ScmrStartServiceW(BindingHandle,
-                                (unsigned int)hService,
-                                dwNumServiceArgs,
-                                (unsigned char *)lpBuffer,
-                                dwBufSize);
-
-    HeapFree(GetProcessHeap(), 0, lpBuffer);
+    RpcEndExcept;
 
     if (dwError != ERROR_SUCCESS)
     {
-        DPRINT1("ScmrStartServiceW() failed (Error %lu)\n", dwError);
+        ERR("RStartServiceW() failed (Error %lu)\n", dwError);
         SetLastError(dwError);
         return FALSE;
     }
@@ -2030,21 +2481,27 @@ StartServiceW(SC_HANDLE hService,
  *
  * @implemented
  */
-BOOL STDCALL
+BOOL WINAPI
 UnlockServiceDatabase(SC_LOCK ScLock)
 {
     DWORD dwError;
 
-    DPRINT("UnlockServiceDatabase(%x)\n", ScLock);
+    TRACE("UnlockServiceDatabase(%x)\n", ScLock);
 
-    HandleBind();
+    RpcTryExcept
+    {
+        /* Call to services.exe using RPC */
+        dwError = RUnlockServiceDatabase((LPSC_RPC_LOCK)&ScLock);
+    }
+    RpcExcept(EXCEPTION_EXECUTE_HANDLER)
+    {
+        dwError = ScmRpcStatusToWinError(RpcExceptionCode());
+    }
+    RpcEndExcept;
 
-    /* Call to services.exe using RPC */
-    dwError = ScmrUnlockServiceDatabase(BindingHandle,
-                                        (unsigned int)ScLock);
     if (dwError != ERROR_SUCCESS)
     {
-        DPRINT1("ScmrUnlockServiceDatabase() failed (Error %lu)\n", dwError);
+        ERR("RUnlockServiceDatabase() failed (Error %lu)\n", dwError);
         SetLastError(dwError);
         return FALSE;
     }
@@ -2058,21 +2515,28 @@ UnlockServiceDatabase(SC_LOCK ScLock)
  *
  * @implemented
  */
-BOOL STDCALL
+BOOL WINAPI
 NotifyBootConfigStatus(BOOL BootAcceptable)
 {
     DWORD dwError;
 
-    DPRINT1("NotifyBootConfigStatus()\n");
+    TRACE("NotifyBootConfigStatus()\n");
 
-    HandleBind();
+    RpcTryExcept
+    {
+        /* Call to services.exe using RPC */
+        dwError = RNotifyBootConfigStatus(NULL,
+                                          BootAcceptable);
+    }
+    RpcExcept(EXCEPTION_EXECUTE_HANDLER)
+    {
+        dwError = ScmRpcStatusToWinError(RpcExceptionCode());
+    }
+    RpcEndExcept;
 
-    /* Call to services.exe using RPC */
-    dwError = ScmrNotifyBootConfigStatus(BindingHandle,
-                                         BootAcceptable);
     if (dwError != ERROR_SUCCESS)
     {
-        DPRINT1("NotifyBootConfigStatus() failed (Error %lu)\n", dwError);
+        ERR("NotifyBootConfigStatus() failed (Error %lu)\n", dwError);
         SetLastError(dwError);
         return FALSE;
     }
@@ -2080,16 +2544,4 @@ NotifyBootConfigStatus(BOOL BootAcceptable)
     return TRUE;
 }
 
-
-void __RPC_FAR * __RPC_USER midl_user_allocate(size_t len)
-{
-    return HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, len);
-}
-
-
-void __RPC_USER midl_user_free(void __RPC_FAR * ptr)
-{
-    HeapFree(GetProcessHeap(), 0, ptr);
-}
-
 /* EOF */