ReactOS  0.4.15-dev-499-g1f31905
printingthread.c File Reference
#include "precomp.h"
Include dependency graph for printingthread.c:

Go to the source code of this file.

Functions

DWORD WINAPI PrintingThreadProc (PLOCAL_JOB pJob)
 

Function Documentation

◆ PrintingThreadProc()

DWORD WINAPI PrintingThreadProc ( PLOCAL_JOB  pJob)

Definition at line 11 of file printingthread.c.

12 {
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";
17 
18  DWORD cchPortName;
19  DWORD cchPrinterName;
20  DWORD dwErrorCode;
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;
27 
28  TRACE("PrintingThreadProc(%p)\n", pJob);
29 
30  // Prepare the pPrinterName parameter.
31  // This is the string for LocalOpenPrinter to open a port (e.g. "LPT1:, Port").
32  cchPortName = wcslen(pJob->pPrinter->pPort->pwszName);
33  pwszPrinterPort = DllAllocSplMem(cchPortName * sizeof(WCHAR) + sizeof(wszPortAppendix));
34  if (!pwszPrinterPort)
35  {
36  dwErrorCode = ERROR_NOT_ENOUGH_MEMORY;
37  ERR("DllAllocSplMem failed!\n");
38  goto Cleanup;
39  }
40 
41  CopyMemory(pwszPrinterPort, pJob->pPrinter->pPort->pwszName, cchPortName * sizeof(WCHAR));
42  CopyMemory(&pwszPrinterPort[cchPortName], wszPortAppendix, sizeof(wszPortAppendix));
43 
44  // Prepare the pPrintProcessorOpenData parameter.
45  OpenData.JobId = pJob->dwJobID;
46  OpenData.pDatatype = pJob->pwszDatatype;
47  OpenData.pDevMode = pJob->pDevMode;
48  OpenData.pDocumentName = pJob->pwszDocumentName;
49  OpenData.pOutputFile = NULL;
51  OpenData.pPrinterName = pJob->pPrinter->pwszPrinterName;
52 
53  // Associate our job to the port. The next port handle created through LocalOpenPrinter will pick this up.
54  // LocalStartDocPrinter needs this information to call StartDocPort of the Print Monitor, but as the parameters
55  // for LocalOpenPrinter and LocalStartDocPrinter are fixed, we can only pass over the information this way.
56  pJob->pPrinter->pPort->pNextJobToProcess = pJob;
57 
58  // Open a handle to the Print Processor.
59  hPrintProcessor = pPrintProcessor->pfnOpenPrintProcessor(pwszPrinterPort, &OpenData);
60  if (!hPrintProcessor)
61  {
62  dwErrorCode = GetLastError();
63  ERR("OpenPrintProcessor failed with error %lu!\n", dwErrorCode);
64  goto Cleanup;
65  }
66 
67  // Let other functions use the Print Processor as well while it's opened.
68  pJob->hPrintProcessor = hPrintProcessor;
69 
70  // Prepare the pDocumentName parameter.
71  cchPrinterName = wcslen(OpenData.pPrinterName);
72  pwszPrinterAndJob = DllAllocSplMem((cchPrinterName + cchJobAppendix + cchMaxJobIdDigits + 1) * sizeof(WCHAR));
73  if (!pwszPrinterAndJob)
74  {
75  dwErrorCode = ERROR_NOT_ENOUGH_MEMORY;
76  ERR("DllAllocSplMem failed!\n");
77  goto Cleanup;
78  }
79 
80  CopyMemory(pwszPrinterAndJob, OpenData.pPrinterName, cchPrinterName * sizeof(WCHAR));
81  CopyMemory(&pwszPrinterAndJob[cchPrinterName], wszJobAppendix, cchJobAppendix * sizeof(WCHAR));
82  _ultow(OpenData.JobId, &pwszPrinterAndJob[cchPrinterName + cchJobAppendix], 10);
83 
84  // Printing starts here.
86 
87  // Print the document.
88  // Note that pJob is freed after this function, so we may not access it anymore.
89  if (!pPrintProcessor->pfnPrintDocumentOnPrintProcessor(hPrintProcessor, pwszPrinterAndJob))
90  {
91  dwErrorCode = GetLastError();
92  ERR("PrintDocumentOnPrintProcessor failed with error %lu!\n", dwErrorCode);
93  goto Cleanup;
94  }
95 
96  // Close the Print Processor.
97  pPrintProcessor->pfnClosePrintProcessor(hPrintProcessor);
98  hPrintProcessor = NULL;
99 
100  // Delete the spool file.
101  pwszSPLFile = DllAllocSplMem(GetJobFilePath(L"SPL", 0, NULL));
102  if (!pwszSPLFile)
103  {
104  dwErrorCode = ERROR_NOT_ENOUGH_MEMORY;
105  ERR("DllAllocSplMem failed!\n");
106  goto Cleanup;
107  }
108 
109  GetJobFilePath(L"SPL", OpenData.JobId, pwszSPLFile);
110  DeleteFileW(pwszSPLFile);
111 
112  // We were successful!
113  dwErrorCode = ERROR_SUCCESS;
114 
115 Cleanup:
116  if (hPrintProcessor)
117  pPrintProcessor->pfnClosePrintProcessor(hPrintProcessor);
118 
119  if (pwszPrinterPort)
120  DllFreeSplMem(pwszPrinterPort);
121 
122  if (pwszPrinterAndJob)
123  DllFreeSplMem(pwszPrinterAndJob);
124 
125  if (pwszSPLFile)
126  DllFreeSplMem(pwszSPLFile);
127 
128  return dwErrorCode;
129 }
_CRTIMP wchar_t *__cdecl _ultow(_In_ unsigned long _Value, _Pre_notnull_ _Post_z_ wchar_t *_Dest, _In_ int _Radix)
#define JOB_STATUS_PRINTING
Definition: winspool.h:342
PWSTR pwszName
Definition: precomp.h:86
#define ERROR_SUCCESS
Definition: deptool.c:10
PPrintDocumentOnPrintProcessor pfnPrintDocumentOnPrintProcessor
Definition: precomp.h:105
uint16_t * PWSTR
Definition: typedefs.h:55
BOOL WINAPI DllFreeSplMem(PVOID pMem)
Definition: memory.c:112
PLOCAL_JOB pNextJobToProcess
Definition: precomp.h:88
#define ERROR_NOT_ENOUGH_MEMORY
Definition: dderror.h:7
DWORD WINAPI GetLastError(VOID)
Definition: except.c:1059
PDEVMODEW pDevMode
Definition: precomp.h:157
DWORD dwJobID
Definition: precomp.h:136
BOOL WINAPI DeleteFileW(IN LPCWSTR lpFileName)
Definition: delete.c:39
PWSTR pwszDatatype
Definition: precomp.h:147
smooth NULL
Definition: ftsmooth.c:416
DWORD GetJobFilePath(PCWSTR pwszExtension, DWORD dwJobID, PWSTR pwszOutput)
Definition: jobs.c:146
PWSTR pwszPrintProcessorParameters
Definition: precomp.h:149
DWORD dwStatus
Definition: precomp.h:155
#define TRACE(s)
Definition: solgame.cpp:4
__wchar_t WCHAR
Definition: xmlstorage.h:180
#define _countof(array)
Definition: sndvol32.h:68
PLOCAL_PRINTER pPrinter
Definition: precomp.h:140
#define CopyMemory
Definition: winbase.h:1646
unsigned long DWORD
Definition: ntddk_ex.h:95
static const WCHAR L[]
Definition: oid.c:1250
PClosePrintProcessor pfnClosePrintProcessor
Definition: precomp.h:100
static const WCHAR Cleanup[]
Definition: register.c:80
#define ERR(fmt,...)
Definition: debug.h:110
PWSTR pwszDocumentName
Definition: precomp.h:146
HANDLE hPrintProcessor
Definition: precomp.h:139
PLOCAL_PRINT_PROCESSOR pPrintProcessor
Definition: precomp.h:141
PVOID WINAPI DllAllocSplMem(DWORD dwBytes)
Definition: memory.c:95
POpenPrintProcessor pfnOpenPrintProcessor
Definition: precomp.h:104
PLOCAL_PORT pPort
Definition: precomp.h:125
PWSTR pwszPrinterName
Definition: precomp.h:115
size_t __cdecl wcslen(_In_z_ const wchar_t *_Str)

Referenced by LocalScheduleJob().