[LOCALMON, LOCALSPL]
[reactos.git] / reactos / win32ss / printing / providers / localspl / printers.c
index 9149f52..48540b1 100644 (file)
@@ -8,72 +8,66 @@
 #include "precomp.h"
 
 // Global Variables
-RTL_GENERIC_TABLE PrinterTable;
+SKIPLIST PrinterList;
 
 
 /**
- * @name _PrinterTableCompareRoutine
+ * @name _PrinterListCompareRoutine
  *
- * RTL_GENERIC_COMPARE_ROUTINE for the Printer Table.
+ * SKIPLIST_COMPARE_ROUTINE for the Printer List.
  * Does a case-insensitive comparison, because e.g. LocalOpenPrinter doesn't match the case when looking for Printers.
  */
-static RTL_GENERIC_COMPARE_RESULTS NTAPI
-_PrinterTableCompareRoutine(PRTL_GENERIC_TABLE Table, PVOID FirstStruct, PVOID SecondStruct)
+static int WINAPI
+_PrinterListCompareRoutine(PVOID FirstStruct, PVOID SecondStruct)
 {
     PLOCAL_PRINTER A = (PLOCAL_PRINTER)FirstStruct;
     PLOCAL_PRINTER B = (PLOCAL_PRINTER)SecondStruct;
 
-    int iResult = wcsicmp(A->pwszPrinterName, B->pwszPrinterName);
-
-    if (iResult < 0)
-        return GenericLessThan;
-    else if (iResult > 0)
-        return GenericGreaterThan;
-    else
-        return GenericEqual;
+    return wcsicmp(A->pwszPrinterName, B->pwszPrinterName);
 }
 
 /**
- * @name InitializePrinterTable
+ * @name InitializePrinterList
  *
- * Initializes a RTL_GENERIC_TABLE of locally available Printers.
- * The table is searchable by name and returns information about the printers, including their job queues.
+ * Initializes a list of locally available Printers.
+ * The list is searchable by name and returns information about the printers, including their job queues.
  * During this process, the job queues are also initialized.
  */
