2 * PROJECT: ReactOS Local Spooler
3 * LICENSE: GNU LGPL v2.1 or any later version as published by the Free Software Foundation
4 * PURPOSE: Implementation of the Thread that actually performs the printing process
5 * COPYRIGHT: Copyright 2015 Colin Finck <colin@reactos.org>
11 PrintingThreadProc(PLOCAL_JOB pJob
)
13 const DWORD cchMaxJobIdDigits
= 5; // Job ID is limited to 5 decimal digits, see IS_VALID_JOB_ID
14 const WCHAR wszJobAppendix
[] = L
", Job ";
15 const DWORD cchJobAppendix
= _countof(wszJobAppendix
) - 1;
16 const WCHAR wszPortAppendix
[] = L
", Port";
21 HANDLE hPrintProcessor
= NULL
;
22 PLOCAL_PRINT_PROCESSOR pPrintProcessor
= pJob
->pPrintProcessor
;
23 PRINTPROCESSOROPENDATA OpenData
;
24 PWSTR pwszPrinterAndJob
= NULL
;
25 PWSTR pwszPrinterPort
= NULL
;
26 PWSTR pwszSPLFile
= NULL
;
28 // Prepare the pPrinterName parameter.
29 // This is the string for LocalOpenPrinter to open a port (e.g. "LPT1:, Port").
30 cchPortName
= wcslen(pJob
->pPrinter
->pPort
->pwszName
);
31 pwszPrinterPort
= DllAllocSplMem(cchPortName
* sizeof(WCHAR
) + sizeof(wszPortAppendix
));
34 dwErrorCode
= ERROR_NOT_ENOUGH_MEMORY
;
35 ERR("DllAllocSplMem failed with error %lu!\n", GetLastError());
39 CopyMemory(pwszPrinterPort
, pJob
->pPrinter
->pPort
->pwszName
, cchPortName
* sizeof(WCHAR
));
40 CopyMemory(&pwszPrinterPort
[cchPortName
], wszPortAppendix
, sizeof(wszPortAppendix
));
42 // Prepare the pPrintProcessorOpenData parameter.
43 OpenData
.JobId
= pJob
->dwJobID
;
44 OpenData
.pDatatype
= pJob
->pwszDatatype
;
45 OpenData
.pDevMode
= pJob
->pDevMode
;
46 OpenData
.pDocumentName
= pJob
->pwszDocumentName
;
47 OpenData
.pOutputFile
= NULL
;
48 OpenData
.pParameters
= pJob
->pwszPrintProcessorParameters
;
49 OpenData
.pPrinterName
= pJob
->pPrinter
->pwszPrinterName
;
51 // Open a handle to the Print Processor.
52 hPrintProcessor
= pPrintProcessor
->pfnOpenPrintProcessor(pwszPrinterPort
, &OpenData
);
55 dwErrorCode
= GetLastError();
56 ERR("OpenPrintProcessor failed with error %lu!\n", dwErrorCode
);
60 // Let other functions use the Print Processor as well while it's opened.
61 pJob
->hPrintProcessor
= hPrintProcessor
;
63 // Prepare the pDocumentName parameter.
64 cchPrinterName
= wcslen(OpenData
.pPrinterName
);
65 pwszPrinterAndJob
= DllAllocSplMem((cchPrinterName
+ cchJobAppendix
+ cchMaxJobIdDigits
+ 1) * sizeof(WCHAR
));
66 if (!pwszPrinterAndJob
)
68 dwErrorCode
= ERROR_NOT_ENOUGH_MEMORY
;
69 ERR("DllAllocSplMem failed with error %lu!\n", GetLastError());
73 CopyMemory(pwszPrinterAndJob
, OpenData
.pPrinterName
, cchPrinterName
* sizeof(WCHAR
));
74 CopyMemory(&pwszPrinterAndJob
[cchPrinterName
], wszJobAppendix
, cchJobAppendix
* sizeof(WCHAR
));
75 _ultow(OpenData
.JobId
, &pwszPrinterAndJob
[cchPrinterName
+ cchJobAppendix
], 10);
77 // Print the document.
78 // Note that pJob is freed after this function, so we may not access it anymore.
79 if (!pPrintProcessor
->pfnPrintDocumentOnPrintProcessor(hPrintProcessor
, pwszPrinterAndJob
))
81 dwErrorCode
= GetLastError();
82 ERR("PrintDocumentOnPrintProcessor failed with error %lu!\n", dwErrorCode
);
86 // Close the Print Processor.
87 pPrintProcessor
->pfnClosePrintProcessor(hPrintProcessor
);
88 hPrintProcessor
= NULL
;
90 // Delete the spool file.
91 pwszSPLFile
= DllAllocSplMem(GetJobFilePath(L
"SPL", 0, NULL
));
94 dwErrorCode
= ERROR_NOT_ENOUGH_MEMORY
;
95 ERR("DllAllocSplMem failed with error %lu!\n", GetLastError());
99 GetJobFilePath(L
"SPL", OpenData
.JobId
, pwszSPLFile
);
100 DeleteFileW(pwszSPLFile
);
102 // We were successful!
103 dwErrorCode
= ERROR_SUCCESS
;
107 pPrintProcessor
->pfnClosePrintProcessor(hPrintProcessor
);
110 DllFreeSplMem(pwszPrinterPort
);
112 if (pwszPrinterAndJob
)
113 DllFreeSplMem(pwszPrinterAndJob
);
116 DllFreeSplMem(pwszSPLFile
);