[LOCALSPL]
[reactos.git] / reactos / win32ss / printing / providers / localspl / printprocessors.c
index 5980ced..d26175e 100644 (file)
@@ -73,7 +73,7 @@ Cleanup:
 }
 
 BOOL
-FindDatatype(PLOCAL_PRINT_PROCESSOR pPrintProcessor, PWSTR pwszDatatype)
+FindDatatype(const PLOCAL_PRINT_PROCESSOR pPrintProcessor, PCWSTR pwszDatatype)
 {
     DWORD i;
     PDATATYPES_INFO_1W pCurrentDatatype = pPrintProcessor->pDatatypesInfo1;
@@ -90,7 +90,7 @@ FindDatatype(PLOCAL_PRINT_PROCESSOR pPrintProcessor, PWSTR pwszDatatype)
 }
 
 PLOCAL_PRINT_PROCESSOR
-FindPrintProcessor(PWSTR pwszName)
+FindPrintProcessor(PCWSTR pwszName)
 {
     PLIST_ENTRY pEntry;
     PLOCAL_PRINT_PROCESSOR pPrintProcessor;
@@ -111,7 +111,7 @@ FindPrintProcessor(PWSTR pwszName)
  *
  * Initializes a singly linked list of locally available Print Processors.
  */
-void
+BOOL
 InitializePrintProcessorList()
 {
     DWORD cbDatatypes;
@@ -119,15 +119,14 @@ InitializePrintProcessorList()
     DWORD cchPrintProcessorPath;
     DWORD cchMaxSubKey;
     DWORD cchPrintProcessorName;
+    DWORD dwErrorCode;
     DWORD dwSubKeys;
     DWORD i;
     HINSTANCE hinstPrintProcessor;
     HKEY hKey = NULL;
     HKEY hSubKey = NULL;
     HKEY hSubSubKey = NULL;
-    LONG lStatus;
     PLOCAL_PRINT_PROCESSOR pPrintProcessor = NULL;
-    PWSTR pwszPrintProcessorName = NULL;
     WCHAR wszFileName[MAX_PATH];
     WCHAR wszPrintProcessorPath[MAX_PATH];
 
@@ -136,36 +135,35 @@ InitializePrintProcessorList()
     
     // Prepare the path to the Print Processor directory.
     if (!LocalGetPrintProcessorDirectory(NULL, NULL, 1, (PBYTE)wszPrintProcessorPath, sizeof(wszPrintProcessorPath), &cchPrintProcessorPath))
+    {
+        dwErrorCode = GetLastError();
         goto Cleanup;
+    }
 
     cchPrintProcessorPath /= sizeof(WCHAR);
     wszPrintProcessorPath[cchPrintProcessorPath++] = L'\\';
 
     // Open the environment registry key.
-    if (_OpenEnvironment(NULL, &hKey) != ERROR_SUCCESS)
-        goto Cleanup;
-
-    // Open the "Print Processors" subkey.
-    lStatus = RegOpenKeyExW(hKey, L"Print Processors", 0, KEY_READ, &hSubKey);
-    if (lStatus != ERROR_SUCCESS)
+    dwErrorCode = _OpenEnvironment(NULL, &hKey);
+    if (dwErrorCode != ERROR_SUCCESS)
     {
-        ERR("RegOpenKeyExW failed with status %ld!\n", lStatus);
+        ERR("_OpenEnvironment failed with error %lu!\n", dwErrorCode);
         goto Cleanup;
     }
 
-    // Get the number of Print Processors and maximum sub key length.
-    lStatus = RegQueryInfoKeyW(hSubKey, NULL, NULL, NULL, &dwSubKeys, &cchMaxSubKey, NULL, NULL, NULL, NULL, NULL, NULL);
-    if (lStatus != ERROR_SUCCESS)
+    // Open the "Print Processors" subkey.
+    dwErrorCode = (DWORD)RegOpenKeyExW(hKey, L"Print Processors", 0, KEY_READ, &hSubKey);
+    if (dwErrorCode != ERROR_SUCCESS)
     {
-        ERR("RegQueryInfoKeyW failed with status %ld!\n", lStatus);
+        ERR("RegOpenKeyExW failed with status %lu!\n", dwErrorCode);
         goto Cleanup;
     }
 
-    // Allocate a temporary buffer for the Print Processor names.
-    pwszPrintProcessorName = DllAllocSplMem((cchMaxSubKey + 1) * sizeof(WCHAR));
-    if (!pwszPrintProcessorName)
+    // Get the number of Print Processors and maximum sub key length.
+    dwErrorCode = (DWORD)RegQueryInfoKeyW(hSubKey, NULL, NULL, NULL, &dwSubKeys, &cchMaxSubKey, NULL, NULL, NULL, NULL, NULL, NULL);
+    if (dwErrorCode != ERROR_SUCCESS)
     {
-        ERR("DllAllocSplMem failed with error %lu!\n", GetLastError());
+        ERR("RegQueryInfoKeyW failed with status %lu!\n", dwErrorCode);
         goto Cleanup;
     }
 
@@ -191,36 +189,54 @@ InitializePrintProcessorList()
             pPrintProcessor = NULL;
         }
 
+        // Create a new LOCAL_PRINT_PROCESSOR structure for it.
+        pPrintProcessor = DllAllocSplMem(sizeof(LOCAL_PRINT_PROCESSOR));
+        if (!pPrintProcessor)
+        {
+            dwErrorCode = ERROR_NOT_ENOUGH_MEMORY;
+            ERR("DllAllocSplMem failed with error %lu!\n", GetLastError());
+            goto Cleanup;
+        }
+
+        // Allocate memory for the Print Monitor Name.
+        pPrintProcessor->pwszName = DllAllocSplMem((cchMaxSubKey + 1) * sizeof(WCHAR));
+        if (!pPrintProcessor->pwszName)
+        {
+            dwErrorCode = ERROR_NOT_ENOUGH_MEMORY;
+            ERR("DllAllocSplMem failed with error %lu!\n", GetLastError());
+            goto Cleanup;
+        }
+
         // Get the name of this Print Processor.
-        cchPrintProcessorName = cchMaxSubKey;
-        lStatus = RegEnumKeyExW(hSubKey, i, pwszPrintProcessorName, &cchPrintProcessorName, NULL, NULL, NULL, NULL);
-        if (lStatus != ERROR_SUCCESS)
+        cchPrintProcessorName = cchMaxSubKey + 1;
+        dwErrorCode = (DWORD)RegEnumKeyExW(hSubKey, i, pPrintProcessor->pwszName, &cchPrintProcessorName, NULL, NULL, NULL, NULL);
+        if (dwErrorCode != ERROR_SUCCESS)
         {
-            ERR("RegEnumKeyExW failed with status %ld!\n", lStatus);
+            ERR("RegEnumKeyExW failed with status %ld!\n", dwErrorCode);
             continue;
         }
 
         // Open this Print Processor's registry key.
-        lStatus = RegOpenKeyExW(hSubKey, pwszPrintProcessorName, 0, KEY_READ, &hSubSubKey);
-        if (lStatus != ERROR_SUCCESS)
+        dwErrorCode = (DWORD)RegOpenKeyExW(hSubKey, pPrintProcessor->pwszName, 0, KEY_READ, &hSubSubKey);
+        if (dwErrorCode != ERROR_SUCCESS)
         {
-            ERR("RegOpenKeyExW failed for Print Processor \"%S\" with status %ld!\n", pwszPrintProcessorName, lStatus);
+            ERR("RegOpenKeyExW failed for Print Processor \"%S\" with status %lu!\n", pPrintProcessor->pwszName, dwErrorCode);
             continue;
         }
 
         // Get the file name of the Print Processor.
         cbFileName = sizeof(wszFileName);
-        lStatus = RegQueryValueExW(hSubSubKey, L"Driver", NULL, NULL, (PBYTE)wszFileName, &cbFileName);
-        if (lStatus != ERROR_SUCCESS)
+        dwErrorCode = (DWORD)RegQueryValueExW(hSubSubKey, L"Driver", NULL, NULL, (PBYTE)wszFileName, &cbFileName);
+        if (dwErrorCode != ERROR_SUCCESS)
         {
-            ERR("RegQueryValueExW failed for Print Processor \"%S\" with status %ld!\n", pwszPrintProcessorName, lStatus);
+            ERR("RegQueryValueExW failed for Print Processor \"%S\" with status %lu!\n", pPrintProcessor->pwszName, dwErrorCode);
             continue;
         }
 
         // Verify that our buffer is large enough.
         if (cchPrintProcessorPath + cbFileName / sizeof(WCHAR) > MAX_PATH)
         {
-            ERR("Print Processor directory \"%S\" for Print Processor \"%S\" is too long!\n", wszFileName, pwszPrintProcessorName);
+            ERR("Print Processor directory \"%S\" for Print Processor \"%S\" is too long!\n", wszFileName, pPrintProcessor->pwszName);
             continue;
         }
 
@@ -229,16 +245,12 @@ InitializePrintProcessorList()
 
         // Try to load it.
         hinstPrintProcessor = LoadLibraryW(wszPrintProcessorPath);
-        if (lStatus != ERROR_SUCCESS)
+        if (!hinstPrintProcessor)
         {
             ERR("LoadLibraryW failed for \"%S\" with error %lu!\n", wszPrintProcessorPath, GetLastError());
             continue;
         }
 
-        // Create a new LOCAL_PRINT_PROCESSOR structure for it.
-        pPrintProcessor = DllAllocSplMem(sizeof(LOCAL_PRINT_PROCESSOR));
-        pPrintProcessor->pwszName = AllocSplStr(pwszPrintProcessorName);
-
         // Get and verify all its function pointers.
         pPrintProcessor->pfnClosePrintProcessor = (PClosePrintProcessor)GetProcAddress(hinstPrintProcessor, "ClosePrintProcessor");
         if (!pPrintProcessor->pfnClosePrintProcessor)
@@ -287,6 +299,7 @@ InitializePrintProcessorList()
         pPrintProcessor->pDatatypesInfo1 = DllAllocSplMem(cbDatatypes);
         if (!pPrintProcessor->pDatatypesInfo1)
         {
+            dwErrorCode = ERROR_NOT_ENOUGH_MEMORY;
             ERR("DllAllocSplMem failed with error %lu!\n", GetLastError());
             goto Cleanup;
         }
@@ -304,6 +317,8 @@ InitializePrintProcessorList()
         pPrintProcessor = NULL;
     }
 
