2 * Printing interface for PuTTY.
8 struct printer_enum_tag
{
17 struct printer_job_tag
{
21 static char *printer_add_enum(int param
, DWORD level
, char *buffer
,
22 int offset
, int *nprinters_ptr
)
24 DWORD needed
, nprinters
;
26 buffer
= sresize(buffer
, offset
+512, char);
29 * Exploratory call to EnumPrinters to determine how much space
30 * we'll need for the output. Discard the return value since it
31 * will almost certainly be a failure due to lack of space.
33 EnumPrinters(param
, NULL
, level
, buffer
+offset
, 512,
39 buffer
= sresize(buffer
, offset
+needed
, char);
41 if (EnumPrinters(param
, NULL
, level
, buffer
+offset
,
42 needed
, &needed
, &nprinters
) == 0)
45 *nprinters_ptr
+= nprinters
;
50 printer_enum
*printer_start_enum(int *nprinters_ptr
)
52 printer_enum
*ret
= snew(printer_enum
);
53 char *buffer
= NULL
, *retval
;
55 *nprinters_ptr
= 0; /* default return value */
56 buffer
= snewn(512, char);
59 * Determine what enumeration level to use.
60 * When enumerating printers, we need to use PRINTER_INFO_4 on
61 * NT-class systems to avoid Windows looking too hard for them and
62 * slowing things down; and we need to avoid PRINTER_INFO_5 as
63 * we've seen network printers not show up.
64 * On 9x-class systems, PRINTER_INFO_4 isn't available and
65 * PRINTER_INFO_5 is recommended.
68 if (osVersion
.dwPlatformId
!= VER_PLATFORM_WIN32_NT
) {
74 retval
= printer_add_enum(PRINTER_ENUM_LOCAL
| PRINTER_ENUM_CONNECTIONS
,
75 ret
->enum_level
, buffer
, 0, nprinters_ptr
);
81 switch (ret
->enum_level
) {
83 ret
->info
.i4
= (LPPRINTER_INFO_4
)buffer
;
86 ret
->info
.i5
= (LPPRINTER_INFO_5
)buffer
;
89 ret
->nprinters
= *nprinters_ptr
;
100 char *printer_get_name(printer_enum
*pe
, int i
)
104 if (i
< 0 || i
>= pe
->nprinters
)
106 switch (pe
->enum_level
) {
108 return pe
->info
.i4
[i
].pPrinterName
;
110 return pe
->info
.i5
[i
].pPrinterName
;
116 void printer_finish_enum(printer_enum
*pe
)
120 switch (pe
->enum_level
) {
131 printer_job
*printer_start_job(char *printer
)
133 printer_job
*ret
= snew(printer_job
);
135 int jobstarted
= 0, pagestarted
= 0;
137 ret
->hprinter
= NULL
;
138 if (!OpenPrinter(printer
, &ret
->hprinter
, NULL
))
141 docinfo
.pDocName
= "PuTTY remote printer output";
142 docinfo
.pOutputFile
= NULL
;
143 docinfo
.pDatatype
= "RAW";
145 if (!StartDocPrinter(ret
->hprinter
, 1, (LPSTR
)&docinfo
))
149 if (!StartPagePrinter(ret
->hprinter
))
157 EndPagePrinter(ret
->hprinter
);
159 EndDocPrinter(ret
->hprinter
);
161 ClosePrinter(ret
->hprinter
);
166 void printer_job_data(printer_job
*pj
, void *data
, int len
)
173 WritePrinter(pj
->hprinter
, data
, len
, &written
);
176 void printer_finish_job(printer_job
*pj
)
181 EndPagePrinter(pj
->hprinter
);
182 EndDocPrinter(pj
->hprinter
);
183 ClosePrinter(pj
->hprinter
);