-void
-InitializePrinterTable()
+BOOL
+InitializePrinterList()
 {
     const WCHAR wszPrintersKey[] = L"SYSTEM\\CurrentControlSet\\Control\\Print\\Printers";
 
-    DWORD cbDevMode;
+    DWORD cbData;
     DWORD cchPrinterName;
+    DWORD dwErrorCode;
     DWORD dwSubKeys;
     DWORD i;
     HKEY hKey = NULL;
     HKEY hSubKey = NULL;
-    LONG lStatus;
-    PLOCAL_PRINT_PROCESSOR pPrintProcessor;
+    PLOCAL_PORT pPort;
     PLOCAL_PRINTER pPrinter = NULL;
+    PLOCAL_PRINT_PROCESSOR pPrintProcessor;
+    PWSTR pwszPort = NULL;
     PWSTR pwszPrintProcessor = NULL;
     WCHAR wszPrinterName[MAX_PRINTER_NAME + 1];
 
-    // Initialize an empty table for our printers.
-    // We will search it by printer name.
-    RtlInitializeGenericTable(&PrinterTable, _PrinterTableCompareRoutine, GenericTableAllocateRoutine, GenericTableFreeRoutine, NULL);
+    // Initialize an empty list for our printers.
+    InitializeSkiplist(&PrinterList, DllAllocSplMem, _PrinterListCompareRoutine, (PSKIPLIST_FREE_ROUTINE)DllFreeSplMem);
 
     // Open our printers registry key. Each subkey is a local printer there.
-    lStatus = RegOpenKeyExW(HKEY_LOCAL_MACHINE, wszPrintersKey, 0, KEY_READ, &hKey);
-    if (lStatus != ERROR_SUCCESS)
+    dwErrorCode = (DWORD)RegOpenKeyExW(HKEY_LOCAL_MACHINE, wszPrintersKey, 0, KEY_READ, &hKey);
+    if (dwErrorCode != ERROR_SUCCESS)
     {
-        ERR("RegOpenKeyExW failed with status %ld!\n", lStatus);
+        ERR("RegOpenKeyExW failed with status %lu!\n", dwErrorCode);
         goto Cleanup;
     }
 
     // Get the number of subkeys.
-    lStatus = RegQueryInfoKeyW(hKey, NULL, NULL, NULL, &dwSubKeys, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
-    if (lStatus != ERROR_SUCCESS)
+    dwErrorCode = (DWORD)RegQueryInfoKeyW(hKey, NULL, NULL, NULL, &dwSubKeys, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
+    if (dwErrorCode != ERROR_SUCCESS)
     {
-        ERR("RegQueryInfoKeyW failed with status %ld!\n", lStatus);
+        ERR("RegQueryInfoKeyW failed with status %lu!\n", dwErrorCode);
         goto Cleanup;
     }
 
@@ -89,38 +83,50 @@ InitializePrinterTable()
 
         if (pPrinter)
         {
+            if (pPrinter->pDefaultDevMode)
+                DllFreeSplMem(pPrinter->pDefaultDevMode);
+
             if (pPrinter->pwszDefaultDatatype)
-                HeapFree(hProcessHeap, 0, pPrinter->pwszDefaultDatatype);
+                DllFreeSplStr(pPrinter->pwszDefaultDatatype);
 
-            HeapFree(hProcessHeap, 0, pPrinter);
+            if (pPrinter->pwszDescription)
+                DllFreeSplStr(pPrinter->pwszDescription);
+
+            if (pPrinter->pwszPrinterDriver)
+                DllFreeSplStr(pPrinter->pwszPrinterDriver);
+
+            if (pPrinter->pwszPrinterName)
+                DllFreeSplStr(pPrinter->pwszPrinterName);
+
+            DllFreeSplMem(pPrinter);
             pPrinter = NULL;
         }
 
         if (pwszPrintProcessor)
         {
-            HeapFree(hProcessHeap, 0, pwszPrintProcessor);
+            DllFreeSplStr(pwszPrintProcessor);
             pwszPrintProcessor = NULL;
         }
 
         // Get the name of this printer.
-        cchPrinterName = sizeof(wszPrinterName) / sizeof(WCHAR);
-        lStatus = RegEnumKeyExW(hKey, i, wszPrinterName, &cchPrinterName, NULL, NULL, NULL, NULL);
-        if (lStatus == ERROR_MORE_DATA)
+        cchPrinterName = _countof(wszPrinterName);
+        dwErrorCode = (DWORD)RegEnumKeyExW(hKey, i, wszPrinterName, &cchPrinterName, NULL, NULL, NULL, NULL);
+        if (dwErrorCode == ERROR_MORE_DATA)
         {
             // This printer name exceeds the maximum length and is invalid.
             continue;
         }
-        else if (lStatus != ERROR_SUCCESS)
+        else if (dwErrorCode != ERROR_SUCCESS)
         {
-            ERR("RegEnumKeyExW failed for iteration %lu with status %ld!\n", i, lStatus);
+            ERR("RegEnumKeyExW failed for iteration %lu with status %lu!\n", i, dwErrorCode);
             continue;
         }
 
         // Open this Printer's registry key.
-        lStatus = RegOpenKeyExW(hKey, wszPrinterName, 0, KEY_READ, &hSubKey);
-        if (lStatus != ERROR_SUCCESS)
+        dwErrorCode = (DWORD)RegOpenKeyExW(hKey, wszPrinterName, 0, KEY_READ, &hSubKey);
+        if (dwErrorCode != ERROR_SUCCESS)
         {
-            ERR("RegOpenKeyExW failed for Printer \"%S\" with status %ld!\n", wszPrinterName, lStatus);
+            ERR("RegOpenKeyExW failed for Printer \"%S\" with status %lu!\n", wszPrinterName, dwErrorCode);
             continue;
         }
 
@@ -129,25 +135,50 @@ InitializePrinterTable()
         if (!pwszPrintProcessor)
             continue;
 
-        // Try to find it in the Print Processor Table.
-        pPrintProcessor = RtlLookupElementGenericTable(&PrintProcessorTable, pwszPrintProcessor);
+        // Try to find it in the Print Processor List.
+        pPrintProcessor = FindPrintProcessor(pwszPrintProcessor);
         if (!pPrintProcessor)
         {
             ERR("Invalid Print Processor \"%S\" for Printer \"%S\"!\n", pwszPrintProcessor, wszPrinterName);
             continue;
         }
 
+        // Get the Port.
+        pwszPort = AllocAndRegQueryWSZ(hSubKey, L"Port");
+        if (!pwszPort)
+            continue;
+
+        // Try to find it in the Port List.
+        pPort = FindPort(pwszPort);
+        if (!pPort)
+        {
+            ERR("Invalid Port \"%S\" for Printer \"%S\"!\n", pwszPort, wszPrinterName);
+            continue;
+        }
+
         // Create a new LOCAL_PRINTER structure for it.
-        pPrinter = HeapAlloc(hProcessHeap, 0, sizeof(LOCAL_PRINTER));
+        pPrinter = DllAllocSplMem(sizeof(LOCAL_PRINTER));
         if (!pPrinter)
         {
-            ERR("HeapAlloc failed with error %lu!\n", GetLastError());
+            dwErrorCode = ERROR_NOT_ENOUGH_MEMORY;
+            ERR("DllAllocSplMem failed with error %lu!\n", GetLastError());
             goto Cleanup;
         }
 
-        pPrinter->pwszPrinterName = DuplicateStringW(wszPrinterName);
+        pPrinter->pwszPrinterName = AllocSplStr(wszPrinterName);
         pPrinter->pPrintProcessor = pPrintProcessor;
-        InitializeListHead(&pPrinter->JobQueue);
+        pPrinter->pPort = pPort;
+        InitializePrinterJobList(pPrinter);
+
+        // Get the printer driver.
+        pPrinter->pwszPrinterDriver = AllocAndRegQueryWSZ(hSubKey, L"Printer Driver");
+        if (!pPrinter->pwszPrinterDriver)
+            continue;
+
+        // Get the description.
+        pPrinter->pwszDescription = AllocAndRegQueryWSZ(hSubKey, L"Description");
+        if (!pPrinter->pwszDescription)
+            continue;
 
         // Get the default datatype.
         pPrinter->pwszDefaultDatatype = AllocAndRegQueryWSZ(hSubKey, L"Datatype");
@@ -155,25 +186,59 @@ InitializePrinterTable()
             continue;
 
         // Verify that it's valid.
-        if (!RtlLookupElementGenericTable(&pPrintProcessor->DatatypeTable, pPrinter->pwszDefaultDatatype))
+        if (!FindDatatype(pPrintProcessor, pPrinter->pwszDefaultDatatype))
         {
             ERR("Invalid default datatype \"%S\" for Printer \"%S\"!\n", pPrinter->pwszDefaultDatatype, wszPrinterName);
             continue;
         }
 
+        // Determine the size of the DevMode.
+        dwErrorCode = (DWORD)RegQueryValueExW(hSubKey, L"Default DevMode", NULL, NULL, NULL, &cbData);
+        if (dwErrorCode != ERROR_SUCCESS)
+        {
+            ERR("Couldn't query the size of the DevMode for Printer \"%S\", status is %lu, cbData is %lu!\n", wszPrinterName, dwErrorCode, cbData);
+            continue;
+        }
+
+        // Allocate enough memory for the DevMode.
+        pPrinter->pDefaultDevMode = DllAllocSplMem(cbData);
+        if (!pPrinter->pDefaultDevMode)
+        {
+            dwErrorCode = ERROR_NOT_ENOUGH_MEMORY;
+            ERR("DllAllocSplMem failed with error %lu!\n", GetLastError());
+            goto Cleanup;
+        }
+
         // Get the default DevMode.
-        cbDevMode = sizeof(DEVMODEW);
-        lStatus = RegQueryValueExW(hSubKey, L"Default DevMode", NULL, NULL, (PBYTE)&pPrinter->DefaultDevMode, &cbDevMode);
-        if (lStatus != ERROR_SUCCESS || cbDevMode != sizeof(DEVMODEW))
+        dwErrorCode = (DWORD)RegQueryValueExW(hSubKey, L"Default DevMode", NULL, NULL, (PBYTE)pPrinter->pDefaultDevMode, &cbData);
+        if (dwErrorCode != ERROR_SUCCESS)
+        {
+            ERR("Couldn't query a DevMode for Printer \"%S\", status is %lu, cbData is %lu!\n", wszPrinterName, dwErrorCode, cbData);
+            continue;
+        }
+
+        // Get the Attributes.
+        cbData = sizeof(DWORD);
+        dwErrorCode = (DWORD)RegQueryValueExW(hSubKey, L"Attributes", NULL, NULL, (PBYTE)&pPrinter->dwAttributes, &cbData);
+        if (dwErrorCode != ERROR_SUCCESS)
         {
-            ERR("Couldn't query DevMode for Printer \"%S\", status is %ld, cbDevMode is %lu!\n", wszPrinterName, lStatus, cbDevMode);
+            ERR("Couldn't query Attributes for Printer \"%S\", status is %lu!\n", wszPrinterName, dwErrorCode);
             continue;
         }
 
-        // Add this printer to the printer table.
-        if (!RtlInsertElementGenericTable(&PrinterTable, pPrinter, sizeof(LOCAL_PRINTER), NULL))
+        // Get the Status.
+        cbData = sizeof(DWORD);
+        dwErrorCode = (DWORD)RegQueryValueExW(hSubKey, L"Status", NULL, NULL, (PBYTE)&pPrinter->dwStatus, &cbData);
+        if (dwErrorCode != ERROR_SUCCESS)
         {
-            ERR("RtlInsertElementGenericTable failed with error %lu!\n", GetLastError());
+            ERR("Couldn't query Status for Printer \"%S\", status is %lu!\n", wszPrinterName, dwErrorCode);
+            continue;
+        }
+
+        // Add this printer to the printer list.
+        if (!InsertElementSkiplist(&PrinterList, pPrinter))
+        {
+            ERR("InsertElementSkiplist failed for Printer \"%S\"!\n", pPrinter->pwszPrinterName);
             goto Cleanup;
         }
 
@@ -181,56 +246,284 @@ InitializePrinterTable()
         pPrinter = NULL;
     }
 
+    dwErrorCode = ERROR_SUCCESS;
+
 Cleanup:
-    if (pwszPrintProcessor)
-        HeapFree(hProcessHeap, 0, pwszPrintProcessor);
+    // Inside the loop
+    if (hSubKey)
+        RegCloseKey(hSubKey);
 
     if (pPrinter)
     {
+        if (pPrinter->pDefaultDevMode)
+            DllFreeSplMem(pPrinter->pDefaultDevMode);
+
         if (pPrinter->pwszDefaultDatatype)
-            HeapFree(hProcessHeap, 0, pPrinter->pwszDefaultDatatype);
+            DllFreeSplStr(pPrinter->pwszDefaultDatatype);
 
-        HeapFree(hProcessHeap, 0, pPrinter);
+        if (pPrinter->pwszDescription)
+            DllFreeSplStr(pPrinter->pwszDescription);
+
+        if (pPrinter->pwszPrinterDriver)
+            DllFreeSplStr(pPrinter->pwszPrinterDriver);
+
+        if (pPrinter->pwszPrinterName)
+            DllFreeSplStr(pPrinter->pwszPrinterName);
+
+        DllFreeSplMem(pPrinter);
     }
 
-    if (hSubKey)
-        RegCloseKey(hSubKey);
+    if (pwszPrintProcessor)
+        DllFreeSplStr(pwszPrintProcessor);
 
+    // Outside the loop
     if (hKey)
         RegCloseKey(hKey);
+
+    SetLastError(dwErrorCode);
+    return (dwErrorCode == ERROR_SUCCESS);
 }
 
 
+DWORD
+_LocalEnumPrintersLevel1(DWORD Flags, LPWSTR Name, LPBYTE pPrinterEnum, DWORD cbBuf, LPDWORD pcbNeeded, LPDWORD pcReturned)
+{
+    const WCHAR wszComma[] = L",";
+
+    DWORD cbName;
+    DWORD cbComment;
+    DWORD cbDescription;
+    DWORD cchComputerName = 0;
+    DWORD dwErrorCode;
+    DWORD i;
+    PBYTE pPrinterInfo;
+    PBYTE pPrinterString;
+    PSKIPLIST_NODE pNode;
+    PLOCAL_PRINTER pPrinter;
+    PRINTER_INFO_1W PrinterInfo1;
+    WCHAR wszComputerName[2 + MAX_COMPUTERNAME_LENGTH + 1 + 1];
+
+    DWORD dwOffsets[] = {
+        FIELD_OFFSET(PRINTER_INFO_1W, pName),
+        FIELD_OFFSET(PRINTER_INFO_1W, pDescription),
+        FIELD_OFFSET(PRINTER_INFO_1W, pComment),
+        MAXDWORD
+    };
+
+    if (Flags & PRINTER_ENUM_NAME)
+    {
+        if (Name)
+        {
+            // The user supplied a Computer Name (with leading double backslashes) or Print Provider Name.
+            // Only process what's directed at us and dismiss every other request with ERROR_INVALID_NAME.
+            if (Name[0] == L'\\' && Name[1] == L'\\')
+            {
+                // Prepend slashes to the computer name.
+                wszComputerName[0] = L'\\';
+                wszComputerName[1] = L'\\';
+
+                // Get the local computer name for comparison.
+                cchComputerName = MAX_COMPUTERNAME_LENGTH + 1;
+                if (!GetComputerNameW(&wszComputerName[2], &cchComputerName))
+                {
+                    dwErrorCode = GetLastError();
+                    ERR("GetComputerNameW failed with error %lu!\n", dwErrorCode);
+                    goto Cleanup;
+                }
+
+                // Add the leading slashes to the total length.
+                cchComputerName += 2;
+
+                // Now compare this with the local computer name and reject if it doesn't match.
+                if (wcsicmp(&Name[2], &wszComputerName[2]) != 0)
+                {
+                    dwErrorCode = ERROR_INVALID_NAME;
+                    goto Cleanup;
+                }
+
+                // Add a trailing backslash to wszComputerName, which will later be prepended in front of the printer names.
+                wszComputerName[cchComputerName++] = L'\\';
+                wszComputerName[cchComputerName] = 0;
+            }
+            else if (wcsicmp(Name, wszPrintProviderInfo[0]) != 0)
+            {
+                // The user supplied a name that cannot be processed by the local print provider.
+                dwErrorCode = ERROR_INVALID_NAME;
+                goto Cleanup;
+            }
+        }
+        else
+        {
+            // The caller wants information about this Print Provider.
+            // spoolss packs this into an array of information about all Print Providers.
+            *pcbNeeded = sizeof(PRINTER_INFO_1W);
+
+            for (i = 0; i < 3; i++)
+                *pcbNeeded += (wcslen(wszPrintProviderInfo[i]) + 1) * sizeof(WCHAR);
+
+            // Check if the supplied buffer is large enough.
+            if (cbBuf < *pcbNeeded)
+            {
+                dwErrorCode = ERROR_INSUFFICIENT_BUFFER;
+                goto Cleanup;
+            }
+
+            // Copy over the print processor information.
+            ((PPRINTER_INFO_1W)pPrinterEnum)->Flags = 0;
+            PackStrings(wszPrintProviderInfo, pPrinterEnum, dwOffsets, &pPrinterEnum[*pcbNeeded]);
+            *pcReturned = 1;
+            dwErrorCode = ERROR_SUCCESS;
+            goto Cleanup;
+        }
+    }
+
+    // Count the required buffer size and the number of printers.
+    i = 0;
+
+    for (pNode = PrinterList.Head.Next[0]; pNode; pNode = pNode->Next[0])
+    {
+        pPrinter = (PLOCAL_PRINTER)pNode->Element;
+
+        // This looks wrong, but is totally right. PRINTER_INFO_1W has three members pName, pComment and pDescription.
+        // But pComment equals the "Description" registry value while pDescription is concatenated out of pName and pComment.
+        // On top of this, the computer name is prepended to the printer name if the user supplied the local computer name during the query.
+        cbName = (wcslen(pPrinter->pwszPrinterName) + 1) * sizeof(WCHAR);
+        cbComment = (wcslen(pPrinter->pwszDescription) + 1) * sizeof(WCHAR);
+        cbDescription = cchComputerName * sizeof(WCHAR) + cbName + cbComment + sizeof(WCHAR);
+
+        *pcbNeeded += sizeof(PRINTER_INFO_1W) + cchComputerName * sizeof(WCHAR) + cbName + cbComment + cbDescription;
+        i++;
+    }
+
+    // Check if the supplied buffer is large enough.
+    if (cbBuf < *pcbNeeded)
+    {
+        dwErrorCode = ERROR_INSUFFICIENT_BUFFER;
+        goto Cleanup;
+    }
+
+    // Put the strings right after the last PRINTER_INFO_1W structure.
+    // Due to all the required string processing, we can't just use PackStrings here :(
+    pPrinterInfo = pPrinterEnum;
+    pPrinterString = pPrinterEnum + i * sizeof(PRINTER_INFO_1W);
+
+    // Copy over the printer information.
+    for (pNode = PrinterList.Head.Next[0]; pNode; pNode = pNode->Next[0])
+    {
+        pPrinter = (PLOCAL_PRINTER)pNode->Element;
+
+        // FIXME: As for now, the Flags member returns no information.
+        PrinterInfo1.Flags = 0;
+
+        // Copy the printer name.
+        PrinterInfo1.pName = (PWSTR)pPrinterString;
+        CopyMemory(pPrinterString, wszComputerName, cchComputerName * sizeof(WCHAR));
+        pPrinterString += cchComputerName * sizeof(WCHAR);
+        cbName = (wcslen(pPrinter->pwszPrinterName) + 1) * sizeof(WCHAR);
+        CopyMemory(pPrinterString, pPrinter->pwszPrinterName, cbName);
+        pPrinterString += cbName;
+
+        // Copy the printer comment (equals the "Description" registry value).
+        PrinterInfo1.pComment = (PWSTR)pPrinterString;
+        cbComment = (wcslen(pPrinter->pwszDescription) + 1) * sizeof(WCHAR);
+        CopyMemory(pPrinterString, pPrinter->pwszDescription, cbComment);
+        pPrinterString += cbComment;
+
+        // Copy the description, which for PRINTER_INFO_1W has the form "Name,Comment,"
+        PrinterInfo1.pDescription = (PWSTR)pPrinterString;
+        CopyMemory(pPrinterString, wszComputerName, cchComputerName * sizeof(WCHAR));
+        pPrinterString += cchComputerName * sizeof(WCHAR);
+        CopyMemory(pPrinterString, pPrinter->pwszPrinterName, cbName - sizeof(WCHAR));
+        pPrinterString += cbName - sizeof(WCHAR);
+        CopyMemory(pPrinterString, wszComma, sizeof(WCHAR));
+        pPrinterString += sizeof(WCHAR);
+        CopyMemory(pPrinterString, pPrinter->pwszDescription, cbComment - sizeof(WCHAR));
+        pPrinterString += cbComment - sizeof(WCHAR);
+        CopyMemory(pPrinterString, wszComma, sizeof(wszComma));
+        pPrinterString += sizeof(wszComma);
+                
+        // Finally copy the structure and advance to the next one in the output buffer.
+        CopyMemory(pPrinterInfo, &PrinterInfo1, sizeof(PRINTER_INFO_1W));
+        pPrinterInfo += sizeof(PRINTER_INFO_1W);
+    }
+
+    *pcReturned = i;
+    dwErrorCode = ERROR_SUCCESS;
+
+Cleanup:
+    return dwErrorCode;
+}
+
 BOOL WINAPI
 LocalEnumPrinters(DWORD Flags, LPWSTR Name, DWORD Level, LPBYTE pPrinterEnum, DWORD cbBuf, LPDWORD pcbNeeded, LPDWORD pcReturned)
 {
-    ///////////// TODO /////////////////////
-    return FALSE;
+    DWORD dwErrorCode;
+
+    // Do no sanity checks here. This is verified by localspl_apitest!
+
+    // Begin counting.
+    *pcbNeeded = 0;
+    *pcReturned = 0;
+
+    // Think positive :)
+    // Treat it as success if the caller queried no information and we don't need to return any.
+    dwErrorCode = ERROR_SUCCESS;
+
+    if (Flags & PRINTER_ENUM_LOCAL)
+    {
+        // The function behaves quite differently for each level.
+        if (Level == 1)
+        {
+            dwErrorCode = _LocalEnumPrintersLevel1(Flags, Name, pPrinterEnum, cbBuf, pcbNeeded, pcReturned);
+        }
+        else
+        {
+            // TODO: Handle other levels.
+            // The caller supplied an invalid level.
+            dwErrorCode = ERROR_INVALID_LEVEL;
+            goto Cleanup;
+        }
+    }
+
+Cleanup:
+    SetLastError(dwErrorCode);
+    return (dwErrorCode == ERROR_SUCCESS);
 }
 
 BOOL WINAPI
 LocalOpenPrinter(PWSTR lpPrinterName, HANDLE* phPrinter, PPRINTER_DEFAULTSW pDefault)
 {
-    BOOL bReturnValue = ROUTER_UNKNOWN;
+    BOOL bReturnValue;
     DWORD cchComputerName;
-    DWORD cchPrinterName;
+    DWORD cchFirstParameter;
+    DWORD dwErrorCode;
     DWORD dwJobID;
+    HANDLE hExternalHandle;
     PWSTR p = lpPrinterName;
-    PWSTR pwszPrinterName = NULL;
+    PWSTR pwszFirstParameter = NULL;
+    PWSTR pwszSecondParameter = NULL;
     PLOCAL_JOB pJob;
-    PLOCAL_HANDLE pHandle;
+    PLOCAL_HANDLE pHandle = NULL;
+    PLOCAL_PORT pPort;
+    PLOCAL_PRINT_MONITOR pPrintMonitor;
     PLOCAL_PRINTER pPrinter;
     PLOCAL_PRINTER_HANDLE pPrinterHandle = NULL;
-    PLIST_ENTRY pEntry;
     WCHAR wszComputerName[MAX_COMPUTERNAME_LENGTH + 1];
+    WCHAR wszFullPath[MAX_PATH];
+
+    // TODO: lpPrinterName == NULL is supported and means access to the local printer server.
+    // Not sure yet if that is passed down to localspl.dll or processed in advance.
 
     // Sanity checks
     if (!lpPrinterName || !phPrinter)
     {
-        SetLastError(ERROR_INVALID_PARAMETER);
+        dwErrorCode = ERROR_INVALID_PARAMETER;
         goto Cleanup;
     }
 
+    *phPrinter = NULL;
+
+    // Skip any server name in the first parameter.
     // Does lpPrinterName begin with two backslashes to indicate a server name?
     if (lpPrinterName[0] == L'\\' && lpPrinterName[1] == L'\\')
     {
@@ -242,25 +535,25 @@ LocalOpenPrinter(PWSTR lpPrinterName, HANDLE* phPrinter, PPRINTER_DEFAULTSW pDef
         if (!p)
         {
             // We didn't get a proper server name.
-            SetLastError(ERROR_INVALID_PRINTER_NAME);
+            dwErrorCode = ERROR_INVALID_PRINTER_NAME;
             goto Cleanup;
         }
 
-        // Null-terminate the string here to enable comparison.
-        *p = 0;
-
         // Get the local computer name for comparison.
-        cchComputerName = sizeof(wszComputerName) / sizeof(WCHAR);
+        cchComputerName = _countof(wszComputerName);
         if (!GetComputerNameW(wszComputerName, &cchComputerName))
         {
-            ERR("GetComputerNameW failed with error %lu!\n", GetLastError());
+            dwErrorCode = GetLastError();
+            ERR("GetComputerNameW failed with error %lu!\n", dwErrorCode);
             goto Cleanup;
         }
 
-        // Now compare this with the local computer name and reject if it doesn't match, because this print provider only supports local printers.
-        if (wcsicmp(lpPrinterName, wszComputerName) != 0)
+        // Now compare this string excerpt with the local computer name.
+        // The input parameter may not be writable, so we can't null-terminate the input string at this point.
+        // This print provider only supports local printers, so both strings have to match.
+        if (p - lpPrinterName != cchComputerName || _wcsnicmp(lpPrinterName, wszComputerName, cchComputerName) != 0)
         {
-            SetLastError(ERROR_INVALID_PRINTER_NAME);
+            dwErrorCode = ERROR_INVALID_PRINTER_NAME;
             goto Cleanup;
         }
 
@@ -268,323 +561,569 @@ LocalOpenPrinter(PWSTR lpPrinterName, HANDLE* phPrinter, PPRINTER_DEFAULTSW pDef
         lpPrinterName = p + 1;
     }
 
-    // Look for a comma. If it exists, it indicates the end of the printer name.
-    p = wcschr(lpPrinterName, L',');
-    if (p)
-        cchPrinterName = p - lpPrinterName;
+    // Look for a comma. If it exists, it indicates the end of the first parameter.
+    pwszSecondParameter = wcschr(lpPrinterName, L',');
+    if (pwszSecondParameter)
+        cchFirstParameter = pwszSecondParameter - p;
     else
-        cchPrinterName = wcslen(lpPrinterName);
+        cchFirstParameter = wcslen(lpPrinterName);
 
-    // No printer name and no comma? This is invalid!
-    if (!cchPrinterName && !p)
+    // We must have at least one parameter.
+    if (!cchFirstParameter && !pwszSecondParameter)
     {
-        SetLastError(ERROR_INVALID_PRINTER_NAME);
+        dwErrorCode = ERROR_INVALID_PRINTER_NAME;
         goto Cleanup;
     }
 
-    // Do we have a printer name?
-    if (cchPrinterName)
+    // Do we have a first parameter?
+    if (cchFirstParameter)
     {
         // Yes, extract it.
-        pwszPrinterName = HeapAlloc(hProcessHeap, 0, (cchPrinterName + 1) * sizeof(WCHAR));
-        CopyMemory(pwszPrinterName, lpPrinterName, cchPrinterName * sizeof(WCHAR));
-        pwszPrinterName[cchPrinterName] = 0;
+        // No null-termination is necessary here, because DllAllocSplMem returns a zero-initialized buffer.
+        pwszFirstParameter = DllAllocSplMem((cchFirstParameter + 1) * sizeof(WCHAR));
+        CopyMemory(pwszFirstParameter, lpPrinterName, cchFirstParameter * sizeof(WCHAR));
+    }
+
+    // Do we have a second parameter?
+    if (pwszSecondParameter)
+    {
+        // Yes, skip the comma at the beginning.
+        ++pwszSecondParameter;
+
+        // Skip whitespace as well.
+        while (*pwszSecondParameter == L' ')
+            ++pwszSecondParameter;
+    }
+
+    // Create a new handle.
+    pHandle = DllAllocSplMem(sizeof(LOCAL_HANDLE));
+    if (!pHandle)
+    {
+        dwErrorCode = ERROR_NOT_ENOUGH_MEMORY;
+        ERR("DllAllocSplMem failed with error %lu!\n", GetLastError());
+        goto Cleanup;
+    }
+
+    // Now we can finally check the type of handle actually requested.
+    if (pwszFirstParameter && pwszSecondParameter && wcsncmp(pwszSecondParameter, L"Port", 4) == 0)
+    {
+        // The caller wants a port handle and provided a string like:
+        //    "LPT1:, Port"
+        //    "\\COMPUTERNAME\LPT1:, Port"
+        
+        // Look for this port in our Print Monitor Port list.
+        pPort = FindPort(pwszFirstParameter);
+        if (!pPort)
+        {
+            // The supplied port is unknown to all our Print Monitors.
+            dwErrorCode = ERROR_INVALID_PRINTER_NAME;
+            goto Cleanup;
+        }
+
+        pPrintMonitor = pPort->pPrintMonitor;
+
+        // Call the monitor's OpenPort function.
+        if (pPrintMonitor->bIsLevel2)
+            bReturnValue = ((PMONITOR2)pPrintMonitor->pMonitor)->pfnOpenPort(pPrintMonitor->hMonitor, pwszFirstParameter, &hExternalHandle);
+        else
+            bReturnValue = ((LPMONITOREX)pPrintMonitor->pMonitor)->Monitor.pfnOpenPort(pwszFirstParameter, &hExternalHandle);
+
+        if (!bReturnValue)
+        {
+            // The OpenPort function failed. Return its last error.
+            dwErrorCode = GetLastError();
+            goto Cleanup;
+        }
+
+        // Return the Port handle through our general handle.
+        pHandle->HandleType = HandleType_Port;
+        pHandle->pSpecificHandle = hExternalHandle;
+    }
+    else if (!pwszFirstParameter && pwszSecondParameter && wcsncmp(pwszSecondParameter, L"Xcv", 3) == 0)
+    {
+        // The caller wants an Xcv handle and provided a string like:
+        //    ", XcvMonitor Local Port"
+        //    "\\COMPUTERNAME\, XcvMonitor Local Port"
+        //    ", XcvPort LPT1:"
+        //    "\\COMPUTERNAME\, XcvPort LPT1:"
+
+        // Skip the "Xcv" string.
+        pwszSecondParameter += 3;
+
+        // Is XcvMonitor or XcvPort requested?
+        if (wcsncmp(pwszSecondParameter, L"Monitor ", 8) == 0)
+        {
+            // Skip the "Monitor " string.
+            pwszSecondParameter += 8;
+
+            // Look for this monitor in our Print Monitor list.
+            pPrintMonitor = FindPrintMonitor(pwszSecondParameter);
+            if (!pPrintMonitor)
+            {
+                // The caller supplied a non-existing Monitor name.
+                dwErrorCode = ERROR_INVALID_PRINTER_NAME;
+                goto Cleanup;
+            }
+        }
+        else if (wcsncmp(pwszSecondParameter, L"Port ", 5) == 0)
+        {
+            // Skip the "Port " string.
+            pwszSecondParameter += 5;
+
+            // Look for this port in our Print Monitor Port list.
+            pPort = FindPort(pwszFirstParameter);
+            if (!pPort)
+            {
+                // The supplied port is unknown to all our Print Monitors.
+                dwErrorCode = ERROR_INVALID_PRINTER_NAME;
+                goto Cleanup;
+            }
+
+            pPrintMonitor = pPort->pPrintMonitor;
+        }
+        else
+        {
+            dwErrorCode = ERROR_INVALID_PRINTER_NAME;
+            goto Cleanup;
+        }
+
+        // Call the monitor's XcvOpenPort function.
+        if (pPrintMonitor->bIsLevel2)
+            bReturnValue = ((PMONITOR2)pPrintMonitor->pMonitor)->pfnXcvOpenPort(pPrintMonitor->hMonitor, pwszSecondParameter, SERVER_EXECUTE, &hExternalHandle);
+        else
+            bReturnValue = ((LPMONITOREX)pPrintMonitor->pMonitor)->Monitor.pfnXcvOpenPort(pwszSecondParameter, SERVER_EXECUTE, &hExternalHandle);
+
+        if (!bReturnValue)
+        {
+            // The XcvOpenPort function failed. Return its last error.
+            dwErrorCode = GetLastError();
+            goto Cleanup;
+        }
 
-        // Retrieve the associated printer from the table.
-        pPrinter = RtlLookupElementGenericTable(&PrinterTable, pwszPrinterName);
+        // Return the Xcv handle through our general handle.
+        pHandle->HandleType = HandleType_Xcv;
+        pHandle->pSpecificHandle = hExternalHandle;
+    }
+    else
+    {
+        // The caller wants a Printer or Printer Job handle and provided a string like:
+        //    "HP DeskJet"
+        //    "\\COMPUTERNAME\HP DeskJet"
+        //    "HP DeskJet, Job 5"
+        //    "\\COMPUTERNAME\HP DeskJet, Job 5"
+
+        // Retrieve the printer from the list.
+        pPrinter = LookupElementSkiplist(&PrinterList, &pwszFirstParameter, NULL);
         if (!pPrinter)
         {
             // The printer does not exist.
-            SetLastError(ERROR_INVALID_PRINTER_NAME);
+            dwErrorCode = ERROR_INVALID_PRINTER_NAME;
             goto Cleanup;
         }
 
         // Create a new printer handle.
-        pPrinterHandle = HeapAlloc(hProcessHeap, HEAP_ZERO_MEMORY, sizeof(LOCAL_PRINTER_HANDLE));
-        pPrinterHandle->Printer = pPrinter;
+        pPrinterHandle = DllAllocSplMem(sizeof(LOCAL_PRINTER_HANDLE));
+        if (!pPrinterHandle)
+        {
+            dwErrorCode = ERROR_NOT_ENOUGH_MEMORY;
+            ERR("DllAllocSplMem failed with error %lu!\n", GetLastError());
+            goto Cleanup;
+        }
+
+        pPrinterHandle->hSPLFile = INVALID_HANDLE_VALUE;
+        pPrinterHandle->pPrinter = pPrinter;
 
         // Check if a datatype was given.
         if (pDefault && pDefault->pDatatype)
         {
             // Use the datatype if it's valid.
-            if (!RtlLookupElementGenericTable(&pPrinter->pPrintProcessor->DatatypeTable, pDefault->pDatatype))
+            if (!FindDatatype(pPrinter->pPrintProcessor, pDefault->pDatatype))
             {
-                SetLastError(ERROR_INVALID_DATATYPE);
+                dwErrorCode = ERROR_INVALID_DATATYPE;
                 goto Cleanup;
             }
 
-            pPrinterHandle->pwszDatatype = DuplicateStringW(pDefault->pDatatype);
+            pPrinterHandle->pwszDatatype = AllocSplStr(pDefault->pDatatype);
         }
         else
         {
             // Use the default datatype.
-            pPrinterHandle->pwszDatatype = DuplicateStringW(pPrinter->pwszDefaultDatatype);
+            pPrinterHandle->pwszDatatype = AllocSplStr(pPrinter->pwszDefaultDatatype);
         }
 
         // Check if a DevMode was given, otherwise use the default.
         if (pDefault && pDefault->pDevMode)
-            CopyMemory(&pPrinterHandle->DevMode, pDefault->pDevMode, sizeof(DEVMODEW));
+            pPrinterHandle->pDevMode = DuplicateDevMode(pDefault->pDevMode);
         else
-            CopyMemory(&pPrinterHandle->DevMode, &pPrinter->DefaultDevMode, sizeof(DEVMODEW));
+            pPrinterHandle->pDevMode = DuplicateDevMode(pPrinter->pDefaultDevMode);
 
-        // Did we have a comma? Then the user may want a handle to an existing job instead of creating a new job.
-        if (p)
+        // Check if the caller wants a handle to an existing Print Job.
+        if (pwszSecondParameter)
         {
-            ++p;
-            
-            // Skip whitespace.
-            do
-            {
-                ++p;
-            }
-            while (*p == ' ');
-
             // The "Job " string has to follow now.
-            if (wcscmp(p, L"Job ") != 0)
+            if (wcsncmp(pwszSecondParameter, L"Job ", 4) != 0)
             {
-                SetLastError(ERROR_INVALID_PRINTER_NAME);
+                dwErrorCode = ERROR_INVALID_PRINTER_NAME;
                 goto Cleanup;
             }
 
             // Skip the "Job " string. 
-            p += sizeof("Job ") - 1;
+            pwszSecondParameter += 4;
 
             // Skip even more whitespace.
-            while (*p == ' ')
-                ++p;
+            while (*pwszSecondParameter == ' ')
+                ++pwszSecondParameter;
 
             // Finally extract the desired Job ID.
-            dwJobID = wcstoul(p, NULL, 10);
+            dwJobID = wcstoul(pwszSecondParameter, NULL, 10);
             if (!IS_VALID_JOB_ID(dwJobID))
             {
                 // The user supplied an invalid Job ID.
-                SetLastError(ERROR_INVALID_PRINTER_NAME);
+                dwErrorCode = ERROR_INVALID_PRINTER_NAME;
                 goto Cleanup;
             }
 
-            // Look for this job in the job queue of the printer.
-            pEntry = pPrinter->JobQueue.Flink;
-
-            for (;;)
+            // Look for this job in the Global Job List.
+            pJob = LookupElementSkiplist(&GlobalJobList, &dwJobID, NULL);
+            if (!pJob || pJob->pPrinter != pPrinter)
             {
-                if (pEntry == &pPrinter->JobQueue)
-                {
-                    // We have reached the end of the list without finding the desired Job ID.
-                    SetLastError(ERROR_INVALID_PRINTER_NAME);
-                    goto Cleanup;
-                }
-
-                // Get our job structure.
-                pJob = CONTAINING_RECORD(pEntry, LOCAL_JOB, Entry);
-
-                if (pJob->dwJobID == dwJobID)
-                {
-                    // We have found the desired job. Give the caller a printer handle referencing it.
-                    pPrinterHandle->StartedJob = pJob;
-                    break;
-                }
-
-                pEntry = pEntry->Flink;
+                // The user supplied a non-existing Job ID or the Job ID does not belong to the supplied printer name.
+                dwErrorCode = ERROR_INVALID_PRINTER_NAME;
+                goto Cleanup;
             }
-        }
-
-        // Create a new handle that references a printer.
-        pHandle = HeapAlloc(hProcessHeap, 0, sizeof(LOCAL_HANDLE));
-        pHandle->HandleType = Printer;
-        pHandle->SpecificHandle = pPrinterHandle;
-    }
-    else
-    {
-        // No printer name, but we have a comma!
-        // This may be a request to a XcvMonitor or XcvPort handle.
-        ++p;
-
-        // Skip whitespace.
-        do
-        {
-            ++p;
-        }
-        while (*p == ' ');
 
-        // Check if this is a request to a XcvMonitor.
-        if (wcscmp(p, L"XcvMonitor ") == 0)
-        {
-            // Skip the "XcvMonitor " string. 
-            p += sizeof("XcvMonitor ") - 1;
+            // Try to open its SPL file.
+            GetJobFilePath(L"SPL", dwJobID, wszFullPath);
+            pPrinterHandle->hSPLFile = CreateFileW(wszFullPath, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
+            if (pPrinterHandle->hSPLFile == INVALID_HANDLE_VALUE)
+            {
+                dwErrorCode = GetLastError();
+                ERR("CreateFileW failed with error %lu for \"%S\"!", dwErrorCode, wszFullPath);
+                goto Cleanup;
+            }
 
-            ///////////// TODO /////////////////////
-            pHandle = HeapAlloc(hProcessHeap, 0, sizeof(LOCAL_HANDLE));
-            pHandle->HandleType = Monitor;
-            //pHandle->SpecificHandle = pMonitorHandle;
+            // Associate the job to our Printer Handle, but don't set bStartedDoc.
+            // This prevents the caller from doing further StartDocPrinter, WritePrinter, etc. calls on it.
+            pPrinterHandle->pJob = pJob;
         }
-        else if (wcscmp(p, L"XcvPort ") == 0)
-        {
-            // Skip the "XcvPort " string. 
-            p += sizeof("XcvPort ") - 1;
 
-            //////////// TODO //////////////////////
-            pHandle = HeapAlloc(hProcessHeap, 0, sizeof(LOCAL_HANDLE));
-            pHandle->HandleType = Port;
-            //pHandle->SpecificHandle = pPortHandle;
-        }
-        else
-        {
-            SetLastError(ERROR_INVALID_PRINTER_NAME);
-            goto Cleanup;
-        }
+        // Return the Printer handle through our general handle.
+        pHandle->HandleType = HandleType_Printer;
+        pHandle->pSpecificHandle = pPrinterHandle;
     }
 
+    // We were successful! Return the handle.
     *phPrinter = (HANDLE)pHandle;
-    bReturnValue = ROUTER_SUCCESS;
+    dwErrorCode = ERROR_SUCCESS;
 
     // Don't let the cleanup routines free this.
+    pHandle = NULL;
     pPrinterHandle = NULL;
-    pwszPrinterName = NULL;
 
 Cleanup:
+    if (pHandle)
+        DllFreeSplMem(pHandle);
+
     if (pPrinterHandle)
     {
         if (pPrinterHandle->pwszDatatype)
-            HeapFree(hProcessHeap, 0, pPrinterHandle->pwszDatatype);
+            DllFreeSplStr(pPrinterHandle->pwszDatatype);
+
+        if (pPrinterHandle->pDevMode)
+            DllFreeSplMem(pPrinterHandle->pDevMode);
 
-        HeapFree(hProcessHeap, 0, pPrinterHandle);
+        DllFreeSplMem(pPrinterHandle);
     }
 
-    if (pwszPrinterName)
-        HeapFree(hProcessHeap, 0, pwszPrinterName);
+    if (pwszFirstParameter)
+        DllFreeSplMem(pwszFirstParameter);
 
-    return bReturnValue;
+    SetLastError(dwErrorCode);
+    return (dwErrorCode == ERROR_SUCCESS);
 }
 
-DWORD WINAPI
-LocalStartDocPrinter(HANDLE hPrinter, DWORD Level, LPBYTE pDocInfo)
+BOOL WINAPI
+LocalReadPrinter(HANDLE hPrinter, PVOID pBuf, DWORD cbBuf, PDWORD pNoBytesRead)
 {
-    DWORD dwReturnValue = 0;
-    PDOC_INFO_1W pDocumentInfo1;
-    PLOCAL_HANDLE pHandle;
+    DWORD dwErrorCode;
+    PLOCAL_HANDLE pHandle = (PLOCAL_HANDLE)hPrinter;
     PLOCAL_PRINTER_HANDLE pPrinterHandle;
-    PLOCAL_JOB pJob;
 
-    // Sanity checks
-    if (!pDocInfo)
+    // Sanity checks.
+    if (!pHandle || pHandle->HandleType != HandleType_Printer)
     {
-        SetLastError(ERROR_INVALID_PARAMETER);
-        return 0;
+        dwErrorCode = ERROR_INVALID_HANDLE;
+        goto Cleanup;
     }
 
-    if (!hPrinter)
+    pPrinterHandle = (PLOCAL_PRINTER_HANDLE)pHandle->pSpecificHandle;
+
+    // ReadPrinter needs an opened SPL file to work.
+    // This only works if a Printer Job Handle was requested in OpenPrinter.
+    if (pPrinterHandle->hSPLFile == INVALID_HANDLE_VALUE)
     {
-        SetLastError(ERROR_INVALID_HANDLE);
-        return 0;
+        dwErrorCode = ERROR_INVALID_HANDLE;
+        goto Cleanup;
     }
 
-    // Check if this is a printer handle.
-    pHandle = (PLOCAL_HANDLE)hPrinter;
-    if (pHandle->HandleType != Printer)
+    // Pass the parameters to ReadFile.
+    if (!ReadFile(pPrinterHandle->hSPLFile, pBuf, cbBuf, pNoBytesRead, NULL))
     {
-        SetLastError(ERROR_INVALID_HANDLE);
-        return 0;
+        dwErrorCode = GetLastError();
+        ERR("ReadFile failed with error %lu!\n", dwErrorCode);
+        goto Cleanup;
     }
 
-    pPrinterHandle = (PLOCAL_PRINTER_HANDLE)pHandle->SpecificHandle;
+    dwErrorCode = ERROR_SUCCESS;
 
-    // Check if this is the right document information level.
-    if (Level != 1)
+Cleanup:
+    SetLastError(dwErrorCode);
+    return (dwErrorCode == ERROR_SUCCESS);
+}
+
+DWORD WINAPI
+LocalStartDocPrinter(HANDLE hPrinter, DWORD Level, PBYTE pDocInfo)
+{
+    DWORD dwErrorCode;
+    DWORD dwReturnValue = 0;
+    PDOC_INFO_1W pDocInfo1 = (PDOC_INFO_1W)pDocInfo;
+    PLOCAL_HANDLE pHandle = (PLOCAL_HANDLE)hPrinter;
+    PLOCAL_PRINTER_HANDLE pPrinterHandle;
+
+    // Sanity checks.
+    if (!pDocInfo1)
     {
-        SetLastError(ERROR_INVALID_LEVEL);
-        return 0;
+        dwErrorCode = ERROR_INVALID_PARAMETER;
+        goto Cleanup;
     }
 
-    pDocumentInfo1 = (PDOC_INFO_1W)pDocInfo;
+    if (!pHandle || pHandle->HandleType != HandleType_Printer)
+    {
+        dwErrorCode = ERROR_INVALID_HANDLE;
+        goto Cleanup;
+    }
 
-    // Create a new job.
-    pJob = HeapAlloc(hProcessHeap, HEAP_ZERO_MEMORY, sizeof(LOCAL_JOB));
-    pJob->Printer = pPrinterHandle->Printer;
+    pPrinterHandle = (PLOCAL_PRINTER_HANDLE)pHandle->pSpecificHandle;
 
-    // Check if a datatype was given.
-    if (pDocumentInfo1->pDatatype)
+    // pJob may already be occupied if this is a Print Job handle. In this case, StartDocPrinter has to fail.
+    if (pPrinterHandle->pJob)
     {
-        // Use the datatype if it's valid.
-        if (!RtlLookupElementGenericTable(&pJob->Printer->pPrintProcessor->DatatypeTable, pDocumentInfo1->pDatatype))
-        {
-            SetLastError(ERROR_INVALID_DATATYPE);
-            goto Cleanup;
-        }
+        dwErrorCode = ERROR_INVALID_PARAMETER;
+        goto Cleanup;
+    }
 
-        pJob->pwszDatatype = DuplicateStringW(pDocumentInfo1->pDatatype);
+    // Check the validity of the datatype if we got one.
+    if (pDocInfo1->pDatatype && !FindDatatype(pPrinterHandle->pJob->pPrintProcessor, pDocInfo1->pDatatype))
+    {
+        dwErrorCode = ERROR_INVALID_DATATYPE;
+        goto Cleanup;
     }
-    else
+
+    // Check if this is the right document information level.
+    if (Level != 1)
     {
-        // Use the printer handle datatype.
-        pJob->pwszDatatype = DuplicateStringW(pPrinterHandle->pwszDatatype);
+        dwErrorCode = ERROR_INVALID_LEVEL;
+        goto Cleanup;
     }
 
-    // Copy over printer defaults.
-    CopyMemory(&pJob->DevMode, &pPrinterHandle->DevMode, sizeof(DEVMODEW));
+    // All requirements are met - create a new job.
+    dwErrorCode = CreateJob(pPrinterHandle);
+    if (dwErrorCode != ERROR_SUCCESS)
+        goto Cleanup;
 
-    // Copy over supplied information.
-    if (pDocumentInfo1->pDocName)
-        pJob->pwszDocumentName = DuplicateStringW(pDocumentInfo1->pDocName);
+    // Use any given datatype.
+    if (pDocInfo1->pDatatype && !ReallocSplStr(&pPrinterHandle->pJob->pwszDatatype, pDocInfo1->pDatatype))
+    {
+        dwErrorCode = ERROR_NOT_ENOUGH_MEMORY;
+        ERR("ReallocSplStr failed, last error is %lu!\n", GetLastError());
+        goto Cleanup;
+    }
 
-    if (pDocumentInfo1->pOutputFile)
-        pJob->pwszOutputFile = DuplicateStringW(pDocumentInfo1->pOutputFile);
+    // Use any given document name.
+    if (pDocInfo1->pDocName && !ReallocSplStr(&pPrinterHandle->pJob->pwszDocumentName, pDocInfo1->pDocName))
+    {
+        dwErrorCode = ERROR_NOT_ENOUGH_MEMORY;
+        ERR("ReallocSplStr failed, last error is %lu!\n", GetLastError());
+        goto Cleanup;
+    }
 
-    // Enqueue the job.
-    ///////////// TODO /////////////////////
+    // We were successful!
+    dwErrorCode = ERROR_SUCCESS;
+    dwReturnValue = pPrinterHandle->pJob->dwJobID;
 
 Cleanup:
-    if (pJob)
-        HeapFree(hProcessHeap, 0, pJob);
-
+    SetLastError(dwErrorCode);
     return dwReturnValue;
 }
 
 BOOL WINAPI
 LocalStartPagePrinter(HANDLE hPrinter)
 {
-    ///////////// TODO /////////////////////
-    return FALSE;
+    DWORD dwErrorCode;
+    PLOCAL_HANDLE pHandle = (PLOCAL_HANDLE)hPrinter;
+    PLOCAL_PRINTER_HANDLE pPrinterHandle;
+
+    // Sanity checks.
+    if (!pHandle || pHandle->HandleType != HandleType_Printer)
+    {
+        dwErrorCode = ERROR_INVALID_HANDLE;
+        goto Cleanup;
+    }
+
+    pPrinterHandle = (PLOCAL_PRINTER_HANDLE)pHandle->pSpecificHandle;
+
+    // We require StartDocPrinter or AddJob to be called first.
+    if (!pPrinterHandle->bStartedDoc)
+    {
+        dwErrorCode = ERROR_SPL_NO_STARTDOC;
+        goto Cleanup;
+    }
+
+    // Increase the page count.
+    ++pPrinterHandle->pJob->dwTotalPages;
+    dwErrorCode = ERROR_SUCCESS;
+
+Cleanup:
+    SetLastError(dwErrorCode);
+    return (dwErrorCode == ERROR_SUCCESS);
 }
 
 BOOL WINAPI
 LocalWritePrinter(HANDLE hPrinter, LPVOID pBuf, DWORD cbBuf, LPDWORD pcWritten)
 {
-    ///////////// TODO /////////////////////
-    return FALSE;
+    DWORD dwErrorCode;
+    PLOCAL_HANDLE pHandle = (PLOCAL_HANDLE)hPrinter;
+    PLOCAL_PRINTER_HANDLE pPrinterHandle;
+
+    // Sanity checks.
+    if (!pHandle || pHandle->HandleType != HandleType_Printer)
+    {
+        dwErrorCode = ERROR_INVALID_HANDLE;
+        goto Cleanup;
+    }
+
+    pPrinterHandle = (PLOCAL_PRINTER_HANDLE)pHandle->pSpecificHandle;
+
+    // We require StartDocPrinter or AddJob to be called first.
+    if (!pPrinterHandle->bStartedDoc)
+    {
+        dwErrorCode = ERROR_SPL_NO_STARTDOC;
+        goto Cleanup;
+    }
+
+    // TODO: This function is only called when doing non-spooled printing.
+    // This needs to be investigated further. We can't just use pPrinterHandle->hSPLFile here, because that's currently reserved for Printer Job handles (see LocalReadPrinter).
+#if 0
+    // Pass the parameters to WriteFile.
+    if (!WriteFile(SOME_SPOOL_FILE_HANDLE, pBuf, cbBuf, pcWritten, NULL))
+    {
+        dwErrorCode = GetLastError();
+        ERR("WriteFile failed with error %lu!\n", GetLastError());
+        goto Cleanup;
+    }
+#endif
+
+    dwErrorCode = ERROR_SUCCESS;
+
+Cleanup:
+    SetLastError(dwErrorCode);
+    return (dwErrorCode == ERROR_SUCCESS);
 }
 
 BOOL WINAPI
 LocalEndPagePrinter(HANDLE hPrinter)
 {
-    ///////////// TODO /////////////////////
-    return FALSE;
+    DWORD dwErrorCode;
+    PLOCAL_HANDLE pHandle = (PLOCAL_HANDLE)hPrinter;
+
+    // Sanity checks.
+    if (!pHandle || pHandle->HandleType != HandleType_Printer)
+    {
+        dwErrorCode = ERROR_INVALID_HANDLE;
+        goto Cleanup;
+    }
+
+    // This function doesn't do anything else for now.
+    dwErrorCode = ERROR_SUCCESS;
+
+Cleanup:
+    SetLastError(dwErrorCode);
+    return (dwErrorCode == ERROR_SUCCESS);
 }
 
 BOOL WINAPI
 LocalEndDocPrinter(HANDLE hPrinter)
 {
-    ///////////// TODO /////////////////////
-    return FALSE;
+    DWORD dwErrorCode;
+    PLOCAL_HANDLE pHandle = (PLOCAL_HANDLE)hPrinter;
+    PLOCAL_PRINTER_HANDLE pPrinterHandle;
+
+    // Sanity checks.
+    if (!pHandle || pHandle->HandleType != HandleType_Printer)
+    {
+        dwErrorCode = ERROR_INVALID_HANDLE;
+        goto Cleanup;
+    }
+
+    pPrinterHandle = (PLOCAL_PRINTER_HANDLE)pHandle->pSpecificHandle;
+
+    // We require StartDocPrinter or AddJob to be called first.
+    if (!pPrinterHandle->bStartedDoc)
+    {
+        dwErrorCode = ERROR_SPL_NO_STARTDOC;
+        goto Cleanup;
+    }
+
+    // TODO: Something like ScheduleJob
+
+    // Finish the job.
+    pPrinterHandle->bStartedDoc = FALSE;
+    pPrinterHandle->pJob = NULL;
+    dwErrorCode = ERROR_SUCCESS;
+
+Cleanup:
+    SetLastError(dwErrorCode);
+    return (dwErrorCode == ERROR_SUCCESS);
 }
 
 BOOL WINAPI
 LocalClosePrinter(HANDLE hPrinter)
 {
-    PLOCAL_HANDLE pHandle;
+    PLOCAL_HANDLE pHandle = (PLOCAL_HANDLE)hPrinter;
+    PLOCAL_PRINTER_HANDLE pPrinterHandle;
 
-    if (!hPrinter)
+    if (!pHandle)
     {
         SetLastError(ERROR_INVALID_HANDLE);
         return FALSE;
     }
 
-    pHandle = (PLOCAL_HANDLE)hPrinter;
+    if (pHandle->HandleType == HandleType_Port)
+    {
+        // TODO
+    }
+    else if (pHandle->HandleType == HandleType_Printer)
+    {
+        pPrinterHandle = (PLOCAL_PRINTER_HANDLE)pHandle->pSpecificHandle;
+
+        // Terminate any started job.
+        if (pPrinterHandle->pJob)
+            FreeJob(pPrinterHandle->pJob);
 
-    ///////////// TODO /////////////////////
-    /// Check the handle type, do thoroughful checks on all data fields and clean them.
-    ////////////////////////////////////////
+        // Free memory for the fields.
+        DllFreeSplMem(pPrinterHandle->pDevMode);
+        DllFreeSplStr(pPrinterHandle->pwszDatatype);
+
+        // Free memory for the printer handle itself.
+        DllFreeSplMem(pPrinterHandle);
+    }
+    else if (pHandle->HandleType == HandleType_Xcv)
+    {
+        // TODO
+    }
 
-    HeapFree(hProcessHeap, 0, pHandle);
+    // Free memory for the handle itself.
+    DllFreeSplMem(pHandle);
 
     return TRUE;
 }