ReactOS  0.4.15-dev-425-gc40b086
printers.c
Go to the documentation of this file.
1 /*
2  * PROJECT: ReactOS Spooler Router
3  * LICENSE: GPL-2.0+ (https://spdx.org/licenses/GPL-2.0+)
4  * PURPOSE: Functions related to Printers and printing
5  * COPYRIGHT: Copyright 2015-2017 Colin Finck (colin@reactos.org)
6  */
7 
8 #include "precomp.h"
9 
10 
13 {
14  BOOL bReturnValue;
16 
17  // Sanity checks.
18  if (!pHandle)
19  {
21  return FALSE;
22  }
23 
24  // FIXME: Call FindClosePrinterChangeNotification for all created change notifications (according to MSDN).
25 
26  // Call CloseHandle of the Print Provider.
27  bReturnValue = pHandle->pPrintProvider->PrintProvider.fpClosePrinter(pHandle->hPrinter);
28 
29  // Free our handle information.
30  DllFreeSplMem(pHandle);
31 
32  return bReturnValue;
33 }
34 
37 {
39 
40  // Sanity checks.
41  if (!pHandle)
42  {
44  return FALSE;
45  }
46 
47  return pHandle->pPrintProvider->PrintProvider.fpEndDocPrinter(pHandle->hPrinter);
48 }
49 
52 {
54 
55  // Sanity checks.
56  if (!pHandle)
57  {
59  return FALSE;
60  }
61 
62  return pHandle->pPrintProvider->PrintProvider.fpEndPagePrinter(pHandle->hPrinter);
63 }
64 
67 {
68  DWORD cbCallBuffer;
69  DWORD cbNeeded;
70  DWORD dwErrorCode = MAXDWORD;
71  DWORD dwReturned;
72  PBYTE pCallBuffer;
73  PSPOOLSS_PRINT_PROVIDER pPrintProvider;
74  PLIST_ENTRY pEntry;
75 
76  // Begin counting.
77  *pcbNeeded = 0;
78  *pcReturned = 0;
79 
80  if (cbBuf && !pPrinterEnum)
81  {
82  dwErrorCode = ERROR_INVALID_USER_BUFFER;
83  goto Cleanup;
84  }
85 
86  // At the beginning, we have the full buffer available.
87  cbCallBuffer = cbBuf;
88  pCallBuffer = pPrinterEnum;
89 
90  // Loop through all Print Providers.
91  for (pEntry = PrintProviderList.Flink; pEntry != &PrintProviderList; pEntry = pEntry->Flink)
92  {
93  pPrintProvider = CONTAINING_RECORD(pEntry, SPOOLSS_PRINT_PROVIDER, Entry);
94 
95  // Call the EnumPrinters function of this Print Provider.
96  cbNeeded = 0;
97  dwReturned = 0;
98  pPrintProvider->PrintProvider.fpEnumPrinters(Flags, Name, Level, pCallBuffer, cbCallBuffer, &cbNeeded, &dwReturned);
99 
100  // Add the returned counts to the total values.
101  *pcbNeeded += cbNeeded;
102  *pcReturned += dwReturned;
103 
104  // Reduce the available buffer size for the next call without risking an underflow.
105  if (cbNeeded < cbCallBuffer)
106  cbCallBuffer -= cbNeeded;
107  else
108  cbCallBuffer = 0;
109 
110  // Advance the buffer if the caller provided it.
111  if (pCallBuffer)
112  pCallBuffer += cbNeeded;
113 
114  // dwErrorCode shall not be overwritten if a previous EnumPrinters call already succeeded.
115  if (dwErrorCode != ERROR_SUCCESS)
116  dwErrorCode = GetLastError();
117  }
118 
119 Cleanup:
120  SetLastError(dwErrorCode);
121  return (dwErrorCode == ERROR_SUCCESS);
122 }
123 
124 BOOL WINAPI
125 GetPrinterDriverW(HANDLE hPrinter, PWSTR pEnvironment, DWORD Level, PBYTE pDriverInfo, DWORD cbBuf, PDWORD pcbNeeded)
126 {
128 
129  // Sanity checks.
130  if (!pHandle)
131  {
133  return FALSE;
134  }
135 
136  return pHandle->pPrintProvider->PrintProvider.fpGetPrinterDriver(pHandle->hPrinter, pEnvironment, Level, pDriverInfo, cbBuf, pcbNeeded);
137 }
138 
139 BOOL WINAPI
140 GetPrinterW(HANDLE hPrinter, DWORD Level, PBYTE pPrinter, DWORD cbBuf, PDWORD pcbNeeded)
141 {
143 
144  // Sanity checks.
145  if (!pHandle)
146  {
148  return FALSE;
149  }
150 
151  return pHandle->pPrintProvider->PrintProvider.fpGetPrinter(pHandle->hPrinter, Level, pPrinter, cbBuf, pcbNeeded);
152 }
153 
154 BOOL WINAPI
155 OpenPrinterW(PWSTR pPrinterName, PHANDLE phPrinter, PPRINTER_DEFAULTSW pDefault)
156 {
157  BOOL bReturnValue;
158  DWORD dwErrorCode = ERROR_INVALID_PRINTER_NAME;
159  HANDLE hPrinter;
160  PLIST_ENTRY pEntry;
161  PSPOOLSS_PRINTER_HANDLE pHandle;
162  PSPOOLSS_PRINT_PROVIDER pPrintProvider;
163 
164  // Loop through all Print Providers to find one able to open this Printer.
165  for (pEntry = PrintProviderList.Flink; pEntry != &PrintProviderList; pEntry = pEntry->Flink)
166  {
167  pPrintProvider = CONTAINING_RECORD(pEntry, SPOOLSS_PRINT_PROVIDER, Entry);
168 
169  bReturnValue = pPrintProvider->PrintProvider.fpOpenPrinter(pPrinterName, &hPrinter, pDefault);
170  if (bReturnValue == ROUTER_SUCCESS)
171  {
172  // This Print Provider has opened this Printer.
173  // Store this information and return a handle.
174  pHandle = DllAllocSplMem(sizeof(SPOOLSS_PRINTER_HANDLE));
175  if (!pHandle)
176  {
177  dwErrorCode = ERROR_NOT_ENOUGH_MEMORY;
178  ERR("DllAllocSplMem failed!\n");
179  goto Cleanup;
180  }
181 
182  pHandle->pPrintProvider = pPrintProvider;
183  pHandle->hPrinter = hPrinter;
184  *phPrinter = (HANDLE)pHandle;
185 
186  dwErrorCode = ERROR_SUCCESS;
187  goto Cleanup;
188  }
189  else if (bReturnValue == ROUTER_STOP_ROUTING)
190  {
191  ERR("A Print Provider returned ROUTER_STOP_ROUTING for Printer \"%S\"!\n", pPrinterName);
192  dwErrorCode = GetLastError();
193  goto Cleanup;
194  }
195  }
196 
197 Cleanup:
198  // ERROR_INVALID_NAME by the Print Provider is translated to ERROR_INVALID_PRINTER_NAME here, but not in other APIs as far as I know.
199  if (dwErrorCode == ERROR_INVALID_NAME)
200  dwErrorCode = ERROR_INVALID_PRINTER_NAME;
201 
202  SetLastError(dwErrorCode);
203  return (dwErrorCode == ERROR_SUCCESS);
204 }
205 
206 BOOL WINAPI
207 ReadPrinter(HANDLE hPrinter, PVOID pBuf, DWORD cbBuf, PDWORD pNoBytesRead)
208 {
210 
211  // Sanity checks.
212  if (!pHandle)
213  {
215  return FALSE;
216  }
217 
218  return pHandle->pPrintProvider->PrintProvider.fpReadPrinter(pHandle->hPrinter, pBuf, cbBuf, pNoBytesRead);
219 }
220 
223 {
225 
226  // Sanity checks.
227  if (!pHandle)
228  {
230  return FALSE;
231  }
232 
233  return pHandle->pPrintProvider->PrintProvider.fpStartDocPrinter(pHandle->hPrinter, Level, pDocInfo);
234 }
235 
236 BOOL WINAPI
238 {
240 
241  // Sanity checks.
242  if (!pHandle)
243  {
245  return FALSE;
246  }
247 
248  return pHandle->pPrintProvider->PrintProvider.fpStartPagePrinter(pHandle->hPrinter);
249 }
250 
251 BOOL WINAPI
252 WritePrinter(HANDLE hPrinter, PVOID pBuf, DWORD cbBuf, PDWORD pcWritten)
253 {
255 
256  // Sanity checks.
257  if (!pHandle)
258  {
260  return FALSE;
261  }
262 
263  return pHandle->pPrintProvider->PrintProvider.fpWritePrinter(pHandle->hPrinter, pBuf, cbBuf, pcWritten);
264 }
265 
266 BOOL WINAPI
267 XcvDataW(HANDLE hXcv, PCWSTR pszDataName, PBYTE pInputData, DWORD cbInputData, PBYTE pOutputData, DWORD cbOutputData, PDWORD pcbOutputNeeded, PDWORD pdwStatus)
268 {
270 
271  // Sanity checks.
272  if (!pHandle)
273  {
275  return FALSE;
276  }
277 
278  return pHandle->pPrintProvider->PrintProvider.fpXcvData(pHandle->hPrinter, pszDataName, pInputData, cbInputData, pOutputData, cbOutputData, pcbOutputNeeded, pdwStatus);
279 }
#define ERROR_INVALID_PARAMETER
Definition: compat.h:91
BOOL WINAPI EnumPrintersW(DWORD Flags, PWSTR Name, DWORD Level, PBYTE pPrinterEnum, DWORD cbBuf, PDWORD pcbNeeded, PDWORD pcReturned)
Definition: printers.c:66
const uint16_t * PCWSTR
Definition: typedefs.h:56
BOOL WINAPI ReadPrinter(HANDLE hPrinter, PVOID pBuf, DWORD cbBuf, PDWORD pNoBytesRead)
Definition: printers.c:207
#define ERROR_SUCCESS
Definition: deptool.c:10
uint16_t * PWSTR
Definition: typedefs.h:55
#define ROUTER_SUCCESS
Definition: winsplp.h:41
BOOL WINAPI DllFreeSplMem(PVOID pMem)
Definition: memory.c:112
#define ERROR_NOT_ENOUGH_MEMORY
Definition: dderror.h:7
DWORD WINAPI GetLastError(VOID)
Definition: except.c:1059
DWORD WINAPI StartDocPrinterW(HANDLE hPrinter, DWORD Level, PBYTE pDocInfo)
Definition: printers.c:222
_IRQL_requires_same_ typedef _In_ ULONG _In_ UCHAR Level
Definition: wmitypes.h:55
struct _SPOOLSS_PRINTER_HANDLE * PSPOOLSS_PRINTER_HANDLE
#define ERROR_INVALID_USER_BUFFER
Definition: winerror.h:1091
_Must_inspect_result_ _In_ ULONG Flags
Definition: wsk.h:170
unsigned int BOOL
Definition: ntddk_ex.h:94
#define ROUTER_STOP_ROUTING
Definition: winsplp.h:42
#define MAXDWORD
BOOL WINAPI GetPrinterDriverW(HANDLE hPrinter, PWSTR pEnvironment, DWORD Level, PBYTE pDriverInfo, DWORD cbBuf, PDWORD pcbNeeded)
Definition: printers.c:125
PFLT_MESSAGE_WAITER_QUEUE CONTAINING_RECORD(Csq, DEVICE_EXTENSION, IrpQueue)) -> WaiterQ.mLock) _IRQL_raises_(DISPATCH_LEVEL) VOID NTAPI FltpAcquireMessageWaiterLock(_In_ PIO_CSQ Csq, _Out_ PKIRQL Irql)
Definition: Messaging.c:560
BOOL WINAPI WritePrinter(HANDLE hPrinter, PVOID pBuf, DWORD cbBuf, PDWORD pcWritten)
Definition: printers.c:252
BOOL WINAPI GetPrinterW(HANDLE hPrinter, DWORD Level, PBYTE pPrinter, DWORD cbBuf, PDWORD pcbNeeded)
Definition: printers.c:140
struct _LIST_ENTRY * Flink
Definition: typedefs.h:120
BOOL WINAPI ClosePrinter(HANDLE hPrinter)
Definition: printers.c:12
#define WINAPI
Definition: msvc.h:6
LIST_ENTRY PrintProviderList
Definition: main.c:12
PVOID HANDLE
Definition: typedefs.h:72
unsigned long DWORD
Definition: ntddk_ex.h:95
#define SetLastError(x)
Definition: compat.h:418
PVOID *typedef PHANDLE
Definition: ntsecpkg.h:454
BOOL WINAPI EndPagePrinter(HANDLE hPrinter)
Definition: printers.c:51
Definition: typedefs.h:118
BOOL WINAPI XcvDataW(HANDLE hXcv, PCWSTR pszDataName, PBYTE pInputData, DWORD cbInputData, PBYTE pOutputData, DWORD cbOutputData, PDWORD pcbOutputNeeded, PDWORD pdwStatus)
Definition: printers.c:267
static const WCHAR Cleanup[]
Definition: register.c:80
#define ERROR_INVALID_PRINTER_NAME
Definition: winerror.h:1108
#define ERR(fmt,...)
Definition: debug.h:110
_In_ DWORD _Out_ PDWORD pcbNeeded
Definition: winddi.h:3827
BOOL WINAPI StartPagePrinter(HANDLE hPrinter)
Definition: printers.c:237
BOOL WINAPI EndDocPrinter(HANDLE hPrinter)
Definition: printers.c:36
DWORD * PDWORD
Definition: pedump.c:68
BOOL WINAPI OpenPrinterW(PWSTR pPrinterName, PHANDLE phPrinter, PPRINTER_DEFAULTSW pDefault)
Definition: printers.c:155
#define ERROR_INVALID_NAME
Definition: compat.h:93
PRINTPROVIDOR PrintProvider
Definition: precomp.h:36
PVOID WINAPI DllAllocSplMem(DWORD dwBytes)
Definition: memory.c:95
static HANDLE hXcv
Definition: localmon.c:73
BYTE * PBYTE
Definition: pedump.c:66
base of all file and directory entries
Definition: entries.h:82
PSPOOLSS_PRINT_PROVIDER pPrintProvider
Definition: precomp.h:46