-
Notifications
You must be signed in to change notification settings - Fork 783
/
win32print.cpp
2795 lines (2658 loc) · 126 KB
/
win32print.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/***********************************************************
win32printmodule.cpp -- module for interface into printer API
Note that this source file contains embedded documentation.
This documentation consists of marked up text inside the
C comments, and is prefixed with an '@' symbol. The source
files are processed by a tool called "autoduck" which
generates Windows .hlp files.
@doc
******************************************************************/
#include "PyWinTypes.h"
#include "PyWinObjects.h"
#include <stdarg.h>
#define CHECK_PFN(fname) \
if (pfn##fname == NULL) \
return PyErr_Format(PyExc_NotImplementedError, "%s is not available on this platform", #fname);
typedef BOOL(WINAPI *EnumFormsfunc)(HANDLE, DWORD, LPBYTE, DWORD, LPDWORD, LPDWORD);
static EnumFormsfunc pfnEnumForms = NULL;
typedef BOOL(WINAPI *AddFormfunc)(HANDLE, DWORD, LPBYTE);
static AddFormfunc pfnAddForm = NULL;
typedef BOOL(WINAPI *DeleteFormfunc)(HANDLE, LPWSTR);
static DeleteFormfunc pfnDeleteForm = NULL;
typedef BOOL(WINAPI *GetFormfunc)(HANDLE, LPWSTR, DWORD, LPBYTE, DWORD, LPDWORD);
static GetFormfunc pfnGetForm = NULL;
typedef BOOL(WINAPI *SetFormfunc)(HANDLE, LPWSTR, DWORD, LPBYTE);
static SetFormfunc pfnSetForm = NULL;
typedef BOOL(WINAPI *AddJobfunc)(HANDLE, DWORD, LPBYTE, DWORD, LPDWORD);
static AddJobfunc pfnAddJob = NULL;
typedef BOOL(WINAPI *ScheduleJobfunc)(HANDLE, DWORD);
static ScheduleJobfunc pfnScheduleJob = NULL;
typedef BOOL(WINAPI *EnumPortsfunc)(LPWSTR, DWORD, LPBYTE, DWORD, LPDWORD, LPDWORD);
static EnumPortsfunc pfnEnumPorts = NULL;
static EnumPortsfunc pfnEnumMonitors = NULL; // same args as EnumPorts
typedef BOOL(WINAPI *GetPrintProcessorDirectoryfunc)(LPWSTR, LPWSTR, DWORD, LPBYTE, DWORD, LPDWORD);
static GetPrintProcessorDirectoryfunc pfnGetPrintProcessorDirectory = NULL;
static GetPrintProcessorDirectoryfunc pfnGetPrinterDriverDirectory = NULL; // same as GetPrintProcessorDirectory
typedef BOOL(WINAPI *DeletePrinterDriverExfunc)(LPWSTR, LPWSTR, LPWSTR, DWORD, DWORD);
static DeletePrinterDriverExfunc pfnDeletePrinterDriverEx = NULL;
typedef BOOL(WINAPI *FlushPrinterfunc)(HANDLE, LPVOID, DWORD, LPDWORD, DWORD);
static FlushPrinterfunc pfnFlushPrinter = NULL;
typedef BOOL(WINAPI *GetDefaultPrinterfunc)(LPWSTR, LPDWORD);
static GetDefaultPrinterfunc pfnGetDefaultPrinter = NULL;
typedef BOOL(WINAPI *SetDefaultPrinterfunc)(LPWSTR);
static SetDefaultPrinterfunc pfnSetDefaultPrinter = NULL;
static PyObject *dummy_tuple = NULL;
// To be used in PyArg_ParseTuple with O& format
BOOL PyWinObject_AsPrinterHANDLE(PyObject *obhprinter, HANDLE *phprinter)
{
return PyWinObject_AsHANDLE(obhprinter, phprinter);
}
// @object PyPrinterHANDLE|Handle to a printer or print server.
// <nl>Created using <om win32print.OpenPrinter> or <om win32print.AddPrinter>
// <nl>Inherits all methods and properties of <o PyHANDLE>.
// <nl>When object is destroyed, handle is released using ClosePrinter.
class PyPrinterHANDLE : public PyHANDLE {
public:
PyPrinterHANDLE(HANDLE hInit) : PyHANDLE(hInit) {}
virtual BOOL Close(void)
{
BOOL ret = ClosePrinter(m_handle);
if (!ret)
PyWin_SetAPIError("ClosePrinter");
m_handle = 0;
return ret;
}
virtual const char *GetTypeName() { return "PyPrinterHANDLE"; }
};
PyObject *PyWinObject_FromPrinterHANDLE(HANDLE hprinter)
{
PyObject *ret = new PyPrinterHANDLE(hprinter);
if (ret == NULL)
PyErr_NoMemory();
return ret;
}
void PyWinObject_FreePRINTER_DEFAULTS(PPRINTER_DEFAULTS pdefaults) { PyWinObject_FreeTCHAR(pdefaults->pDatatype); }
// @object PRINTER_DEFAULTS|A dictionary representing a PRINTER_DEFAULTS structure
// @prop string|pDatatype|Data type to be used for print jobs, see <om win32print.EnumPrintProcessorDatatypes>,
// optional, can be None
// @prop <o PyDEVMODE>|pDevMode|A PyDEVMODE that specifies default printer parameters, optional, can be None
// @prop int|DesiredAccess|An ACCESS_MASK specifying what level of access is needed, eg PRINTER_ACCESS_ADMINISTER,
// PRINTER_ACCESS_USE
BOOL PyWinObject_AsPRINTER_DEFAULTS(PyObject *obdefaults, PPRINTER_DEFAULTS pdefaults)
{
static char *printer_default_keys[] = {"DesiredAccess", "pDataType", "pDevMode", NULL};
static char *printer_default_format = "k|OO";
ZeroMemory(pdefaults, sizeof(PRINTER_DEFAULTS));
PyObject *obDataType = Py_None, *obdevmode = Py_None;
if (!PyDict_Check(obdefaults)) {
PyErr_SetString(PyExc_TypeError, "PRINTER_DEFAULTS must be a dictionary");
return FALSE;
}
return PyArg_ParseTupleAndKeywords(dummy_tuple, obdefaults, printer_default_format, printer_default_keys,
&pdefaults->DesiredAccess, &pdefaults->pDatatype, &obdevmode) &&
PyWinObject_AsDEVMODE(obdevmode, &pdefaults->pDevMode, TRUE) &&
PyWinObject_AsTCHAR(obDataType, &pdefaults->pDatatype, TRUE);
}
// Printer stuff.
// @pymethod <o PyPrinterHANDLE>|win32print|OpenPrinter|Retrieves a handle to a printer.
static PyObject *PyOpenPrinter(PyObject *self, PyObject *args)
{
TCHAR *printer;
HANDLE handle;
PRINTER_DEFAULTS printer_defaults = {NULL, NULL, 0};
PRINTER_DEFAULTS *pprinter_defaults = NULL;
PyObject *obprinter, *obdefaults = Py_None, *ret = NULL;
if (!PyArg_ParseTuple(
args, "O|O:OpenPrinter",
&obprinter, // @pyparm string|printer||Printer or print server name. Use None to open local print server.
&obdefaults)) // @pyparm dict|Defaults|None|<o PRINTER_DEFAULTS> dict, or None
return NULL;
if (obdefaults != Py_None) {
if (!PyWinObject_AsPRINTER_DEFAULTS(obdefaults, &printer_defaults))
return NULL;
pprinter_defaults = &printer_defaults;
}
if (PyWinObject_AsTCHAR(obprinter, &printer, TRUE)) {
BOOL bsuccess;
Py_BEGIN_ALLOW_THREADS bsuccess = OpenPrinter(printer, &handle, pprinter_defaults);
Py_END_ALLOW_THREADS if (bsuccess) ret = PyWinObject_FromPrinterHANDLE(handle);
else PyWin_SetAPIError("OpenPrinter");
}
PyWinObject_FreePRINTER_DEFAULTS(&printer_defaults);
PyWinObject_FreeTCHAR(printer);
return ret;
}
// @pymethod |win32print|ClosePrinter|Closes a handle to a printer.
static PyObject *PyClosePrinter(PyObject *self, PyObject *args)
{
PyObject *obhprinter;
if (!PyArg_ParseTuple(args, "O:ClosePrinter",
&obhprinter)) // @pyparm <o PyPrinterHANDLE>|hPrinter||handle to printer object
return NULL;
/* If the object is a PyPrinterHANDLE, its Close method must be called to ensure that the m_handle member is
cleared. A second handle with the same value can be created as soon as the first handle is closed here, and if
this happens between the time this function is executed and the first object is deref'ed, the original object's
destruction would close a valid handle contained in the second object. */
if (PyHANDLE_Check(obhprinter)) {
// Make sure we can't Close any other type of handle
const char *handletype = ((PyHANDLE *)obhprinter)->GetTypeName();
if (strcmp(handletype, "PyPrinterHANDLE") != 0)
return PyErr_Format(PyExc_TypeError, "ClosePrinter: Object must be a printer handle, not %s", handletype);
if (((PyHANDLE *)obhprinter)->Close()) {
Py_INCREF(Py_None);
return Py_None;
}
return NULL;
}
HANDLE hprinter;
if (!PyWinObject_AsPrinterHANDLE(obhprinter, &hprinter))
return NULL;
if (!ClosePrinter(hprinter))
return PyWin_SetAPIError("ClosePrinter");
Py_INCREF(Py_None);
return Py_None;
}
static PyObject *PyWinObject_FromPRINTER_INFO(LPBYTE printer_info, DWORD level)
{
switch (level) {
case 1:
PRINTER_INFO_1 *pi1;
pi1 = (PRINTER_INFO_1 *)printer_info;
return Py_BuildValue("{s:k,s:N,s:N,s:N}", "Flags", pi1->Flags, "pDescription",
PyWinObject_FromTCHAR(pi1->pDescription), "pName", PyWinObject_FromTCHAR(pi1->pName),
"pComment", PyWinObject_FromTCHAR(pi1->pComment));
case 2:
PRINTER_INFO_2 *pi2;
pi2 = (PRINTER_INFO_2 *)printer_info;
return Py_BuildValue(
"{s:N,s:N,s:N,s:N,s:N,s:N,s:N,s:N,s:N,s:N,s:N,s:N,s:N,s:k,s:k,s:k,s:k,s:k,s:k,s:k,s:k}", "pServerName",
PyWinObject_FromTCHAR(pi2->pServerName), "pPrinterName", PyWinObject_FromTCHAR(pi2->pPrinterName),
"pShareName", PyWinObject_FromTCHAR(pi2->pShareName), "pPortName",
PyWinObject_FromTCHAR(pi2->pPortName), "pDriverName", PyWinObject_FromTCHAR(pi2->pDriverName),
"pComment", PyWinObject_FromTCHAR(pi2->pComment), "pLocation", PyWinObject_FromTCHAR(pi2->pLocation),
"pDevMode", PyWinObject_FromDEVMODE(pi2->pDevMode), "pSepFile", PyWinObject_FromTCHAR(pi2->pSepFile),
"pPrintProcessor", PyWinObject_FromTCHAR(pi2->pPrintProcessor), "pDatatype",
PyWinObject_FromTCHAR(pi2->pDatatype), "pParameters", PyWinObject_FromTCHAR(pi2->pParameters),
"pSecurityDescriptor", PyWinObject_FromSECURITY_DESCRIPTOR(pi2->pSecurityDescriptor), "Attributes",
pi2->Attributes, "Priority", pi2->Priority, "DefaultPriority", pi2->DefaultPriority, "StartTime",
pi2->StartTime, "UntilTime", pi2->UntilTime, "Status", pi2->Status, "cJobs", pi2->cJobs, "AveragePPM",
pi2->AveragePPM);
case 3:
PRINTER_INFO_3 *pi3;
pi3 = (PRINTER_INFO_3 *)printer_info;
return Py_BuildValue("{s:N}", "pSecurityDescriptor",
PyWinObject_FromSECURITY_DESCRIPTOR(pi3->pSecurityDescriptor));
case 4:
PRINTER_INFO_4 *pi4;
pi4 = (PRINTER_INFO_4 *)printer_info;
return Py_BuildValue("{s:N,s:N,s:k}", "pPrinterName", PyWinObject_FromTCHAR(pi4->pPrinterName),
"pServerName", PyWinObject_FromTCHAR(pi4->pServerName), "Attributes", pi4->Attributes);
case 5:
PRINTER_INFO_5 *pi5;
pi5 = (PRINTER_INFO_5 *)printer_info;
return Py_BuildValue("{s:N,s:N,s:k,s:k,s:k}", "pPrinterName", PyWinObject_FromTCHAR(pi5->pPrinterName),
"pPortName", PyWinObject_FromTCHAR(pi5->pPortName), "Attributes", pi5->Attributes,
"DeviceNotSelectedTimeout", pi5->DeviceNotSelectedTimeout, "TransmissionRetryTimeout",
pi5->TransmissionRetryTimeout);
case 7:
PRINTER_INFO_7 *pi7;
pi7 = (PRINTER_INFO_7 *)printer_info;
return Py_BuildValue("{s:N,s:k}", "ObjectGUID", PyWinObject_FromTCHAR(pi7->pszObjectGUID), "Action",
pi7->dwAction);
case 8: // global printer defaults
PRINTER_INFO_8 *pi8;
pi8 = (PRINTER_INFO_8 *)printer_info;
return Py_BuildValue("{s:N}", "pDevMode", PyWinObject_FromDEVMODE(pi8->pDevMode));
case 9: // per user printer defaults
PRINTER_INFO_9 *pi9;
pi9 = (PRINTER_INFO_9 *)printer_info;
return Py_BuildValue("{s:N}", "pDevMode", PyWinObject_FromDEVMODE(pi9->pDevMode));
default:
return PyErr_Format(PyExc_NotImplementedError, "Level %d is not supported", level);
}
}
// @pymethod dict|win32print|GetPrinter|Retrieves information about a printer
// @rdesc Returns a dictionary containing PRINTER_INFO_* data for level, or
// returns a tuple of PRINTER_INFO_2 data if no level is passed in.
static PyObject *PyGetPrinter(PyObject *self, PyObject *args)
{
HANDLE hprinter;
DWORD needed, level;
BOOL backward_compat;
LPBYTE buf = NULL;
PyObject *rc = NULL;
PRINTER_INFO_2 *pi2;
// @comm Original implementation used level 2 only and returned a tuple
// Pass single arg as indicator to use old behaviour for backward compatibility
if (PyArg_ParseTuple(args, "O&:GetPrinter", PyWinObject_AsPrinterHANDLE,
&hprinter)) { // @pyparm <o PyPrinterHANDLE>|hPrinter||handle to printer object as returned by
// <om win32print.OpenPrinter>
backward_compat = TRUE;
level = 2;
}
else {
PyErr_Clear();
if (!PyArg_ParseTuple(args, "O&k:GetPrinter", PyWinObject_AsPrinterHANDLE, &hprinter,
&level)) // @pyparm int|Level|2|Level of data returned (1,2,3,4,5,7,8,9)
return NULL;
backward_compat = FALSE;
}
// first allocate memory.
GetPrinter(hprinter, level, NULL, 0, &needed);
if (GetLastError() != ERROR_INSUFFICIENT_BUFFER)
return PyWin_SetAPIError("GetPrinter");
buf = (LPBYTE)malloc(needed);
if (buf == NULL)
return PyErr_Format(PyExc_MemoryError, "GetPrinter: Unable to allocate buffer of %d bytes", needed);
if (!GetPrinter(hprinter, level, buf, needed, &needed)) {
free(buf);
return PyWin_SetAPIError("GetPrinter");
}
if (backward_compat) {
pi2 = (PRINTER_INFO_2 *)buf;
rc = Py_BuildValue("NNNNNNNONNNNOkkkkkkkk", PyWinObject_FromTCHAR(pi2->pServerName),
PyWinObject_FromTCHAR(pi2->pPrinterName), PyWinObject_FromTCHAR(pi2->pShareName),
PyWinObject_FromTCHAR(pi2->pPortName), PyWinObject_FromTCHAR(pi2->pDriverName),
PyWinObject_FromTCHAR(pi2->pComment), PyWinObject_FromTCHAR(pi2->pLocation), Py_None,
PyWinObject_FromTCHAR(pi2->pSepFile), PyWinObject_FromTCHAR(pi2->pPrintProcessor),
PyWinObject_FromTCHAR(pi2->pDatatype), PyWinObject_FromTCHAR(pi2->pParameters), Py_None,
pi2->Attributes, pi2->Priority, pi2->DefaultPriority, pi2->StartTime, pi2->UntilTime,
pi2->Status, pi2->cJobs, pi2->AveragePPM);
}
else
rc = PyWinObject_FromPRINTER_INFO(buf, level);
free(buf);
return rc;
}
void PyWinObject_FreePRINTER_INFO(DWORD level, LPBYTE pbuf)
{
if ((level == 0) || (pbuf == NULL))
return;
switch (level) {
case 2: {
PRINTER_INFO_2 *pi2 = (PRINTER_INFO_2 *)pbuf;
PyWinObject_FreeTCHAR(pi2->pServerName);
PyWinObject_FreeTCHAR(pi2->pPrinterName);
PyWinObject_FreeTCHAR(pi2->pShareName);
PyWinObject_FreeTCHAR(pi2->pPortName);
PyWinObject_FreeTCHAR(pi2->pDriverName);
PyWinObject_FreeTCHAR(pi2->pComment);
PyWinObject_FreeTCHAR(pi2->pLocation);
PyWinObject_FreeTCHAR(pi2->pSepFile);
PyWinObject_FreeTCHAR(pi2->pPrintProcessor);
PyWinObject_FreeTCHAR(pi2->pDatatype);
PyWinObject_FreeTCHAR(pi2->pParameters);
break;
}
case 4: {
PRINTER_INFO_4 *pi4 = (PRINTER_INFO_4 *)pbuf;
PyWinObject_FreeTCHAR(pi4->pPrinterName);
PyWinObject_FreeTCHAR(pi4->pServerName);
break;
}
case 5: {
PRINTER_INFO_5 *pi5 = (PRINTER_INFO_5 *)pbuf;
PyWinObject_FreeTCHAR(pi5->pPrinterName);
PyWinObject_FreeTCHAR(pi5->pPortName);
break;
}
case 7: {
PRINTER_INFO_7 *pi7 = (PRINTER_INFO_7 *)pbuf;
PyWinObject_FreeTCHAR(pi7->pszObjectGUID);
break;
}
default:
break;
}
free(pbuf);
}
BOOL PyWinObject_AsPRINTER_INFO(DWORD level, PyObject *obinfo, LPBYTE *pbuf)
{
BOOL ret = FALSE;
size_t bufsize;
*pbuf = NULL;
if (level == 0)
if (obinfo == Py_None)
return TRUE;
else {
*pbuf = (LPBYTE)PyInt_AsLong(obinfo);
if ((*pbuf == (LPBYTE)-1) && PyErr_Occurred()) {
PyErr_Clear();
PyErr_SetString(PyExc_TypeError, "Info must be None or a PRINTER_STATUS_* integer when level is 0.");
return FALSE;
}
return TRUE;
}
if (!PyDict_Check(obinfo)) {
PyErr_Format(PyExc_TypeError, "PRINTER_INFO_%d must be a dictionary", level);
return FALSE;
}
switch (level) {
case 2: {
static char *pi2_keys[] = {"pServerName",
"pPrinterName",
"pShareName",
"pPortName",
"pDriverName",
"pComment",
"pLocation",
"pDevMode",
"pSepFile",
"pPrintProcessor",
"pDatatype",
"pParameters",
"pSecurityDescriptor",
"Attributes",
"Priority",
"DefaultPriority",
"StartTime",
"UntilTime",
"Status",
"cJobs",
"AveragePPM",
NULL};
static char *pi2_format = "OOOOOOOOOOOOOkkkkkkkk:PRINTER_INFO_2";
PyObject *obServerName = Py_None, *obPrinterName = Py_None, *obShareName = Py_None, *obPortName = Py_None,
*obDriverName = Py_None, *obComment = Py_None, *obLocation = Py_None, *obDevMode = Py_None,
*obSepFile = Py_None, *obPrintProcessor = Py_None, *obDatatype = Py_None, *obParameters = Py_None,
*obSecurityDescriptor = Py_None;
PRINTER_INFO_2 *pi2;
bufsize = sizeof(PRINTER_INFO_2);
if (NULL == (*pbuf = (LPBYTE)malloc(bufsize))) {
PyErr_Format(PyExc_MemoryError, "Malloc failed for %d bytes", bufsize);
break;
}
ZeroMemory(*pbuf, bufsize);
pi2 = (PRINTER_INFO_2 *)*pbuf;
ret = PyArg_ParseTupleAndKeywords(dummy_tuple, obinfo, pi2_format, pi2_keys, &obServerName, &obPrinterName,
&obShareName, &obPortName, &obDriverName, &obComment, &obLocation,
&obDevMode, &obSepFile, &obPrintProcessor, &obDatatype, &obParameters,
&obSecurityDescriptor, &pi2->Attributes, &pi2->Priority,
&pi2->DefaultPriority, &pi2->StartTime, &pi2->UntilTime, &pi2->Status,
&pi2->cJobs, &pi2->AveragePPM) &&
PyWinObject_AsTCHAR(obServerName, &pi2->pServerName, TRUE) &&
PyWinObject_AsTCHAR(obPrinterName, &pi2->pPrinterName, TRUE) &&
PyWinObject_AsTCHAR(obShareName, &pi2->pShareName, TRUE) &&
PyWinObject_AsTCHAR(obPortName, &pi2->pPortName, TRUE) &&
PyWinObject_AsTCHAR(obDriverName, &pi2->pDriverName, TRUE) &&
PyWinObject_AsTCHAR(obComment, &pi2->pComment, TRUE) &&
PyWinObject_AsTCHAR(obLocation, &pi2->pLocation, TRUE) &&
PyWinObject_AsDEVMODE(obDevMode, &pi2->pDevMode, FALSE) &&
PyWinObject_AsTCHAR(obSepFile, &pi2->pSepFile, TRUE) &&
PyWinObject_AsTCHAR(obPrintProcessor, &pi2->pPrintProcessor, TRUE) &&
PyWinObject_AsTCHAR(obDatatype, &pi2->pDatatype, TRUE) &&
PyWinObject_AsTCHAR(obParameters, &pi2->pParameters, TRUE) &&
PyWinObject_AsSECURITY_DESCRIPTOR(obSecurityDescriptor, &pi2->pSecurityDescriptor, TRUE);
break;
}
case 3: {
static char *pi3_keys[] = {"pSecurityDescriptor", NULL};
static char *pi3_format = "O:PRINTER_INFO_3";
PyObject *obSecurityDescriptor;
PRINTER_INFO_3 *pi3;
bufsize = sizeof(PRINTER_INFO_3);
if (NULL == (*pbuf = (LPBYTE)malloc(bufsize))) {
PyErr_Format(PyExc_MemoryError, "Malloc failed for %d bytes", bufsize);
break;
}
ZeroMemory(*pbuf, bufsize);
pi3 = (PRINTER_INFO_3 *)*pbuf;
ret = PyArg_ParseTupleAndKeywords(dummy_tuple, obinfo, pi3_format, pi3_keys, &obSecurityDescriptor) &&
PyWinObject_AsSECURITY_DESCRIPTOR(obSecurityDescriptor, &pi3->pSecurityDescriptor, FALSE);
break;
}
case 4: {
static char *pi4_keys[] = {"pPrinterName", "pServerName", "Attributes", NULL};
static char *pi4_format = "OOk:PRINTER_INFO_4";
PyObject *obPrinterName = Py_None, *obServerName = Py_None;
PRINTER_INFO_4 *pi4;
bufsize = sizeof(PRINTER_INFO_4);
if (NULL == (*pbuf = (LPBYTE)malloc(bufsize))) {
PyErr_Format(PyExc_MemoryError, "Malloc failed for %d bytes", bufsize);
break;
}
ZeroMemory(*pbuf, bufsize);
pi4 = (PRINTER_INFO_4 *)*pbuf;
ret = PyArg_ParseTupleAndKeywords(dummy_tuple, obinfo, pi4_format, pi4_keys, &obPrinterName, &obServerName,
&pi4->Attributes) &&
PyWinObject_AsTCHAR(obPrinterName, &pi4->pPrinterName, TRUE) &&
PyWinObject_AsTCHAR(obServerName, &pi4->pServerName, TRUE);
break;
}
case 5: {
static char *pi5_keys[] = {"pPrinterName",
"pPortName",
"Attributes",
"DeviceNotSelectedTimeout",
"TransmissionRetryTimeout",
NULL};
static char *pi5_format = "OOkkk:PRINTER_INFO_5";
PyObject *obPrinterName = Py_None, *obPortName = Py_None;
PRINTER_INFO_5 *pi5;
bufsize = sizeof(PRINTER_INFO_5);
if (NULL == (*pbuf = (LPBYTE)malloc(bufsize))) {
PyErr_Format(PyExc_MemoryError, "Malloc failed for %d bytes", bufsize);
break;
}
ZeroMemory(*pbuf, bufsize);
pi5 = (PRINTER_INFO_5 *)*pbuf;
ret = PyArg_ParseTupleAndKeywords(dummy_tuple, obinfo, pi5_format, pi5_keys, &obPrinterName, &obPortName,
&pi5->Attributes, &pi5->DeviceNotSelectedTimeout,
&pi5->TransmissionRetryTimeout) &&
PyWinObject_AsTCHAR(obPrinterName, &pi5->pPrinterName, TRUE) &&
PyWinObject_AsTCHAR(obPortName, &pi5->pPortName, TRUE);
break;
}
case 7: {
static char *pi7_keys[] = {"ObjectGUID", "Action", NULL};
static char *pi7_format = "Ok:PRINTER_INFO_7";
PyObject *obObjectGUID = Py_None;
PRINTER_INFO_7 *pi7;
bufsize = sizeof(PRINTER_INFO_7);
if (NULL == (*pbuf = (LPBYTE)malloc(bufsize))) {
PyErr_Format(PyExc_MemoryError, "Malloc failed for %d bytes", bufsize);
break;
}
ZeroMemory(*pbuf, bufsize);
pi7 = (PRINTER_INFO_7 *)*pbuf;
ret =
PyArg_ParseTupleAndKeywords(dummy_tuple, obinfo, pi7_format, pi7_keys, &obObjectGUID, &pi7->dwAction) &&
PyWinObject_AsTCHAR(obObjectGUID, &pi7->pszObjectGUID, TRUE);
break;
}
case 8:
case 9: { // identical structs, 8 is for global defaults and 9 is for user defaults
static char *pi8_keys[] = {"pDevMode", NULL};
static char *pi8_format = "O:PRINTER_INFO_8";
PyObject *obDevMode;
PRINTER_INFO_8 *pi8;
bufsize = sizeof(PRINTER_INFO_8);
if (NULL == (*pbuf = (LPBYTE)malloc(bufsize))) {
PyErr_Format(PyExc_MemoryError, "Malloc failed for %d bytes", bufsize);
break;
}
ZeroMemory(*pbuf, bufsize);
pi8 = (PRINTER_INFO_8 *)*pbuf;
ret = PyArg_ParseTupleAndKeywords(dummy_tuple, obinfo, pi8_format, pi8_keys, &obDevMode) &&
PyWinObject_AsDEVMODE(obDevMode, &pi8->pDevMode, FALSE);
break;
}
default:
PyErr_Format(PyExc_NotImplementedError, "Information level %d is not supported", level);
}
if (!ret) {
if ((*pbuf != NULL) && (level != 0))
free(*pbuf);
*pbuf = NULL;
}
return ret;
}
// @pymethod |win32print|SetPrinter|Change printer configuration and status
static PyObject *PySetPrinter(PyObject *self, PyObject *args)
{
HANDLE hprinter;
LPBYTE buf = NULL;
DWORD level, command;
PyObject *obinfo = NULL, *ret = NULL;
// @pyparm <o PyPrinterHANDLE>|hPrinter||Printer handle as returned by <om win32print.OpenPrinter>
// @pyparm int|Level||Level of data contained in pPrinter
// @pyparm dict|pPrinter||PRINTER_INFO_* dict as returned by <om win32print.GetPrinter>, can be None if level is 0
// @pyparm int|Command||Command to send to printer - one of the PRINTER_CONTROL_* constants, or 0
// @comm If Level is 0 and Command is PRINTER_CONTROL_SET_STATUS, pPrinter should be an integer,
// and is interpreted as the new printer status to set (one of the PRINTER_STATUS_* constants).
if (!PyArg_ParseTuple(args, "O&kOk:SetPrinter", PyWinObject_AsPrinterHANDLE, &hprinter, &level, &obinfo, &command))
return NULL;
if (!PyWinObject_AsPRINTER_INFO(level, obinfo, &buf))
return NULL;
if (!SetPrinter(hprinter, level, buf, command))
PyWin_SetAPIError("SetPrinter");
else {
Py_INCREF(Py_None);
ret = Py_None;
}
PyWinObject_FreePRINTER_INFO(level, buf);
return ret;
}
// @pymethod None|win32print|AddPrinterConnection|Connects to remote printer
static PyObject *PyAddPrinterConnection(PyObject *self, PyObject *args)
{
TCHAR *printer;
PyObject *obprinter;
if (!PyArg_ParseTuple(args, "O:AddPrinterConnection",
&obprinter)) // @pyparm string|printer||printer to connect to (eg: \\server\printer).
return NULL;
if (!PyWinObject_AsTCHAR(obprinter, &printer, FALSE))
return NULL;
BOOL bsuccess = AddPrinterConnection(printer);
PyWinObject_FreeTCHAR(printer);
if (!bsuccess)
return PyWin_SetAPIError("AddPrinterConnection");
Py_INCREF(Py_None);
return Py_None;
}
// @pymethod None|win32print|DeletePrinterConnection|Removes connection to remote printer
static PyObject *PyDeletePrinterConnection(PyObject *self, PyObject *args)
{
TCHAR *printer;
PyObject *obprinter;
if (!PyArg_ParseTuple(args, "O:DeletePrinterConnection",
&obprinter)) // @pyparm string|printer||printer to disconnect from (eg: \\server\printer).
return NULL;
if (!PyWinObject_AsTCHAR(obprinter, &printer, FALSE))
return NULL;
BOOL bsuccess = DeletePrinterConnection(printer);
PyWinObject_FreeTCHAR(printer);
if (!bsuccess)
return PyWin_SetAPIError("DeletePrinterConnection");
Py_INCREF(Py_None);
return Py_None;
}
// @pymethod string|win32print|GetDefaultPrinter|Returns the default printer.
static PyObject *PyGetDefaultPrinter(PyObject *self, PyObject *args)
{
TCHAR *printer, *s;
int printer_size = 100;
/* Windows < 2000 does not have a GetDefaultPrinter so the default printer
must be retrieved from registry */
if (NULL == (printer = (TCHAR *)malloc(printer_size * sizeof(TCHAR)))) {
PyErr_SetString(PyExc_MemoryError, "Malloc failed.");
return NULL;
}
if (0 == GetProfileString(TEXT("Windows"), TEXT("Device"), TEXT(""), printer, printer_size)) {
PyErr_SetString(PyExc_RuntimeError, "The default printer was not found.");
return NULL;
}
if (NULL == (s = _tcschr(printer, TEXT(',')))) {
PyErr_SetString(PyExc_RuntimeError, "The returned printer is malformed.");
return NULL;
}
*s = 0;
PyObject *ret = PyWinObject_FromTCHAR(printer);
free(printer);
return ret;
}
// @pymethod <o PyUnicode>|win32print|GetDefaultPrinterW|Returns the default printer.
// @comm Unlike <om win32print.GetDefaultPrinter>, this method calls the GetDefaultPrinter API function.
static PyObject *PyGetDefaultPrinterW(PyObject *self, PyObject *args)
{
CHECK_PFN(GetDefaultPrinter);
WCHAR *printer = NULL;
DWORD err, printer_size = 100;
PyObject *ret = NULL;
printer = (WCHAR *)malloc(printer_size * sizeof(WCHAR));
if (printer == NULL)
return PyErr_Format(PyExc_MemoryError, "Unable to allocate %d bytes", printer_size * sizeof(WCHAR));
if (!(*pfnGetDefaultPrinter)(printer, &printer_size)) {
err = GetLastError();
if (err != ERROR_INSUFFICIENT_BUFFER) {
PyWin_SetAPIError("GetDefaultPrinter");
goto done;
}
free(printer);
printer = (WCHAR *)malloc(printer_size * sizeof(WCHAR));
if (printer == NULL)
return PyErr_Format(PyExc_MemoryError, "Unable to allocate %d bytes", printer_size * sizeof(WCHAR));
if (!(*pfnGetDefaultPrinter)(printer, &printer_size)) {
PyWin_SetAPIError("GetDefaultPrinter");
goto done;
}
}
ret = PyWinObject_FromWCHAR(printer);
done:
if (printer)
free(printer);
return ret;
}
// @pymethod None|win32print|SetDefaultPrinter|Sets the default printer.
// @comm This function uses the pre-win2k method of WriteProfileString rather than the SetDefaultPrinter API function
static PyObject *PySetDefaultPrinter(PyObject *self, PyObject *args)
{
TCHAR *printer = NULL, *info = NULL, *dprinter = NULL;
int info_size = 100;
PyObject *obprinter;
/* Windows < 2000 does not have a SetDefaultPrinter so the default printer
must be set in the registry */
if (!PyArg_ParseTuple(args, "O:SetDefaultPrinter",
&obprinter)) // @pyparm string|printer||printer to set as default
return NULL;
if (!PyWinObject_AsTCHAR(obprinter, &printer, FALSE))
return NULL;
if (NULL == (info = (TCHAR *)malloc(info_size * sizeof(TCHAR))))
PyErr_NoMemory();
else if (0 == GetProfileString(TEXT("Devices"), printer, TEXT(""), info, info_size))
PyErr_SetString(PyExc_RuntimeError, "The printer was not found.");
else if (NULL == (dprinter = (TCHAR *)malloc((_tcslen(printer) + _tcslen(info) + 3) * sizeof(TCHAR))))
PyErr_NoMemory();
else {
_tcscpy(dprinter, printer);
_tcscat(dprinter, TEXT(","));
_tcscat(dprinter, info);
WriteProfileString(TEXT("Windows"), TEXT("device"), dprinter);
SendNotifyMessage(HWND_BROADCAST, WM_SETTINGCHANGE, 0, 0);
}
if (dprinter)
free(dprinter);
if (info)
free(info);
PyWinObject_FreeTCHAR(printer);
Py_INCREF(Py_None);
return Py_None;
}
// @pymethod None|win32print|SetDefaultPrinterW|Sets the default printer
// @comm Unlike <om win32print.SetDefaultPrinter>, this method calls the SetDefaultPrinter API function.
static PyObject *PySetDefaultPrinterW(PyObject *self, PyObject *args)
{
CHECK_PFN(SetDefaultPrinter);
WCHAR *printer = NULL;
PyObject *obprinter, *ret = NULL;
// @pyparm <o PyUnicode>|Printer||Name of printer, can be None to use first available printer
if (!PyArg_ParseTuple(args, "O:SetDefaultPrinter", &obprinter))
return NULL;
if (!PyWinObject_AsWCHAR(obprinter, &printer, TRUE))
return NULL;
if (!(*pfnSetDefaultPrinter)(printer))
PyWin_SetAPIError("SetDefaultPrinter");
else {
Py_INCREF(Py_None);
ret = Py_None;
}
PyWinObject_FreeWCHAR(printer);
return ret;
}
// @pymethod tuple|win32print|EnumPrinters|Enumerates printers, print servers, domains and print providers.
// @comm Use Flags=PRINTER_ENUM_NAME, Name=None, Level=1 to enumerate print providers.<nl>
// Use Flags=PRINTER_ENUM_NAME, Name=\\servername, Level=2 or 5 to list printers on another server.<nl>
// See MSDN docs for EnumPrinters for other specific combinations
static PyObject *PyEnumPrinters(PyObject *self, PyObject *args)
{
DWORD flags;
DWORD level = 1;
BYTE *buf = NULL;
DWORD bufsize;
DWORD bufneeded;
DWORD printersreturned;
TCHAR *name = NULL;
PyObject *obname = Py_None;
DWORD i;
PyObject *ret = NULL, *obprinter_info;
static size_t printer_info_offset[] = {sizeof(PRINTER_INFO_1), sizeof(PRINTER_INFO_2), sizeof(PRINTER_INFO_3),
sizeof(PRINTER_INFO_4), sizeof(PRINTER_INFO_5), sizeof(PRINTER_INFO_6),
sizeof(PRINTER_INFO_7), sizeof(PRINTER_INFO_8), sizeof(PRINTER_INFO_9)};
if (!PyArg_ParseTuple(args, "k|Ok:EnumPrinters",
&flags, // @pyparm int|flags||types of printer objects to enumerate (combination of
// PRINTER_ENUM_* constants).
&obname, // @pyparm string|name|None|name of printer object.
&level)) // @pyparm int|level|1|type of printer info structure (Levels 1,2,4,5 supported)
return NULL;
if (level < 1 || level > 9)
return PyErr_Format(PyExc_ValueError, "Level %d is not supported", level);
if (!PyWinObject_AsTCHAR(obname, &name, TRUE))
return NULL; // last exit without cleanup
// if call with NULL buffer succeeds, there's nothing to enumerate
if (EnumPrinters(flags, name, level, NULL, 0, &bufneeded, &printersreturned)) {
ret = PyTuple_New(0);
goto done;
}
if (GetLastError() != ERROR_INSUFFICIENT_BUFFER) {
PyWin_SetAPIError("EnumPrinters");
goto done;
}
bufsize = bufneeded;
if (NULL == (buf = (BYTE *)malloc(bufsize))) {
PyErr_Format(PyExc_MemoryError, "EnumPrinters: unable to allocate %d bytes", bufsize);
goto done;
}
// @rdesc Level 1 returns a tuple of tuples for backward compatibility.
// Each individual element is a tuple of (flags, description, name, comment)<nl>
// All other levels return a tuple of dictionaries representing PRINTER_INFO_* structures
if (!EnumPrinters(flags, name, level, buf, bufsize, &bufneeded, &printersreturned))
PyWin_SetAPIError("EnumPrinters");
else {
ret = PyTuple_New(printersreturned);
if (ret != NULL)
for (i = 0; i < printersreturned; i++) {
if (level == 1) {
PRINTER_INFO_1 *info;
info = (PRINTER_INFO_1 *)(buf + i * sizeof(PRINTER_INFO_1));
obprinter_info =
Py_BuildValue("kNNN", info->Flags, PyWinObject_FromTCHAR(info->pDescription),
PyWinObject_FromTCHAR(info->pName), PyWinObject_FromTCHAR(info->pComment));
}
else
obprinter_info = PyWinObject_FromPRINTER_INFO(buf + i * printer_info_offset[level - 1], level);
if (obprinter_info == NULL) {
Py_DECREF(ret);
ret = NULL;
break;
}
PyTuple_SET_ITEM(ret, i, obprinter_info);
}
}
done:
PyWinObject_FreeTCHAR(name);
if (buf)
free(buf);
return ret;
}
// @pymethod int|win32print|StartDocPrinter|Notifies the print spooler that a document is to be spooled for printing. To
// be used before using WritePrinter. Returns the Jobid of the started job.
static PyObject *PyStartDocPrinter(PyObject *self, PyObject *args)
{
HANDLE hprinter;
DWORD level = 1;
TCHAR *pDocName = NULL, *pOutputFile = NULL, *pDatatype = NULL;
PyObject *obDocName, *obOutputFile, *obDatatype, *ret = NULL;
DOC_INFO_1 info;
DWORD JobID;
if (!PyArg_ParseTuple(
args, "O&k(OOO):StartDocPrinter", PyWinObject_AsPrinterHANDLE,
&hprinter, // @pyparm <o PyPrinterHANDLE>|hprinter||handle to printer (from <om win32print.OpenPrinter>)
&level, // @pyparm int|level|1|type of docinfo structure (only docinfo level 1 supported)
&obDocName, &obOutputFile, &obDatatype // @pyparm data|tuple||A tuple corresponding to the level parameter.
))
return NULL;
if (level != 1) {
PyErr_SetString(PyExc_ValueError, "This information level is not supported");
return NULL;
}
// @comm For level 1, the tuple is:
// @tupleitem 0|string|docName|Specifies the name of the document.
// @tupleitem 1|string|outputFile|Specifies the name of an output file. To print to a printer, set this to None.
// @tupleitem 2|string|dataType|Identifies the type of data used to record the document, such
// as "raw" or "emf", used to record the print job. This member can be None. If it is not None,
// the StartDoc function passes it to the printer driver. Note that the printer driver might
// ignore the requested data type.
if (PyWinObject_AsTCHAR(obDocName, &pDocName, FALSE) && PyWinObject_AsTCHAR(obOutputFile, &pOutputFile, TRUE) &&
PyWinObject_AsTCHAR(obDatatype, &pDatatype, TRUE)) {
info.pDocName = pDocName;
info.pOutputFile = pOutputFile;
info.pDatatype = pDatatype;
Py_BEGIN_ALLOW_THREADS JobID = StartDocPrinter(hprinter, level, (LPBYTE)&info);
Py_END_ALLOW_THREADS if (0 == JobID) PyWin_SetAPIError("StartDocPrinter");
else ret = PyLong_FromUnsignedLong(JobID);
}
PyWinObject_FreeTCHAR(pDocName);
PyWinObject_FreeTCHAR(pOutputFile);
PyWinObject_FreeTCHAR(pDatatype);
return ret;
}
// @pymethod None|win32print|EndDocPrinter|The EndDocPrinter function ends a print job for the specified printer. To be
// used after using WritePrinter.
static PyObject *PyEndDocPrinter(PyObject *self, PyObject *args)
{
HANDLE hprinter;
if (!PyArg_ParseTuple(
args, "O&:EndDocPrinter", PyWinObject_AsPrinterHANDLE,
&hprinter)) // @pyparm <o PyPrinterHANDLE>|hPrinter||handle to printer (from <om win32print.OpenPrinter>)
return NULL;
if (!EndDocPrinter(hprinter))
return PyWin_SetAPIError("EndDocPrinter");
Py_INCREF(Py_None);
return Py_None;
}
// @pymethod |win32print|AbortPrinter|Deletes spool file for a printer
static PyObject *PyAbortPrinter(PyObject *self, PyObject *args)
{
// @pyparm <o PyPrinterHANDLE>|hPrinter||Handle to printer as returned by <om win32print.OpenPrinter>
HANDLE hprinter;
if (!PyArg_ParseTuple(args, "O&:AbortPrinter", PyWinObject_AsPrinterHANDLE, &hprinter))
return NULL;
if (!AbortPrinter(hprinter))
return PyWin_SetAPIError("AbortPrinter");
Py_INCREF(Py_None);
return Py_None;
}
// @pymethod |win32print|StartPagePrinter|Notifies the print spooler that a page is to be printed on specified printer
static PyObject *PyStartPagePrinter(PyObject *self, PyObject *args)
{
// @pyparm <o PyPrinterHANDLE>|hprinter||Printer handle as returned by <om win32print.OpenPrinter>
HANDLE hprinter;
if (!PyArg_ParseTuple(args, "O&:StartPagePrinter", PyWinObject_AsPrinterHANDLE, &hprinter))
return NULL;
if (!StartPagePrinter(hprinter))
return PyWin_SetAPIError("StartPagePrinter");
Py_INCREF(Py_None);
return Py_None;
}
// @pymethod |win32print|EndPagePrinter|Ends a page in a print job
static PyObject *PyEndPagePrinter(PyObject *self, PyObject *args)
{
// @pyparm <o PyPrinterHANDLE>|hprinter||Printer handle as returned by <om win32print.OpenPrinter>
HANDLE hprinter;
if (!PyArg_ParseTuple(args, "O&:EndPagePrinter", PyWinObject_AsPrinterHANDLE, &hprinter))
return NULL;
if (!EndPagePrinter(hprinter))
return PyWin_SetAPIError("EndPagePrinter");
Py_INCREF(Py_None);
return Py_None;
}
void PyWinObject_FreeDOCINFO(DOCINFO *di)
{
PyWinObject_FreeTCHAR((TCHAR *)di->lpszDocName);
PyWinObject_FreeTCHAR((TCHAR *)di->lpszOutput);
PyWinObject_FreeTCHAR((TCHAR *)di->lpszDatatype);
}
// @object DOCINFO|A tuple of information representing a DOCINFO struct
// @prop string/<o PyUnicode>|DocName|Name of document
// @prop string/<o PyUnicode>|Output|Name of output file when printing to file. Use None for normal printing.
// @prop string/<o PyUnicode>|DataType|Type of data to be sent to printer, eg RAW, EMF, TEXT. Use None for printer
// default.
// @prop int|Type|Flag specifying mode of operation. Can be DI_APPBANDING, DI_ROPS_READ_DESTINATION, or 0
BOOL PyWinObject_AsDOCINFO(PyObject *obdocinfo, DOCINFO *di)
{
PyObject *obDocName, *obOutput, *obDataType;
ZeroMemory(di, sizeof(*di));
if (!PyTuple_Check(obdocinfo)) {
PyErr_SetString(PyExc_TypeError, "DOCINFO must be a tuple");
return FALSE;
}
di->cbSize = sizeof(DOCINFO);
return PyArg_ParseTuple(obdocinfo, "OOOk", &obDocName, &obOutput, &obDataType, &di->fwType) &&
PyWinObject_AsTCHAR(obDocName, (TCHAR **)&di->lpszDocName, TRUE) &&
PyWinObject_AsTCHAR(obOutput, (TCHAR **)&di->lpszOutput, TRUE) &&
PyWinObject_AsTCHAR(obDataType, (TCHAR **)&di->lpszDatatype, TRUE);
}
// @pymethod int|win32print|StartDoc|Starts spooling a print job on a printer device context
static PyObject *PyStartDoc(PyObject *self, PyObject *args)
{
// @pyparm <o PyHANDLE>|hdc||Printer device context handle as returned by <om win32gui.CreateDC>
// @pyparm tuple|docinfo||<o DOCINFO> tuple specifying print job parameters
// @rdesc On success, returns the job id of the print job
HDC hdc;
DOCINFO docinfo = {0};
int jobid;
PyObject *obdocinfo;
if (!PyArg_ParseTuple(args, "O&O:StartDoc", PyWinObject_AsPrinterHANDLE, &hdc, &obdocinfo))
return NULL;
if (!PyWinObject_AsDOCINFO(obdocinfo, &docinfo))
return NULL;
jobid = StartDoc(hdc, &docinfo);
PyWinObject_FreeDOCINFO(&docinfo);
if (jobid > 0)
return PyLong_FromUnsignedLong(jobid);
return PyWin_SetAPIError("StartDoc");
}
// @pymethod |win32print|EndDoc|Stops spooling a print job on a printer device context
static PyObject *PyEndDoc(PyObject *self, PyObject *args)
{
// @pyparm <o PyHANDLE>|hdc||Printer device context handle as returned by <om win32gui.CreateDC>
HDC hdc;
int err;
if (!PyArg_ParseTuple(args, "O&:EndDoc", PyWinObject_AsPrinterHANDLE, &hdc))
return NULL;
err = EndDoc(hdc);
if (err > 0) {
Py_INCREF(Py_None);
return Py_None;
}
return PyWin_SetAPIError("EndDoc");
}
// @pymethod |win32print|AbortDoc|Cancels a print job
static PyObject *PyAbortDoc(PyObject *self, PyObject *args)
{
// @pyparm <o PyHANDLE>|hdc||Printer device context handle as returned by <om win32gui.CreateDC>
HDC hdc;
int err;
if (!PyArg_ParseTuple(args, "O&:AbortDoc", PyWinObject_AsPrinterHANDLE, &hdc))
return NULL;
err = AbortDoc(hdc);
if (err > 0) {
Py_INCREF(Py_None);
return Py_None;
}
return PyWin_SetAPIError("AbortDoc");
}
// @pymethod |win32print|StartPage|Starts a page on a printer device context
static PyObject *PyStartPage(PyObject *self, PyObject *args)
{
// @pyparm <o PyHANDLE>|hdc||Printer device context handle as returned by <om win32gui.CreateDC>
HDC hdc;
int err;
if (!PyArg_ParseTuple(args, "O&:StartPage", PyWinObject_AsPrinterHANDLE, &hdc))
return NULL;
err = StartPage(hdc);
if (err > 0) {
Py_INCREF(Py_None);
return Py_None;
}
return PyWin_SetAPIError("StartPage");
}
// @pymethod |win32print|EndPage|Ends a page on a printer device context
static PyObject *PyEndPage(PyObject *self, PyObject *args)
{
// @pyparm <o PyHANDLE>|hdc||Printer device context handle as returned by <om win32gui.CreateDC>
HDC hdc;
int err;
if (!PyArg_ParseTuple(args, "O&:EndPage", PyWinObject_AsPrinterHANDLE, &hdc))
return NULL;
err = EndPage(hdc);
if (err > 0) {
Py_INCREF(Py_None);
return Py_None;
}
return PyWin_SetAPIError("EndPage");
}
// @pymethod int|win32print|WritePrinter|Copies the specified bytes to the specified printer.
// Suitable for copying raw Postscript or HPGL files to a printer.
// StartDocPrinter and EndDocPrinter should be called before and after.
// @rdesc Returns number of bytes written to printer.
static PyObject *PyWritePrinter(PyObject *self, PyObject *args)
{
HANDLE hprinter;
LPVOID buf;