|
|
1.1 root 1: #include <windows.h>
2: #include <winspool.h>
3: #include <winsplp.h>
4:
5: #include "winprint.h"
6:
7: #include <excpt.h>
8: #include <string.h>
9:
10: #define PRINTPROCESSOR_TYPE_RAW 0
11: #define PRINTPROCESSOR_TYPE_JOURNAL 1
12: #define PRINTPROCESSOR_TYPE_TEXT 2
13: #define PRINTPROCESSOR_TYPE_NUM 3
14:
15: LPWSTR Datatypes[]={L"RAW", L"NT JNL 1.000", L"TEXT", 0};
16:
17: LPWSTR
18: AllocStr(
19: LPWSTR pString
20: )
21: {
22: LPWSTR pNewString;
23:
24: if (pString) {
25: pNewString = LocalAlloc(LMEM_FIXED, (wcslen(pString)+1)*sizeof(WCHAR));
26: wcscpy(pNewString, pString);
27: return pNewString;
28: }
29:
30: return NULL;
31: }
32:
33: BOOL
34: EnumPrintProcessorDatatypes(
35: LPWSTR pName,
36: LPWSTR pPrintProcessorName,
37: DWORD Level,
38: LPBYTE pDatatypes,
39: DWORD cbBuf,
40: LPDWORD pcbNeeded,
41: LPDWORD pcReturned
42: )
43: {
44: DATATYPES_INFO_1 *pInfo1 = (DATATYPES_INFO_1 *)pDatatypes;
45: LPWSTR *pMyDatatypes = Datatypes;
46: DWORD cbTotal=0;
47: LPBYTE pEnd;
48:
49: *pcReturned = 0;
50:
51: pEnd = (LPBYTE)pInfo1 + cbBuf;
52:
53: while (*pMyDatatypes) {
54:
55: cbTotal += wcslen(*pMyDatatypes)*sizeof(WCHAR) + sizeof(WCHAR) +
56: sizeof(DATATYPES_INFO_1);
57:
58: pMyDatatypes++;
59: }
60:
61: *pcbNeeded = cbTotal;
62:
63: if (cbTotal <= cbBuf) {
64:
65: pMyDatatypes = Datatypes;
66:
67: while (*pMyDatatypes) {
68:
69: pEnd -= wcslen(*pMyDatatypes)*sizeof(WCHAR) + sizeof(WCHAR);
70: wcscpy((LPWSTR)pEnd, *pMyDatatypes);
71: pInfo1->pName = (LPWSTR)pEnd;
72: pInfo1++;
73: (*pcReturned)++;
74:
75: pMyDatatypes++;
76: }
77:
78: } else {
79:
80: SetLastError(ERROR_INSUFFICIENT_BUFFER);
81: return FALSE;
82: }
83:
84: return TRUE;
85: }
86:
87: HANDLE
88: OpenPrintProcessor(
89: LPWSTR pPrinterName,
90: PPRINTPROCESSOROPENDATA pPrintProcessorOpenData
91: )
92: {
93: PPRINTPROCESSORDATA pData;
94: LPWSTR *pMyDatatypes=Datatypes;
95: DWORD uDatatype=0;
96: HANDLE hPrinter=0;
97: HDC hDC;
98:
99: while (*pMyDatatypes && wcscmp(*pMyDatatypes,
100: pPrintProcessorOpenData->pDatatype)) {
101: pMyDatatypes++;
102: uDatatype++;
103: }
104:
105: switch (uDatatype) {
106:
107: case 0:
108: if (!OpenPrinter(pPrinterName, &hPrinter, NULL))
109: return FALSE;
110: break;
111:
112: case 1:
113: if (!(hDC = CreateDC(L"", pPrinterName, L"",
114: pPrintProcessorOpenData->pDevMode)))
115: return FALSE;
116: break;
117:
118: default:
119: SetLastError(ERROR_INVALID_DATATYPE);
120: return FALSE;
121: }
122:
123: pData = (PPRINTPROCESSORDATA)LocalAlloc(LPTR, sizeof(PRINTPROCESSORDATA));
124:
125: pData->cb = sizeof(PRINTPROCESSORDATA);
126: pData->signature = PRINTPROCESSORDATA_SIGNATURE;
127: pData->JobId = pPrintProcessorOpenData->JobId;
128: pData->hPrinter = hPrinter;
129: pData->semPaused = CreateEvent(NULL, FALSE, TRUE,NULL);
130: pData->uDatatype = uDatatype;
131: pData->hDC = hDC;
132:
133: pData->pPrinterName = AllocStr(pPrinterName);
134: pData->pDatatype = AllocStr(pPrintProcessorOpenData->pDatatype);
135: pData->pDocument = AllocStr(pPrintProcessorOpenData->pDocumentName);
136: pData->pParameters = AllocStr(pPrintProcessorOpenData->pParameters);
137:
138: return (HANDLE)pData;
139: }
140:
141: BOOL
142: PrintDocumentOnPrintProcessor(
143: HANDLE hPrintProcessor,
144: LPWSTR pDocumentName
145: )
146: {
147: PPRINTPROCESSORDATA pData;
148: DOC_INFO_1 DocInfo;
149: DWORD rc;
150: DWORD NoRead, NoWritten;
151: HANDLE hPrinter;
152: BYTE Buffer[4096];
153:
154: if (!(pData = ValidateHandle(hPrintProcessor))) {
155:
156: SetLastError(ERROR_INVALID_HANDLE);
157: return FALSE;
158: }
159:
160: if (pData->uDatatype == 1) {
161:
162: try {
163: //!!! BUG BUG BUG - LPCWSTR
164: return GdiPlayJournal(pData->hDC, (LPCSTR)pDocumentName, 0, -1);
165:
166: } except (TRUE) {
167:
168: OutputDebugString(L"GdiPlayJournal gave an exception\n");
169:
170: return FALSE;
171: }
172: }
173:
174: if (!OpenPrinter(pDocumentName, &hPrinter, NULL)) {
175: return FALSE;
176: }
177:
178: DocInfo.pDocName = pData->pDocument;
179: DocInfo.pOutputFile = 0;
180: DocInfo.pDatatype = pData->pDatatype;
181:
182: if (!StartDocPrinter(pData->hPrinter, 1, (LPBYTE)&DocInfo)) {
183: ClosePrinter(hPrinter);
184: return FALSE;
185: }
186:
187: while ((rc = ReadPrinter(hPrinter, Buffer, sizeof(Buffer), &NoRead)) &&
188: NoRead) {
189:
190: if (pData->fsStatus & PRINTPROCESSOR_PAUSED)
191: WaitForSingleObject(pData->semPaused, INFINITE);
192:
193: if (pData->fsStatus & PRINTPROCESSOR_ABORTED) {
194: break;
195: }
196:
197: WritePrinter(pData->hPrinter, Buffer, NoRead, &NoWritten);
198: }
199:
200: EndDocPrinter(pData->hPrinter);
201:
202: ClosePrinter(hPrinter);
203:
204: return TRUE;
205: }
206:
207: BOOL
208: ClosePrintProcessor(
209: HANDLE hPrintProcessor
210: )
211: {
212: PPRINTPROCESSORDATA pData;
213:
214: pData = ValidateHandle(hPrintProcessor);
215:
216: if (!pData) {
217: SetLastError(ERROR_INVALID_HANDLE);
218: return FALSE;
219: }
220:
221: pData->signature = 0;
222:
223: /* Release any allocated resources */
224:
225: if (pData->hPrinter)
226: ClosePrinter(pData->hPrinter);
227:
228: if (pData->hDC)
229: DeleteDC(pData->hDC);
230:
231: CloseHandle(pData->semPaused);
232:
233: if (pData->pPrinterName)
234: LocalFree(pData->pPrinterName);
235:
236: if (pData->pDatatype)
237: LocalFree(pData->pDatatype);
238:
239: if (pData->pDocument)
240: LocalFree(pData->pDocument);
241:
242: if (pData->pParameters)
243: LocalFree(pData->pParameters);
244:
245: LocalFree(pData);
246:
247: return TRUE;
248: }
249:
250: BOOL
251: ControlPrintProcessor(
252: HANDLE hPrintProcessor,
253: DWORD Command
254: )
255: {
256: PPRINTPROCESSORDATA pData;
257:
258: if (pData = ValidateHandle(hPrintProcessor)) {
259:
260: switch (Command) {
261:
262: case JOB_CONTROL_PAUSE:
263:
264: ResetEvent(pData->semPaused);
265: pData->fsStatus |= PRINTPROCESSOR_PAUSED;
266: return TRUE;
267: break;
268:
269: case JOB_CONTROL_CANCEL:
270:
271: pData->fsStatus |= PRINTPROCESSOR_ABORTED;
272:
273: if (pData->uDatatype == PRINTPROCESSOR_TYPE_JOURNAL)
274: CancelDC(pData->hDC);
275:
276: /* fall through to release job if paused */
277:
278: case JOB_CONTROL_RESUME:
279:
280: if (pData->fsStatus & PRINTPROCESSOR_PAUSED) {
281:
282: SetEvent(pData->semPaused);
283: pData->fsStatus &= ~PRINTPROCESSOR_PAUSED;
284: }
285:
286: return TRUE;
287: break;
288:
289: default:
290:
291: return FALSE;
292: break;
293: }
294:
295: }
296:
297: return FALSE;
298: }
299:
300: BOOL
301: InstallPrintProcessor(
302: HWND hWnd
303: )
304: {
305: MessageBox(hWnd, L"WinPrint", L"Print Processor Setup", MB_OK);
306:
307: return TRUE;
308: }
309:
310: PPRINTPROCESSORDATA
311: ValidateHandle(
312: HANDLE hQProc
313: )
314: {
315: PPRINTPROCESSORDATA pData = (PPRINTPROCESSORDATA)hQProc;
316:
317: if (pData && pData->signature == PRINTPROCESSORDATA_SIGNATURE)
318: return( pData );
319:
320: return( NULL );
321: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.