+    dwErrorCode = ERROR_SUCCESS;
+
 Cleanup:
     // Inside the loop
     if (hSubSubKey)
@@ -321,14 +336,14 @@ Cleanup:
     }
 
     // Outside the loop
-    if (pwszPrintProcessorName)
-        DllFreeSplStr(pwszPrintProcessorName);
-
     if (hSubKey)
         RegCloseKey(hSubKey);
 
     if (hKey)
         RegCloseKey(hKey);
+
+    SetLastError(dwErrorCode);
+    return (dwErrorCode == ERROR_SUCCESS);
 }
 
 /**
@@ -438,6 +453,7 @@ LocalEnumPrintProcessors(LPWSTR pName, LPWSTR pEnvironment, DWORD Level, LPBYTE
     DWORD cchMaxSubKey;
     DWORD cchPrintProcessor;
     DWORD dwErrorCode;
+    DWORD dwPrintProcessorCount;
     DWORD i;
     HKEY hKey = NULL;
     HKEY hSubKey = NULL;
@@ -464,7 +480,10 @@ LocalEnumPrintProcessors(LPWSTR pName, LPWSTR pEnvironment, DWORD Level, LPBYTE
     // We use the registry and not the PrintProcessorList here, because the caller may request information about a different environment.
     dwErrorCode = _OpenEnvironment(pEnvironment, &hKey);
     if (dwErrorCode != ERROR_SUCCESS)
+    {
+        ERR("_OpenEnvironment failed with error %lu!\n", dwErrorCode);
         goto Cleanup;
+    }
 
     // Open the "Print Processors" subkey.
     dwErrorCode = (DWORD)RegOpenKeyExW(hKey, L"Print Processors", 0, KEY_READ, &hSubKey);
@@ -475,7 +494,7 @@ LocalEnumPrintProcessors(LPWSTR pName, LPWSTR pEnvironment, DWORD Level, LPBYTE
     }
 
     // Get the number of Print Processors and maximum sub key length.
-    dwErrorCode = (DWORD)RegQueryInfoKeyW(hSubKey, NULL, NULL, NULL, pcReturned, &cchMaxSubKey, NULL, NULL, NULL, NULL, NULL, NULL);
+    dwErrorCode = (DWORD)RegQueryInfoKeyW(hSubKey, NULL, NULL, NULL, &dwPrintProcessorCount, &cchMaxSubKey, NULL, NULL, NULL, NULL, NULL, NULL);
     if (dwErrorCode != ERROR_SUCCESS)
     {
         ERR("RegQueryInfoKeyW failed with status %lu!\n", dwErrorCode);
@@ -494,11 +513,11 @@ LocalEnumPrintProcessors(LPWSTR pName, LPWSTR pEnvironment, DWORD Level, LPBYTE
     // Determine the required size of the output buffer.
     *pcbNeeded = 0;
 
-    for (i = 0; i < *pcReturned; i++)
+    for (i = 0; i < dwPrintProcessorCount; i++)
     {
         // RegEnumKeyExW sucks! Unlike similar API functions, it only returns the actual numbers of characters copied when you supply a buffer large enough.
         // So use pwszTemp with its size cchMaxSubKey for this.
-        cchPrintProcessor = cchMaxSubKey;
+        cchPrintProcessor = cchMaxSubKey + 1;
         dwErrorCode = (DWORD)RegEnumKeyExW(hSubKey, i, pwszTemp, &cchPrintProcessor, NULL, NULL, NULL, NULL);
         if (dwErrorCode != ERROR_SUCCESS)
         {
@@ -518,13 +537,13 @@ LocalEnumPrintProcessors(LPWSTR pName, LPWSTR pEnvironment, DWORD Level, LPBYTE
 
     // Put the Print Processor strings right after the last PRINTPROCESSOR_INFO_1W structure.
     pCurrentOutputPrintProcessorInfo = pPrintProcessorInfo;
-    pCurrentOutputPrintProcessor = pPrintProcessorInfo + *pcReturned * sizeof(PRINTPROCESSOR_INFO_1W);
+    pCurrentOutputPrintProcessor = pPrintProcessorInfo + dwPrintProcessorCount * sizeof(PRINTPROCESSOR_INFO_1W);
 
     // Copy over all Print Processors.
-    for (i = 0; i < *pcReturned; i++)
+    for (i = 0; i < dwPrintProcessorCount; i++)
     {
         // This isn't really correct, but doesn't cause any harm, because we've extensively checked the size of the supplied buffer above.
-        cchPrintProcessor = cchMaxSubKey;
+        cchPrintProcessor = cchMaxSubKey + 1;
 
         // Copy the Print Processor name.
         dwErrorCode = (DWORD)RegEnumKeyExW(hSubKey, i, (PWSTR)pCurrentOutputPrintProcessor, &cchPrintProcessor, NULL, NULL, NULL, NULL);
@@ -544,6 +563,7 @@ LocalEnumPrintProcessors(LPWSTR pName, LPWSTR pEnvironment, DWORD Level, LPBYTE
     }
 
     // We've finished successfully!
+    *pcReturned = dwPrintProcessorCount;
     dwErrorCode = ERROR_SUCCESS;
 
 Cleanup:
@@ -618,7 +638,10 @@ LocalGetPrintProcessorDirectory(LPWSTR pName, LPWSTR pEnvironment, DWORD Level,
     // Verify pEnvironment and open its registry key.
     dwErrorCode = _OpenEnvironment(pEnvironment, &hKey);
     if (dwErrorCode != ERROR_SUCCESS)
+    {
+        ERR("_OpenEnvironment failed with error %lu!\n", dwErrorCode);
         goto Cleanup;
+    }
 
     // Determine the size of the required buffer.
     dwErrorCode = (DWORD)RegQueryValueExW(hKey, L"Directory", NULL, NULL, NULL, pcbNeeded);