ReactOS 0.4.16-dev-336-gb667d82
raw.c
Go to the documentation of this file.
1/*
2 * PROJECT: ReactOS Standard Print Processor
3 * LICENSE: GPL-2.0+ (https://spdx.org/licenses/GPL-2.0+)
4 * PURPOSE: Printing a job with RAW datatype
5 * COPYRIGHT: Copyright 2015 Colin Finck (colin@reactos.org)
6 */
7
8#include "precomp.h"
9
23PrintRawJob(PWINPRINT_HANDLE pHandle, PWSTR pwszPrinterAndJob)
24{
25 // Use a read buffer of 256 KB size like Windows does.
26 const DWORD cbReadBuffer = 262144;
27
28 BOOL bStartedDoc = FALSE;
29 DOC_INFO_1W DocInfo1;
30 DWORD cbRead;
31 DWORD cbWritten;
32 DWORD dwErrorCode;
33 HANDLE hPrintJob;
34 HANDLE hPrintMonitor = NULL;
36
37 // Open the spooled job to read from it.
38 if (!OpenPrinterW(pwszPrinterAndJob, &hPrintJob, NULL))
39 {
40 dwErrorCode = GetLastError();
41 ERR("OpenPrinterW failed for \"%S\" with error %lu!\n", pwszPrinterAndJob, GetLastError());
42 goto Cleanup;
43 }
44
45 // Open a Print Monitor handle to write to it.
46 if (!OpenPrinterW(pHandle->pwszPrinterPort, &hPrintMonitor, NULL))
47 {
48 dwErrorCode = GetLastError();
49 ERR("OpenPrinterW failed for \"%S\" with error %lu!\n", pHandle->pwszPrinterPort, GetLastError());
50 goto Cleanup;
51 }
52
53 // Fill the Document Information.
54 DocInfo1.pDatatype = pHandle->pwszDatatype;
55 DocInfo1.pDocName = pHandle->pwszDocumentName;
56 DocInfo1.pOutputFile = pHandle->pwszOutputFile;
57
58 // Tell the Print Monitor that we're starting a new document.
59 if (!StartDocPrinterW(hPrintMonitor, 1, (PBYTE)&DocInfo1))
60 {
61 dwErrorCode = GetLastError();
62 ERR("StartDocPrinterW failed with error %lu!\n", GetLastError());
63 goto Cleanup;
64 }
65
66 bStartedDoc = TRUE;
67
68 // Allocate a read buffer on the heap. This would easily exceed the stack size.
69 pBuffer = DllAllocSplMem(cbReadBuffer);
70 if (!pBuffer)
71 {
72 dwErrorCode = ERROR_NOT_ENOUGH_MEMORY;
73 ERR("DllAllocSplMem failed with error %lu!\n", GetLastError());
74 goto Cleanup;
75 }
76
77 // Loop as long as data is available.
78 while (ReadPrinter(hPrintJob, pBuffer, cbReadBuffer, &cbRead) && cbRead)
79 {
80 // Write it to the Print Monitor.
81 WritePrinter(hPrintMonitor, pBuffer, cbRead, &cbWritten);
82 }
83
84 dwErrorCode = ERROR_SUCCESS;
85
87 if (pBuffer)
89
90 if (bStartedDoc)
91 EndDocPrinter(hPrintMonitor);
92
93 if (hPrintMonitor)
94 ClosePrinter(hPrintMonitor);
95
96 if (hPrintJob)
97 ClosePrinter(hPrintJob);
98
99 return dwErrorCode;
100}
#define ERR(fmt,...)
Definition: precomp.h:57
#define ERROR_NOT_ENOUGH_MEMORY
Definition: dderror.h:7
#define ERROR_SUCCESS
Definition: deptool.c:10
#define NULL
Definition: types.h:112
#define TRUE
Definition: types.h:120
#define FALSE
Definition: types.h:117
static const WCHAR Cleanup[]
Definition: register.c:80
unsigned int BOOL
Definition: ntddk_ex.h:94
unsigned long DWORD
Definition: ntddk_ex.h:95
BYTE * PBYTE
Definition: pedump.c:66
PVOID pBuffer
LPWSTR pOutputFile
Definition: winspool.h:617
LPWSTR pDatatype
Definition: winspool.h:618
LPWSTR pDocName
Definition: winspool.h:616
PWSTR pwszOutputFile
Definition: precomp.h:32
PWSTR pwszPrinterPort
Definition: precomp.h:33
PWSTR pwszDocumentName
Definition: precomp.h:31
PWSTR pwszDatatype
Definition: precomp.h:30
uint16_t * PWSTR
Definition: typedefs.h:56
BOOL WINAPI DllFreeSplMem(PVOID pMem)
Definition: memory.c:112
PVOID WINAPI DllAllocSplMem(DWORD dwBytes)
Definition: memory.c:95
DWORD PrintRawJob(PWINPRINT_HANDLE pHandle, PWSTR pwszPrinterAndJob)
Definition: raw.c:23
DWORD WINAPI GetLastError(void)
Definition: except.c:1042
WINBOOL WINAPI ReadPrinter(HANDLE hPrinter, LPVOID pBuf, DWORD cbBuf, LPDWORD pNoBytesRead)
DWORD WINAPI StartDocPrinterW(HANDLE hPrinter, DWORD Level, LPBYTE pDocInfo)
WINBOOL WINAPI OpenPrinterW(LPWSTR pPrinterName, LPHANDLE phPrinter, LPPRINTER_DEFAULTSW pDefault)
Definition: printers.c:2653
WINBOOL WINAPI EndDocPrinter(HANDLE hPrinter)
Definition: printers.c:217
WINBOOL WINAPI ClosePrinter(HANDLE hPrinter)
Definition: printers.c:176
WINBOOL WINAPI WritePrinter(HANDLE hPrinter, LPVOID pBuf, DWORD cbBuf, LPDWORD pcWritten)