[ADVAPI32]
[reactos.git] / reactos / dll / win32 / advapi32 / service / scm.c
index 97fcf8e..0e80fc2 100644 (file)
@@ -165,6 +165,8 @@ ChangeServiceConfig2A(SC_HANDLE hService,
 
     TRACE("ChangeServiceConfig2A() called\n");
 
+    if (lpInfo == NULL) return TRUE;
+
     /* Fill relevent field of the Info structure */
     Info.dwInfoLevel = dwInfoLevel;
     switch (dwInfoLevel)
@@ -184,9 +186,6 @@ ChangeServiceConfig2A(SC_HANDLE hService,
             return FALSE;
     }
 
-    if (lpInfo == NULL)
-        return TRUE;
-
     RpcTryExcept
     {
         dwError = RChangeServiceConfig2A((SC_RPC_HANDLE)hService,
@@ -224,6 +223,8 @@ ChangeServiceConfig2W(SC_HANDLE hService,
 
     TRACE("ChangeServiceConfig2W() called\n");
 
+    if (lpInfo == NULL) return TRUE;
+
     /* Fill relevent field of the Info structure */
     Info.dwInfoLevel = dwInfoLevel;
     switch (dwInfoLevel)
@@ -242,9 +243,6 @@ ChangeServiceConfig2W(SC_HANDLE hService,
             return FALSE;
     }
 
-    if (lpInfo == NULL)
-        return TRUE;
-
     RpcTryExcept
     {
         dwError = RChangeServiceConfig2W((SC_RPC_HANDLE)hService,
@@ -287,7 +285,7 @@ ChangeServiceConfigA(SC_HANDLE hService,
 {
     DWORD dwError;
     DWORD dwDependenciesLength = 0;
-    DWORD dwLength;
+    SIZE_T cchLength;
     LPCSTR lpStr;
     DWORD dwPasswordLength = 0;
     LPBYTE lpEncryptedPassword = NULL;
@@ -300,16 +298,16 @@ ChangeServiceConfigA(SC_HANDLE hService,
         lpStr = lpDependencies;
         while (*lpStr)
         {
-            dwLength = strlen(lpStr) + 1;
-            dwDependenciesLength += dwLength;
-            lpStr = lpStr + dwLength;
+            cchLength = strlen(lpStr) + 1;
+            dwDependenciesLength += (DWORD)cchLength;
+            lpStr = lpStr + cchLength;
         }
         dwDependenciesLength++;
     }
 
     /* FIXME: Encrypt the password */
     lpEncryptedPassword = (LPBYTE)lpPassword;
-    dwPasswordLength = (strlen(lpPassword) + 1) * sizeof(CHAR);
+    dwPasswordLength = (DWORD)(lpPassword ? (strlen(lpPassword) + 1) * sizeof(CHAR) : 0);
 
     RpcTryExcept
     {
@@ -365,7 +363,7 @@ ChangeServiceConfigW(SC_HANDLE hService,
 {
     DWORD dwError;
     DWORD dwDependenciesLength = 0;
-    DWORD dwLength;
+    SIZE_T cchLength;
     LPCWSTR lpStr;
     DWORD dwPasswordLength = 0;
     LPBYTE lpEncryptedPassword = NULL;
@@ -378,16 +376,17 @@ ChangeServiceConfigW(SC_HANDLE hService,
         lpStr = lpDependencies;
         while (*lpStr)
         {
-            dwLength = wcslen(lpStr) + 1;
-            dwDependenciesLength += dwLength;
-            lpStr = lpStr + dwLength;
+            cchLength = wcslen(lpStr) + 1;
+            dwDependenciesLength += (DWORD)cchLength;
+            lpStr = lpStr + cchLength;
         }
         dwDependenciesLength++;
+        dwDependenciesLength *= sizeof(WCHAR);
     }
 
     /* FIXME: Encrypt the password */
     lpEncryptedPassword = (LPBYTE)lpPassword;
-    dwPasswordLength = (wcslen(lpPassword) + 1) * sizeof(WCHAR);
+    dwPasswordLength = (lpPassword ? (wcslen(lpPassword) + 1) * sizeof(WCHAR) : 0);
 
     RpcTryExcept
     {
@@ -547,7 +546,7 @@ CreateServiceA(SC_HANDLE hSCManager,
     SC_HANDLE hService = NULL;
     DWORD dwDependenciesLength = 0;
     DWORD dwError;
-    DWORD dwLength;
+    SIZE_T cchLength;
     LPCSTR lpStr;
     DWORD dwPasswordLength = 0;
     LPBYTE lpEncryptedPassword = NULL;
@@ -568,16 +567,16 @@ CreateServiceA(SC_HANDLE hSCManager,
         lpStr = lpDependencies;
         while (*lpStr)
         {
-            dwLength = strlen(lpStr) + 1;
-            dwDependenciesLength += dwLength;
-            lpStr = lpStr + dwLength;
+            cchLength = strlen(lpStr) + 1;
+            dwDependenciesLength += (DWORD)cchLength;
+            lpStr = lpStr + cchLength;
         }
         dwDependenciesLength++;
     }
 
     /* FIXME: Encrypt the password */
     lpEncryptedPassword = (LPBYTE)lpPassword;
-    dwPasswordLength = (strlen(lpPassword) + 1) * sizeof(CHAR);
+    dwPasswordLength = (DWORD)(lpPassword ? (strlen(lpPassword) + 1) * sizeof(CHAR) : 0);
 
     RpcTryExcept
     {
@@ -639,7 +638,7 @@ CreateServiceW(SC_HANDLE hSCManager,
     SC_HANDLE hService = NULL;
     DWORD dwDependenciesLength = 0;
     DWORD dwError;
-    DWORD dwLength;
+    SIZE_T cchLength;
     LPCWSTR lpStr;
     DWORD dwPasswordLength = 0;
     LPBYTE lpEncryptedPassword = NULL;
@@ -660,18 +659,17 @@ CreateServiceW(SC_HANDLE hSCManager,
         lpStr = lpDependencies;
         while (*lpStr)
         {
-            dwLength = wcslen(lpStr) + 1;
-            dwDependenciesLength += dwLength;
-            lpStr = lpStr + dwLength;
+            cchLength = wcslen(lpStr) + 1;
+            dwDependenciesLength += (DWORD)cchLength;
+            lpStr = lpStr + cchLength;
         }
         dwDependenciesLength++;
-
         dwDependenciesLength *= sizeof(WCHAR);
     }
 
     /* FIXME: Encrypt the password */
     lpEncryptedPassword = (LPBYTE)lpPassword;
-    dwPasswordLength = (wcslen(lpPassword) + 1) * sizeof(WCHAR);
+    dwPasswordLength = (DWORD)(lpPassword ? (wcslen(lpPassword) + 1) * sizeof(WCHAR) : 0);
 
     RpcTryExcept
     {
@@ -926,6 +924,12 @@ EnumServiceGroupW(SC_HANDLE hSCManager,
         return FALSE;
     }
 
+    if (pcbBytesNeeded == NULL || lpServicesReturned == NULL)
+    {
+        SetLastError(ERROR_INVALID_ADDRESS);
+        return FALSE;
+    }
+
     if (lpServices == NULL || cbBufSize < sizeof(ENUM_SERVICE_STATUSW))
     {
         lpStatusPtr = &ServiceStatus;
@@ -1027,6 +1031,12 @@ EnumServicesStatusA(SC_HANDLE hSCManager,
         return FALSE;
     }
 
+    if (pcbBytesNeeded == NULL || lpServicesReturned == NULL)
+    {
+        SetLastError(ERROR_INVALID_ADDRESS);
+        return FALSE;
+    }
+
     if (lpServices == NULL || cbBufSize < sizeof(ENUM_SERVICE_STATUSA))
     {
         lpStatusPtr = &ServiceStatus;
@@ -1113,6 +1123,12 @@ EnumServicesStatusW(SC_HANDLE hSCManager,
         return FALSE;
     }
 
+    if (pcbBytesNeeded == NULL || lpServicesReturned == NULL)
+    {
+        SetLastError(ERROR_INVALID_ADDRESS);
+        return FALSE;
+    }
+
     if (lpServices == NULL || cbBufSize < sizeof(ENUM_SERVICE_STATUSW))
     {
         lpStatusPtr = &ServiceStatus;
@@ -1207,8 +1223,13 @@ EnumServicesStatusExA(SC_HANDLE hSCManager,
         return FALSE;
     }
 
-    if (lpServices == NULL ||
-        cbBufSize < sizeof(ENUM_SERVICE_STATUS_PROCESSA))
+    if (pcbBytesNeeded == NULL || lpServicesReturned == NULL)
+    {
+        SetLastError(ERROR_INVALID_ADDRESS);
+        return FALSE;
+    }
+
+    if (lpServices == NULL || cbBufSize < sizeof(ENUM_SERVICE_STATUS_PROCESSA))
     {
         lpStatusPtr = &ServiceStatus;
         dwBufferSize = sizeof(ENUM_SERVICE_STATUS_PROCESSA);
@@ -1307,8 +1328,13 @@ EnumServicesStatusExW(SC_HANDLE hSCManager,
         return FALSE;
     }
 
-    if (lpServices == NULL ||
-        cbBufSize < sizeof(ENUM_SERVICE_STATUS_PROCESSW))
+    if (pcbBytesNeeded == NULL || lpServicesReturned == NULL)
+    {
+        SetLastError(ERROR_INVALID_ADDRESS);
+        return FALSE;
+    }
+
+    if (lpServices == NULL || cbBufSize < sizeof(ENUM_SERVICE_STATUS_PROCESSW))
     {
         lpStatusPtr = &ServiceStatus;
         dwBufferSize = sizeof(ENUM_SERVICE_STATUS_PROCESSW);
@@ -1647,28 +1673,14 @@ WaitForSCManager(VOID)
     TRACE("WaitForSCManager() called\n");
 
     /* Try to open the existing event */
-    hEvent = OpenEventW(SYNCHRONIZE,
-                        FALSE,
-                        L"SvcctrlStartEvent_A3752DX");
+    hEvent = OpenEventW(SYNCHRONIZE, FALSE, SCM_START_EVENT);
     if (hEvent == NULL)
     {
-        if (GetLastError() != ERROR_FILE_NOT_FOUND)
-            return;
+        if (GetLastError() != ERROR_FILE_NOT_FOUND) return;
 
         /* Try to create a new event */
-        hEvent = CreateEventW(NULL,
-                              TRUE,
-                              FALSE,
-                              L"SvcctrlStartEvent_A3752DX");
-        if (hEvent == NULL)
-        {
-            /* Try to open the existing event again */
-            hEvent = OpenEventW(SYNCHRONIZE,
-                                FALSE,
-                                L"SvcctrlStartEvent_A3752DX");
-            if (hEvent == NULL)
-                return;
-        }
+        hEvent = CreateEventW(NULL, TRUE, FALSE, SCM_START_EVENT);
+        if (hEvent == NULL) return;
     }
 
     /* Wait for 3 minutes */
@@ -2111,6 +2123,7 @@ QueryServiceConfig2A(SC_HANDLE hService,
     if (bUseTempBuffer == TRUE)
     {
         TRACE("RQueryServiceConfig2A() returns ERROR_INSUFFICIENT_BUFFER\n");
+        *pcbBytesNeeded = dwBufferSize;
         SetLastError(ERROR_INSUFFICIENT_BUFFER);
         return FALSE;
     }
@@ -2228,6 +2241,7 @@ QueryServiceConfig2W(SC_HANDLE hService,
     if (bUseTempBuffer == TRUE)
     {
         TRACE("RQueryServiceConfig2W() returns ERROR_INSUFFICIENT_BUFFER\n");
+        *pcbBytesNeeded = dwBufferSize;
         SetLastError(ERROR_INSUFFICIENT_BUFFER);
         return FALSE;
     }